|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Test: Nette\Utils\DateTime::fromParts(). |
| 5 | + */ |
| 6 | + |
| 7 | +use Tester\Assert; |
| 8 | +use Nette\Utils\DateTime; |
| 9 | + |
| 10 | +require __DIR__ . '/../bootstrap.php'; |
| 11 | + |
| 12 | + |
| 13 | +date_default_timezone_set('Europe/Prague'); |
| 14 | + |
| 15 | +Assert::same('0001-12-09 00:00:00.000000', DateTime::fromParts(1, 12, 9)->format('Y-m-d H:i:s.u')); |
| 16 | +Assert::same('0085-12-09 00:00:00.000000', DateTime::fromParts(85, 12, 9)->format('Y-m-d H:i:s.u')); |
| 17 | +Assert::same('1985-01-01 00:00:00.000000', DateTime::fromParts(1985, 1, 1)->format('Y-m-d H:i:s.u')); |
| 18 | +Assert::same('1985-12-19 00:00:00.000000', DateTime::fromParts(1985, 12, 19)->format('Y-m-d H:i:s.u')); |
| 19 | +Assert::same('1985-12-09 01:02:00.000000', DateTime::fromParts(1985, 12, 9, 1, 2)->format('Y-m-d H:i:s.u')); |
| 20 | +Assert::same('1985-12-09 01:02:03.000000', DateTime::fromParts(1985, 12, 9, 1, 2, 3)->format('Y-m-d H:i:s.u')); |
| 21 | +Assert::same('1985-12-09 11:22:33.000000', DateTime::fromParts(1985, 12, 9, 11, 22, 33)->format('Y-m-d H:i:s.u')); |
| 22 | +Assert::same('1985-12-09 11:22:59.123000', DateTime::fromParts(1985, 12, 9, 11, 22, 59.123)->format('Y-m-d H:i:s.u')); |
| 23 | + |
| 24 | +Assert::exception(function () { |
| 25 | + DateTime::fromParts(1985, 2, 29); |
| 26 | +}, Nette\InvalidArgumentException::class, "Invalid date '1985-02-29 00:00:0.00000'"); |
| 27 | + |
| 28 | +Assert::exception(function () { // year must be at least 1 due to limitation of checkdate() |
| 29 | + DateTime::fromParts(0, 12, 9); |
| 30 | +}, Nette\InvalidArgumentException::class); |
| 31 | + |
| 32 | +Assert::exception(function () { |
| 33 | + DateTime::fromParts(1985, 0, 9); |
| 34 | +}, Nette\InvalidArgumentException::class); |
| 35 | + |
| 36 | +Assert::exception(function () { |
| 37 | + DateTime::fromParts(1985, 13, 9); |
| 38 | +}, Nette\InvalidArgumentException::class); |
| 39 | + |
| 40 | +Assert::exception(function () { |
| 41 | + DateTime::fromParts(1985, 12, 0); |
| 42 | +}, Nette\InvalidArgumentException::class); |
| 43 | + |
| 44 | +Assert::exception(function () { |
| 45 | + DateTime::fromParts(1985, 12, 32); |
| 46 | +}, Nette\InvalidArgumentException::class); |
| 47 | + |
| 48 | +Assert::exception(function () { |
| 49 | + DateTime::fromParts(1985, 12, 9, -1); |
| 50 | +}, Nette\InvalidArgumentException::class); |
| 51 | + |
| 52 | +Assert::exception(function () { |
| 53 | + DateTime::fromParts(1985, 12, 9, 60); |
| 54 | +}, Nette\InvalidArgumentException::class); |
| 55 | + |
| 56 | +Assert::exception(function () { |
| 57 | + DateTime::fromParts(1985, 12, 9, 0, -1); |
| 58 | +}, Nette\InvalidArgumentException::class); |
| 59 | + |
| 60 | +Assert::exception(function () { |
| 61 | + DateTime::fromParts(1985, 12, 9, 0, 60); |
| 62 | +}, Nette\InvalidArgumentException::class); |
| 63 | + |
| 64 | +Assert::exception(function () { |
| 65 | + DateTime::fromParts(1985, 12, 9, 0, 0, -1); |
| 66 | +}, Nette\InvalidArgumentException::class); |
| 67 | + |
| 68 | +Assert::exception(function () { |
| 69 | + DateTime::fromParts(1985, 12, 9, 0, 0, 60); |
| 70 | +}, Nette\InvalidArgumentException::class); |
0 commit comments