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

AdminLocaleListenerTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 20 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 8
loc 40
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testListener() 0 25 5
A requestDataProvider() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
Documentation introduced by
$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...
Documentation introduced by
$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...
Documentation introduced by
$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
Documentation introduced by
$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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
    {
49
        return [
50
            ['/en/admin/', true, 1],
51
            ['/en/whatever/', false, 0],
52
            ['/en/admin/preview/', true, 1],
53
        ];
54
    }
55
}
56