AbstractTestCase::assertInterval()   B
last analyzed

Complexity

Conditions 6
Paths 32

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
rs 8.439
cc 6
eloc 19
nc 32
nop 7
1
<?php
2
3
namespace HMLB\Date\Tests;
4
5
use Closure;
6
use HMLB\Date\Date;
7
use HMLB\Date\Interval;
8
use PHPUnit_Framework_TestCase;
9
10
abstract class AbstractTestCase extends PHPUnit_Framework_TestCase
11
{
12
    private $saveTz;
13
14
    protected function setUp()
15
    {
16
        //save current timezone
17
        $this->saveTz = date_default_timezone_get();
18
19
        date_default_timezone_set('America/Toronto');
20
    }
21
22
    protected function tearDown()
23
    {
24
        date_default_timezone_set($this->saveTz);
25
    }
26
27
    protected function assertDate(Date $d, $year, $month, $day, $hour = null, $minute = null, $second = null)
28
    {
29
        $this->assertSame($year, $d->getYear(), 'Date->year');
30
        $this->assertSame($month, $d->getMonth(), 'Date->month');
31
        $this->assertSame($day, $d->getDay(), 'Date->day');
32
33
        if ($hour !== null) {
34
            $this->assertSame($hour, $d->getHour(), 'Date->hour');
35
        }
36
37
        if ($minute !== null) {
38
            $this->assertSame($minute, $d->getMinute(), 'Date->minute');
39
        }
40
41
        if ($second !== null) {
42
            $this->assertSame($second, $d->getSecond(), 'Date->second');
43
        }
44
    }
45
46
    protected function assertInstanceOfDate($d)
47
    {
48
        $this->assertInstanceOf(Date::class, $d);
49
    }
50
51
    protected function assertInterval(
52
        Interval $ci,
53
        $years,
54
        $months = null,
55
        $days = null,
56
        $hours = null,
57
        $minutes = null,
58
        $seconds = null
59
    ) {
60
        $this->assertSame($years, $ci->years, 'Interval->years');
61
62
        if ($months !== null) {
63
            $this->assertSame($months, $ci->months, 'Interval->months');
64
        }
65
66
        if ($days !== null) {
67
            $this->assertSame($days, $ci->dayz, 'Interval->dayz');
68
        }
69
70
        if ($hours !== null) {
71
            $this->assertSame($hours, $ci->hours, 'Interval->hours');
72
        }
73
74
        if ($minutes !== null) {
75
            $this->assertSame($minutes, $ci->minutes, 'Interval->minutes');
76
        }
77
78
        if ($seconds !== null) {
79
            $this->assertSame($seconds, $ci->seconds, 'Interval->seconds');
80
        }
81
    }
82
83
    protected function assertInstanceOfInterval($d)
84
    {
85
        $this->assertInstanceOf('HMLB\Date\Interval', $d);
86
    }
87
88
    protected function wrapWithTestNow(Closure $func, Date $dt = null)
89
    {
90
        Date::setTestNow($dt ?: Date::now());
91
        $func();
92
        Date::setTestNow();
93
    }
94
}
95