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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
<?php
declare(strict_types=1);
namespace Faker\Test\Core;
use Faker\Extension\DateTimeExtension;
use Faker\Test\TestCase;
/**
* @covers \Faker\Core\DateTime
*/
final class DateTimeTest extends TestCase
{
/**
* @var DateTimeExtension
*/
protected $extension;
protected function setUp(): void
{
parent::setUp();
$this->extension = $this->faker->ext(DateTimeExtension::class);
}
public function testDateTime()
{
$dateTime = $this->extension->dateTime('2005-10-19T14:12:00');
self::assertInstanceOf(\DateTime::class, $dateTime);
self::assertEquals(new \DateTime('1990-09-29T12:12:53'), $dateTime);
}
public function testDateTimeWithTimezone()
{
$dateTime = $this->extension->dateTime('2021-09-05T15:10:00', 'America/Los_Angeles');
self::assertInstanceOf(\DateTime::class, $dateTime);
self::assertEquals(new \DateTime('1999-12-11T22:41:46.000000-0800'), $dateTime);
self::assertEquals(new \DateTimeZone('America/Los_Angeles'), $dateTime->getTimezone());
}
public function testDateTimeAD()
{
$this->skipOn32bitSystem();
$dateTime = $this->extension->dateTimeAD('2012-04-12T19:22:23');
self::assertInstanceOf(\DateTime::class, $dateTime);
self::assertEquals(new \DateTime('1166-06-01T17:43:42'), $dateTime);
}
public function testDateTimeBetween()
{
$dateTime = $this->extension->dateTimeBetween('1998-12-18T11:23:40', '2004-09-15T22:10:45');
self::assertInstanceOf(\DateTime::class, $dateTime);
self::assertEquals(new \DateTime('2002-04-17T09:33:38'), $dateTime);
}
public function testDateTimeBetweenShouldThrowIfFromIsNotAnteriorToUntil()
{
self::expectException(\InvalidArgumentException::class);
$this->extension->dateTimeBetween('2004-09-15T22:10:45', '1998-12-18T11:23:40');
}
public function testDateTimeInInterval()
{
$dateTime = $this->extension->dateTimeInInterval('1999-07-16T17:30:12', '+2 years');
self::assertInstanceOf(\DateTime::class, $dateTime);
self::assertEquals(new \DateTime('2000-09-12T07:10:58'), $dateTime);
}
public function testDateTimeThisWeek()
{
$dateTime = $this->extension->dateTimeThisWeek();
self::assertInstanceOf(\DateTime::class, $dateTime);
self::assertGreaterThanOrEqual(new \DateTime('monday this week'), $dateTime);
self::assertLessThanOrEqual(new \DateTime('sunday this week'), $dateTime);
}
public function testDateTimeThisMonth()
{
$dateTime = $this->extension->dateTimeThisMonth();
self::assertInstanceOf(\DateTime::class, $dateTime);
self::assertGreaterThanOrEqual(new \DateTime('first day of this month'), $dateTime);
self::assertLessThanOrEqual(new \DateTime('last day of this month'), $dateTime);
}
public function testDateTimeThisYear()
{
$dateTime = $this->extension->dateTimeThisYear();
self::assertInstanceOf(\DateTime::class, $dateTime);
self::assertGreaterThanOrEqual(new \DateTime('first day of january'), $dateTime);
self::assertLessThanOrEqual(new \DateTime('last day of december'), $dateTime);
}
public function testDateTimeThisDecade()
{
$dateTime = $this->extension->dateTimeThisDecade();
$year = floor(date('Y') / 10) * 10;
self::assertInstanceOf(\DateTime::class, $dateTime);
self::assertGreaterThanOrEqual(new \DateTime("first day of january $year"), $dateTime);
self::assertLessThanOrEqual(new \DateTime('now'), $dateTime);
}
public function testDateTimeThisCentury()
{
$dateTime = $this->extension->dateTimeThisCentury();
$year = floor(date('Y') / 100) * 100;
self::assertInstanceOf(\DateTime::class, $dateTime);
self::assertGreaterThanOrEqual(new \DateTime("first day of january $year"), $dateTime);
self::assertLessThanOrEqual(new \DateTime('now'), $dateTime);
}
public function testDate()
{
$this->skipOn32bitSystem();
$date = $this->extension->date('Y-m-d', '2102-11-12T14:45:29');
self::assertIsString($date);
self::assertEquals('2046-12-26', $date);
}
public function testTime()
{
$time = $this->extension->time('H:i:s', '1978-06-27T09:43:21');
self::assertIsString($time);
self::assertEquals('21:59:44', $time);
}
public function testUnixTime()
{
$unixTime = $this->extension->unixTime('1993-08-29T15:10:00');
self::assertIsInt($unixTime);
self::assertEquals(432630664, $unixTime);
}
public function testUnitTimeWithNumericUntil()
{
$unixTime = $this->extension->unixTime(1643830258);
self::assertIsInt($unixTime);
self::assertEquals(952499510, $unixTime);
}
public function testIso8601()
{
$iso8601 = $this->extension->iso8601('1993-07-11T15:10:00');
self::assertIsString($iso8601);
self::assertMatchesRegularExpression('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+\d{4}$/', $iso8601);
self::assertEquals('1983-08-19T21:45:51+0000', $iso8601);
}
public function testAmPm()
{
$amPm = $this->extension->amPm('1929-04-14T15:10:23');
self::assertIsString($amPm);
self::assertEquals('pm', $amPm);
self::assertContains($amPm, ['am', 'pm']);
}
public function testDayOfMonth()
{
$dayOfMonth = $this->extension->dayOfMonth('2001-04-29T15:10:12');
self::assertIsString($dayOfMonth);
self::assertEquals('25', $dayOfMonth);
}
public function testDayOfWeek()
{
$dayOfWeek = $this->extension->dayOfWeek('2021-12-12T15:10:00');
self::assertIsString($dayOfWeek);
self::assertEquals('Monday', $dayOfWeek);
}
public function testMonth()
{
$month = $this->extension->month('2021-05-23T15:10:00');
self::assertIsString($month);
self::assertEquals('10', $month);
}
public function testMonthName()
{
$monthName = $this->extension->monthName('2021-06-06T15:10:00');
self::assertIsString($monthName);
self::assertEquals('October', $monthName);
}
public function testYear()
{
$year = $this->extension->year('2021-09-12T15:10:00');
self::assertIsString($year);
self::assertEquals('1999', $year);
}
public function testCentury()
{
$century = $this->extension->century();
self::assertIsString($century);
self::assertEquals('XIX', $century);
}
public function testTimezone()
{
$timezone = $this->extension->timezone();
$countryTimezone = $this->extension->timezone('US');
self::assertIsString($timezone);
self::assertContains($timezone, \DateTimeZone::listIdentifiers());
self::assertIsString($countryTimezone);
self::assertContains($countryTimezone, \DateTimeZone::listIdentifiers(\DateTimeZone::PER_COUNTRY, 'US'));
}
}
|