Passed
Push — issue#767 ( 50feef...0909e2 )
by Guilherme
05:13
created

LocaleListenerTest::testLocaleFromAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\LocaleBundle\Tests\EventListener;
12
13
use LoginCidadao\LocaleBundle\EventListener\LocaleListener;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Session\SessionInterface;
16
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
17
use Symfony\Component\HttpKernel\KernelEvents;
18
19
class LocaleListenerTest extends \PHPUnit_Framework_TestCase
20
{
21
22
    public function testGetSubscribedEvents()
23
    {
24
        $this->assertSame([
25
            KernelEvents::REQUEST => [['onKernelRequest', 17]],
26
        ], LocaleListener::getSubscribedEvents());
27
    }
28
29
    public function testOnKernelRequestNoSession()
30
    {
31
        $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')
32
            ->disableOriginalConstructor()->getMock();
33
        $event->expects($this->once())
34
            ->method('getRequest')
35
            ->willReturn(new Request());
36
37
        $listener = new LocaleListener('en');
38
        $listener->onKernelRequest($event);
39
    }
40
41
    public function testLocaleFromAttributes()
42
    {
43
        $locale = 'pt_BR';
44
        $sessionName = 'sessionName';
45
46
        /** @var SessionInterface|\PHPUnit_Framework_MockObject_MockObject $session */
47
        $session = $this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface');
48
        $session->expects($this->once())->method('getName')->willReturn($sessionName);
49
        $session->expects($this->once())->method('set')->with('_locale', $locale);
50
51
        $request = new Request([], [], ['_locale' => $locale], [$sessionName => 'something']);
52
        $request->setSession($session);
53
54
        /** @var GetResponseEvent|\PHPUnit_Framework_MockObject_MockObject $event */
55
        $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')
56
            ->disableOriginalConstructor()->getMock();
57
        $event->expects($this->once())
58
            ->method('getRequest')
59
            ->willReturn($request);
60
61
        $listener = new LocaleListener('en');
62
        $listener->onKernelRequest($event);
63
    }
64
65
    public function testLocaleFromSession()
66
    {
67
        $locale = 'pt_BR';
68
        $sessionName = 'sessionName';
69
70
        /** @var SessionInterface|\PHPUnit_Framework_MockObject_MockObject $session */
71
        $session = $this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface');
72
        $session->expects($this->once())->method('getName')->willReturn($sessionName);
73
        $session->expects($this->once())->method('get')->with('_locale', 'en')->willReturn($locale);
74
75
        $request = new Request([], [], [], [$sessionName => 'something']);
76
        $request->setSession($session);
77
78
        /** @var GetResponseEvent|\PHPUnit_Framework_MockObject_MockObject $event */
79
        $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')
80
            ->disableOriginalConstructor()->getMock();
81
        $event->expects($this->once())
82
            ->method('getRequest')
83
            ->willReturn($request);
84
85
        $listener = new LocaleListener('en');
86
        $listener->onKernelRequest($event);
87
88
        $this->assertSame($locale, $request->getLocale());
89
    }
90
}
91