|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kunstmaan\AdminBundle\Tests\EventListener; |
|
4
|
|
|
|
|
5
|
|
|
use Kunstmaan\AdminBundle\Entity\User; |
|
6
|
|
|
use Kunstmaan\AdminBundle\EventListener\AdminLocaleListener; |
|
7
|
|
|
use Kunstmaan\AdminBundle\Helper\AdminRouteHelper; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
10
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
|
11
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
12
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
13
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
|
14
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
15
|
|
|
|
|
16
|
|
|
class AdminLocaleListenerTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @dataProvider requestDataProvider |
|
20
|
|
|
*/ |
|
21
|
|
|
public function testListener($uri, $shouldPerformCheck, $tokenStorageCallCount) |
|
22
|
|
|
{ |
|
23
|
|
|
$request = new Request([], [], [], [], [], ['REQUEST_URI' => $uri]); |
|
24
|
|
|
$storage = $this->createMock(TokenStorageInterface::class); |
|
25
|
|
|
$trans = $this->createMock(TranslatorInterface::class); |
|
26
|
|
|
$adminRouteHelper = $this->createMock(AdminRouteHelper::class); |
|
27
|
|
|
$event = $this->createMock(GetResponseEvent::class); |
|
28
|
|
|
$token = $this->createMock(UsernamePasswordToken::class); |
|
29
|
|
|
$user = $this->createMock(User::class); |
|
30
|
|
|
|
|
31
|
|
|
$storage->expects($this->exactly($tokenStorageCallCount))->method('getToken')->willReturn($token); |
|
32
|
|
|
$token->expects($this->exactly($shouldPerformCheck ? 1 : 0))->method('getProviderKey')->willReturn('main'); |
|
33
|
|
|
$token->expects($this->exactly($shouldPerformCheck ? 1 : 0))->method('getUser')->willReturn($user); |
|
34
|
|
|
$event->expects($this->any())->method('getRequest')->willReturn($request); |
|
35
|
|
|
$user->expects($this->exactly($shouldPerformCheck ? 1 : 0))->method('getAdminLocale')->willReturn(null); |
|
36
|
|
|
$trans->expects($this->exactly($shouldPerformCheck ? 1 : 0))->method('setLocale')->willReturn(null); |
|
37
|
|
|
$adminRouteHelper->method('isAdminRoute')->willReturn($shouldPerformCheck); |
|
38
|
|
|
|
|
39
|
|
|
$listener = new AdminLocaleListener($storage, $trans, $adminRouteHelper, 'en'); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
$events = AdminLocaleListener::getSubscribedEvents(); |
|
42
|
|
|
$this->assertArrayHasKey(KernelEvents::REQUEST, $events); |
|
43
|
|
|
|
|
44
|
|
|
$listener->onKernelRequest($event); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
View Code Duplication |
public function requestDataProvider() |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
return [ |
|
50
|
|
|
['/en/admin/', true, 1], |
|
51
|
|
|
['/en/whatever/', false, 0], |
|
52
|
|
|
['/en/admin/preview/', true, 1], |
|
53
|
|
|
]; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: