|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JhFlexiTimeTest\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use JhFlexiTime\Entity\Booking; |
|
6
|
|
|
use JhFlexiTime\Repository\BookingRepository; |
|
7
|
|
|
use JhFlexiTimeTest\Fixture\FullMonthBookings; |
|
8
|
|
|
use JhFlexiTimeTest\Fixture\MultiUserBookings; |
|
9
|
|
|
use JhFlexiTimeTest\Fixture\MultiUserMultiMonthBookings; |
|
10
|
|
|
use JhFlexiTimeTest\Fixture\SingleBookingInMonth; |
|
11
|
|
|
use JhUser\Entity\User; |
|
12
|
|
|
use JhFlexiTimeTest\Util\ServiceManagerFactory; |
|
13
|
|
|
use JhFlexiTimeTest\Fixture\SingleUser; |
|
14
|
|
|
use JhFlexiTimeTest\Fixture\BookingsNotInMonth; |
|
15
|
|
|
use JhFlexiTime\DateTime\DateTime; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class BookingRepositoryTest |
|
19
|
|
|
* @package JhFlexiTimeTest\Repository |
|
20
|
|
|
* @author Aydin Hassan <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class BookingRepositoryTest extends \PHPUnit_Framework_TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
protected $repository; |
|
26
|
|
|
protected $objectRepository; |
|
27
|
|
|
protected $fixtureExecutor; |
|
28
|
|
|
|
|
29
|
|
|
public function setUp() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->objectRepository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository'); |
|
32
|
|
|
$sm = ServiceManagerFactory::getServiceManager(); |
|
33
|
|
|
$this->repository = $sm->get('JhFlexiTime\Repository\BookingRepository'); |
|
34
|
|
|
$this->fixtureExecutor = $sm->get('Doctrine\Common\DataFixtures\Executor\AbstractExecutor'); |
|
35
|
|
|
$this->assertInstanceOf('JhFlexiTime\Repository\BookingRepository', $this->repository); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testFindAllByUserReturnsRecordsOnlyByUserSortedByDateAscending() |
|
39
|
|
|
{ |
|
40
|
|
|
$user = new User(); |
|
41
|
|
|
$user |
|
42
|
|
|
->setEmail("[email protected]") |
|
43
|
|
|
->setPassword("password"); |
|
44
|
|
|
$bookingsFixture = new MultiUserBookings($user); |
|
45
|
|
|
$this->fixtureExecutor->execute([$bookingsFixture]); |
|
46
|
|
|
$bookings = $this->repository->findAllByUser($user); |
|
47
|
|
|
$this->assertEquals(count($bookingsFixture->getBookingsForUser()), count($bookings)); |
|
48
|
|
|
|
|
49
|
|
|
$lastDate = null; |
|
50
|
|
|
foreach ($bookings as $booking) { |
|
51
|
|
|
|
|
52
|
|
|
if (!$lastDate) { |
|
53
|
|
|
$lastDate = $booking->getDate(); |
|
54
|
|
|
} else { |
|
55
|
|
|
$this->assertGreaterThanOrEqual($lastDate, $booking->getDate()); |
|
56
|
|
|
} |
|
57
|
|
|
$this->assertEquals($booking->getUser()->getId(), $user->getId()); |
|
58
|
|
|
$this->assertEquals($booking->getUser()->getEmail(), $user->getEmail()); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testFindByUserAndMonth() |
|
63
|
|
|
{ |
|
64
|
|
|
$user = new User(); |
|
65
|
|
|
$user |
|
66
|
|
|
->setEmail("[email protected]") |
|
67
|
|
|
->setPassword("password"); |
|
68
|
|
|
|
|
69
|
|
|
$date = new DateTime("10 September 2014"); |
|
70
|
|
|
$bookingsFixture = new MultiUserMultiMonthBookings($user); |
|
71
|
|
|
$this->fixtureExecutor->execute([$bookingsFixture]); |
|
72
|
|
|
$bookings = $this->repository->findByUserAndMonth($user, $date); |
|
73
|
|
|
$this->assertEquals(5, count($bookings)); |
|
74
|
|
|
array_map(function (Booking $booking) use ($user) { |
|
75
|
|
|
$this->assertEquals($booking->getUser()->getId(), $user->getId()); |
|
76
|
|
|
$this->assertEquals($booking->getUser()->getEmail(), $user->getEmail()); |
|
77
|
|
|
}, $bookings); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function testIsUsersFirstBookingForMonthReturnsTrueIfNoBookingsExist() |
|
81
|
|
|
{ |
|
82
|
|
|
$userFixture = new SingleUser; |
|
83
|
|
|
$this->fixtureExecutor->execute([$userFixture]); |
|
84
|
|
|
|
|
85
|
|
|
$date = new DateTime("10 May 2014"); |
|
86
|
|
|
$this->assertTrue($this->repository->isUsersFirstBookingForMonth($userFixture->getUser(), $date)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testIsUsersFirstBookingForMonthReturnsTrueIfNoBookingsExistInGivenMonth() |
|
90
|
|
|
{ |
|
91
|
|
|
$user = new User(); |
|
92
|
|
|
$user |
|
93
|
|
|
->setEmail("[email protected]") |
|
94
|
|
|
->setPassword("password"); |
|
95
|
|
|
|
|
96
|
|
|
$bookingFixture = new BookingsNotInMonth($user); |
|
97
|
|
|
$this->fixtureExecutor->execute([$bookingFixture]); |
|
98
|
|
|
|
|
99
|
|
|
$this->assertTrue( |
|
100
|
|
|
$this->repository->isUsersFirstBookingForMonth($user, $bookingFixture->getMonthWithNoBookings()) |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function testIsUsersFirstBookingForMonthReturnsFalseIfBookingExistsInMonth() |
|
105
|
|
|
{ |
|
106
|
|
|
$user = new User(); |
|
107
|
|
|
$user |
|
108
|
|
|
->setEmail("[email protected]") |
|
109
|
|
|
->setPassword("password"); |
|
110
|
|
|
|
|
111
|
|
|
$date = new DateTime("10 May 2014"); |
|
112
|
|
|
|
|
113
|
|
|
$bookingFixture = new SingleBookingInMonth($user, $date); |
|
114
|
|
|
$this->fixtureExecutor->execute([$bookingFixture]); |
|
115
|
|
|
|
|
116
|
|
|
$this->assertFalse($this->repository->isUsersFirstBookingForMonth($user, $date)); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function testGetMonthBookedToDateTotalByUserReturnsTotalUpToDate() |
|
120
|
|
|
{ |
|
121
|
|
|
$user = new User(); |
|
122
|
|
|
$user |
|
123
|
|
|
->setEmail("[email protected]") |
|
124
|
|
|
->setPassword("password"); |
|
125
|
|
|
|
|
126
|
|
|
$bookingFixture = new FullMonthBookings($user); |
|
127
|
|
|
$this->fixtureExecutor->execute([$bookingFixture]); |
|
128
|
|
|
|
|
129
|
|
|
$date = clone $bookingFixture->getMonth(); |
|
130
|
|
|
$date->modify("+3 days"); |
|
131
|
|
|
$bookingsTotal = $this->repository->getMonthBookedToDateTotalByUser($user, $date); |
|
132
|
|
|
|
|
133
|
|
|
$total = array_reduce($bookingFixture->getBookings(), function ($totalHours, Booking $booking) use ($date) { |
|
134
|
|
|
if ($booking->getDate() <= $date) { |
|
135
|
|
|
$totalHours += $booking->getTotal(); |
|
136
|
|
|
} |
|
137
|
|
|
return $totalHours; |
|
138
|
|
|
}, 0); |
|
139
|
|
|
|
|
140
|
|
|
$this->assertEquals($total, $bookingsTotal); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function testGetMonthBookedToDateTotalByUserReturnsZeroIfNoBookings() |
|
144
|
|
|
{ |
|
145
|
|
|
$userFixture = new SingleUser; |
|
146
|
|
|
$this->fixtureExecutor->execute([$userFixture]); |
|
147
|
|
|
|
|
148
|
|
|
$date = new DateTime; |
|
149
|
|
|
$bookingsTotal = $this->repository->getMonthBookedToDateTotalByUser($userFixture->getUser(), $date); |
|
150
|
|
|
$this->assertEquals(0, $bookingsTotal); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function testGetMonthBookedTotalByUserReturnsTotalForWholeMonth() |
|
154
|
|
|
{ |
|
155
|
|
|
$user = new User(); |
|
156
|
|
|
$user |
|
157
|
|
|
->setEmail("[email protected]") |
|
158
|
|
|
->setPassword("password"); |
|
159
|
|
|
|
|
160
|
|
|
$bookingFixture = new FullMonthBookings($user); |
|
161
|
|
|
$this->fixtureExecutor->execute([$bookingFixture]); |
|
162
|
|
|
|
|
163
|
|
|
$date = clone $bookingFixture->getMonth(); |
|
164
|
|
|
$date->modify("+3 days"); |
|
165
|
|
|
$bookingsTotal = $this->repository->getMonthBookedTotalByUser($user, $date); |
|
166
|
|
|
|
|
167
|
|
|
$total = array_reduce($bookingFixture->getBookings(), function ($totalHours, Booking $booking) use ($date) { |
|
168
|
|
|
return $totalHours + $booking->getTotal(); |
|
169
|
|
|
}, 0); |
|
170
|
|
|
|
|
171
|
|
|
$this->assertEquals($total, $bookingsTotal); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
public function testGetMonthBookedTotalByUserReturnsZeroIfNoBookings() |
|
175
|
|
|
{ |
|
176
|
|
|
$userFixture = new SingleUser; |
|
177
|
|
|
$this->fixtureExecutor->execute([$userFixture]); |
|
178
|
|
|
|
|
179
|
|
|
$date = new DateTime; |
|
180
|
|
|
$bookingsTotal = $this->repository->getMonthBookedTotalByUser($userFixture->getUser(), $date); |
|
181
|
|
|
$this->assertEquals(0, $bookingsTotal); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
public function testGetTotalBookedBetweenByUserReturnsTotalForPeriod() |
|
185
|
|
|
{ |
|
186
|
|
|
$user = new User(); |
|
187
|
|
|
$user |
|
188
|
|
|
->setEmail("[email protected]") |
|
189
|
|
|
->setPassword("password"); |
|
190
|
|
|
|
|
191
|
|
|
$bookingFixture = new FullMonthBookings($user); |
|
192
|
|
|
$this->fixtureExecutor->execute([$bookingFixture]); |
|
193
|
|
|
|
|
194
|
|
|
$startDate = clone $bookingFixture->getMonth(); |
|
195
|
|
|
$startDate->modify("+3 days"); |
|
196
|
|
|
$endDate = clone $bookingFixture->getMonth(); |
|
197
|
|
|
$endDate->modify("+15 days"); |
|
198
|
|
|
|
|
199
|
|
|
$bookingsTotal = $this->repository->getTotalBookedBetweenByUser($user, $startDate, $endDate); |
|
200
|
|
|
|
|
201
|
|
|
$total = array_reduce( |
|
202
|
|
|
$bookingFixture->getBookings(), |
|
203
|
|
|
function ($totalHours, Booking $booking) use ($startDate, $endDate) { |
|
204
|
|
|
if ($booking->getDate() >= $startDate && $booking->getDate() <= $endDate) { |
|
205
|
|
|
$totalHours += $booking->getTotal(); |
|
206
|
|
|
} |
|
207
|
|
|
return $totalHours; |
|
208
|
|
|
}, |
|
209
|
|
|
0 |
|
210
|
|
|
); |
|
211
|
|
|
|
|
212
|
|
|
$this->assertEquals($total, $bookingsTotal); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
public function testGetTotalBookedBetweenByUserReturnsZeroIfNoBookings() |
|
216
|
|
|
{ |
|
217
|
|
|
$userFixture = new SingleUser; |
|
218
|
|
|
$this->fixtureExecutor->execute([$userFixture]); |
|
219
|
|
|
|
|
220
|
|
|
$dateA = new DateTime; |
|
221
|
|
|
$dateB = new DateTime; |
|
222
|
|
|
$bookingsTotal = $this->repository->getTotalBookedBetweenByUser($userFixture->getUser(), $dateA, $dateB); |
|
223
|
|
|
$this->assertEquals(0, $bookingsTotal); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
public function testFindByIdThrowsExceptionBecauseOfCompositeKey() |
|
227
|
|
|
{ |
|
228
|
|
|
$user = new User(); |
|
229
|
|
|
$user |
|
230
|
|
|
->setEmail("[email protected]") |
|
231
|
|
|
->setPassword("password"); |
|
232
|
|
|
$bookingFixture = new SingleBookingInMonth($user, new DateTime); |
|
233
|
|
|
$this->fixtureExecutor->execute([$bookingFixture]); |
|
234
|
|
|
|
|
235
|
|
|
$message = 'Binding an entity with a composite primary key to a query is not supported. You should split the'; |
|
236
|
|
|
$message .= ' parameter into the explicit fields and bind them separately.'; |
|
237
|
|
|
$this->setExpectedException('Doctrine\ORM\ORMInvalidArgumentException', $message); |
|
238
|
|
|
$this->repository->find($bookingFixture->getBooking()->getId()); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
public function testFindByReturnsEmptyIfNonExist() |
|
242
|
|
|
{ |
|
243
|
|
|
$this->assertEmpty($this->repository->findBy(['user' => 1])); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
public function testFindAll() |
|
247
|
|
|
{ |
|
248
|
|
|
$objectRepository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository'); |
|
249
|
|
|
$objectRepository |
|
250
|
|
|
->expects($this->once()) |
|
251
|
|
|
->method('findAll'); |
|
252
|
|
|
|
|
253
|
|
|
$repository = new BookingRepository($objectRepository); |
|
254
|
|
|
$repository->findAll(); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
public function testFindOneBy() |
|
258
|
|
|
{ |
|
259
|
|
|
$objectRepository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository'); |
|
260
|
|
|
$args = []; |
|
261
|
|
|
$objectRepository |
|
262
|
|
|
->expects($this->once()) |
|
263
|
|
|
->method('findOneBy') |
|
264
|
|
|
->with($args); |
|
265
|
|
|
|
|
266
|
|
|
$repository = new BookingRepository($objectRepository); |
|
267
|
|
|
$repository->findOneBy($args); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
public function testGetClassNameReturnsCorrectEntityClass() |
|
271
|
|
|
{ |
|
272
|
|
|
$this->assertSame( |
|
273
|
|
|
'JhFlexiTime\Entity\Booking', |
|
274
|
|
|
$this->repository->getClassName() |
|
275
|
|
|
); |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
|