Skip to content

Commit

Permalink
Upgrade request handling to Symfony 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Staroverov committed Dec 1, 2021
1 parent 4aa8564 commit 70d2add
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 14 deletions.
4 changes: 2 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class Configuration implements ConfigurationInterface
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('vipx_bot_detect');
$treeBuilder = new TreeBuilder('vipx_bot_detect');
$rootNode = $treeBuilder->getRootNode();

$rootNode->children()
->scalarNode('metadata_file')->defaultValue('basic_bot.yml')->end()
Expand Down
6 changes: 3 additions & 3 deletions Security/BotAuthenticationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Vipx\BotDetectBundle\Security;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
Expand Down Expand Up @@ -39,9 +39,9 @@ public function __construct(TokenStorageInterface $tokenStorage, BotDetectorInte
/**
* Listens to the kernel.request events and sets a bot token if the visitor is a spider or crawler
*
* @param GetResponseEvent $event
* @param ResponseEvent $event
*/
public function onKernelRequest(GetResponseEvent $event)
public function onKernelRequest(ResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
Expand Down
2 changes: 1 addition & 1 deletion Security/BotToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function getMetadata()
*/
public static function fromAnonymousToken(MetadataInterface $metadata, AnonymousToken $token)
{
return new self($token->getSecret(), $metadata, $token->getRoles());
return new self($token->getSecret(), $metadata, $token->getRoleNames());
}

}
10 changes: 8 additions & 2 deletions Tests/Security/BotAuthenticationListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Vipx\BotDetect\BotDetectorInterface;
use Vipx\BotDetect\Metadata\MetadataInterface;
use Vipx\BotDetectBundle\Security\BotAuthenticationListener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Vipx\BotDetectBundle\Security\BotToken;
Expand Down Expand Up @@ -61,7 +62,12 @@ public function testSettingToken()

/** @var HttpKernelInterface $kernel */
$kernel = $this->getMockBuilder(HttpKernelInterface::class)->getMock();
$event = new GetResponseEvent($kernel, new Request(), HttpKernelInterface::MASTER_REQUEST);
$event = new ResponseEvent(
$kernel,
new Request(),
HttpKernelInterface::MASTER_REQUEST,
new Response()
);

$listener->onKernelRequest($event);
}
Expand Down
8 changes: 3 additions & 5 deletions Tests/Security/BotTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\Role\Role;
use Vipx\BotDetect\Metadata\MetadataInterface;
use Vipx\BotDetectBundle\Security\BotToken;

Expand All @@ -29,13 +28,12 @@ public function testBotRole()

$this->assertEquals($metadata, $token->getMetadata());

/** @var Role[] $roles */
$roles = $token->getRoles();
$roles = $token->getRoleNames();

$contains = false;

foreach ($roles as $role) {
if ('ROLE_BOT' === $role->getRole()) {
if ('ROLE_BOT' === $role) {
$contains = true;
}
}
Expand All @@ -54,7 +52,7 @@ public function testCanCreateBotTokenFromAnonymousToken()
->getMock();

$anonymousToken->method('getSecret')->willReturn('theSecretKey');
$anonymousToken->method('getRoles')->willReturn([]);
$anonymousToken->method('getRoleNames')->willReturn([]);

$botToken = BotToken::fromAnonymousToken($metadata, $anonymousToken);

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"php": "^5.6|^7.0",
"symfony/framework-bundle": "^2.8|^3.0|^4.0||^5.0",
"symfony/security-bundle": "^2.8|^3.0|^4.0||^5.0",
"vipx/bot-detect": "~2.0"
"vipx/bot-detect": "~2.0",
"symfony/runtime": "^5.4"
},
"require-dev": {
"phpunit/phpunit": "^5.7|^6.5",
Expand Down

0 comments on commit 70d2add

Please sign in to comment.