]>
Commit | Line | Data |
---|---|---|
f81a34e3 JB |
1 | <?php |
2 | ||
3 | namespace Tests\Wallabag\UserBundle\EventListener; | |
4 | ||
f808b016 JB |
5 | use Monolog\Handler\TestHandler; |
6 | use Monolog\Logger; | |
f81a34e3 JB |
7 | use Symfony\Component\EventDispatcher\EventDispatcher; |
8 | use Symfony\Component\HttpFoundation\Request; | |
f81a34e3 JB |
9 | use Symfony\Component\HttpFoundation\RequestStack; |
10 | use Symfony\Component\Security\Core\AuthenticationEvents; | |
11 | use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent; | |
f808b016 | 12 | use Wallabag\UserBundle\EventListener\AuthenticationFailureListener; |
f81a34e3 JB |
13 | |
14 | class AuthenticationFailureListenerTest extends \PHPUnit_Framework_TestCase | |
15 | { | |
16 | private $requestStack; | |
17 | private $logHandler; | |
18 | private $listener; | |
19 | private $dispatcher; | |
20 | ||
21 | protected function setUp() | |
22 | { | |
23 | $request = Request::create('/'); | |
24 | $request->request->set('_username', 'admin'); | |
25 | ||
26 | $this->requestStack = new RequestStack(); | |
27 | $this->requestStack->push($request); | |
28 | ||
29 | $this->logHandler = new TestHandler(); | |
30 | $logger = new Logger('test', [$this->logHandler]); | |
31 | ||
32 | $this->listener = new AuthenticationFailureListener( | |
33 | $this->requestStack, | |
34 | $logger | |
35 | ); | |
36 | ||
37 | $this->dispatcher = new EventDispatcher(); | |
38 | $this->dispatcher->addSubscriber($this->listener); | |
39 | } | |
40 | ||
41 | public function testOnAuthenticationFailure() | |
42 | { | |
43 | $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface') | |
44 | ->disableOriginalConstructor() | |
45 | ->getMock(); | |
46 | ||
47 | $exception = $this->getMockBuilder('Symfony\Component\Security\Core\Exception\AuthenticationException') | |
48 | ->disableOriginalConstructor() | |
49 | ->getMock(); | |
50 | ||
51 | $event = new AuthenticationFailureEvent( | |
52 | $token, | |
53 | $exception | |
54 | ); | |
55 | ||
56 | $this->dispatcher->dispatch( | |
57 | AuthenticationEvents::AUTHENTICATION_FAILURE, | |
58 | $event | |
59 | ); | |
60 | ||
61 | $records = $this->logHandler->getRecords(); | |
62 | ||
63 | $this->assertCount(1, $records); | |
64 | $this->assertSame('Authentication failure for user "admin", from IP "127.0.0.1", with UA: "Symfony/3.X".', $records[0]['message']); | |
65 | } | |
66 | } |