BookingServiceTest::getOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace JhFlexiTimeTest\Service;
4
5
use JhFlexiTime\DateTime\DateTime;
6
use JhFlexiTime\Entity\Booking;
7
use JhFlexiTime\Service\BookingService;
8
use JhFlexiTime\Options\ModuleOptions;
9
use JhUser\Entity\User;
10
use Zend\Stdlib\Hydrator\HydratorInterface;
11
use Zend\InputFilter\InputFilterInterface;
12
use Zend\EventManager\EventManagerInterface;
13
14
/**
15
 * Class BookingServiceTest
16
 * @package JhFlexiTimeTest\Service
17
 * @author Aydin Hassan <[email protected]>
18
 */
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);
0 ignored issues
show
Unused Code introduced by
The call to BookingService::create() has too many arguments starting with $user.

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.

Loading history...
71
        $this->assertSame(['messages' => ['notes' => 'ERROR!']], $ret);
72
    }
73
74
    public function testCreateSavesAfterSuccessfulValidation()
75
    {
76
        $data       = ['notes' => 'yo' ];
77
        $user       = new User();
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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()
277
    {
278
        $userId     = 2;
279
        $date       = new DateTime("20 September 2014");
280
281
        $this->bookingRepository
282
            ->expects($this->once())
283
            ->method('findOneBy')
284
            ->with(['date' => $date, 'user' => $userId])
285
            ->will($this->returnValue(null));
286
287
        $ret = $this->bookingService->delete($userId, $date);
288
        $this->assertSame(['messages' => ['Booking Does Not Exist']], $ret);
289
    }
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()
343
    {
344
        $user = new User();
345
        $date = new DateTime("14 May 2014");
346
347
        $bookings = $this->getBookingCollection($date);
348
349
        $this->bookingRepository
350
             ->expects($this->once())
351
             ->method('findByUserAndMonth')
352
             ->with($user, $date)
353
             ->will($this->returnValue($bookings));
354
355
        $ret = $this->bookingService->getUserBookingsForMonth($user, $date);
356
357
        $expected = [
358
            'weeks' => [
359
                [
360
                    'dates' => [
361
                        [
362
                            'date'      => new \DateTime('1-05-2014'),
363
                            'day_num'   => 4,
364
                            'booking'   => $bookings[6]
365
                        ],
366
                        [
367
                            'date'      => new \DateTime('2-05-2014'),
368
                            'day_num'   => 5,
369
                        ],
370
371
                    ],
372
                    'totalHours'    => 15,
373
                    'balance'       => -7.5,
374
                    'workedHours'   => 7.5
375
                ],
376
                [
377
                    'dates' => [
378
                        [
379
                            'date'      => new \DateTime('5-05-2014'),
380
                            'day_num'   => 1,
381
                        ],
382
                        [
383
                            'date'      => new \DateTime('6-05-2014'),
384
                            'day_num'   => 2,
385
                        ],
386
                        [
387
                            'date'      => new \DateTime('7-05-2014'),
388
                            'day_num'   => 3,
389
                        ],
390
                        [
391
                            'date'      => new \DateTime('8-05-2014'),
392
                            'day_num'   => 4,
393
                        ],
394
                        [
395
                            'date'      => new \DateTime('9-05-2014'),
396
                            'day_num'   => 5,
397
                        ],
398
399
                    ],
400
                    'totalHours'    => 37.5,
401
                    'balance'       => -37.5,
402
                    'workedHours'   => 0
403
                ],
404
                [
405
                    'dates' => [
406
                        [
407
                            'date'      => new \DateTime('12-05-2014'),
408
                            'day_num'   => 1,
409
                        ],
410
                        [
411
                            'date'      => new \DateTime('13-05-2014'),
412
                            'day_num'   => 2,
413
                        ],
414
                        [
415
                            'date'      => new \DateTime('14-05-2014'),
416
                            'day_num'   => 3,
417
                            'booking'   => $bookings[0]
418
                        ],
419
                        [
420
                            'date'      => new \DateTime('15-05-2014'),
421
                            'day_num'   => 4,
422
                            'booking'   => $bookings[1]
423
                        ],
424
                        [
425
                            'date'      => new \DateTime('16-05-2014'),
426
                            'day_num'   => 5,
427
                            'booking'   => $bookings[2]
428
                        ],
429
430
                    ],
431
                    'totalHours'    => 37.5,
432
                    'balance'       => -15,
433
                    'workedHours'   => 22.5
434
                ],
435
                [
436
                    'dates' => [
437
                        [
438
                            'date'      => new \DateTime('19-05-2014'),
439
                            'day_num'   => 1,
440
                            'booking'   => $bookings[5]
441
                        ],
442
                        [
443
                            'date'      => new \DateTime('20-05-2014'),
444
                            'day_num'   => 2,
445
                        ],
446
                        [
447
                            'date'      => new \DateTime('21-05-2014'),
448
                            'day_num'   => 3,
449
                        ],
450
                        [
451
                            'date'      => new \DateTime('22-05-2014'),
452
                            'day_num'   => 4,
453
                        ],
454
                        [
455
                            'date'      => new \DateTime('23-05-2014'),
456
                            'day_num'   => 5,
457
                        ],
458
459
                    ],
460
                    'totalHours'    => 37.5,
461
                    'balance'       => -30,
462
                    'workedHours'   => 7.5
463
                ],
464
                [
465
                    'dates' => [
466
                        [
467
                            'date'      => new \DateTime('26-05-2014'),
468
                            'day_num'   => 1,
469
                        ],
470
                        [
471
                            'date'      => new \DateTime('27-05-2014'),
472
                            'day_num'   => 2,
473
                        ],
474
                        [
475
                            'date'      => new \DateTime('28-05-2014'),
476
                            'day_num'   => 3,
477
                        ],
478
                        [
479
                            'date'      => new \DateTime('29-05-2014'),
480
                            'day_num'   => 4,
481
                        ],
482
                        [
483
                            'date'      => new \DateTime('30-05-2014'),
484
                            'day_num'   => 5,
485
                            'booking'   => $bookings[7]
486
                        ],
487
488
                    ],
489
                    'totalHours'    => 37.5,
490
                    'balance'       => -30,
491
                    'workedHours'   => 7.5
492
                ],
493
            ],
494
            'workedMonth'       => [
495
                'availableHours'    => 165,
496
                'monthBalance'      => -120,
497
                'hoursWorked'       => 45
498
            ],
499
        ];
500
501
        $this->assertEquals($expected, $ret);
502
503
    }
504
505
    protected function getBookingCollection(\DateTime $date, $addFalsePositives = true)
0 ignored issues
show
Unused Code introduced by
The parameter $date is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
506
    {
507
        $bookings = [
508
            $this->getBooking(new DateTime("14 May 2014")),
509
            $this->getBooking(new DateTime("15 May 2014")),
510
            $this->getBooking(new DateTime("16 May 2014")),
511
            $this->getBooking(new DateTime("17 May 2014")),
512
            $this->getBooking(new DateTime("18 May 2014")),
513
            $this->getBooking(new DateTime("19 May 2014")),
514
            $this->getBooking(new DateTime("1 May 2014")),
515
            $this->getBooking(new DateTime("30 May 2014")),
516
            $this->getBooking(new DateTime("31 May 2014")),
517
        ];
518
519
        if ($addFalsePositives) {
520
            $bookings[] = $this->getBooking(new DateTime("5 June 2014"));
521
            $bookings[] = $this->getBooking(new DateTime("3 April 2014"));
522
        }
523
524
        return $bookings;
525
    }
526
527
    protected function getBooking(DateTime $date)
528
    {
529
        $booking = new Booking();
530
        $booking->setDate($date);
531
        $booking->setTotal(7.5);
532
        return $booking;
533
    }
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
546
        return $options;
547
    }
548
}
549