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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
<?php
declare(strict_types=1);
/**
* 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\Factory;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use Carbon\Factory;
use Carbon\FactoryImmutable;
use DateTimeImmutable;
use Psr\Clock\ClockInterface;
use ReflectionFunction;
use RuntimeException;
use Tests\AbstractTestCase;
use Tests\Carbon\Fixtures\MyCarbon;
class FactoryTest extends AbstractTestCase
{
public function testFactory()
{
$factory = new Factory();
$this->assertInstanceOf(Carbon::class, $factory->parse('2018-01-01'));
$this->assertSame('01/01/2018', $factory->parse('2018-01-01')->format('d/m/Y'));
$factory = new Factory([
'locale' => 'fr',
]);
$this->assertSame('fr', $factory->parse('2018-01-01')->locale);
$factory = new Factory([
'locale' => 'fr',
], MyCarbon::class);
$this->assertInstanceOf(MyCarbon::class, $factory->parse('2018-01-01'));
$this->assertSame('01/01/2018', $factory->parse('2018-01-01')->format('d/m/Y'));
$factory = new FactoryImmutable([
'locale' => 'fr',
]);
$this->assertInstanceOf(CarbonImmutable::class, $factory->parse('2018-01-01'));
$this->assertSame('01/01/2018', $factory->parse('2018-01-01')->format('d/m/Y'));
}
public function testFactoryModification()
{
$factory = new Factory();
$this->assertSame(Carbon::class, $factory->className());
$this->assertSame($factory, $factory->className(MyCarbon::class));
$this->assertSame(MyCarbon::class, $factory->className());
$this->assertSame([], $factory->settings());
$this->assertSame($factory, $factory->settings([
'locale' => 'fr',
]));
$this->assertSame([
'locale' => 'fr',
], $factory->settings());
$this->assertSame($factory, $factory->mergeSettings([
'timezone' => 'Europe/Paris',
]));
$this->assertSame([
'locale' => 'fr',
'timezone' => 'Europe/Paris',
], $factory->settings());
$this->assertSame($factory, $factory->settings([
'timezone' => 'Europe/Paris',
]));
$this->assertSame([
'timezone' => 'Europe/Paris',
], $factory->settings());
}
public function testFactoryTimezone()
{
Carbon::setTestNowAndTimezone(Carbon::parse('2020-09-04 03:39:04.123456', 'UTC'));
$factory = new Factory();
$date = $factory->now();
$this->assertInstanceOf(Carbon::class, $date);
$this->assertSame('2020-09-04 03:39:04.123456 UTC', $date->format('Y-m-d H:i:s.u e'));
$factory = new Factory([
'timezone' => 'Europe/Paris',
]);
$this->assertSame('2020-09-04 05:39:04.123456 Europe/Paris', $factory->now()->format('Y-m-d H:i:s.u e'));
$this->assertSame('2020-09-04 00:00:00.000000 Europe/Paris', $factory->today()->format('Y-m-d H:i:s.u e'));
$this->assertSame('2020-09-05 00:00:00.000000 Europe/Paris', $factory->tomorrow()->format('Y-m-d H:i:s.u e'));
$this->assertSame('2020-09-04 09:39:04.123456 Europe/Paris', $factory->parse('2020-09-04 09:39:04.123456')->format('Y-m-d H:i:s.u e'));
$factory = new Factory([
'timezone' => 'America/Toronto',
]);
$this->assertSame('2020-09-03 23:39:04.123456 America/Toronto', $factory->now()->format('Y-m-d H:i:s.u e'));
$this->assertSame('2020-09-03 00:00:00.000000 America/Toronto', $factory->today()->format('Y-m-d H:i:s.u e'));
$this->assertSame('2020-09-04 00:00:00.000000 America/Toronto', $factory->tomorrow()->format('Y-m-d H:i:s.u e'));
$this->assertSame('2020-09-04 09:39:04.123456 America/Toronto', $factory->parse('2020-09-04 09:39:04.123456')->format('Y-m-d H:i:s.u e'));
$factory = new Factory([
'timezone' => 'Asia/Shanghai',
]);
$baseDate = Carbon::parse('2021-08-01 08:00:00', 'UTC');
$date = $factory->createFromTimestamp($baseDate->getTimestamp());
$this->assertSame('2021-08-01T16:00:00+08:00', $date->format('c'));
$date = $factory->make('2021-08-01 08:00:00');
$this->assertSame('2021-08-01T08:00:00+08:00', $date->format('c'));
$date = $factory->make($baseDate);
$this->assertSame('2021-08-01T16:00:00+08:00', $date->format('c'));
$date = $factory->create($baseDate);
$this->assertSame('2021-08-01T16:00:00+08:00', $date->format('c'));
$date = $factory->parse($baseDate);
$this->assertSame('2021-08-01T16:00:00+08:00', $date->format('c'));
$date = $factory->instance($baseDate);
$this->assertSame('2021-08-01T16:00:00+08:00', $date->format('c'));
$date = $factory->make('2021-08-01 08:00:00+00:20');
$this->assertSame('2021-08-01T08:00:00+00:20', $date->format('c'));
$date = $factory->parse('2021-08-01T08:00:00Z');
$this->assertSame('2021-08-01T08:00:00+00:00', $date->format('c'));
$date = $factory->create('2021-08-01 08:00:00 UTC');
$this->assertSame('2021-08-01T08:00:00+00:00', $date->format('c'));
$date = $factory->make('2021-08-01 08:00:00 Europe/Paris');
$this->assertSame('2021-08-01T08:00:00+02:00', $date->format('c'));
}
public function testPsrClock()
{
FactoryImmutable::setCurrentClock(null);
FactoryImmutable::getDefaultInstance()->setTestNow(null);
$initial = Carbon::now('UTC');
$factory = new FactoryImmutable();
$factory->setTestNow($initial);
$this->assertInstanceOf(ClockInterface::class, $factory);
$this->assertInstanceOf(DateTimeImmutable::class, $factory->now());
$this->assertInstanceOf(CarbonImmutable::class, $factory->now());
$this->assertSame('America/Toronto', $factory->now()->tzName);
$this->assertSame('UTC', $factory->now('UTC')->tzName);
$timezonedFactory = $factory->withTimeZone('Asia/Tokyo');
$this->assertInstanceOf(CarbonImmutable::class, $timezonedFactory->now());
$this->assertSame('Asia/Tokyo', $timezonedFactory->now()->tzName);
$this->assertSame('America/Toronto', $timezonedFactory->now('America/Toronto')->tzName);
$this->assertSame(
$initial->format('Y-m-d H:i:s.u'),
$factory->now('UTC')->format('Y-m-d H:i:s.u'),
);
$before = microtime(true);
$factory->sleep(5);
$factory->sleep(20);
$after = microtime(true);
$this->assertLessThan(0.1, $after - $before);
$this->assertSame(
$initial->copy()->addSeconds(25)->format('Y-m-d H:i:s.u'),
$factory->now('UTC')->format('Y-m-d H:i:s.u'),
);
$factory = new FactoryImmutable();
$factory->setTestNow(null);
$before = new DateTimeImmutable('now UTC');
$now = $factory->now('UTC');
$after = new DateTimeImmutable('now UTC');
$this->assertGreaterThanOrEqual($before, $now);
$this->assertLessThanOrEqual($after, $now);
$before = new DateTimeImmutable('now UTC');
$factory->sleep(0.5);
$after = new DateTimeImmutable('now UTC');
$this->assertSame(
5,
(int) round(10 * ((float) $after->format('U.u') - ((float) $before->format('U.u')))),
);
}
public function testIsolation(): void
{
CarbonImmutable::setTestNow('1990-07-31 23:59:59');
$libAFactory = new FactoryImmutable();
$libAFactory->setTestNow('2000-02-05 15:20:00');
$libBFactory = new FactoryImmutable();
$libBFactory->setTestNow('2050-12-01 00:00:00');
$this->assertSame('2000-02-05 15:20:00', (string) $libAFactory->now());
$this->assertSame('2050-12-01 00:00:00', (string) $libBFactory->now());
$this->assertSame('1990-07-31 23:59:59', (string) CarbonImmutable::now());
CarbonImmutable::setTestNow();
}
public function testClosureMock(): void
{
$factory = new Factory();
$now = Carbon::parse('2024-01-18 00:00:00');
$factory->setTestNow(static fn () => $now);
$result = $factory->now();
$this->assertNotSame($now, $result);
$this->assertSame($now->format('Y-m-d H:i:s.u e'), $result->format('Y-m-d H:i:s.u e'));
}
public function testClosureMockTypeFailure(): void
{
$factory = new Factory();
$closure = static fn () => 42;
$factory->setTestNow($closure);
$function = new ReflectionFunction($closure);
$this->expectExceptionObject(new RuntimeException(
'The test closure defined in '.$function->getFileName().
' at line '.$function->getStartLine().' returned integer'.
'; expected '.CarbonInterface::class.'|null',
));
$factory->now();
}
}
|