TimeCalculatorServiceTest::getService()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 8
nc 1
nop 1
1
<?php
2
3
namespace JhFlexiTimeTest\Service;
4
5
use JhFlexiTime\Entity\RunningBalance;
6
use JhFlexiTime\Options\ModuleOptions;
7
use JhFlexiTime\Service\PeriodService;
8
use JhFlexiTime\Service\TimeCalculatorService;
9
use ZfcUser\Entity\User;
10
use JhFlexiTime\DateTime\DateTime;
11
12
/**
13
 * Class TimeCalculatorServiceTest
14
 * @package JhFlexiTimeTest\Service
15
 * @author Aydin Hassan <[email protected]>
16
 */
17
class TimeCalculatorServiceTest extends \PHPUnit_Framework_TestCase
18
{
19
    /**
20
     * @var \JhFlexiTime\Service\TimeCalculatorService
21
     */
22
    protected $timeCalculatorService;
23
24
    /**
25
     * @var float
26
     */
27
    protected $hoursInDay = 7.5;
28
29
    /**
30
     * @var int
31
     */
32
    protected $lunchDuration = 1;
33
34
    /**
35
     * @var \JhFlexiTime\Options\ModuleOptions
36
     */
37
    protected $options;
38
39
    /**
40
     * @var \DateTime $date
41
     */
42
    protected $date;
43
44
    /**
45
     * @var \JhFlexiTime\Repository\BookingRepositoryInterface
46
     */
47
    protected $bookingRepository;
48
49
    /**
50
     * @var \JhFlexiTime\Service\BalanceServiceInterface
51
     */
52
    protected $balanceService;
53
54
    protected $balanceRepository;
55
56
    /**
57
     * @var \JhFlexiTime\Service\PeriodService
58
     */
59
    protected $periodService;
60
61
    /**
62
     * Create Service
63
     */
64
    public function setUp()
65
    {
66
        $this->date                 = new DateTime;
67
        $this->bookingRepository    = $this->getMock('JhFlexiTime\Repository\BookingRepositoryInterface');
68
        $this->balanceRepository    = $this->getMock('JhFlexiTime\Repository\BalanceRepositoryInterface');
69
70
        $this->options = new ModuleOptions();
71
        $this->options->setHoursInDay($this->hoursInDay)
72
                      ->setLunchDuration($this->lunchDuration);
73
74
        $this->periodService = new PeriodService($this->options);
75
76
    }
77
78
    /**
79
     * @return TimeCalculatorService
80
     */
81
    public function getService(DateTime $today = null)
82
    {
83
        $timeCalculatorService = new TimeCalculatorService(
84
            $this->options,
85
            $this->bookingRepository,
86
            $this->balanceRepository,
87
            $this->periodService,
88
            $today ? $today : $this->date
0 ignored issues
show
Compatibility introduced by
$today ? $today : $this->date of type object<DateTime> is not a sub-type of object<JhFlexiTime\DateTime\DateTime>. It seems like you assume a child class of the class DateTime to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
89
        );
90
91
        return $timeCalculatorService;
92
    }
93
94
    /**
95
     * @param float $initialBalance
96
     * @param float $expectedBalance
97
     * @param float $monthToDateTotalHours
98
     * @param float $bookedToDate
99
     *
100
     * @dataProvider runningBalanceProvider
101
     */
102
//    public function testGetRunningBalance($initialBalance, $expectedBalance, $bookedToDate)
103
//    {
104
//
105
//        $mockUser       = $this->getMock('ZfcUser\Entity\UserInterface');
106
//        $runningBalance = new RunningBalance();
107
//        $runningBalance->setBalance($initialBalance);
108
//
109
//        $this->balanceRepository->expects($this->once())
110
//            ->method('findOneByUser')
111
//            ->with($mockUser)
112
//            ->will($this->returnValue($runningBalance));
113
//
114
//        $this->bookingRepository->expects($this->once())
115
//             ->method('getMonthBookedToDateTotalByUser')
116
//             ->with($mockUser, $this->date)
117
//             ->will($this->returnValue($bookedToDate));
118
//
119
//        $balance = $this->getService()->getRunningBalance($mockUser);
120
//        $this->assertEquals($expectedBalance, $balance);
121
//    }
122
123
    /**
124
     * @return array
125
     */
126
//    public function runningBalanceProvider()
127
//    {
128
//        /**
129
//         * Initial Balance | Expected Balance | Month Booked
130
//         */
131
//        return [
132
//            [0,     -5,          10],
133
//            [0,     0,           15],
134
//            [5,     0,           10],
135
//            [5,     5,           15],
136
//            [-5,    0,           15],
137
//            [-5,    -5,          15],
138
//            [-5,    -10,         10],
139
//            [-50,   -50,        150],
140
//            [-50,   -100,       100],
141
//            [50,    100,        200],
142
//            [0,     -1.75,      18.25],
143
//            [-10.5, 0,          30.5],
144
//
145
//        ];
146
//    }
147
148
//    public function testGetBalanceForwardReturnsZeroIfNoRowPresent()
149
//    {
150
//        $user = new User;
151
//
152
//        $this->balanceRepository
153
//             ->expects($this->once())
154
//             ->method('findOneByUser')
155
//             ->with($user)
156
//             ->will($this->returnValue(null));
157
//
158
//        $this->assertEquals(0, $this->getService()->getBalanceForward($user));
159
//    }
160
161
//    public function testGetBalanceForwardReturnsRunningBalance()
162
//    {
163
//        $balance    = 25;
164
//        $user       = new User;
165
//        $runningBalance = new RunningBalance();
166
//        $runningBalance->setBalance($balance);
167
//
168
//        $this->balanceRepository
169
//            ->expects($this->once())
170
//            ->method('findOneByUser')
171
//            ->with($user)
172
//            ->will($this->returnValue($runningBalance));
173
//
174
//        $this->assertEquals($balance, $this->getService()->getBalanceForward($user));
175
//    }
176
177
//    public function testGetMonthBalanceWithPreviousMonthReturnsFullMonthBalance()
178
//    {
179
//        $user       = new User;
180
//        $this->date = new DateTime("15 May 2014");
181
//        $date       = new DateTime("4 April 2014");
182
//        $startDate  = new DateTime('4 March 2014');
183
//
184
//        $this->bookingRepository
185
//             ->expects($this->once())
186
//             ->method('getMonthBookedTotalByUser')
187
//             ->with($user, $date)
188
//             ->will($this->returnValue(40));
189
//
190
//        $this->assertEquals(-125.0, $this->getService()->getMonthBalance($user, $startDate, $date));
191
//    }
192
//
193
//    public function testGetMonthBalanceWithSameMonthReturnsToDateBalance()
194
//    {
195
//        $user       = new User;
196
//        $this->date = new DateTime("15 May 2014");
197
//        $date       = new DateTime("4 May 2014");
198
//        $startDate  = new DateTime('4 April 2014');
199
//
200
//        $this->bookingRepository
201
//            ->expects($this->once())
202
//            ->method('getMonthBookedToDateTotalByUser')
203
//            ->with($user, $this->date)
204
//            ->will($this->returnValue(40));
205
//
206
//        $this->assertEquals(-42.5, $this->getService()->getMonthBalance($user, $startDate, $date));
207
//    }
208
//
209
//    public function testGetMonthBalanceWithFutureMonthDateReturnsZero()
210
//    {
211
//        $user       = new User;
212
//        $this->date = new DateTime("15 May 2014");
213
//        $date       = new DateTime("16 June 2014");
214
//        $startDate  = new DateTime('4 March 2014');
215
//
216
//        $this->assertEquals(0, $this->getService()->getMonthBalance($user, $startDate, $date));
217
//    }
218
219
//    public function testGetMonthTotalWorkedHoursForPreviousMonth()
220
//    {
221
//        $user       = new User;
222
//        $this->date = new DateTime("15 May 2014");
223
//        $date       = new DateTime("4 April 2014");
224
//        $startDate  = new DateTime('4 March 2014');
225
//
226
//        $this->bookingRepository
227
//            ->expects($this->once())
228
//            ->method('getMonthBookedTotalByUser')
229
//            ->with($user, $date)
230
//            ->will($this->returnValue(40));
231
//
232
//        $this->assertEquals(40, $this->getService()->getMonthTotalWorked($user, $startDate, $date));
233
//    }
234
//
235
//    public function testGetMonthTotalWorkedHoursForCurrentMonth()
236
//    {
237
//        $user       = new User;
238
//        $this->date = new DateTime("15 May 2014");
239
//        $date       = new DateTime("15 May 2014");
240
//        $startDate  = new DateTime('4 March 2014');
241
//
242
//        $this->bookingRepository
243
//            ->expects($this->once())
244
//            ->method('getMonthBookedToDateTotalByUser')
245
//            ->with($user, $date)
246
//            ->will($this->returnValue(40));
247
//
248
//        $this->assertEquals(40, $this->getService()->getMonthTotalWorked($user, $startDate, $date));
249
//    }
250
251
    public function testGetWeekTotals()
252
    {
253
        $user = new User;
254
        $date = new DateTime("15 May 2014");
255
256
        $week = ['firstDay' => new DateTime("12 May 2014"), 'lastDay' => new DateTime("18 May 2014")];
257
258
        $this->bookingRepository
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<JhFlexiTime\Repos...ingRepositoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
259
             ->expects($this->once())
260
             ->method('getTotalBookedBetweenByUser')
261
             ->with($user, $week['firstDay'], $week['lastDay'])
262
             ->will($this->returnValue(10));
263
264
        $ret = $this->getService()->getWeekTotals($user, $date);
265
266
        $expected = [
267
            'weekTotalWorkedHours'  => 10,
268
            'weekTotalHours'        => 37.5,
269
            'balance'               => -27.5
270
        ];
271
        $this->assertEquals($expected, $ret);
272
    }
273
274
    public function testGetTotalsForCurrentMonthWhereStartDateWasInAPreviousMonth()
275
    {
276
        $user       = new User;
277
        $startDate  = new DateTime("1 January 2014");
278
        $today      = new DateTime("2 March 2014");
279
        $service    = $this->getService($today);
280
281
        $this->bookingRepository
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<JhFlexiTime\Repos...ingRepositoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
282
            ->expects($this->exactly(1))
283
            ->method('getTotalBookedBetweenByUser')
284
            ->with(
285
                $user,
286
                $this->equalTo(new DateTime('1 March 2014 00:00:00')),
287
                $this->equalTo(new DateTime('2 March 2014 00:00:00'))
288
            )
289
            ->will($this->returnValue(75));
290
291
292
        $result     = $service->getTotals($user, $startDate, new DateTime("1 March 2014"));
293
294
        $expected = [
295
            'monthTotalWorkedHours' => 75,
296
            'monthTotalHours'       => 157.5,
297
            'monthBalance'          => 75.0,
298
            'runningBalance'        => 0.0,
299
            'monthRemainingHours'   => 157.5,
300
            'balanceForward'        => 0,
301
        ];
302
303
        $this->assertEquals($expected, $result);
304
    }
305
306
    public function testGetTotalsForCurrentMonthWhereStartDateWasInAPreviousMonthWithRunningBalance()
307
    {
308
        $user       = new User;
309
        $startDate  = new DateTime("1 January 2014");
310
        $today      = new DateTime("2 March 2014");
311
        $service    = $this->getService($today);
312
313
        $this->bookingRepository
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<JhFlexiTime\Repos...ingRepositoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
314
            ->expects($this->exactly(1))
315
            ->method('getTotalBookedBetweenByUser')
316
            ->with(
317
                $user,
318
                $this->equalTo(new DateTime('1 March 2014 00:00:00')),
319
                $this->equalTo(new DateTime('2 March 2014 00:00:00'))
320
            )
321
            ->will($this->returnValue(75));
322
323
        $balance = new RunningBalance;
324
        $balance->setBalance(10);
325
326
        $this->balanceRepository
327
            ->expects($this->once())
328
            ->method('findOneByUser')
329
            ->with($user)
330
            ->will($this->returnValue($balance));
331
332
        $result = $service->getTotals($user, $startDate, new DateTime("1 March 2014"));
333
334
        $expected = [
335
            'monthTotalWorkedHours' => 75,
336
            'monthTotalHours'       => 157.5,
337
            'monthBalance'          => 75.0,
338
            'runningBalance'        => 10,
339
            'monthRemainingHours'   => 157.5,
340
            'balanceForward'        => 10,
341
        ];
342
343
        $this->assertEquals($expected, $result);
344
    }
345
346
347
348
349
    public function testGetTotalsForCurrentMonthWhereStartDateWasInThisMonth()
350
    {
351
        $user       = new User;
352
        $startDate  = new DateTime("3 February 2014");
353
        $today      = new DateTime("10 February 2014");
354
        $service    = $this->getService($today);
355
356
        $this->bookingRepository
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<JhFlexiTime\Repos...ingRepositoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
357
            ->expects($this->exactly(1))
358
            ->method('getTotalBookedBetweenByUser')
359
            ->with(
360
                $user,
361
                $this->equalTo(new DateTime('3 February 2014 00:00:00')),
362
                $this->equalTo(new DateTime('10 February 2014 00:00:00'))
363
            )
364
            ->will($this->returnValue(30));
365
366
367
        $result     = $service->getTotals($user, $startDate, new DateTime("20 February 2014"));
368
369
        $expected = [
370
            'monthTotalWorkedHours' => 30,
371
            'monthTotalHours'       => 150.0,
372
            'monthBalance'          => -15.0,
373
            'runningBalance'        => -15.0,
374
            'monthRemainingHours'   => 112.5,
375
            'balanceForward'        => 0,
376
        ];
377
378
        $this->assertEquals($expected, $result);
379
    }
380
381
    public function testGetTotalsForAPreviousMonthWhereStartDateWasInAPreviousMonthToThat()
382
    {
383
        $user       = new User;
384
        $startDate  = new DateTime("1 January 2014");
385
        $today      = new DateTime("2 March 2014");
386
        $service    = $this->getService($today);
387
388
        $this->bookingRepository
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<JhFlexiTime\Repos...ingRepositoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
389
            ->expects($this->exactly(1))
390
            ->method('getTotalBookedBetweenByUser')
391
            ->with(
392
                $user,
393
                $this->equalTo(new DateTime('1 February 2014 00:00:00')),
394
                $this->equalTo(new DateTime('28 February 2014 23:59:59'))
395
            )
396
            ->will($this->returnValue(75));
397
398
399
        $result     = $service->getTotals($user, $startDate, new DateTime("1 February 2014"));
400
401
        $expected = [
402
            'monthTotalWorkedHours' => 75,
403
            'monthTotalHours'       => 150.0,
404
            'monthBalance'          => -75.0,
405
            'runningBalance'        => 0.0,
406
            'monthRemainingHours'   => 0,
407
            'balanceForward'        => 0,
408
        ];
409
410
        $this->assertEquals($expected, $result);
411
    }
412
413
    public function testGetTotalsForAPreviousMonthWhereStartDateWasInThatMonth()
414
    {
415
        $user       = new User;
416
        $startDate  = new DateTime("4 March 2014");
417
        $today      = new DateTime("10 April 2014");
418
        $service    = $this->getService($today);
419
420
        $this->bookingRepository
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<JhFlexiTime\Repos...ingRepositoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
421
            ->expects($this->exactly(1))
422
            ->method('getTotalBookedBetweenByUser')
423
            ->with(
424
                $user,
425
                $this->equalTo(new DateTime('4 March 2014 00:00:00')),
426
                $this->equalTo(new DateTime('31 March 2014 23:59:59'))
427
            )
428
            ->will($this->returnValue(75));
429
430
431
        $result     = $service->getTotals($user, $startDate, new DateTime("1 March 2014"));
432
433
        $expected = [
434
            'monthTotalWorkedHours' => 75,
435
            'monthTotalHours'       => 150.0,
436
            'monthBalance'          => -75.0,
437
            'runningBalance'        => -60.0,
438
            'monthRemainingHours'   => 0,
439
            'balanceForward'        => 0,
440
        ];
441
442
        $this->assertEquals($expected, $result);
443
    }
444
445
    public function testGetTotalsBalanceIsZeroIfInFutureMonth()
446
    {
447
        $user       = new User;
448
        $startDate  = new DateTime("4 March 2014");
449
        $today      = new DateTime("10 April 2014");
450
        $service    = $this->getService($today);
451
452
        $result  = $service->getTotals($user, $startDate, new DateTime("1 June 2014"));
453
454
        $expected = [
455
            'monthTotalWorkedHours' => 0,
456
            'monthTotalHours'       => 157.5,
457
            'monthBalance'          => 0,
458
            'runningBalance'        => -60.0, //because 8 days have not been booked in april
459
            'monthRemainingHours'   => 157.5,
460
            'balanceForward'        => 0,
461
        ];
462
463
        $this->assertEquals($expected, $result);
464
    }
465
466
    public function testGetTotalsBalanceIsZeroIfInFutureMonthWithRunningBalanceAddedOn()
467
    {
468
        $user       = new User;
469
        $startDate  = new DateTime("4 March 2014");
470
        $today      = new DateTime("10 April 2014");
471
        $service    = $this->getService($today);
472
473
        $balance = new RunningBalance;
474
        $balance->setBalance(50);
475
476
        $this->balanceRepository
477
            ->expects($this->once())
478
            ->method('findOneByUser')
479
            ->with($user)
480
            ->will($this->returnValue($balance));
481
482
        $result  = $service->getTotals($user, $startDate, new DateTime("1 June 2014"));
483
484
        $expected = [
485
            'monthTotalWorkedHours' => 0,
486
            'monthTotalHours'       => 157.5,
487
            'monthBalance'          => 0,
488
            'runningBalance'        => -10, //because 8 days have not been booked in april
489
            'monthRemainingHours'   => 157.5,
490
            'balanceForward'        => 50,
491
        ];
492
493
        $this->assertEquals($expected, $result);
494
    }
495
}
496