Skip to content

Commit

Permalink
Update PHPUnit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pyksid authored and tobias-93 committed Jan 23, 2024
1 parent 92c6d04 commit 8aaf3ae
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 14 deletions.
42 changes: 37 additions & 5 deletions Tests/Command/DumpCommandTest.php
Expand Up @@ -14,6 +14,7 @@
namespace FOS\JsRoutingBundle\Tests\Command;

use FOS\JsRoutingBundle\Command\DumpCommand;
use FOS\JsRoutingBundle\Response\RoutesResponse;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Serializer\SerializerInterface;
Expand All @@ -22,12 +23,17 @@

class DumpCommandTest extends TestCase
{
protected RoutesResponse $routesResponse;
protected $extractor;
protected $router;
private $serializer;

public function setUp(): void
{
$this->routesResponse = $this->getMockBuilder(RoutesResponse::class)
->disableOriginalConstructor()
->getMock();

$this->extractor = $this->getMockBuilder(ExposedRoutesExtractor::class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -47,7 +53,12 @@ public function testExecute(): void
->method('serialize')
->will($this->returnValue('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[]},"blog":{"tokens":[["variable","\/","[^\/]++","slug"],["text","\/blog-post"]],"defaults":[],"requirements":[],"hosttokens":[["text","localhost"]]}},"prefix":"","host":"","scheme":""}'));

$command = new DumpCommand($this->extractor, $this->serializer, '/root/dir');
$command = new DumpCommand(
$this->routesResponse,
$this->extractor,
$this->serializer,
'/root/dir',
);

$tester = new CommandTester($command);
$tester->execute(['--target' => '/tmp/dump-command-test']);
Expand All @@ -64,7 +75,12 @@ public function testExecuteCallbackOption(): void
->method('serialize')
->will($this->returnValue('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[]},"blog":{"tokens":[["variable","\/","[^\/]++","slug"],["text","\/blog-post"]],"defaults":[],"requirements":[],"hosttokens":[["text","localhost"]]}},"prefix":"","host":"","scheme":""}'));

$command = new DumpCommand($this->extractor, $this->serializer, '/root/dir');
$command = new DumpCommand(
$this->routesResponse,
$this->extractor,
$this->serializer,
'/root/dir',
);

$tester = new CommandTester($command);
$tester->execute([
Expand All @@ -86,7 +102,12 @@ public function testExecuteFormatOption(): void
->method('serialize')
->will($this->returnValue($json));

$command = new DumpCommand($this->extractor, $this->serializer, '/root/dir');
$command = new DumpCommand(
$this->routesResponse,
$this->extractor,
$this->serializer,
'/root/dir',
);

$tester = new CommandTester($command);
$tester->execute([
Expand All @@ -104,7 +125,13 @@ public function testExecuteUnableToCreateDirectory(): void
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Unable to create directory /root/dir/public/js');
$command = new DumpCommand($this->extractor, $this->serializer, '/root/dir');

$command = new DumpCommand(
$this->routesResponse,
$this->extractor,
$this->serializer,
'/root/dir',
);

$tester = new CommandTester($command);
$tester->execute([]);
Expand All @@ -118,7 +145,12 @@ public function testExecuteUnableToWriteFile(): void
->method('serialize')
->will($this->returnValue('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[]},"blog":{"tokens":[["variable","\/","[^\/]++","slug"],["text","\/blog-post"]],"defaults":[],"requirements":[],"hosttokens":[["text","localhost"]]}},"prefix":"","host":"","scheme":""}'));

$command = new DumpCommand($this->extractor, $this->serializer, '/root/dir');
$command = new DumpCommand(
$this->routesResponse,
$this->extractor,
$this->serializer,
'/root/dir',
);

$tester = new CommandTester($command);
$tester->execute(['--target' => '/tmp']);
Expand Down
43 changes: 34 additions & 9 deletions Tests/Controller/ControllerTest.php
Expand Up @@ -14,6 +14,7 @@
namespace FOS\JsRoutingBundle\Tests\Controller;

use FOS\JsRoutingBundle\Controller\Controller;
use FOS\JsRoutingBundle\Response\RoutesResponse;
use FOS\JsRoutingBundle\Serializer\Denormalizer\RouteCollectionDenormalizer;
use FOS\JsRoutingBundle\Serializer\Normalizer\RouteCollectionNormalizer;
use FOS\JsRoutingBundle\Serializer\Normalizer\RoutesResponseNormalizer;
Expand All @@ -28,10 +29,12 @@

class ControllerTest extends TestCase
{
protected RoutesResponse $routesResponse;
private string $cachePath;

public function setUp(): void
{
$this->routesResponse = new RoutesResponse();
$this->cachePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'fosJsRouting'.DIRECTORY_SEPARATOR.'data.json';
}

Expand All @@ -47,8 +50,9 @@ public function testIndexAction(): void
$routes->add('blog', new Route('/blog-post/{slug}', [], [], [], 'localhost'));

$controller = new Controller(
$this->routesResponse,
$this->getSerializer(),
$this->getExtractor($routes)
$this->getExtractor($routes),
);

$response = $controller->indexAction($this->getRequest('/'), 'json');
Expand All @@ -63,8 +67,9 @@ public function testIndexWithExplicitTokenAction(): void
$routes->add('blog', new Route('/blog-post/{!slug}', [], [], [], 'localhost'));

$controller = new Controller(
$this->routesResponse,
$this->getSerializer(),
$this->getExtractor($routes)
$this->getExtractor($routes),
);

$response = $controller->indexAction($this->getRequest('/'), 'json');
Expand All @@ -79,8 +84,9 @@ public function testIndexActionWithLocalizedRoutes(): void
$routes->add('blog', new Route('/blog-post/{slug}/{_locale}', [], [], [], 'localhost'));

$controller = new Controller(
$this->routesResponse,
$this->getSerializer(),
$this->getExtractor($routes)
$this->getExtractor($routes),
);

$response = $controller->indexAction($this->getRequest('/'), 'json');
Expand All @@ -94,8 +100,9 @@ public function testConfigCache(): void
$routes->add('literal', new Route('/homepage'));

$controller = new Controller(
$this->routesResponse,
$this->getSerializer(),
$this->getExtractor($routes)
$this->getExtractor($routes),
);

$response = $controller->indexAction($this->getRequest('/'), 'json');
Expand All @@ -111,7 +118,11 @@ public function testConfigCache(): void
*/
public function testGenerateWithCallback($callback): void
{
$controller = new Controller($this->getSerializer(), $this->getExtractor());
$controller = new Controller(
$this->routesResponse,
$this->getSerializer(),
$this->getExtractor(),
);
$response = $controller->indexAction($this->getRequest('/', 'GET', ['callback' => $callback]), 'json');

$this->assertEquals(
Expand All @@ -131,13 +142,21 @@ public static function dataProviderForTestGenerateWithCallback(): array
public function testGenerateWithInvalidCallback(): void
{
$this->expectException(HttpException::class);
$controller = new Controller($this->getSerializer(), $this->getExtractor());
$controller = new Controller(
$this->routesResponse,
$this->getSerializer(),
$this->getExtractor(),
);
$controller->indexAction($this->getRequest('/', 'GET', ['callback' => '(function xss(x) {evil()})']), 'json');
}

public function testIndexActionWithoutRoutes(): void
{
$controller = new Controller($this->getSerializer(), $this->getExtractor());
$controller = new Controller(
$this->routesResponse,
$this->getSerializer(),
$this->getExtractor(),
);
$response = $controller->indexAction($this->getRequest('/'), 'json');

$this->assertEquals('{"base_url":"","routes":[],"prefix":"","host":"","port":null,"scheme":"","locale":"en"}', $response->getContent());
Expand All @@ -161,7 +180,12 @@ public function testCacheControl(): void
'vary' => [],
];

$controller = new Controller($this->getSerializer(), $this->getExtractor(), $cacheControlConfig);
$controller = new Controller(
$this->routesResponse,
$this->getSerializer(),
$this->getExtractor(),
$cacheControlConfig,
);
$response = $controller->indexAction($this->getRequest('/'), 'json');

$this->assertTrue($response->headers->hasCacheControlDirective('public'));
Expand Down Expand Up @@ -189,8 +213,9 @@ public function testExposeDomain(): void
['expose' => 'blog'], 'localhost'));

$controller = new Controller(
$this->routesResponse,
$this->getSerializer(),
$this->getExtractor($routes)
$this->getExtractor($routes),
);

$response = $controller->indexAction($this->getRequest('/'), 'json');
Expand Down

0 comments on commit 8aaf3ae

Please sign in to comment.