diff --git a/Tests/Command/DumpCommandTest.php b/Tests/Command/DumpCommandTest.php index 810ab66..583ecda 100644 --- a/Tests/Command/DumpCommandTest.php +++ b/Tests/Command/DumpCommandTest.php @@ -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; @@ -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(); @@ -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']); @@ -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([ @@ -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([ @@ -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([]); @@ -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']); diff --git a/Tests/Controller/ControllerTest.php b/Tests/Controller/ControllerTest.php index fd94a5a..9ef10fe 100644 --- a/Tests/Controller/ControllerTest.php +++ b/Tests/Controller/ControllerTest.php @@ -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; @@ -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'; } @@ -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'); @@ -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'); @@ -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'); @@ -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'); @@ -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( @@ -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()); @@ -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')); @@ -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');