Completed
Push — master ( 900267...e0fdb0 )
by Jeroen
06:53 queued 14s
created

PasswordCheckListenerTest::testListener()   B

Complexity

Conditions 9
Paths 1

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.0555
c 0
b 0
f 0
cc 9
nc 1
nop 3
1
<?php
2
3
namespace Kunstmaan\AdminBundle\Tests\EventListener;
4
5
use Kunstmaan\AdminBundle\Entity\User;
6
use Kunstmaan\AdminBundle\EventListener\PasswordCheckListener;
7
use Kunstmaan\AdminBundle\Helper\AdminRouteHelper;
8
use PHPUnit\Framework\TestCase;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
11
use Symfony\Component\HttpFoundation\Session\Session;
12
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
13
use Symfony\Component\Routing\RouterInterface;
14
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
15
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
16
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
17
use Symfony\Component\Translation\Translator;
18
19
class PasswordCheckListenerTest extends TestCase
20
{
21
    /**
22
     * @dataProvider requestDataProvider
23
     */
24
    public function testListener($uri, $shouldPerformCheck, $tokenStorageCallCount)
25
    {
26
        $request = new Request([], [], [], [], [], ['REQUEST_URI' => $uri]);
27
        $auth = $this->createMock(AuthorizationCheckerInterface::class);
28
        $storage = $this->createMock(TokenStorageInterface::class);
29
        $token = $this->createMock(UsernamePasswordToken::class);
30
        $user = $user = $this->createMock(User::class);
31
        $router = $this->createMock(RouterInterface::class);
32
        $session = $this->createMock(Session::class);
33
        $flash = $this->createMock(FlashBag::class);
34
        $trans = $this->createMock(Translator::class);
35
        $adminRouteHelper = $this->createMock(AdminRouteHelper::class);
36
        $event = $this->createMock(GetResponseEvent::class);
37
38
        $storage->expects($this->exactly($tokenStorageCallCount))->method('getToken')->willReturn($token);
39
        $event->expects($this->exactly($shouldPerformCheck ? 2 : 1))->method('getRequest')->willReturn($request);
40
        $auth->expects($this->exactly($shouldPerformCheck ? 1 : 0))->method('isGranted')->willReturn(true);
41
        $token->expects($this->exactly($shouldPerformCheck ? 1 : 0))->method('getUser')->willReturn($user);
42
        $user->expects($this->exactly($shouldPerformCheck ? 1 : 0))->method('isPasswordChanged')->willReturn(false);
43
        $router->expects($this->exactly($shouldPerformCheck ? 1 : 0))->method('generate')->willReturn(true);
44
        $session->expects($this->exactly($shouldPerformCheck ? 1 : 0))->method('getFlashBag')->willReturn($flash);
45
        $flash->expects($this->exactly($shouldPerformCheck ? 1 : 0))->method('add')->willReturn(true);
46
        $trans->expects($this->exactly($shouldPerformCheck ? 1 : 0))->method('trans')->willReturn(true);
47
        $adminRouteHelper->method('isAdminRoute')->willReturn($shouldPerformCheck);
48
49
        $listener = new PasswordCheckListener($auth, $storage, $router, $session, $trans, $adminRouteHelper);
0 ignored issues
show
Documentation introduced by
$auth is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...zationCheckerInterface>.

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
$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
$router is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component\Routing\RouterInterface>.

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
$session is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...dation\Session\Session>.

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...
50
        $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...
51
    }
52
53 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...
54
    {
55
        return [
56
            ['/en/admin/', true, 2],
57
            ['/en/random', false, 0],
58
            ['/en/admin/preview/', true, 2],
59
        ];
60
    }
61
}
62