/
CreateTest.php
185 lines (157 loc) · 4.65 KB
/
CreateTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/*
* This file is part of the Carbon package.
*
* (c) Brian Nesbitt <brian@nesbot.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tests\Carbon;
use Carbon\Carbon;
use DateTimeZone;
use Tests\AbstractTestCase;
class CreateTest extends AbstractTestCase
{
public function testCreateReturnsDatingInstance()
{
$d = Carbon::create();
$this->assertInstanceOfCarbon($d);
}
public function testCreateWithDefaults()
{
$d = Carbon::create();
$this->assertSame($d->getTimestamp(), Carbon::now()->getTimestamp());
}
public function testCreateWithYear()
{
$d = Carbon::create(2012);
$this->assertSame(2012, $d->year);
}
public function testCreateHandlesNegativeYear()
{
$c = Carbon::create(-1, 10, 12, 1, 2, 3);
$this->assertCarbon($c, -1, 10, 12, 1, 2, 3);
}
public function testCreateHandlesFiveDigitsPositiveYears()
{
$c = Carbon::create(999999999, 10, 12, 1, 2, 3);
$this->assertCarbon($c, 999999999, 10, 12, 1, 2, 3);
}
public function testCreateHandlesFiveDigitsNegativeYears()
{
$c = Carbon::create(-999999999, 10, 12, 1, 2, 3);
$this->assertCarbon($c, -999999999, 10, 12, 1, 2, 3);
}
public function testCreateWithMonth()
{
$d = Carbon::create(null, 3);
$this->assertSame(3, $d->month);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testCreateWithInvalidMonth()
{
Carbon::create(null, -5);
}
public function testCreateMonthWraps()
{
$d = Carbon::create(2011, 0, 1, 0, 0, 0);
$this->assertCarbon($d, 2010, 12, 1, 0, 0, 0);
}
public function testCreateWithDay()
{
$d = Carbon::create(null, null, 21);
$this->assertSame(21, $d->day);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testCreateWithInvalidDay()
{
Carbon::create(null, null, -4);
}
public function testCreateDayWraps()
{
$d = Carbon::create(2011, 1, 40, 0, 0, 0);
$this->assertCarbon($d, 2011, 2, 9, 0, 0, 0);
}
public function testCreateWithHourAndDefaultMinSecToZero()
{
$d = Carbon::create(null, null, null, 14);
$this->assertSame(14, $d->hour);
$this->assertSame(0, $d->minute);
$this->assertSame(0, $d->second);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testCreateWithInvalidHour()
{
Carbon::create(null, null, null, -1);
}
public function testCreateHourWraps()
{
$d = Carbon::create(2011, 1, 1, 24, 0, 0);
$this->assertCarbon($d, 2011, 1, 2, 0, 0, 0);
}
public function testCreateWithMinute()
{
$d = Carbon::create(null, null, null, null, 58);
$this->assertSame(58, $d->minute);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testCreateWithInvalidMinute()
{
Carbon::create(2011, 1, 1, 0, -2, 0);
}
public function testCreateMinuteWraps()
{
$d = Carbon::create(2011, 1, 1, 0, 62, 0);
$this->assertCarbon($d, 2011, 1, 1, 1, 2, 0);
}
public function testCreateWithSecond()
{
$d = Carbon::create(null, null, null, null, null, 59);
$this->assertSame(59, $d->second);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testCreateWithInvalidSecond()
{
Carbon::create(null, null, null, null, null, -2);
}
public function testCreateSecondsWrap()
{
$d = Carbon::create(2012, 1, 1, 0, 0, 61);
$this->assertCarbon($d, 2012, 1, 1, 0, 1, 1);
}
public function testCreateWithDateTimeZone()
{
$d = Carbon::create(2012, 1, 1, 0, 0, 0, new DateTimeZone('Europe/London'));
$this->assertCarbon($d, 2012, 1, 1, 0, 0, 0);
$this->assertSame('Europe/London', $d->tzName);
}
public function testCreateWithTimeZoneString()
{
$d = Carbon::create(2012, 1, 1, 0, 0, 0, 'Europe/London');
$this->assertCarbon($d, 2012, 1, 1, 0, 0, 0);
$this->assertSame('Europe/London', $d->tzName);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testCreateWithInvalidTimezoneOffset()
{
Carbon::createFromDate(2000, 1, 1, -28236);
}
public function testCreateWithValidTimezoneOffset()
{
$dt = Carbon::createFromDate(2000, 1, 1, -4);
$this->assertSame('America/New_York', $dt->tzName);
}
}