|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LoginCidadao\CoreBundle\Tests\Handler; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
14
|
|
|
use LoginCidadao\CoreBundle\Handler\AuthenticationSuccessHandler; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
17
|
|
|
use Symfony\Component\Security\Http\HttpUtils; |
|
18
|
|
|
|
|
19
|
|
|
class AuthenticationSuccessHandlerTest extends \PHPUnit_Framework_TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
public function testOnAuthenticationSuccess() |
|
23
|
|
|
{ |
|
24
|
|
|
$ip = '::1'; |
|
25
|
|
|
$username = 'username'; |
|
26
|
|
|
|
|
27
|
|
|
$repo = $this->getMockBuilder('Doctrine\ORM\EntityRepository')->disableOriginalConstructor()->getMock(); |
|
28
|
|
|
$repo->expects($this->once()) |
|
29
|
|
|
->method('findOneBy')->with(['ip' => $ip, 'username' => $username]) |
|
30
|
|
|
->willReturn(null); |
|
31
|
|
|
|
|
32
|
|
|
/** @var EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject $em */ |
|
33
|
|
|
$em = $this->getMock('Doctrine\ORM\EntityManagerInterface'); |
|
34
|
|
|
$em->expects($this->once())->method('persist') |
|
35
|
|
|
->with($this->isInstanceOf('LoginCidadao\CoreBundle\Entity\AccessSession')); |
|
36
|
|
|
$em->expects($this->once())->method('flush'); |
|
37
|
|
|
$em->expects($this->once())->method('getRepository')->with('LoginCidadaoCoreBundle:AccessSession') |
|
38
|
|
|
->willReturn($repo); |
|
39
|
|
|
|
|
40
|
|
|
/** @var HttpUtils $httpUtils */ |
|
41
|
|
|
$httpUtils = $this->getMock('Symfony\Component\Security\Http\HttpUtils'); |
|
42
|
|
|
|
|
43
|
|
|
/** @var Request|\PHPUnit_Framework_MockObject_MockObject $request */ |
|
44
|
|
|
$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request') |
|
45
|
|
|
->disableOriginalConstructor()->getMock(); |
|
46
|
|
|
$request->expects($this->once())->method('getClientIp')->willReturn($ip); |
|
47
|
|
|
$request->expects($this->atLeastOnce())->method('get') |
|
48
|
|
|
->willReturn(['username' => $username]); |
|
49
|
|
|
|
|
50
|
|
|
/** @var TokenInterface $token */ |
|
51
|
|
|
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); |
|
52
|
|
|
|
|
53
|
|
|
$handler = new AuthenticationSuccessHandler($httpUtils, $em, []); |
|
54
|
|
|
$handler->onAuthenticationSuccess($request, $token); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|