1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yokai\SecurityTokenBundle\Tests\Manager; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
7
|
|
|
use Yokai\SecurityTokenBundle\Entity\Token; |
8
|
|
|
use Yokai\SecurityTokenBundle\Factory\TokenFactoryInterface; |
9
|
|
|
use Yokai\SecurityTokenBundle\InformationGuesser\InformationGuesserInterface; |
10
|
|
|
use Yokai\SecurityTokenBundle\Manager\TokenManager; |
11
|
|
|
use Yokai\SecurityTokenBundle\Manager\UserManagerInterface; |
12
|
|
|
use Yokai\SecurityTokenBundle\Repository\TokenRepositoryInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Yann Eugoné <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class TokenManagerTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
21
|
|
|
*/ |
22
|
|
|
private $factory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var TokenRepositoryInterface|ObjectProphecy |
26
|
|
|
*/ |
27
|
|
|
private $repository; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var InformationGuesserInterface|ObjectProphecy |
31
|
|
|
*/ |
32
|
|
|
private $informationGuesser; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var UserManagerInterface|ObjectProphecy |
36
|
|
|
*/ |
37
|
|
|
private $userManager; |
38
|
|
|
|
39
|
|
|
protected function setUp() |
40
|
|
|
{ |
41
|
|
|
$this->factory = $this->createMock(TokenFactoryInterface::class); |
42
|
|
|
$this->repository = $this->prophesize(TokenRepositoryInterface::class); |
43
|
|
|
$this->informationGuesser = $this->prophesize(InformationGuesserInterface::class); |
44
|
|
|
$this->userManager = $this->prophesize(UserManagerInterface::class); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function tearDown() |
48
|
|
|
{ |
49
|
|
|
unset( |
50
|
|
|
$this->factory, |
51
|
|
|
$this->repository, |
52
|
|
|
$this->informationGuesser, |
53
|
|
|
$this->userManager |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function manager() |
58
|
|
|
{ |
59
|
|
|
return new TokenManager( |
60
|
|
|
$this->factory, |
61
|
|
|
$this->repository->reveal(), |
|
|
|
|
62
|
|
|
$this->informationGuesser->reveal(), |
|
|
|
|
63
|
|
|
$this->userManager->reveal() |
|
|
|
|
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @test |
69
|
|
|
*/ |
70
|
|
|
public function it_get_token_from_repository() |
71
|
|
|
{ |
72
|
|
|
$this->repository->get('unique-token', 'forgot_password') |
|
|
|
|
73
|
|
|
->shouldBeCalledTimes(1) |
74
|
|
|
->willReturn($expected = $this->prophesize(Token::class)->reveal()); |
75
|
|
|
|
76
|
|
|
$token = $this->manager()->get('forgot_password', 'unique-token'); |
77
|
|
|
|
78
|
|
|
self::assertSame($expected, $token); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @test |
83
|
|
|
*/ |
84
|
|
|
public function it_create_unique_token() |
85
|
|
|
{ |
86
|
|
|
$token1 = new Token('string', 'jdoe','unique-token-1', 'reset-password', '+1 day', []); |
87
|
|
|
$token2 = new Token('string', 'jdoe','unique-token-2', 'reset-password', '+1 day', []); |
88
|
|
|
|
89
|
|
|
$this->factory->expects($this->exactly(2)) |
90
|
|
|
->method('create') |
91
|
|
|
->will($this->onConsecutiveCalls($token1, $token2)); |
92
|
|
|
|
93
|
|
|
$this->repository->exists('unique-token-1', 'forgot_password') |
|
|
|
|
94
|
|
|
->shouldBeCalledTimes(1) |
95
|
|
|
->willReturn(true); |
96
|
|
|
$this->repository->exists('unique-token-2', 'forgot_password') |
97
|
|
|
->shouldBeCalledTimes(1) |
98
|
|
|
->willReturn(false); |
99
|
|
|
|
100
|
|
|
$this->repository->create($token1) |
|
|
|
|
101
|
|
|
->shouldNotBeCalled(); |
102
|
|
|
$this->repository->create($token2) |
103
|
|
|
->shouldBeCalledTimes(1); |
104
|
|
|
|
105
|
|
|
$token = $this->manager()->create('forgot_password', 'john-doe'); |
106
|
|
|
|
107
|
|
|
self::assertSame($token2, $token); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @test |
112
|
|
|
*/ |
113
|
|
|
public function it_consume_token() |
114
|
|
|
{ |
115
|
|
|
$token = new Token('string', 'jdoe','unique-token', 'reset-password', '+1 day', []); |
116
|
|
|
|
117
|
|
|
$this->informationGuesser->get() |
|
|
|
|
118
|
|
|
->shouldBeCalledTimes(1) |
119
|
|
|
->willReturn(['some', 'precious', 'information']); |
120
|
|
|
|
121
|
|
|
$this->repository->update($token) |
|
|
|
|
122
|
|
|
->shouldBeCalledTimes(1); |
123
|
|
|
|
124
|
|
|
$this->manager()->setUsed($token); |
125
|
|
|
|
126
|
|
|
self::assertSame(['some', 'precious', 'information'], $token->getUsedInformation()); |
127
|
|
|
self::assertInstanceOf(\DateTime::class, $token->getUsedAt()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @test |
132
|
|
|
*/ |
133
|
|
|
public function it_extract_user_from_token() |
134
|
|
|
{ |
135
|
|
|
$token = new Token('string', 'jdoe','unique-token', 'reset-password', '+1 day', []); |
136
|
|
|
|
137
|
|
|
$this->userManager->get('string', 'jdoe') |
|
|
|
|
138
|
|
|
->shouldBeCalledTimes(1) |
139
|
|
|
->willReturn('john doe'); |
140
|
|
|
|
141
|
|
|
$user = $this->manager()->getUser($token); |
142
|
|
|
|
143
|
|
|
self::assertSame('john doe', $user); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: