Skip to content

Commit

Permalink
Check if method getLocale exists before to use it (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored and OskarStark committed Jul 27, 2019
1 parent 330dee8 commit 961657b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/EventSubscriber/UserLocaleSubscriber.php
Expand Up @@ -40,7 +40,7 @@ public function onInteractiveLogin(InteractiveLoginEvent $event)
{
$user = $event->getAuthenticationToken()->getUser();

if (null !== $user->getLocale()) {
if (\is_callable([$user, 'getLocale']) && null !== $user->getLocale()) {
$this->session->set('_locale', $user->getLocale());
}
}
Expand Down
39 changes: 39 additions & 0 deletions tests/EventSubscriber/LocalizedUser.php
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\TranslationBundle\Tests\EventSubscriber;

use Symfony\Component\Security\Core\User\UserInterface;

/**
* @author Jonathan Vautrin <jvautrin@pro-info.be>
*/
class LocalizedUser extends User implements UserInterface
{
private $locale;

public function __construct(string $locale)
{
$this->locale = $locale;
}

public function getLocale()
{
return $this->locale;
}

public function setLocale($locale)
{
$this->locale = $locale;
}
}
17 changes: 0 additions & 17 deletions tests/EventSubscriber/User.php
Expand Up @@ -20,13 +20,6 @@
*/
class User implements UserInterface
{
private $locale;

public function __construct($locale)
{
$this->locale = $locale;
}

public function getRoles()
{
return [];
Expand All @@ -50,14 +43,4 @@ public function getUsername()
public function eraseCredentials()
{
}

public function getLocale()
{
return $this->locale;
}

public function setLocale($locale)
{
$this->locale = $locale;
}
}
18 changes: 14 additions & 4 deletions tests/EventSubscriber/UserLocaleSubscriberTest.php
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;

/**
Expand All @@ -28,21 +29,30 @@ class UserLocaleSubscriberTest extends TestCase
{
/**
* Check if session locale is set to user locale at login.
*
* @dataProvider userLocaleSubscriberDataProvider
*/
public function testUserLocaleSubscriber()
public function testUserLocaleSubscriber(UserInterface $user, string $expectedLocale): void
{
$session = new Session(new MockArraySessionStorage());
$session->set('_locale', 'en');
$request = new Request();
$request->setSession($session);
$user = new User('fr');
$event = $this->getEvent($request, $user);
$userLocaleSubscriber = new UserLocaleSubscriber($session);
$userLocaleSubscriber->onInteractiveLogin($event);
$this->assertSame('fr', $session->get('_locale'));
$this->assertSame($expectedLocale, $session->get('_locale'));
}

private function getEvent(Request $request, User $user)
public function userLocaleSubscriberDataProvider(): array
{
return [
[new LocalizedUser('fr'), 'fr'],
[new User(), 'en'],
];
}

private function getEvent(Request $request, UserInterface $user): InteractiveLoginEvent
{
$token = new UsernamePasswordToken($user, null, 'dev', []);

Expand Down

0 comments on commit 961657b

Please sign in to comment.