Completed
Pull Request — 2.x (#251)
by
unknown
02:11 queued 40s
created

UserLocaleSubscriberTest::getEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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