From ff2ce497d6cfc89951e0b8b87ceafdb7b7f83303 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Mon, 21 Sep 2026 10:56:40 +0200 Subject: [PATCH] Add a kernel test for the bundle using nyholm/symfony-bundle-test Boots a real kernel with FrameworkBundle, TwigBundle and this bundle, and checks that the bundle's services are registered and that the is_bot() and is_bot_request() Twig functions work through the container. --- composer.json | 3 + tests/SetonoBotDetectionBundleTest.php | 96 ++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 tests/SetonoBotDetectionBundleTest.php diff --git a/composer.json b/composer.json index a36d769..08c5795 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ "matomo/device-detector": "^5.0 || ^6.0", "matthiasnoback/symfony-dependency-injection-test": "^5.1 || ^6.3", "nette/php-generator": "^3.6 || ^4.1", + "nyholm/symfony-bundle-test": "^3.1", "phpspec/prophecy-phpunit": "^2.0", "phpstan/extension-installer": "^1.4", "phpstan/phpstan": "^2.1", @@ -35,6 +36,8 @@ "shipmonk/composer-dependency-analyser": "^1.8", "sylius-labs/coding-standard": "^4.1.1", "symfony/console": "^6.4 || ^7.4 || ^8.0", + "symfony/framework-bundle": "^6.4 || ^7.4 || ^8.0", + "symfony/twig-bundle": "^6.4 || ^7.4 || ^8.0", "symfony/yaml": "^6.4 || ^7.4 || ^8.0", "webmozart/assert": "^1.11" }, diff --git a/tests/SetonoBotDetectionBundleTest.php b/tests/SetonoBotDetectionBundleTest.php new file mode 100644 index 0000000..8fd7f82 --- /dev/null +++ b/tests/SetonoBotDetectionBundleTest.php @@ -0,0 +1,96 @@ + $options + */ + protected static function createKernel(array $options = []): KernelInterface + { + /** @var TestKernel $kernel */ + $kernel = parent::createKernel($options); + $kernel->addTestBundle(TwigBundle::class); + $kernel->addTestBundle(SetonoBotDetectionBundle::class); + $kernel->handleOptions($options); + + // Unused private services are removed from the container, so make the bundle's services public to test them + $kernel->addTestCompilerPass(new class() implements CompilerPassInterface { + public function process(ContainerBuilder $container): void + { + foreach ($container->getDefinitions() as $id => $definition) { + if (str_starts_with($id, 'setono_bot_detection.')) { + $definition->setPublic(true); + } + } + + $container->getAlias(BotDetectorInterface::class)->setPublic(true); + } + }); + + return $kernel; + } + + /** + * @test + */ + public function it_registers_services(): void + { + $container = self::getContainer(); + + self::assertInstanceOf(BotDetector::class, $container->get(BotDetectorInterface::class)); + self::assertInstanceOf(BotDetector::class, $container->get('setono_bot_detection.bot_detector.default')); + self::assertInstanceOf(Extension::class, $container->get('setono_bot_detection.twig.extension')); + self::assertInstanceOf(Runtime::class, $container->get('setono_bot_detection.twig.runtime')); + } + + /** + * @test + */ + public function it_provides_the_twig_functions(): void + { + $container = self::getContainer(); + + $requestStack = $container->get('request_stack'); + self::assertInstanceOf(RequestStack::class, $requestStack); + $requestStack->push(new Request([], [], [], [], [], [ + 'HTTP_USER_AGENT' => 'Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)', + ])); + + $twig = $container->get('twig'); + self::assertInstanceOf(Environment::class, $twig); + + self::assertSame('bot', $twig->createTemplate("{{ is_bot_request() ? 'bot' : 'human' }}")->render()); + self::assertSame('bot', $twig->createTemplate("{{ is_bot('Googlebot/2.1') ? 'bot' : 'human' }}")->render()); + self::assertSame('human', $twig->createTemplate( + "{{ is_bot('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36') ? 'bot' : 'human' }}", + )->render()); + } +}