1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DronePHP (http://www.dronephp.com) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/Pleets/DronePHP |
6
|
|
|
* @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org) |
7
|
|
|
* @license http://www.dronephp.com/license |
8
|
|
|
* @author Darío Rivera <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace DroneTest\Util; |
12
|
|
|
|
13
|
|
|
use Drone\Util\DateTime as Dt; |
14
|
|
|
use Drone\Util\Exception\MonthOutOfRange; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
|
17
|
|
|
class DateTimeTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Tests if we can get the month name |
21
|
|
|
* |
22
|
|
|
* @return null |
23
|
|
|
*/ |
24
|
|
|
public function testGetMonthName() |
25
|
|
|
{ |
26
|
|
|
$this->assertEquals('June', Dt::getMonth(6)); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Tests throwing exception when month is out of rage |
31
|
|
|
* |
32
|
|
|
* @return null |
33
|
|
|
*/ |
34
|
|
|
public function testMonthOutOfRangeException() |
35
|
|
|
{ |
36
|
|
|
$errorObject = null; |
37
|
|
|
$message = "No exception"; |
38
|
|
|
|
39
|
|
|
try |
40
|
|
|
{ |
41
|
|
|
$this->assertEquals('June', Dt::getMonth(13)); |
42
|
|
|
} |
43
|
|
|
catch (\Exception $e) |
44
|
|
|
{ |
45
|
|
|
$errorObject = ($e instanceof MonthOutOfRange); |
46
|
|
|
$message = $e->getMessage(); |
47
|
|
|
} |
48
|
|
|
finally |
49
|
|
|
{ |
50
|
|
|
$this->assertTrue($errorObject, $message); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Tests if we can get an instance of \DateTime by ::create() |
56
|
|
|
* |
57
|
|
|
* @return null |
58
|
|
|
*/ |
59
|
|
|
public function testGettingDateTimeInstance() |
60
|
|
|
{ |
61
|
|
|
$dateTime = Dt::create([ |
62
|
|
|
"day" => Dt::TODAY, "month" => dt::THIS_MONTH, "year" => dt::THIS_YEAR |
63
|
|
|
]); |
64
|
|
|
|
65
|
|
|
$this->assertTrue($dateTime instanceof \DateTime); |
66
|
|
|
$this->assertEquals($dateTime->format('d/m/Y'), date('d/m/Y')); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Tests getting first and last day from some periods |
71
|
|
|
* |
72
|
|
|
* @return null |
73
|
|
|
*/ |
74
|
|
|
public function testFirstAndLastDays() |
75
|
|
|
{ |
76
|
|
|
# First day of this month |
77
|
|
|
$dt = Dt::create([ |
78
|
|
|
"day" => Dt::FIRST_DAY, "month" => dt::THIS_MONTH, "year" => dt::THIS_YEAR |
79
|
|
|
]); |
80
|
|
|
|
81
|
|
|
$dateTime = new \DateTime("first day of " . Dt::getMonth((int) date('m')) . " " . date('Y')); |
82
|
|
|
|
83
|
|
|
$this->assertEquals($dt->format('d/m/Y'), $dateTime->format('d/m/Y')); |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
# Last day of this month |
87
|
|
|
$dt = Dt::create([ |
88
|
|
|
"day" => Dt::LAST_DAY, "month" => dt::THIS_MONTH, "year" => dt::THIS_YEAR |
89
|
|
|
]); |
90
|
|
|
|
91
|
|
|
$dateTime = new \DateTime("last day of " . Dt::getMonth((int) date('m')) . " " . date('Y')); |
92
|
|
|
|
93
|
|
|
$this->assertEquals($dt->format('d/m/Y'), $dateTime->format('d/m/Y')); |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
# first day of last month |
97
|
|
|
$dt = Dt::create([ |
98
|
|
|
"day" => Dt::FIRST_DAY, "month" => dt::LAST_MONTH, "year" => dt::THIS_YEAR |
99
|
|
|
]); |
100
|
|
|
|
101
|
|
|
$dateTime = new \DateTime("first day of -1 month"); |
102
|
|
|
|
103
|
|
|
$this->assertEquals($dt->format('d/m/Y'), $dateTime->format('d/m/Y')); |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
# last day of last month |
107
|
|
|
$dt = Dt::create([ |
108
|
|
|
"day" => Dt::LAST_DAY, "month" => dt::LAST_MONTH, "year" => dt::THIS_YEAR |
109
|
|
|
]); |
110
|
|
|
|
111
|
|
|
$dateTime = new \DateTime("last day of -1 month"); |
112
|
|
|
|
113
|
|
|
$this->assertEquals($dt->format('d/m/Y'), $dateTime->format('d/m/Y')); |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
# first day of June of last year |
117
|
|
|
$dt = Dt::create([ |
118
|
|
|
"day" => Dt::FIRST_DAY, "month" => 6, "year" => dt::LAST_YEAR |
119
|
|
|
]); |
120
|
|
|
|
121
|
|
|
$dateTime = new \DateTime("first day of June " . (date('Y') - 1)); |
122
|
|
|
|
123
|
|
|
$this->assertEquals($dt->format('d/m/Y'), $dateTime->format('d/m/Y')); |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
# last day of June of last year |
127
|
|
|
$dt = Dt::create([ |
128
|
|
|
"day" => Dt::LAST_DAY, "month" => 6, "year" => dt::LAST_YEAR |
129
|
|
|
]); |
130
|
|
|
|
131
|
|
|
$dateTime = new \DateTime("last day of June " . (date('Y') - 1)); |
132
|
|
|
|
133
|
|
|
$this->assertEquals($dt->format('d/m/Y'), $dateTime->format('d/m/Y')); |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
# 10th day of June of last year |
137
|
|
|
$dt = Dt::create([ |
138
|
|
|
"day" => 10, "month" => 6, "year" => dt::LAST_YEAR |
139
|
|
|
]); |
140
|
|
|
|
141
|
|
|
$dateTime = new \DateTime("first day of June " . (date('Y') - 1)); |
142
|
|
|
$dateTime->add(new \DateInterval('P9D')); |
143
|
|
|
|
144
|
|
|
$this->assertEquals($dt->format('d/m/Y'), $dateTime->format('d/m/Y')); |
145
|
|
|
} |
146
|
|
|
} |