diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2015-10-16 07:40:09 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2015-10-16 07:40:09 +0200 |
commit | 2aac2f278f1ca63f2097f80ddbdb924dea9ec59e (patch) | |
tree | cc09f37927d4a7ffbf576f636db0d36015190b08 /src/Wallabag | |
parent | c89d35e851d26b78f89bd7ece5e3eaa109c8cac0 (diff) | |
download | wallabag-2aac2f278f1ca63f2097f80ddbdb924dea9ec59e.tar.gz wallabag-2aac2f278f1ca63f2097f80ddbdb924dea9ec59e.tar.zst wallabag-2aac2f278f1ca63f2097f80ddbdb924dea9ec59e.zip |
Add tests on listeners
Diffstat (limited to 'src/Wallabag')
4 files changed, 147 insertions, 0 deletions
diff --git a/src/Wallabag/CoreBundle/EventListener/LocaleListener.php b/src/Wallabag/CoreBundle/EventListener/LocaleListener.php index 71795316..80f59504 100644 --- a/src/Wallabag/CoreBundle/EventListener/LocaleListener.php +++ b/src/Wallabag/CoreBundle/EventListener/LocaleListener.php | |||
@@ -6,6 +6,9 @@ use Symfony\Component\HttpKernel\Event\GetResponseEvent; | |||
6 | use Symfony\Component\HttpKernel\KernelEvents; | 6 | use Symfony\Component\HttpKernel\KernelEvents; |
7 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | 7 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
8 | 8 | ||
9 | /** | ||
10 | * @see http://symfony.com/doc/current/cookbook/session/locale_sticky_session.html | ||
11 | */ | ||
9 | class LocaleListener implements EventSubscriberInterface | 12 | class LocaleListener implements EventSubscriberInterface |
10 | { | 13 | { |
11 | private $defaultLocale; | 14 | private $defaultLocale; |
diff --git a/src/Wallabag/CoreBundle/EventListener/UserLocaleListener.php b/src/Wallabag/CoreBundle/EventListener/UserLocaleListener.php index 97bfabc8..82d1a63a 100644 --- a/src/Wallabag/CoreBundle/EventListener/UserLocaleListener.php +++ b/src/Wallabag/CoreBundle/EventListener/UserLocaleListener.php | |||
@@ -8,6 +8,8 @@ use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; | |||
8 | /** | 8 | /** |
9 | * Stores the locale of the user in the session after the | 9 | * Stores the locale of the user in the session after the |
10 | * login. This can be used by the LocaleListener afterwards. | 10 | * login. This can be used by the LocaleListener afterwards. |
11 | * | ||
12 | * @see http://symfony.com/doc/master/cookbook/session/locale_sticky_session.html | ||
11 | */ | 13 | */ |
12 | class UserLocaleListener | 14 | class UserLocaleListener |
13 | { | 15 | { |
diff --git a/src/Wallabag/CoreBundle/Tests/EventListener/LocaleListenerTest.php b/src/Wallabag/CoreBundle/Tests/EventListener/LocaleListenerTest.php new file mode 100644 index 00000000..356a411e --- /dev/null +++ b/src/Wallabag/CoreBundle/Tests/EventListener/LocaleListenerTest.php | |||
@@ -0,0 +1,83 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Tests\EventListener; | ||
4 | |||
5 | use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
6 | use Symfony\Component\EventDispatcher\EventDispatcher; | ||
7 | use Symfony\Component\HttpFoundation\Request; | ||
8 | use Symfony\Component\HttpKernel\KernelEvents; | ||
9 | use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
10 | use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
11 | use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; | ||
12 | use Symfony\Component\HttpFoundation\Session\Session; | ||
13 | use Wallabag\CoreBundle\EventListener\LocaleListener; | ||
14 | |||
15 | class LocaleListenerTest extends KernelTestCase | ||
16 | { | ||
17 | private function getEvent(Request $request) | ||
18 | { | ||
19 | return new GetResponseEvent($this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'), $request, HttpKernelInterface::MASTER_REQUEST); | ||
20 | } | ||
21 | |||
22 | public function testWithoutSession() | ||
23 | { | ||
24 | $request = Request::create('/'); | ||
25 | |||
26 | $listener = new LocaleListener('fr'); | ||
27 | $event = $this->getEvent($request); | ||
28 | |||
29 | $listener->onKernelRequest($event); | ||
30 | $this->assertEquals('en', $request->getLocale()); | ||
31 | } | ||
32 | |||
33 | public function testWithPreviousSession() | ||
34 | { | ||
35 | $request = Request::create('/'); | ||
36 | // generate a previous session | ||
37 | $request->cookies->set('MOCKSESSID', 'foo'); | ||
38 | $request->setSession(new Session(new MockArraySessionStorage())); | ||
39 | |||
40 | $listener = new LocaleListener('fr'); | ||
41 | $event = $this->getEvent($request); | ||
42 | |||
43 | $listener->onKernelRequest($event); | ||
44 | $this->assertEquals('fr', $request->getLocale()); | ||
45 | } | ||
46 | |||
47 | public function testLocaleFromRequestAttribute() | ||
48 | { | ||
49 | $request = Request::create('/'); | ||
50 | // generate a previous session | ||
51 | $request->cookies->set('MOCKSESSID', 'foo'); | ||
52 | $request->setSession(new Session(new MockArraySessionStorage())); | ||
53 | $request->attributes->set('_locale', 'es'); | ||
54 | |||
55 | $listener = new LocaleListener('fr'); | ||
56 | $event = $this->getEvent($request); | ||
57 | |||
58 | $listener->onKernelRequest($event); | ||
59 | $this->assertEquals('en', $request->getLocale()); | ||
60 | $this->assertEquals('es', $request->getSession()->get('_locale')); | ||
61 | } | ||
62 | |||
63 | public function testSubscribedEvents() | ||
64 | { | ||
65 | $request = Request::create('/'); | ||
66 | // generate a previous session | ||
67 | $request->cookies->set('MOCKSESSID', 'foo'); | ||
68 | $request->setSession(new Session(new MockArraySessionStorage())); | ||
69 | |||
70 | $listener = new LocaleListener('fr'); | ||
71 | $event = $this->getEvent($request); | ||
72 | |||
73 | $dispatcher = new EventDispatcher(); | ||
74 | $dispatcher->addSubscriber($listener); | ||
75 | |||
76 | $dispatcher->dispatch( | ||
77 | KernelEvents::REQUEST, | ||
78 | $event | ||
79 | ); | ||
80 | |||
81 | $this->assertEquals('fr', $request->getLocale()); | ||
82 | } | ||
83 | } | ||
diff --git a/src/Wallabag/CoreBundle/Tests/EventListener/UserLocaleListenerTest.php b/src/Wallabag/CoreBundle/Tests/EventListener/UserLocaleListenerTest.php new file mode 100644 index 00000000..e8a65fbf --- /dev/null +++ b/src/Wallabag/CoreBundle/Tests/EventListener/UserLocaleListenerTest.php | |||
@@ -0,0 +1,59 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Tests\EventListener; | ||
4 | |||
5 | use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
6 | use Symfony\Component\HttpFoundation\Request; | ||
7 | use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; | ||
8 | use Symfony\Component\HttpFoundation\Session\Session; | ||
9 | use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; | ||
10 | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | ||
11 | use Wallabag\CoreBundle\EventListener\UserLocaleListener; | ||
12 | use Wallabag\CoreBundle\Entity\Config; | ||
13 | use Wallabag\UserBundle\Entity\User; | ||
14 | |||
15 | class UserLocaleListenerTest extends KernelTestCase | ||
16 | { | ||
17 | public function testWithLanguage() | ||
18 | { | ||
19 | $session = new Session(new MockArraySessionStorage()); | ||
20 | $listener = new UserLocaleListener($session); | ||
21 | |||
22 | $user = new User(); | ||
23 | $user->setEnabled(true); | ||
24 | |||
25 | $config = new Config($user); | ||
26 | $config->setLanguage('fr'); | ||
27 | |||
28 | $user->setConfig($config); | ||
29 | |||
30 | $userToken = new UsernamePasswordToken($user, '', 'test'); | ||
31 | $request = Request::create('/'); | ||
32 | $event = new InteractiveLoginEvent($request, $userToken); | ||
33 | |||
34 | $listener->onInteractiveLogin($event); | ||
35 | |||
36 | $this->assertEquals('fr', $session->get('_locale')); | ||
37 | } | ||
38 | |||
39 | public function testWithoutLanguage() | ||
40 | { | ||
41 | $session = new Session(new MockArraySessionStorage()); | ||
42 | $listener = new UserLocaleListener($session); | ||
43 | |||
44 | $user = new User(); | ||
45 | $user->setEnabled(true); | ||
46 | |||
47 | $config = new Config($user); | ||
48 | |||
49 | $user->setConfig($config); | ||
50 | |||
51 | $userToken = new UsernamePasswordToken($user, '', 'test'); | ||
52 | $request = Request::create('/'); | ||
53 | $event = new InteractiveLoginEvent($request, $userToken); | ||
54 | |||
55 | $listener->onInteractiveLogin($event); | ||
56 | |||
57 | $this->assertEquals('', $session->get('_locale')); | ||
58 | } | ||
59 | } | ||