|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\ODM\MongoDB\Tests\Types; |
| 6 | + |
| 7 | +use DateTime; |
| 8 | +use DateTimeImmutable; |
| 9 | +use DateTimeZone; |
| 10 | +use Doctrine\ODM\MongoDB\Types\DateImmutableType; |
| 11 | +use Doctrine\ODM\MongoDB\Types\Type; |
| 12 | +use InvalidArgumentException; |
| 13 | +use MongoDB\BSON\UTCDateTime; |
| 14 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use stdClass; |
| 17 | + |
| 18 | +use function assert; |
| 19 | +use function date; |
| 20 | +use function strtotime; |
| 21 | + |
| 22 | +use const PHP_INT_SIZE; |
| 23 | + |
| 24 | +class DatePointTypeTest extends TestCase |
| 25 | +{ |
| 26 | + public function testGetDateTime(): void |
| 27 | + { |
| 28 | + $type = Type::getType(Type::DATE_POINT); |
| 29 | + assert($type instanceof DateImmutableType); |
| 30 | + |
| 31 | + $timestamp = 100000000.001; |
| 32 | + $dateTime = $type->getDateTime($timestamp); |
| 33 | + self::assertEquals($timestamp, $dateTime->format('U.u')); |
| 34 | + |
| 35 | + $mongoDate = new UTCDateTime(100000000001); |
| 36 | + $dateTime = $type->getDateTime($mongoDate); |
| 37 | + self::assertEquals($timestamp, $dateTime->format('U.u')); |
| 38 | + } |
| 39 | + |
| 40 | + public function testConvertToDatabaseValue(): void |
| 41 | + { |
| 42 | + $type = Type::getType(Type::DATE_POINT); |
| 43 | + |
| 44 | + self::assertNull($type->convertToDatabaseValue(null), 'null is not converted'); |
| 45 | + |
| 46 | + $mongoDate = new UTCDateTime(); |
| 47 | + self::assertSame($mongoDate, $type->convertToDatabaseValue($mongoDate), 'MongoDate objects are not converted'); |
| 48 | + |
| 49 | + $timestamp = 100000000.123; |
| 50 | + $dateTime = DateTimeImmutable::createFromFormat('U.u', (string) $timestamp); |
| 51 | + $mongoDate = new UTCDateTime(100000000123); |
| 52 | + self::assertEquals($mongoDate, $type->convertToDatabaseValue($dateTime), 'DateTimeImmutable objects are converted to MongoDate objects'); |
| 53 | + self::assertEquals($mongoDate, $type->convertToDatabaseValue($timestamp), 'Numeric timestamps are converted to MongoDate objects'); |
| 54 | + self::assertEquals($mongoDate, $type->convertToDatabaseValue('' . $timestamp), 'String dates are converted to MongoDate objects'); |
| 55 | + self::assertEquals($mongoDate, $type->convertToDatabaseValue($mongoDate), 'MongoDate objects are converted to MongoDate objects'); |
| 56 | + self::assertEquals(null, $type->convertToDatabaseValue(null), 'null are converted to null'); |
| 57 | + } |
| 58 | + |
| 59 | + public function testConvertDateTime(): void |
| 60 | + { |
| 61 | + $type = Type::getType(Type::DATE); |
| 62 | + |
| 63 | + $timestamp = 100000000.123; |
| 64 | + $mongoDate = new UTCDateTime(100000000123); |
| 65 | + |
| 66 | + $dateTime = DateTime::createFromFormat('U.u', (string) $timestamp); |
| 67 | + self::assertEquals($mongoDate, $type->convertToDatabaseValue($dateTime), 'DateTime objects are converted to MongoDate objects'); |
| 68 | + } |
| 69 | + |
| 70 | + public function testConvertOldDate(): void |
| 71 | + { |
| 72 | + $type = Type::getType(Type::DATE_POINT); |
| 73 | + |
| 74 | + $date = new DateTimeImmutable('1900-01-01 00:00:00.123', new DateTimeZone('UTC')); |
| 75 | + $timestamp = '-2208988800.123'; |
| 76 | + self::assertEquals($type->convertToDatabaseValue($timestamp), $type->convertToDatabaseValue($date)); |
| 77 | + } |
| 78 | + |
| 79 | + /** @param mixed $value */ |
| 80 | + #[DataProvider('provideInvalidDateValues')] |
| 81 | + public function testConvertToDatabaseValueWithInvalidValues($value): void |
| 82 | + { |
| 83 | + $type = Type::getType(Type::DATE_POINT); |
| 84 | + $this->expectException(InvalidArgumentException::class); |
| 85 | + $type->convertToDatabaseValue($value); |
| 86 | + } |
| 87 | + |
| 88 | + public static function provideInvalidDateValues(): array |
| 89 | + { |
| 90 | + return [ |
| 91 | + 'array' => [[]], |
| 92 | + 'string' => ['whatever'], |
| 93 | + 'bool' => [false], |
| 94 | + 'object' => [new stdClass()], |
| 95 | + 'invalid string' => ['foo'], |
| 96 | + ]; |
| 97 | + } |
| 98 | + |
| 99 | + /** @param mixed $input */ |
| 100 | + #[DataProvider('provideDatabaseToPHPValues')] |
| 101 | + public function testConvertToPHPValue($input, DateTimeImmutable $output): void |
| 102 | + { |
| 103 | + $type = Type::getType(Type::DATE_POINT); |
| 104 | + $return = $type->convertToPHPValue($input); |
| 105 | + |
| 106 | + self::assertInstanceOf('DateTimeImmutable', $return); |
| 107 | + $this->assertTimestampEquals($output, $return); |
| 108 | + } |
| 109 | + |
| 110 | + public function testConvertToPHPValueDoesNotConvertNull(): void |
| 111 | + { |
| 112 | + $type = Type::getType(Type::DATE_POINT); |
| 113 | + |
| 114 | + self::assertNull($type->convertToPHPValue(null)); |
| 115 | + } |
| 116 | + |
| 117 | + /** @param mixed $input */ |
| 118 | + #[DataProvider('provideDatabaseToPHPValues')] |
| 119 | + public function testClosureToPHP($input, DateTimeImmutable $output): void |
| 120 | + { |
| 121 | + $type = Type::getType(Type::DATE_POINT); |
| 122 | + |
| 123 | + $return = (static function ($value) use ($type) { |
| 124 | + $return = null; |
| 125 | + eval($type->closureToPHP()); |
| 126 | + |
| 127 | + return $return; |
| 128 | + })($input); |
| 129 | + |
| 130 | + // @phpstan-ignore-next-line |
| 131 | + self::assertInstanceOf(DateTimeImmutable::class, $return); |
| 132 | + $this->assertTimestampEquals($output, $return); |
| 133 | + } |
| 134 | + |
| 135 | + public static function provideDatabaseToPHPValues(): array |
| 136 | + { |
| 137 | + $yesterday = strtotime('yesterday'); |
| 138 | + $mongoDate = new UTCDateTime($yesterday * 1000); |
| 139 | + $dateTime = new DateTimeImmutable('@' . $yesterday); |
| 140 | + |
| 141 | + return [ |
| 142 | + [$dateTime, $dateTime], |
| 143 | + [$mongoDate, $dateTime], |
| 144 | + [$yesterday, $dateTime], |
| 145 | + [date('c', $yesterday), $dateTime], |
| 146 | + [new UTCDateTime(100000000123), DateTimeImmutable::createFromFormat('U.u', '100000000.123')], |
| 147 | + ]; |
| 148 | + } |
| 149 | + |
| 150 | + public function test32bit1900Date(): void |
| 151 | + { |
| 152 | + if (PHP_INT_SIZE !== 4) { |
| 153 | + $this->markTestSkipped('Platform is not 32-bit'); |
| 154 | + } |
| 155 | + |
| 156 | + $type = Type::getType(Type::DATE_POINT); |
| 157 | + $this->expectException(InvalidArgumentException::class); |
| 158 | + $type->convertToDatabaseValue('1900-01-01'); |
| 159 | + } |
| 160 | + |
| 161 | + public function test64bit1900Date(): void |
| 162 | + { |
| 163 | + if (PHP_INT_SIZE !== 8) { |
| 164 | + $this->markTestSkipped('Platform is not 64-bit'); |
| 165 | + } |
| 166 | + |
| 167 | + $type = Type::getType(Type::DATE_POINT); |
| 168 | + $return = $type->convertToDatabaseValue('1900-01-01'); |
| 169 | + |
| 170 | + self::assertInstanceOf(UTCDateTime::class, $return); |
| 171 | + self::assertEquals(new UTCDateTime(strtotime('1900-01-01') * 1000), $return); |
| 172 | + } |
| 173 | + |
| 174 | + private function assertTimestampEquals(DateTimeImmutable $expected, DateTimeImmutable $actual): void |
| 175 | + { |
| 176 | + self::assertEquals($expected->format('U.u'), $actual->format('U.u')); |
| 177 | + } |
| 178 | +} |
0 commit comments