|
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
|
|
|
$this->assertEquals('June', Dt::getMonth(13)); |
|
41
|
|
|
} catch (\Exception $e) { |
|
42
|
|
|
$errorObject = ($e instanceof MonthOutOfRange); |
|
43
|
|
|
$message = $e->getMessage(); |
|
44
|
|
|
} finally { |
|
45
|
|
|
$this->assertTrue($errorObject, $message); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Tests if we can get an instance of \DateTime by ::create() |
|
51
|
|
|
* |
|
52
|
|
|
* @return null |
|
53
|
|
|
*/ |
|
54
|
|
|
public function testGettingDateTimeInstance() |
|
55
|
|
|
{ |
|
56
|
|
|
$dateTime = Dt::create([ |
|
57
|
|
|
"day" => Dt::TODAY, "month" => dt::THIS_MONTH, "year" => dt::THIS_YEAR, |
|
58
|
|
|
]); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertTrue($dateTime instanceof \DateTime); |
|
61
|
|
|
$this->assertEquals($dateTime->format('d/m/Y'), date('d/m/Y')); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Tests getting first and last day from some periods |
|
66
|
|
|
* |
|
67
|
|
|
* @return null |
|
68
|
|
|
*/ |
|
69
|
|
|
public function testFirstAndLastDays() |
|
70
|
|
|
{ |
|
71
|
|
|
# First day of this month |
|
72
|
|
|
$dt = Dt::create([ |
|
73
|
|
|
"day" => Dt::FIRST_DAY, "month" => dt::THIS_MONTH, "year" => dt::THIS_YEAR, |
|
74
|
|
|
]); |
|
75
|
|
|
|
|
76
|
|
|
$dateTime = new \DateTime("first day of " . Dt::getMonth((int) date('m')) . " " . date('Y')); |
|
77
|
|
|
|
|
78
|
|
|
$this->assertEquals($dt->format('d/m/Y'), $dateTime->format('d/m/Y')); |
|
79
|
|
|
|
|
80
|
|
|
# Last day of this month |
|
81
|
|
|
$dt = Dt::create([ |
|
82
|
|
|
"day" => Dt::LAST_DAY, "month" => dt::THIS_MONTH, "year" => dt::THIS_YEAR, |
|
83
|
|
|
]); |
|
84
|
|
|
|
|
85
|
|
|
$dateTime = new \DateTime("last day of " . Dt::getMonth((int) date('m')) . " " . date('Y')); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertEquals($dt->format('d/m/Y'), $dateTime->format('d/m/Y')); |
|
88
|
|
|
|
|
89
|
|
|
# first day of last month |
|
90
|
|
|
$dt = Dt::create([ |
|
91
|
|
|
"day" => Dt::FIRST_DAY, "month" => dt::LAST_MONTH, "year" => dt::THIS_YEAR, |
|
92
|
|
|
]); |
|
93
|
|
|
|
|
94
|
|
|
$dateTime = new \DateTime("first day of -1 month"); |
|
95
|
|
|
|
|
96
|
|
|
$this->assertEquals($dt->format('d/m/Y'), $dateTime->format('d/m/Y')); |
|
97
|
|
|
|
|
98
|
|
|
# last day of last month |
|
99
|
|
|
$dt = Dt::create([ |
|
100
|
|
|
"day" => Dt::LAST_DAY, "month" => dt::LAST_MONTH, "year" => dt::THIS_YEAR, |
|
101
|
|
|
]); |
|
102
|
|
|
|
|
103
|
|
|
$dateTime = new \DateTime("last day of -1 month"); |
|
104
|
|
|
|
|
105
|
|
|
$this->assertEquals($dt->format('d/m/Y'), $dateTime->format('d/m/Y')); |
|
106
|
|
|
|
|
107
|
|
|
# first day of June of last year |
|
108
|
|
|
$dt = Dt::create([ |
|
109
|
|
|
"day" => Dt::FIRST_DAY, "month" => 6, "year" => dt::LAST_YEAR, |
|
110
|
|
|
]); |
|
111
|
|
|
|
|
112
|
|
|
$dateTime = new \DateTime("first day of June " . (date('Y') - 1)); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertEquals($dt->format('d/m/Y'), $dateTime->format('d/m/Y')); |
|
115
|
|
|
|
|
116
|
|
|
# last day of June of last year |
|
117
|
|
|
$dt = Dt::create([ |
|
118
|
|
|
"day" => Dt::LAST_DAY, "month" => 6, "year" => dt::LAST_YEAR, |
|
119
|
|
|
]); |
|
120
|
|
|
|
|
121
|
|
|
$dateTime = new \DateTime("last day of June " . (date('Y') - 1)); |
|
122
|
|
|
|
|
123
|
|
|
$this->assertEquals($dt->format('d/m/Y'), $dateTime->format('d/m/Y')); |
|
124
|
|
|
|
|
125
|
|
|
# 10th day of June of last year |
|
126
|
|
|
$dt = Dt::create([ |
|
127
|
|
|
"day" => 10, "month" => 6, "year" => dt::LAST_YEAR, |
|
128
|
|
|
]); |
|
129
|
|
|
|
|
130
|
|
|
$dateTime = new \DateTime("first day of June " . (date('Y') - 1)); |
|
131
|
|
|
$dateTime->add(new \DateInterval('P9D')); |
|
132
|
|
|
|
|
133
|
|
|
$this->assertEquals($dt->format('d/m/Y'), $dateTime->format('d/m/Y')); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|