Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

unit/EventListener/AdminLocaleListenerTest.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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');
0 ignored issues
show
$storage is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...\TokenStorageInterface>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
$trans is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...on\TranslatorInterface>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
$adminRouteHelper is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Kunstmaan\AdminBu...elper\AdminRouteHelper>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
40
41
        $events = AdminLocaleListener::getSubscribedEvents();
42
        $this->assertArrayHasKey(KernelEvents::REQUEST, $events);
43
44
        $listener->onKernelRequest($event);
0 ignored issues
show
$event is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...el\Event\ResponseEvent>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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