diff options
Diffstat (limited to 'tests/Wallabag/CoreBundle/Event/Listener/LocaleListenerTest.php')
-rw-r--r-- | tests/Wallabag/CoreBundle/Event/Listener/LocaleListenerTest.php | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/tests/Wallabag/CoreBundle/Event/Listener/LocaleListenerTest.php b/tests/Wallabag/CoreBundle/Event/Listener/LocaleListenerTest.php new file mode 100644 index 00000000..84a54d3a --- /dev/null +++ b/tests/Wallabag/CoreBundle/Event/Listener/LocaleListenerTest.php | |||
@@ -0,0 +1,86 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\CoreBundle\Event\Listener; | ||
4 | |||
5 | use Symfony\Component\EventDispatcher\EventDispatcher; | ||
6 | use Symfony\Component\HttpFoundation\Request; | ||
7 | use Symfony\Component\HttpFoundation\Session\Session; | ||
8 | use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; | ||
9 | use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
10 | use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
11 | use Symfony\Component\HttpKernel\KernelEvents; | ||
12 | use Wallabag\CoreBundle\Event\Listener\LocaleListener; | ||
13 | |||
14 | class LocaleListenerTest extends \PHPUnit_Framework_TestCase | ||
15 | { | ||
16 | private function getEvent(Request $request) | ||
17 | { | ||
18 | $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface') | ||
19 | ->disableOriginalConstructor() | ||
20 | ->getMock(); | ||
21 | |||
22 | return new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); | ||
23 | } | ||
24 | |||
25 | public function testWithoutSession() | ||
26 | { | ||
27 | $request = Request::create('/'); | ||
28 | |||
29 | $listener = new LocaleListener('fr'); | ||
30 | $event = $this->getEvent($request); | ||
31 | |||
32 | $listener->onKernelRequest($event); | ||
33 | $this->assertEquals('en', $request->getLocale()); | ||
34 | } | ||
35 | |||
36 | public function testWithPreviousSession() | ||
37 | { | ||
38 | $request = Request::create('/'); | ||
39 | // generate a previous session | ||
40 | $request->cookies->set('MOCKSESSID', 'foo'); | ||
41 | $request->setSession(new Session(new MockArraySessionStorage())); | ||
42 | |||
43 | $listener = new LocaleListener('fr'); | ||
44 | $event = $this->getEvent($request); | ||
45 | |||
46 | $listener->onKernelRequest($event); | ||
47 | $this->assertEquals('fr', $request->getLocale()); | ||
48 | } | ||
49 | |||
50 | public function testLocaleFromRequestAttribute() | ||
51 | { | ||
52 | $request = Request::create('/'); | ||
53 | // generate a previous session | ||
54 | $request->cookies->set('MOCKSESSID', 'foo'); | ||
55 | $request->setSession(new Session(new MockArraySessionStorage())); | ||
56 | $request->attributes->set('_locale', 'es'); | ||
57 | |||
58 | $listener = new LocaleListener('fr'); | ||
59 | $event = $this->getEvent($request); | ||
60 | |||
61 | $listener->onKernelRequest($event); | ||
62 | $this->assertEquals('en', $request->getLocale()); | ||
63 | $this->assertEquals('es', $request->getSession()->get('_locale')); | ||
64 | } | ||
65 | |||
66 | public function testSubscribedEvents() | ||
67 | { | ||
68 | $request = Request::create('/'); | ||
69 | // generate a previous session | ||
70 | $request->cookies->set('MOCKSESSID', 'foo'); | ||
71 | $request->setSession(new Session(new MockArraySessionStorage())); | ||
72 | |||
73 | $listener = new LocaleListener('fr'); | ||
74 | $event = $this->getEvent($request); | ||
75 | |||
76 | $dispatcher = new EventDispatcher(); | ||
77 | $dispatcher->addSubscriber($listener); | ||
78 | |||
79 | $dispatcher->dispatch( | ||
80 | KernelEvents::REQUEST, | ||
81 | $event | ||
82 | ); | ||
83 | |||
84 | $this->assertEquals('fr', $request->getLocale()); | ||
85 | } | ||
86 | } | ||