Skip to content

Commit

Permalink
Test controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
loicsapone committed Apr 25, 2024
1 parent 3f82b24 commit 0e47d00
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 6 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
"phpunit/phpunit": "^10.5",
"rector/rector": "^0.19",
"symfony/asset": "^6.4|^7.0",
"symfony/browser-kit": "^6.4|^7.0",
"symfony/css-selector": "^6.4|^7.0",
"symfony/framework-bundle": "^6.4|^7.0",
"symfony/runtime": "^6.4|^7.0",
"symfony/string": "^6.4|^7.0",
Expand Down
6 changes: 6 additions & 0 deletions phpunit.dist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
processIsolation="false"
stopOnFailure="false"
>
<php>
<env name="APP_ENV" value="test" />
<env name="KERNEL_CLASS" value="IQ2i\StoriaBundle\Tests\TestApplication\Kernel" />
<server name="SHELL_VERBOSITY" value="-1" />
</php>

<testsuites>
<testsuite name="Storia Test Suite">
<directory>tests</directory>
Expand Down
4 changes: 4 additions & 0 deletions src/View/ViewBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public function createFromRequest(Request $request): ?View
return null;
}

if (!file_exists($this->defaultPath.'/'.$viewPath.'.yaml')) {
return null;
}

$yaml = Yaml::parse(file_get_contents($this->defaultPath.'/'.$viewPath.'.yaml'));
$viewConfiguration = new ViewConfiguration();
$config = (new Processor())->processConfiguration($viewConfiguration, [$yaml]);
Expand Down
47 changes: 47 additions & 0 deletions tests/Controller/IframeControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the UI Storia project.
*
* (c) Loïc Sapone <loic@sapone.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace IQ2i\StoriaBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class IframeControllerTest extends WebTestCase
{
public function testComponent(): void
{
$client = static::createClient();
$client->request('GET', '/storia/iframe/components/badge?variant=default');

$this->assertResponseIsSuccessful();
$this->assertSelectorTextContains('title', 'UI Storia');
$this->assertSelectorTextContains('body > span', 'Default');
}

public function testPage(): void
{
$client = static::createClient();
$client->request('GET', '/storia/iframe/pages/homepage/homepage?variant=default');

$this->assertResponseIsSuccessful();
$this->assertSelectorTextContains('title', 'My website');
$this->assertSelectorTextContains('body > p', 'This is the homepage hero');
}

public function testUnknownView(): void
{
$client = static::createClient();
$client->request('GET', '/storia/iframe/components/unknown?variant=default');

$this->assertResponseStatusCodeSame(404);
}
}
39 changes: 39 additions & 0 deletions tests/Controller/ViewControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the UI Storia project.
*
* (c) Loïc Sapone <loic@sapone.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace IQ2i\StoriaBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class ViewControllerTest extends WebTestCase
{
public function testComponent(): void
{
$client = static::createClient();
$crawler = $client->request('GET', '/storia/components/badge?variant=default');

$this->assertResponseIsSuccessful();
$this->assertCount(4, $crawler->filter('header > .tabs > .tabs__item'));
$this->assertCount(3, $crawler->filter('main > div > div > .tabs > .tabs__item'));
}

public function testUnknownView(): void
{
$client = static::createClient();
$crawler = $client->request('GET', '/storia/components/unknown?variant=default');

$this->assertResponseIsSuccessful();
$this->assertSelectorExists('main');
$this->assertEquals('', $crawler->filter('.main__inner')->innerText());
}
}
2 changes: 1 addition & 1 deletion tests/TestApplication/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
require_once dirname(__DIR__).'/../../vendor/autoload_runtime.php';

return function (array $context) {
return new Kernel();
return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
};
5 changes: 0 additions & 5 deletions tests/TestApplication/src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ final class Kernel extends SymfonyKernel
{
use MicroKernelTrait;

public function __construct()
{
parent::__construct('dev', true);
}

public function registerBundles(): iterable
{
return [
Expand Down
1 change: 1 addition & 0 deletions tests/TestApplication/templates/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My website</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
Expand Down

0 comments on commit 0e47d00

Please sign in to comment.