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() |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
['/en/admin/', true, 2], |
57
|
|
|
['/en/random', false, 0], |
58
|
|
|
['/en/admin/preview/', true, 2], |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
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: