]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/UserBundle/EventListener/AuthenticationFailureListenerTest.php
Use namespaced PHPUnit classes
[github/wallabag/wallabag.git] / tests / Wallabag / UserBundle / EventListener / AuthenticationFailureListenerTest.php
CommitLineData
f81a34e3
JB
1<?php
2
3namespace Tests\Wallabag\UserBundle\EventListener;
4
f808b016
JB
5use Monolog\Handler\TestHandler;
6use Monolog\Logger;
bd91bd5c 7use PHPUnit\Framework\TestCase;
f81a34e3
JB
8use Symfony\Component\EventDispatcher\EventDispatcher;
9use Symfony\Component\HttpFoundation\Request;
f81a34e3
JB
10use Symfony\Component\HttpFoundation\RequestStack;
11use Symfony\Component\Security\Core\AuthenticationEvents;
12use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
f808b016 13use Wallabag\UserBundle\EventListener\AuthenticationFailureListener;
f81a34e3 14
bd91bd5c 15class AuthenticationFailureListenerTest extends TestCase
f81a34e3
JB
16{
17 private $requestStack;
18 private $logHandler;
19 private $listener;
20 private $dispatcher;
21
22 protected function setUp()
23 {
24 $request = Request::create('/');
25 $request->request->set('_username', 'admin');
26
27 $this->requestStack = new RequestStack();
28 $this->requestStack->push($request);
29
30 $this->logHandler = new TestHandler();
31 $logger = new Logger('test', [$this->logHandler]);
32
33 $this->listener = new AuthenticationFailureListener(
34 $this->requestStack,
35 $logger
36 );
37
38 $this->dispatcher = new EventDispatcher();
39 $this->dispatcher->addSubscriber($this->listener);
40 }
41
42 public function testOnAuthenticationFailure()
43 {
44 $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')
45 ->disableOriginalConstructor()
46 ->getMock();
47
48 $exception = $this->getMockBuilder('Symfony\Component\Security\Core\Exception\AuthenticationException')
49 ->disableOriginalConstructor()
50 ->getMock();
51
52 $event = new AuthenticationFailureEvent(
53 $token,
54 $exception
55 );
56
57 $this->dispatcher->dispatch(
58 AuthenticationEvents::AUTHENTICATION_FAILURE,
59 $event
60 );
61
62 $records = $this->logHandler->getRecords();
63
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']);
66 }
67}