]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Tests\Wallabag\UserBundle\EventListener; | |
4 | ||
5 | use Monolog\Handler\TestHandler; | |
6 | use Monolog\Logger; | |
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; | |
14 | ||
15 | class AuthenticationFailureListenerTest extends TestCase | |
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 | } |