|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Netdudes\DataSourceryBundle\Tests\Extension; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
|
6
|
|
|
use Doctrine\ORM\EntityManager; |
|
7
|
|
|
use Netdudes\DataSourceryBundle\Extension\BuiltInFunctionsExtension; |
|
8
|
|
|
use Netdudes\DataSourceryBundle\Util\CurrentDateTimeProvider; |
|
9
|
|
|
use Prophecy\Argument; |
|
10
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; |
|
11
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
12
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
13
|
|
|
|
|
14
|
|
|
class BuiltInFunctionsExtensionTest extends \PHPUnit_Framework_TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* This is just used to manually test the function. |
|
18
|
|
|
*/ |
|
19
|
|
|
public function testNow() |
|
20
|
|
|
{ |
|
21
|
|
|
$extension = $this->getExtension(); |
|
22
|
|
|
|
|
23
|
|
|
$todayResult = $extension->now(null); |
|
24
|
|
|
$this->assertSame('2012-06-03T22:22:22+0200', $todayResult, 'The today function result did not produce the expected with no offset'); |
|
25
|
|
|
|
|
26
|
|
|
$offset = '+5 days'; |
|
27
|
|
|
$todayResult = $extension->now($offset); |
|
28
|
|
|
$this->assertSame('2012-06-08T22:22:22+0200', $todayResult, 'The today function result did not produce the expected result with ofset '.$offset); |
|
29
|
|
|
|
|
30
|
|
|
$offset = '-3 days'; |
|
31
|
|
|
$todayResult = $extension->now($offset); |
|
32
|
|
|
$this->assertSame('2012-05-31T22:22:22+0200', $todayResult, 'The today function result did not produce the expected result with ofset '.$offset); |
|
33
|
|
|
|
|
34
|
|
|
$offset = '-6 days - 3 minutes'; |
|
35
|
|
|
$todayResult = $extension->now($offset); |
|
36
|
|
|
$this->assertSame('2012-05-28T22:19:22+0200', $todayResult, 'The today function result did not produce the expected result with ofset '.$offset); |
|
37
|
|
|
|
|
38
|
|
|
$offset = '-30 minutes'; |
|
39
|
|
|
$todayResult = $extension->now($offset); |
|
40
|
|
|
$this->assertSame('2012-06-03T21:52:22+0200', $todayResult, 'The today function result did not produce the expected result with ofset '.$offset); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
View Code Duplication |
public function testStartOfDay() |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
$extension = $this->getExtension(); |
|
46
|
|
|
|
|
47
|
|
|
$startOfDayResult = $extension->startOfDay(); |
|
48
|
|
|
$this->assertSame('2012-06-03T00:00:00+0200', $startOfDayResult, 'The startOfDay function result did not produce the expected result'); |
|
49
|
|
|
|
|
50
|
|
|
$startOfDayResult = $extension->startOfDay('+2 hours'); |
|
51
|
|
|
$this->assertSame('2012-06-04T00:00:00+0200', $startOfDayResult, 'The startOfDay function result did not produce the expected result'); |
|
52
|
|
|
|
|
53
|
|
|
$startOfDayResult = $extension->startOfDay('-5 days'); |
|
54
|
|
|
$this->assertSame('2012-05-29T00:00:00+0200', $startOfDayResult, 'The startOfDay function result did not produce the expected result'); |
|
55
|
|
|
|
|
56
|
|
|
$startOfDayResult = $extension->startOfDay('+1 month'); |
|
57
|
|
|
$this->assertSame('2012-07-03T00:00:00+0200', $startOfDayResult, 'The startOfDay function result did not produce the expected result'); |
|
58
|
|
|
|
|
59
|
|
|
$startOfDayResult = $extension->startOfDay('15-05-2012'); |
|
60
|
|
|
$this->assertSame('2012-05-15T00:00:00+0200', $startOfDayResult, 'The startOfDay function result did not produce the expected result'); |
|
61
|
|
|
|
|
62
|
|
|
$startOfDayResult = $extension->startOfDay('2012-05-15'); |
|
63
|
|
|
$this->assertSame('2012-05-15T00:00:00+0200', $startOfDayResult, 'The startOfDay function result did not produce the expected result'); |
|
64
|
|
|
|
|
65
|
|
|
$startOfDayResult = $extension->startOfDay('15.05.2012'); |
|
66
|
|
|
$this->assertSame('2012-05-15T00:00:00+0200', $startOfDayResult, 'The startOfDay function result did not produce the expected result'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
View Code Duplication |
public function testStartOfWeek() |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
|
|
$extension = $this->getExtension(); |
|
72
|
|
|
|
|
73
|
|
|
$startOfWeekResult = $extension->startOfWeek(); |
|
74
|
|
|
$this->assertSame('2012-05-28T00:00:00+0200', $startOfWeekResult, 'The startOfWeek function result did not produce the expected result'); |
|
75
|
|
|
|
|
76
|
|
|
$startOfWeekResult = $extension->startOfWeek('+2 hours'); |
|
77
|
|
|
$this->assertSame('2012-06-04T00:00:00+0200', $startOfWeekResult, 'The startOfWeek function result did not produce the expected result'); |
|
78
|
|
|
|
|
79
|
|
|
$startOfWeekResult = $extension->startOfWeek('-5 days'); |
|
80
|
|
|
$this->assertSame('2012-05-28T00:00:00+0200', $startOfWeekResult, 'The startOfWeek function result did not produce the expected result'); |
|
81
|
|
|
|
|
82
|
|
|
$startOfWeekResult = $extension->startOfWeek('+1 month'); |
|
83
|
|
|
$this->assertSame('2012-07-02T00:00:00+0200', $startOfWeekResult, 'The startOfWeek function result did not produce the expected result'); |
|
84
|
|
|
|
|
85
|
|
|
$startOfWeekResult = $extension->startOfWeek('15-05-2012'); |
|
86
|
|
|
$this->assertSame('2012-05-14T00:00:00+0200', $startOfWeekResult, 'The startOfWeek function result did not produce the expected result'); |
|
87
|
|
|
|
|
88
|
|
|
$startOfWeekResult = $extension->startOfWeek('2012-05-15'); |
|
89
|
|
|
$this->assertSame('2012-05-14T00:00:00+0200', $startOfWeekResult, 'The startOfWeek function result did not produce the expected result'); |
|
90
|
|
|
|
|
91
|
|
|
$startOfWeekResult = $extension->startOfWeek('15.05.2012'); |
|
92
|
|
|
$this->assertSame('2012-05-14T00:00:00+0200', $startOfWeekResult, 'The startOfWeek function result did not produce the expected result'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
View Code Duplication |
public function testStartOfMonth() |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
$extension = $this->getExtension(); |
|
98
|
|
|
|
|
99
|
|
|
$startOfMonthResult = $extension->startOfMonth(); |
|
100
|
|
|
$this->assertSame('2012-06-01T00:00:00+0200', $startOfMonthResult, 'The startOfMonth function result did not produce the expected result'); |
|
101
|
|
|
|
|
102
|
|
|
$startOfMonthResult = $extension->startOfMonth('+2 hours'); |
|
103
|
|
|
$this->assertSame('2012-06-01T00:00:00+0200', $startOfMonthResult, 'The startOfMonth function result did not produce the expected result'); |
|
104
|
|
|
|
|
105
|
|
|
$startOfMonthResult = $extension->startOfMonth('-5 days'); |
|
106
|
|
|
$this->assertSame('2012-05-01T00:00:00+0200', $startOfMonthResult, 'The startOfMonth function result did not produce the expected result'); |
|
107
|
|
|
|
|
108
|
|
|
$startOfMonthResult = $extension->startOfMonth('+1 month'); |
|
109
|
|
|
$this->assertSame('2012-07-01T00:00:00+0200', $startOfMonthResult, 'The startOfMonth function result did not produce the expected result'); |
|
110
|
|
|
|
|
111
|
|
|
$startOfMonthResult = $extension->startOfMonth('15-05-2012'); |
|
112
|
|
|
$this->assertSame('2012-05-01T00:00:00+0200', $startOfMonthResult, 'The startOfMonth function result did not produce the expected result'); |
|
113
|
|
|
|
|
114
|
|
|
$startOfMonthResult = $extension->startOfMonth('2012-05-15'); |
|
115
|
|
|
$this->assertSame('2012-05-01T00:00:00+0200', $startOfMonthResult, 'The startOfMonth function result did not produce the expected result'); |
|
116
|
|
|
|
|
117
|
|
|
$startOfMonthResult = $extension->startOfMonth('15.05.2012'); |
|
118
|
|
|
$this->assertSame('2012-05-01T00:00:00+0200', $startOfMonthResult, 'The startOfMonth function result did not produce the expected result'); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
View Code Duplication |
public function testStartOfYear() |
|
|
|
|
|
|
122
|
|
|
{ |
|
123
|
|
|
$extension = $this->getExtension(); |
|
124
|
|
|
|
|
125
|
|
|
$startOfYearResult = $extension->startOfYear(); |
|
126
|
|
|
$this->assertSame('2012-01-01T00:00:00+0200', $startOfYearResult, 'The startOfYear function result did not produce the expected result'); |
|
127
|
|
|
|
|
128
|
|
|
$startOfYearResult = $extension->startOfYear('+2 hours'); |
|
129
|
|
|
$this->assertSame('2012-01-01T00:00:00+0200', $startOfYearResult, 'The startOfYear function result did not produce the expected result'); |
|
130
|
|
|
|
|
131
|
|
|
$startOfYearResult = $extension->startOfYear('-5 days'); |
|
132
|
|
|
$this->assertSame('2012-01-01T00:00:00+0200', $startOfYearResult, 'The startOfYear function result did not produce the expected result'); |
|
133
|
|
|
|
|
134
|
|
|
$startOfYearResult = $extension->startOfYear('+1 month'); |
|
135
|
|
|
$this->assertSame('2012-01-01T00:00:00+0200', $startOfYearResult, 'The startOfYear function result did not produce the expected result'); |
|
136
|
|
|
|
|
137
|
|
|
$startOfYearResult = $extension->startOfYear('15-05-2012'); |
|
138
|
|
|
$this->assertSame('2012-01-01T00:00:00+0200', $startOfYearResult, 'The startOfYear function result did not produce the expected result'); |
|
139
|
|
|
|
|
140
|
|
|
$startOfYearResult = $extension->startOfYear('2012-05-15'); |
|
141
|
|
|
$this->assertSame('2012-01-01T00:00:00+0200', $startOfYearResult, 'The startOfYear function result did not produce the expected result'); |
|
142
|
|
|
|
|
143
|
|
|
$startOfYearResult = $extension->startOfYear('15.05.2012'); |
|
144
|
|
|
$this->assertSame('2012-01-01T00:00:00+0200', $startOfYearResult, 'The startOfYear function result did not produce the expected result'); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function testCurrentUser() |
|
148
|
|
|
{ |
|
149
|
|
|
$extension = $this->getExtensionForUserRelatedTests(); |
|
150
|
|
|
$currentUser = $extension->currentUser(); |
|
151
|
|
|
$this->assertSame('current.user', $currentUser); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function testLastLoginWithoutProvidedUsername() |
|
155
|
|
|
{ |
|
156
|
|
|
$extension = $this->getExtensionForUserRelatedTests(); |
|
157
|
|
|
$lastLoginOfCurrentUser = $extension->lastLogin(); |
|
158
|
|
|
$this->assertSame('2015.01.01', $lastLoginOfCurrentUser); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function testLastLoginWithProvidedUsername() |
|
162
|
|
|
{ |
|
163
|
|
|
$extension = $this->getExtensionForUserRelatedTests(); |
|
164
|
|
|
$lastLoginOfCurrentUser = $extension->lastLogin('test.user'); |
|
165
|
|
|
$this->assertSame('2014.01.01', $lastLoginOfCurrentUser); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @return BuiltInFunctionsExtension |
|
170
|
|
|
*/ |
|
171
|
|
View Code Duplication |
private function getExtension() |
|
|
|
|
|
|
172
|
|
|
{ |
|
173
|
|
|
$now = new \DateTime('2012-06-03T22:22:22+0200'); |
|
174
|
|
|
|
|
175
|
|
|
$tokenStorage = $this->prophesize(TokenStorageInterface::class); |
|
176
|
|
|
$provider = $this->prophesize(CurrentDateTimeProvider::class); |
|
177
|
|
|
$provider->get()->willReturn($now); |
|
178
|
|
|
$entityManager = $this->prophesize(EntityManager::class); |
|
179
|
|
|
$extension = new BuiltInFunctionsExtension($tokenStorage->reveal(), $provider->reveal(), $entityManager->reveal(), 'TestUser'); |
|
180
|
|
|
|
|
181
|
|
|
return $extension; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @return BuiltInFunctionsExtension |
|
186
|
|
|
*/ |
|
187
|
|
View Code Duplication |
private function getExtensionForUserRelatedTests() |
|
|
|
|
|
|
188
|
|
|
{ |
|
189
|
|
|
$now = new \DateTime('2012-06-03T22:22:22+0200'); |
|
190
|
|
|
|
|
191
|
|
|
$tokenStorageProphecy = $this->getTokenStorageWithCurrentUser(); |
|
192
|
|
|
$provider = $this->prophesize(CurrentDateTimeProvider::class); |
|
193
|
|
|
$entityManagerProphecy = $this->getEntityManagerWithUserRepository(); |
|
194
|
|
|
|
|
195
|
|
|
$provider->get()->willReturn($now); |
|
196
|
|
|
$extension = new BuiltInFunctionsExtension($tokenStorageProphecy->reveal(), $provider->reveal(), $entityManagerProphecy->reveal(), 'TestUser'); |
|
197
|
|
|
|
|
198
|
|
|
return $extension; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @return TokenStorage |
|
203
|
|
|
*/ |
|
204
|
|
View Code Duplication |
private function getTokenStorageWithCurrentUser() |
|
|
|
|
|
|
205
|
|
|
{ |
|
206
|
|
|
$currentUser = $this->prophesize(TestUser::class); |
|
207
|
|
|
$currentUser->getLastLogin()->willReturn('2015.01.01'); |
|
208
|
|
|
|
|
209
|
|
|
$token = $this->prophesize(TokenInterface::class); |
|
210
|
|
|
$token->getUser(Argument::any())->willReturn($currentUser->reveal()); |
|
211
|
|
|
$token->getUsername()->willReturn('current.user'); |
|
212
|
|
|
$tokenStorage = $this->prophesize(TokenStorageInterface::class); |
|
213
|
|
|
$tokenStorage->getToken(Argument::any())->willReturn($token); |
|
214
|
|
|
|
|
215
|
|
|
return $tokenStorage; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @return EntityManager |
|
220
|
|
|
*/ |
|
221
|
|
View Code Duplication |
private function getEntityManagerWithUserRepository() |
|
|
|
|
|
|
222
|
|
|
{ |
|
223
|
|
|
$entityManager = $this->prophesize(EntityManager::class); |
|
224
|
|
|
|
|
225
|
|
|
$userFromRepository = $this->prophesize(TestUser::class); |
|
226
|
|
|
$userFromRepository->getLastLogin()->willReturn('2014.01.01'); |
|
227
|
|
|
$repository = $this->prophesize(ObjectRepository::class); |
|
228
|
|
|
$repository->findOneBy(Argument::any())->willReturn($userFromRepository->reveal()); |
|
229
|
|
|
$entityManager->getRepository(Argument::any())->willReturn($repository->reveal()); |
|
230
|
|
|
|
|
231
|
|
|
return $entityManager; |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
class TestUser |
|
235
|
|
|
{ |
|
236
|
|
|
public function getUsername() |
|
237
|
|
|
{ |
|
238
|
|
|
} |
|
239
|
|
|
public function getLastLogin() |
|
240
|
|
|
{ |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|
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.