Completed
Push — master ( 06c1ce...67d37c )
by Jeroen
06:20
created

unit/EventListener/PasswordCheckListenerTest.php (1 issue)

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\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);
50
        $listener->onKernelRequest($event);
51
    }
52
53 View Code Duplication
    public function requestDataProvider()
0 ignored issues
show
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