Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
},
Expand Down
96 changes: 96 additions & 0 deletions tests/SetonoBotDetectionBundleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

namespace Setono\BotDetectionBundle\Tests;

use Nyholm\BundleTest\TestKernel;
use Setono\BotDetectionBundle\BotDetector\BotDetector;
use Setono\BotDetectionBundle\BotDetector\BotDetectorInterface;
use Setono\BotDetectionBundle\SetonoBotDetectionBundle;
use Setono\BotDetectionBundle\Twig\Extension;
use Setono\BotDetectionBundle\Twig\Runtime;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\KernelInterface;
use Twig\Environment;

/**
* @covers \Setono\BotDetectionBundle\DependencyInjection\SetonoBotDetectionExtension
* @covers \Setono\BotDetectionBundle\SetonoBotDetectionBundle
*/
final class SetonoBotDetectionBundleTest extends KernelTestCase
{
protected static function getKernelClass(): string
{
return TestKernel::class;
}

/**
* @param array<mixed> $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());
}
}
Loading