From e4d491ac01212e37f3510912d0d667dbe50fd642 Mon Sep 17 00:00:00 2001 From: Kevin Raynel Date: Wed, 20 Sep 2023 17:39:31 +0200 Subject: [PATCH] Tests: Add first simple test --- .gitignore | 1 + composer.json | 13 +++- tests/BundleInitializationTest.php | 97 +++++++++++++++++++++++++ tests/Entity/Book.php | 21 ++++++ tests/config/packages/api_platform.yaml | 3 + tests/config/packages/doctrine.yaml | 3 + tests/config/routes.yaml | 4 + 7 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 tests/BundleInitializationTest.php create mode 100644 tests/Entity/Book.php create mode 100644 tests/config/packages/api_platform.yaml create mode 100644 tests/config/packages/doctrine.yaml create mode 100644 tests/config/routes.yaml diff --git a/.gitignore b/.gitignore index 57872d0..3a9875b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /vendor/ +composer.lock diff --git a/composer.json b/composer.json index 51efbbe..2007e71 100644 --- a/composer.json +++ b/composer.json @@ -30,6 +30,17 @@ ], "require-dev": { "phpstan/phpstan": "^1.8", - "friendsofphp/php-cs-fixer": "^3.9" + "friendsofphp/php-cs-fixer": "^3.9", + "phpunit/phpunit": "^10", + "nyholm/symfony-bundle-test": "^2.0", + "symfony/expression-language": "^6.3", + "doctrine/doctrine-bundle": "^2.10", + "doctrine/orm": "^2.16", + "symfony/security-bundle": "^6.3", + "phpdocumentor/reflection-docblock": "^5.3", + "phpstan/phpdoc-parser": "^1.24", + "symfony/property-access": "^6.3", + "symfony/property-info": "^6.3", + "symfony/serializer": "^6.3" } } diff --git a/tests/BundleInitializationTest.php b/tests/BundleInitializationTest.php new file mode 100644 index 0000000..a6c3aef --- /dev/null +++ b/tests/BundleInitializationTest.php @@ -0,0 +1,97 @@ +addTestBundle(DoctrineBundle::class); + $kernel->addTestBundle(ApiPlatformBundle::class); + $kernel->addTestBundle(TheodoAccentBundle::class); + $kernel->addTestConfig(__DIR__.'/config/packages/doctrine.yaml'); + $kernel->addTestConfig(__DIR__.'/config/packages/api_platform.yaml'); + $kernel->addTestRoutingFile(__DIR__.'/config/routes.yaml'); + + // Unused, non public services are removed during kernel boot. + // We force them as public during test. + $kernel->addTestCompilerPass(new class () implements CompilerPassInterface { + public function process(ContainerBuilder $container): void + { + $services = $container->getDefinitions() + $container->getAliases(); + + foreach ($services as $id => $definition) { + if (stripos($id, 'theodo_accent') === 0) { + $definition->setPublic(true); + } + } + } + }); + + $kernel->handleOptions($options); + + return $kernel; + } + + public function testInitBundle(): void + { + $container = self::getContainer(); + + $this->assertTrue($container->has('theodo_accent.access_control_checker_command')); + $service = $container->get('theodo_accent.access_control_checker_command'); + $this->assertInstanceOf(Command::class, $service); + } + + public function testExposedRoutes(): void + { + $container = self::getContainer(); + + $this->assertTrue($container->has('theodo_accent.accent_report_factory')); + + /** @var AccentReportFactory $reportFactory */ + $reportFactory = $container->get('theodo_accent.accent_report_factory'); + $accentReport = $reportFactory->createAccentReport(); + + $this->assertEquals(2, $accentReport->getUnprotectedRoutesCount()); + + $resourceRelatedRoutes = array_filter( + $accentReport->getRouteAccessControlList(), + fn ($route) => RouteAccessControlData::RESOURCE_UNRELATED_ROUTE !== $route->getExpression() + ); + + $this->assertCount(3, $resourceRelatedRoutes); + $checkedRoutes = []; + foreach($resourceRelatedRoutes as $routeData) { + $checkedRoutes[$routeData->getRouteName()] = $routeData->getExpression(); + } + + $expectedRoutes = [ + '_api_/books/{id}{._format}_get' => "is_granted('ROLE_USER_GET')", + '_api_/books{._format}_post' => "is_granted('ROLE_USER_DEFAULT')", + '_api_/books/{id}{._format}_patch' => "is_granted('ROLE_USER_PATCH')", + ]; + + $this->assertEquals($expectedRoutes, $checkedRoutes); + } +} diff --git a/tests/Entity/Book.php b/tests/Entity/Book.php new file mode 100644 index 0000000..44b5606 --- /dev/null +++ b/tests/Entity/Book.php @@ -0,0 +1,21 @@ +