1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JwPersistentUserTest\Service; |
4
|
|
|
|
5
|
|
|
use JwPersistentUser\Test\TestCase; |
6
|
|
|
use JwPersistentUser\Model\SerieToken; |
7
|
|
|
use JwPersistentUser\Model\ModuleOptions; |
8
|
|
|
use JwPersistentUser\Service\RememberMeService; |
9
|
|
|
use JwPersistentUser\Mapper\SerieTokenMapperInterface; |
10
|
|
|
use JwPersistentUser\Service\UserValidityInterface; |
11
|
|
|
|
12
|
|
|
class RememberMeServiceTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var RememberMeService |
16
|
|
|
*/ |
17
|
|
|
protected $service; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var SerieTokenMapperInterface|\PHPUnit_Framework_MockObject_MockObject |
21
|
|
|
*/ |
22
|
|
|
protected $mapper; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ModuleOptions|\PHPUnit_Framework_MockObject_MockObject |
26
|
|
|
*/ |
27
|
|
|
protected $options; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var UserValidityInterface|\PHPUnit_Framework_MockObject_MockObject |
31
|
|
|
*/ |
32
|
|
|
protected $userValidityInterface; |
33
|
|
|
|
34
|
|
|
public function setUp() |
35
|
|
|
{ |
36
|
|
|
parent::setUp(); |
37
|
|
|
|
38
|
|
|
$this->service = new RememberMeService; |
39
|
|
|
|
40
|
|
|
$this->mapper = $this->getMock('JwPersistentUser\Mapper\SerieTokenMapperInterface'); |
41
|
|
|
$this->service->setMapper($this->mapper); |
42
|
|
|
|
43
|
|
|
$this->userValidityInterface = $this->getMock(UserValidityInterface::class); |
44
|
|
|
$this->service->setUserValidityInterface($this->userValidityInterface); |
45
|
|
|
|
46
|
|
|
$this->service->setModuleOptions($this->options = new ModuleOptions); |
47
|
|
|
$this->options->setSerieTokenEntityClass('JwPersistentUser\Model\SerieToken'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testCreateNew() |
51
|
|
|
{ |
52
|
|
|
$this->mapper->expects($this->once()) |
|
|
|
|
53
|
|
|
->method('persist'); |
54
|
|
|
|
55
|
|
|
$this |
|
|
|
|
56
|
|
|
->userValidityInterface |
57
|
|
|
->expects($this->any()) |
58
|
|
|
->method('getValidUntilForUser') |
59
|
|
|
->with(3) |
60
|
|
|
->willReturn(null); |
61
|
|
|
|
62
|
|
|
$response = $this->service->createNew(3); |
63
|
|
|
|
64
|
|
|
$this->assertEquals(3, $response->getUserId()); |
65
|
|
|
$this->assertEquals(10, strlen($response->getSerie())); |
66
|
|
|
$this->assertEquals(10, strlen($response->getToken())); |
67
|
|
|
$this->assertNull($response->getValidUntil()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testCreateNewWithValidity() |
71
|
|
|
{ |
72
|
|
|
$this->mapper->expects($this->once()) |
|
|
|
|
73
|
|
|
->method('persist'); |
74
|
|
|
|
75
|
|
|
$validUntil = new \DateTime('tomorrow'); |
76
|
|
|
$this |
|
|
|
|
77
|
|
|
->userValidityInterface |
78
|
|
|
->expects($this->any()) |
79
|
|
|
->method('getValidUntilForUser') |
80
|
|
|
->with(3) |
81
|
|
|
->willReturn($validUntil); |
82
|
|
|
|
83
|
|
|
$response = $this->service->createNew(3); |
84
|
|
|
|
85
|
|
|
$this->assertEquals(3, $response->getUserId()); |
86
|
|
|
$this->assertEquals(10, strlen($response->getSerie())); |
87
|
|
|
$this->assertEquals(10, strlen($response->getToken())); |
88
|
|
|
$this->assertEquals($validUntil, $response->getValidUntil()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testRemoveSerie() |
92
|
|
|
{ |
93
|
|
|
$this->assumeSerie($serie = new SerieToken(3, 'abc', 'def')); |
94
|
|
|
$serie->setExpiresAt(new \DateTime('tomorrow')); |
95
|
|
|
|
96
|
|
|
$this->mapper->expects($this->once()) |
|
|
|
|
97
|
|
|
->method('remove') |
98
|
|
|
->with($serie); |
99
|
|
|
|
100
|
|
|
$this->service->removeSerie(3, 'abc'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testGetNextSerieWhenRefreshDisabled() |
104
|
|
|
{ |
105
|
|
|
$this->options->setTokenRefreshedAfterLogin(false); |
|
|
|
|
106
|
|
|
|
107
|
|
|
$this->assumeSerie($serie = new SerieToken(3, 'abc', 'def')); |
108
|
|
|
$serie->setExpiresAt(new \DateTime('tomorrow')); |
109
|
|
|
|
110
|
|
|
$nextSerie = $this->service->getNextInSerie($serie); |
111
|
|
|
|
112
|
|
|
$this->assertSame($serie, $nextSerie); |
113
|
|
|
$this->assertEquals('def', $serie->getToken()); |
114
|
|
|
|
115
|
|
|
$this->assertDateTimeEquals(new \DateTime('+1 year'), $serie->getExpiresAt()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testGetNextSerie() |
119
|
|
|
{ |
120
|
|
|
$this->assumeSerie($serie = new SerieToken(3, 'abc', 'def')); |
121
|
|
|
$serie->setExpiresAt(new \DateTime('tomorrow')); |
122
|
|
|
|
123
|
|
|
$nextSerie = $this->service->getNextInSerie($serie); |
124
|
|
|
|
125
|
|
|
$this->assertSame($serie, $nextSerie); |
126
|
|
|
$this->assertNotEquals('def', $serie->getToken()); |
127
|
|
|
|
128
|
|
|
$this->assertDateTimeEquals(new \DateTime('+1 year'), $serie->getExpiresAt()); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testSerieExpired() |
132
|
|
|
{ |
133
|
|
|
$this->assumeSerie($serie = new SerieToken(3, 'abc', 'def')); |
134
|
|
|
$serie->setExpiresAt(new \DateTime('yesterday')); |
135
|
|
|
|
136
|
|
|
$nextSerie = $this->service->getNextInSerie($serie); |
137
|
|
|
|
138
|
|
|
$this->assertNull($nextSerie); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testNoSerieFound() |
142
|
|
|
{ |
143
|
|
|
$serie = new SerieToken(3, 'abc', 'def'); |
144
|
|
|
$serie->setExpiresAt(new \DateTime('tomorrow')); |
145
|
|
|
|
146
|
|
|
$nextSerie = $this->service->getNextInSerie($serie); |
147
|
|
|
|
148
|
|
|
$this->assertNull($nextSerie); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function testSerieWithInvalidTokenResets() |
152
|
|
|
{ |
153
|
|
|
$this->assumeSerie($correctSerie = new SerieToken(3, 'abc', 'def')); |
154
|
|
|
$correctSerie->setExpiresAt(new \DateTime('tomorrow')); |
155
|
|
|
$falseSerie = new SerieToken(3, 'abc', 'gdaf'); |
156
|
|
|
|
157
|
|
|
$this->mapper->expects($this->once()) |
|
|
|
|
158
|
|
|
->method('remove') |
159
|
|
|
->with($correctSerie); |
160
|
|
|
|
161
|
|
|
$nextSerie = $this->service->getNextInSerie($falseSerie); |
162
|
|
|
|
163
|
|
|
$this->assertNull($nextSerie); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function testNoLongerValidTokenResets() |
167
|
|
|
{ |
168
|
|
|
$this->assumeSerie($serie = new SerieToken(3, 'abc', 'def')); |
169
|
|
|
$serie->setExpiresAt(new \DateTime('tomorrow')); |
170
|
|
|
$serie->setValidUntil(new \DateTime('yesterday')); |
171
|
|
|
|
172
|
|
|
$nextSerie = $this->service->getNextInSerie($serie); |
173
|
|
|
|
174
|
|
|
$this->assertNull($nextSerie); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param SerieToken $serie |
179
|
|
|
*/ |
180
|
|
|
protected function assumeSerie(SerieToken $serie) |
181
|
|
|
{ |
182
|
|
|
$this->mapper->expects($this->any()) |
|
|
|
|
183
|
|
|
->method('find') |
184
|
|
|
->with($serie->getUserId(), $serie->getSerie()) |
185
|
|
|
->will($this->returnValue($serie)); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
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: