3 namespace Tests\Wallabag\UserBundle\EventListener
;
5 use Monolog\Handler\TestHandler
;
7 use PHPUnit\Framework\TestCase
;
8 use Symfony\Component\EventDispatcher\EventDispatcher
;
9 use Symfony\Component\HttpFoundation\Request
;
10 use Symfony\Component\HttpFoundation\RequestStack
;
11 use Symfony\Component\Security\Core\AuthenticationEvents
;
12 use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent
;
13 use Wallabag\UserBundle\EventListener\AuthenticationFailureListener
;
15 class AuthenticationFailureListenerTest
extends TestCase
17 private $requestStack;
22 protected function setUp()
24 $request = Request
::create('/');
25 $request->request
->set('_username', 'admin');
27 $this->requestStack
= new RequestStack();
28 $this->requestStack
->push($request);
30 $this->logHandler
= new TestHandler();
31 $logger = new Logger('test', [$this->logHandler
]);
33 $this->listener
= new AuthenticationFailureListener(
38 $this->dispatcher
= new EventDispatcher();
39 $this->dispatcher
->addSubscriber($this->listener
);
42 public function testOnAuthenticationFailure()
44 $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')
45 ->disableOriginalConstructor()
48 $exception = $this->getMockBuilder('Symfony\Component\Security\Core\Exception\AuthenticationException')
49 ->disableOriginalConstructor()
52 $event = new AuthenticationFailureEvent(
57 $this->dispatcher
->dispatch(
58 AuthenticationEvents
::AUTHENTICATION_FAILURE
,
62 $records = $this->logHandler
->getRecords();
64 $this->assertCount(1, $records);
65 $this->assertSame('Authentication failure for user "admin", from IP "127.0.0.1", with UA: "Symfony/3.X".', $records[0]['message']);