|
19 | 19 | use Cake\Chronos\ChronosDate; |
20 | 20 | use Cake\Chronos\Test\TestCase\TestCase; |
21 | 21 | use DateTimeZone; |
| 22 | +use RuntimeException; |
22 | 23 |
|
23 | 24 | class TestingAidsTest extends TestCase |
24 | 25 | { |
@@ -231,4 +232,89 @@ public function testSetTestNowSingular() |
231 | 232 |
|
232 | 233 | $this->assertSame($c, Chronos::getTestNow()); |
233 | 234 | } |
| 235 | + |
| 236 | + public function testWithTestNowSetsAndRestoresNull() |
| 237 | + { |
| 238 | + $this->assertNull(Chronos::getTestNow()); |
| 239 | + |
| 240 | + $result = Chronos::withTestNow('2023-06-15 12:00:00', function () { |
| 241 | + $this->assertNotNull(Chronos::getTestNow()); |
| 242 | + $this->assertSame('2023-06-15', Chronos::now()->format('Y-m-d')); |
| 243 | + |
| 244 | + return 'callback result'; |
| 245 | + }); |
| 246 | + |
| 247 | + $this->assertSame('callback result', $result); |
| 248 | + $this->assertNull(Chronos::getTestNow()); |
| 249 | + } |
| 250 | + |
| 251 | + public function testWithTestNowRestoresPreviousTestNow() |
| 252 | + { |
| 253 | + $original = new Chronos('2020-01-01 00:00:00'); |
| 254 | + Chronos::setTestNow($original); |
| 255 | + |
| 256 | + Chronos::withTestNow('2023-06-15 12:00:00', function () { |
| 257 | + $this->assertSame('2023-06-15', Chronos::now()->format('Y-m-d')); |
| 258 | + }); |
| 259 | + |
| 260 | + $this->assertSame($original, Chronos::getTestNow()); |
| 261 | + $this->assertSame('2020-01-01', Chronos::now()->format('Y-m-d')); |
| 262 | + } |
| 263 | + |
| 264 | + public function testWithTestNowNested() |
| 265 | + { |
| 266 | + Chronos::setTestNow('2020-01-01 00:00:00'); |
| 267 | + |
| 268 | + Chronos::withTestNow('2021-06-15 00:00:00', function () { |
| 269 | + $this->assertSame('2021-06-15', Chronos::now()->format('Y-m-d')); |
| 270 | + |
| 271 | + Chronos::withTestNow('2022-12-25 00:00:00', function () { |
| 272 | + $this->assertSame('2022-12-25', Chronos::now()->format('Y-m-d')); |
| 273 | + }); |
| 274 | + |
| 275 | + $this->assertSame('2021-06-15', Chronos::now()->format('Y-m-d')); |
| 276 | + }); |
| 277 | + |
| 278 | + $this->assertSame('2020-01-01', Chronos::now()->format('Y-m-d')); |
| 279 | + } |
| 280 | + |
| 281 | + public function testWithTestNowRestoresOnException() |
| 282 | + { |
| 283 | + $original = new Chronos('2020-01-01 00:00:00'); |
| 284 | + Chronos::setTestNow($original); |
| 285 | + |
| 286 | + try { |
| 287 | + Chronos::withTestNow('2023-06-15 12:00:00', function () { |
| 288 | + throw new RuntimeException('Test exception'); |
| 289 | + }); |
| 290 | + $this->fail('Exception should have been thrown'); |
| 291 | + } catch (RuntimeException $e) { |
| 292 | + $this->assertSame('Test exception', $e->getMessage()); |
| 293 | + } |
| 294 | + |
| 295 | + $this->assertSame($original, Chronos::getTestNow()); |
| 296 | + } |
| 297 | + |
| 298 | + public function testWithTestNowWithChronosInstance() |
| 299 | + { |
| 300 | + $testTime = new Chronos('2023-06-15 14:30:00'); |
| 301 | + |
| 302 | + $result = Chronos::withTestNow($testTime, function () { |
| 303 | + return Chronos::now()->format('Y-m-d H:i:s'); |
| 304 | + }); |
| 305 | + |
| 306 | + $this->assertSame('2023-06-15 14:30:00', $result); |
| 307 | + $this->assertNull(Chronos::getTestNow()); |
| 308 | + } |
| 309 | + |
| 310 | + public function testWithTestNowWithNull() |
| 311 | + { |
| 312 | + Chronos::setTestNow('2020-01-01 00:00:00'); |
| 313 | + |
| 314 | + Chronos::withTestNow(null, function () { |
| 315 | + $this->assertNull(Chronos::getTestNow()); |
| 316 | + }); |
| 317 | + |
| 318 | + $this->assertSame('2020-01-01', Chronos::now()->format('Y-m-d')); |
| 319 | + } |
234 | 320 | } |
0 commit comments