Completed
Pull Request — 2.x (#251)
by
unknown
01:40
created

UserLocaleSubscriberTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 7
dl 0
loc 25
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testUserLocaleSubscriber() 0 12 1
A getEvent() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\TranslationBundle\Tests\EventSubscriber;
13
14
use PHPUnit\Framework\TestCase;
15
use Sonata\TranslationBundle\EventSubscriber\UserLocaleSubscriber;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Session\Session;
18
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
19
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
20
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
21
22
/**
23
 * @author Jonathan Vautrin <[email protected]>
24
 */
25
class UserLocaleSubscriberTest extends TestCase
26
{
27
    /**
28
     * Check if session locale is set to user locale at login.
29
     */
30
    public function testUserLocaleSubscriber()
31
    {
32
        $session = new Session(new MockArraySessionStorage());
33
        $session->set('_locale', 'en');
34
        $request = new Request();
35
        $request->setSession($session);
36
        $user = new User('fr');
37
        $event = $this->getEvent($request, $user);
38
        $userLocaleSubscriber = new UserLocaleSubscriber($session);
39
        $userLocaleSubscriber->onInteractiveLogin($event);
40
        $this->assertSame('fr', $session->get('_locale'));
41
    }
42
43
    private function getEvent(Request $request, User $user)
44
    {
45
        $token = new UsernamePasswordToken($user, null, 'dev', []);
46
47
        return new InteractiveLoginEvent($request, $token);
48
    }
49
}
50