1 | <?php |
||
19 | class BookingServiceTest extends \PHPUnit_Framework_TestCase |
||
20 | { |
||
21 | protected $periodService; |
||
22 | protected $options; |
||
23 | protected $bookingRepository; |
||
24 | protected $objectManager; |
||
25 | protected $hydrator; |
||
26 | protected $inputFilter; |
||
27 | protected $bookingService; |
||
28 | |||
29 | /** |
||
30 | * Create Service |
||
31 | */ |
||
32 | public function setUp() |
||
33 | { |
||
34 | $this->periodService = $this->getMock('JhFlexiTime\Service\PeriodServiceInterface'); |
||
35 | $this->options = $this->getOptions(); |
||
36 | $this->bookingRepository = $this->getMock('JhFlexiTime\Repository\BookingRepositoryInterface'); |
||
37 | $this->objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager'); |
||
38 | $this->hydrator = $this->getMock('Zend\Stdlib\Hydrator\HydratorInterface'); |
||
39 | $this->inputFilter = $this->getMock('Zend\InputFilter\InputFilterInterface'); |
||
40 | $this->bookingService = new BookingService( |
||
41 | $this->options, |
||
42 | $this->bookingRepository, |
||
43 | $this->objectManager, |
||
44 | $this->periodService, |
||
45 | $this->hydrator, |
||
46 | $this->inputFilter |
||
47 | ); |
||
48 | } |
||
49 | |||
50 | public function testCreateBookingReturnsErrorIfValidationFails() |
||
51 | { |
||
52 | $data = ['notes' => 'yo']; |
||
53 | $user = new User(); |
||
54 | |||
55 | $this->inputFilter |
||
56 | ->expects($this->once()) |
||
57 | ->method('setData') |
||
58 | ->with($data); |
||
59 | |||
60 | $this->inputFilter |
||
61 | ->expects($this->once()) |
||
62 | ->method('isValid') |
||
63 | ->will($this->returnValue(false)); |
||
64 | |||
65 | $this->inputFilter |
||
66 | ->expects($this->once()) |
||
67 | ->method('getMessages') |
||
68 | ->will($this->returnValue(['notes' => 'ERROR!'])); |
||
69 | |||
70 | $ret = $this->bookingService->create($data, $user); |
||
|
|||
71 | $this->assertSame(['messages' => ['notes' => 'ERROR!']], $ret); |
||
72 | } |
||
73 | |||
74 | public function testCreateSavesAfterSuccessfulValidation() |
||
75 | { |
||
76 | $data = ['notes' => 'yo' ]; |
||
77 | $user = new User(); |
||
78 | $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); |
||
79 | |||
80 | $this->inputFilter |
||
81 | ->expects($this->once()) |
||
82 | ->method('setData') |
||
83 | ->with($data); |
||
84 | |||
85 | $this->inputFilter |
||
86 | ->expects($this->once()) |
||
87 | ->method('isValid') |
||
88 | ->will($this->returnValue(true)); |
||
89 | |||
90 | $this->inputFilter |
||
91 | ->expects($this->once()) |
||
92 | ->method('getValues') |
||
93 | ->will($this->returnValue(['notes' => 'yo'])); |
||
94 | |||
95 | $this->hydrator |
||
96 | ->expects($this->once()) |
||
97 | ->method('hydrate') |
||
98 | ->with(['notes' => 'yo'], $this->isInstanceOf('JhFlexiTime\Entity\Booking')); |
||
99 | |||
100 | $this->periodService |
||
101 | ->expects($this->once()) |
||
102 | ->method('calculateHourDiff') |
||
103 | ->with($this->isInstanceOf('DateTime'), $this->isInstanceOf('DateTime')) |
||
104 | ->will($this->returnValue(2)); |
||
105 | |||
106 | $this->objectManager |
||
107 | ->expects($this->once()) |
||
108 | ->method('persist') |
||
109 | ->with($this->isInstanceOf('JhFlexiTime\Entity\Booking')); |
||
110 | |||
111 | $this->objectManager |
||
112 | ->expects($this->once()) |
||
113 | ->method('flush'); |
||
114 | |||
115 | $eventManager |
||
116 | ->expects($this->at(1)) |
||
117 | ->method('trigger'); |
||
118 | |||
119 | $eventManager |
||
120 | ->expects($this->at(2)) |
||
121 | ->method('trigger'); |
||
122 | |||
123 | $this->bookingService->setEventManager($eventManager); |
||
124 | $ret = $this->bookingService->create($data); |
||
125 | $this->assertEquals(-5.5, $ret->getBalance()); |
||
126 | $this->assertEquals(2, $ret->getTotal()); |
||
127 | } |
||
128 | |||
129 | public function testUpdateBookingReturnsErrorIfBookingNotExist() |
||
130 | { |
||
131 | $data = []; |
||
132 | $userId = 2; |
||
133 | $date = new DateTime("20 September 2014"); |
||
134 | |||
135 | $this->bookingRepository |
||
136 | ->expects($this->once()) |
||
137 | ->method('findOneBy') |
||
138 | ->with(['date' => $date, 'user' => $userId]) |
||
139 | ->will($this->returnValue(null)); |
||
140 | |||
141 | $ret = $this->bookingService->update($userId, $date, $data); |
||
142 | $this->assertSame(['messages' => ['Booking Does Not Exist']], $ret); |
||
143 | } |
||
144 | |||
145 | public function testUpdateBookingReturnsErrorIfValidationFails() |
||
146 | { |
||
147 | $data = [ |
||
148 | 'notes' => 'yo' |
||
149 | ]; |
||
150 | $booking = new Booking(); |
||
151 | $userId = 2; |
||
152 | $date = new DateTime("20 September 2014"); |
||
153 | |||
154 | $this->bookingRepository |
||
155 | ->expects($this->once()) |
||
156 | ->method('findOneBy') |
||
157 | ->with(['date' => $date, 'user' => $userId]) |
||
158 | ->will($this->returnValue($booking)); |
||
159 | |||
160 | $this->inputFilter |
||
161 | ->expects($this->once()) |
||
162 | ->method('setData') |
||
163 | ->with($data); |
||
164 | |||
165 | $this->inputFilter |
||
166 | ->expects($this->once()) |
||
167 | ->method('isValid') |
||
168 | ->will($this->returnValue(false)); |
||
169 | |||
170 | $this->inputFilter |
||
171 | ->expects($this->once()) |
||
172 | ->method('getMessages') |
||
173 | ->will($this->returnValue(['notes' => 'ERROR!'])); |
||
174 | |||
175 | $ret = $this->bookingService->update($userId, $date, $data); |
||
176 | $this->assertSame(['messages' => ['notes' => 'ERROR!']], $ret); |
||
177 | } |
||
178 | |||
179 | public function testUpdateSavesAfterSuccessfulValidation() |
||
180 | { |
||
181 | $data = ['notes' => 'yo' ]; |
||
182 | $booking = new Booking(); |
||
183 | $userId = 2; |
||
184 | $date = new DateTime("20 September 2014"); |
||
185 | $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); |
||
186 | |||
187 | $this->bookingRepository |
||
188 | ->expects($this->once()) |
||
189 | ->method('findOneBy') |
||
190 | ->with(['date' => $date, 'user' => $userId]) |
||
191 | ->will($this->returnValue($booking)); |
||
192 | |||
193 | $this->inputFilter |
||
194 | ->expects($this->once()) |
||
195 | ->method('setData') |
||
196 | ->with($data); |
||
197 | |||
198 | $this->inputFilter |
||
199 | ->expects($this->once()) |
||
200 | ->method('isValid') |
||
201 | ->will($this->returnValue(true)); |
||
202 | |||
203 | $this->inputFilter |
||
204 | ->expects($this->once()) |
||
205 | ->method('getValues') |
||
206 | ->will($this->returnValue(['notes' => 'yo'])); |
||
207 | |||
208 | $this->hydrator |
||
209 | ->expects($this->once()) |
||
210 | ->method('hydrate') |
||
211 | ->with(['notes' => 'yo'], $booking); |
||
212 | |||
213 | $this->periodService |
||
214 | ->expects($this->once()) |
||
215 | ->method('calculateHourDiff') |
||
216 | ->with($booking->getStartTime(), $booking->getEndTime()) |
||
217 | ->will($this->returnValue(2)); |
||
218 | |||
219 | $this->objectManager |
||
220 | ->expects($this->once()) |
||
221 | ->method('flush'); |
||
222 | |||
223 | $eventManager |
||
224 | ->expects($this->at(1)) |
||
225 | ->method('trigger') |
||
226 | ->with('update.pre', null, ['booking' => $booking]); |
||
227 | |||
228 | $eventManager |
||
229 | ->expects($this->at(2)) |
||
230 | ->method('trigger') |
||
231 | ->with('update.post', null, ['booking' => $booking]); |
||
232 | |||
233 | $this->bookingService->setEventManager($eventManager); |
||
234 | $ret = $this->bookingService->update($userId, $date, $data); |
||
235 | $this->assertEquals(2, $booking->getTotal()); |
||
236 | $this->assertSame($ret, $booking); |
||
237 | } |
||
238 | |||
239 | public function testDeleteBooking() |
||
240 | { |
||
241 | $booking = new Booking(); |
||
242 | $userId = 2; |
||
243 | $date = new DateTime("20 September 2014"); |
||
244 | |||
245 | $this->bookingRepository |
||
246 | ->expects($this->once()) |
||
247 | ->method('findOneBy') |
||
248 | ->with(['date' => $date, 'user' => $userId]) |
||
249 | ->will($this->returnValue($booking)); |
||
250 | |||
251 | $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); |
||
252 | $eventManager |
||
253 | ->expects($this->at(1)) |
||
254 | ->method('trigger') |
||
255 | ->with('delete.pre', null, ['booking' => $booking]); |
||
256 | |||
257 | $eventManager |
||
258 | ->expects($this->at(2)) |
||
259 | ->method('trigger') |
||
260 | ->with('delete.post', null, ['booking' => $booking]); |
||
261 | |||
262 | $this->objectManager |
||
263 | ->expects($this->once()) |
||
264 | ->method('remove') |
||
265 | ->with($booking); |
||
266 | |||
267 | $this->objectManager |
||
268 | ->expects($this->once()) |
||
269 | ->method('flush'); |
||
270 | |||
271 | $this->bookingService->setEventManager($eventManager); |
||
272 | $ret = $this->bookingService->delete($userId, $date); |
||
273 | $this->assertSame($booking, $ret); |
||
274 | } |
||
275 | |||
276 | public function testDeleteBookingReturnsErrorIfBookingNotExist() |
||
290 | |||
291 | public function testGetBookingByUserAndIdReturnsBooking() |
||
292 | { |
||
293 | $userId = 2; |
||
294 | $date = new DateTime("20 September 2014"); |
||
295 | $booking = new Booking(); |
||
296 | |||
297 | $this->bookingRepository |
||
298 | ->expects($this->once()) |
||
299 | ->method('findOneBy') |
||
300 | ->with(['date' => $date, 'user' => $userId]) |
||
301 | ->will($this->returnValue($booking)); |
||
302 | |||
303 | $ret = $this->bookingService->getBookingByUserAndDate($userId, $date); |
||
304 | $this->assertSame($booking, $ret); |
||
305 | } |
||
306 | |||
307 | public function testGetPagination() |
||
308 | { |
||
309 | $date = new \DateTime("15 May 2014"); |
||
310 | |||
311 | $return = $this->bookingService->getPagination($date); |
||
312 | $expected = [ |
||
313 | 'current' => ['m' => 'May', 'y' => '2014'], |
||
314 | 'next' => ['m' => 'Jun', 'y' => '2014'], |
||
315 | 'prev' => ['m' => 'Apr', 'y' => '2014'], |
||
316 | ]; |
||
317 | |||
318 | $this->assertSame($expected, $return); |
||
319 | $this->assertEquals('15-May-2014', $date->format('d-M-Y')); |
||
320 | } |
||
321 | |||
322 | public function testGetEventManagerReturnsNewEventManagerIfNotSet() |
||
323 | { |
||
324 | $refObject = new \ReflectionObject($this->bookingService); |
||
325 | $refProperty = $refObject->getProperty('eventManager'); |
||
326 | $refProperty->setAccessible(true); |
||
327 | $this->assertNull($refProperty->getValue($this->bookingService)); |
||
328 | $this->assertInstanceOf('Zend\EventManager\EventManagerInterface', $this->bookingService->getEventManager()); |
||
329 | } |
||
330 | |||
331 | public function testSetEventManager() |
||
332 | { |
||
333 | $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); |
||
334 | $this->bookingService->setEventManager($eventManager); |
||
335 | |||
336 | $refObject = new \ReflectionObject($this->bookingService); |
||
337 | $refProperty = $refObject->getProperty('eventManager'); |
||
338 | $refProperty->setAccessible(true); |
||
339 | $this->assertSame($eventManager, $refProperty->getValue($this->bookingService)); |
||
340 | } |
||
341 | |||
342 | public function testGetUserBookingsForMonth() |
||
504 | |||
505 | protected function getBookingCollection(\DateTime $date, $addFalsePositives = true) |
||
526 | |||
527 | protected function getBooking(DateTime $date) |
||
534 | |||
535 | |||
536 | |||
537 | /** |
||
538 | * @return ModuleOptions |
||
539 | */ |
||
540 | public function getOptions() |
||
541 | { |
||
542 | $options = new ModuleOptions(); |
||
543 | $options->setHoursInDay(7.5) |
||
544 | ->setLunchDuration(1); |
||
545 | |||
548 | } |
||
549 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.