aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2015-10-16 07:40:09 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2015-10-16 07:40:09 +0200
commit2aac2f278f1ca63f2097f80ddbdb924dea9ec59e (patch)
treecc09f37927d4a7ffbf576f636db0d36015190b08 /src/Wallabag
parentc89d35e851d26b78f89bd7ece5e3eaa109c8cac0 (diff)
downloadwallabag-2aac2f278f1ca63f2097f80ddbdb924dea9ec59e.tar.gz
wallabag-2aac2f278f1ca63f2097f80ddbdb924dea9ec59e.tar.zst
wallabag-2aac2f278f1ca63f2097f80ddbdb924dea9ec59e.zip
Add tests on listeners
Diffstat (limited to 'src/Wallabag')
-rw-r--r--src/Wallabag/CoreBundle/EventListener/LocaleListener.php3
-rw-r--r--src/Wallabag/CoreBundle/EventListener/UserLocaleListener.php2
-rw-r--r--src/Wallabag/CoreBundle/Tests/EventListener/LocaleListenerTest.php83
-rw-r--r--src/Wallabag/CoreBundle/Tests/EventListener/UserLocaleListenerTest.php59
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;
6use Symfony\Component\HttpKernel\KernelEvents; 6use Symfony\Component\HttpKernel\KernelEvents;
7use Symfony\Component\EventDispatcher\EventSubscriberInterface; 7use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8 8
9/**
10 * @see http://symfony.com/doc/current/cookbook/session/locale_sticky_session.html
11 */
9class LocaleListener implements EventSubscriberInterface 12class 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 */
12class UserLocaleListener 14class 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
3namespace Wallabag\CoreBundle\Tests\EventListener;
4
5use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
6use Symfony\Component\EventDispatcher\EventDispatcher;
7use Symfony\Component\HttpFoundation\Request;
8use Symfony\Component\HttpKernel\KernelEvents;
9use Symfony\Component\HttpKernel\Event\GetResponseEvent;
10use Symfony\Component\HttpKernel\HttpKernelInterface;
11use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
12use Symfony\Component\HttpFoundation\Session\Session;
13use Wallabag\CoreBundle\EventListener\LocaleListener;
14
15class 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
3namespace Wallabag\CoreBundle\Tests\EventListener;
4
5use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
6use Symfony\Component\HttpFoundation\Request;
7use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
8use Symfony\Component\HttpFoundation\Session\Session;
9use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
10use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
11use Wallabag\CoreBundle\EventListener\UserLocaleListener;
12use Wallabag\CoreBundle\Entity\Config;
13use Wallabag\UserBundle\Entity\User;
14
15class 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}