|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Unit tests for JsonMapper's DateTime handling under strict object type checking. |
| 4 | + * |
| 5 | + * @category Tests |
| 6 | + * @package JsonMapper |
| 7 | + * @license OSL-3.0 http://opensource.org/licenses/osl-3.0 |
| 8 | + * @link https://github.com/cweiske/jsonmapper |
| 9 | + */ |
| 10 | +class DateTime_Strict_Test extends \PHPUnit\Framework\TestCase |
| 11 | +{ |
| 12 | + public static function validFormatData() |
| 13 | + { |
| 14 | + return [ |
| 15 | + 'date only' => ['2024-01-15'], |
| 16 | + 'space separator' => ['2024-01-15 12:30:45'], |
| 17 | + 'iso with colon offset' => ['2024-01-15T12:30:45+00:00'], |
| 18 | + 'rfc 3339 fractional Z' => ['2024-01-15T12:30:45.123Z'], |
| 19 | + 'compact offset' => ['2024-01-15T12:30:45+0100'], |
| 20 | + ]; |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * @dataProvider validFormatData |
| 25 | + */ |
| 26 | + public function testMapValidFormat($value) |
| 27 | + { |
| 28 | + $jm = new JsonMapper(); |
| 29 | + $jm->bStrictObjectTypeChecking = true; |
| 30 | + $sn = $jm->map( |
| 31 | + json_decode("{\"datetime\":\"$value\"}"), |
| 32 | + new JsonMapperTest_Object() |
| 33 | + ); |
| 34 | + $this->assertInstanceOf(DateTime::class, $sn->datetime); |
| 35 | + } |
| 36 | + |
| 37 | + public static function invalidFormatData() |
| 38 | + { |
| 39 | + return [ |
| 40 | + 'relative string' => ['now'], |
| 41 | + 'wrong format' => ['2024/01/15'], |
| 42 | + 'overflow date' => ['2024-02-30'], |
| 43 | + ]; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @dataProvider invalidFormatData |
| 48 | + */ |
| 49 | + public function testMapInvalidFormat($value) |
| 50 | + { |
| 51 | + $jm = new JsonMapper(); |
| 52 | + $jm->bStrictObjectTypeChecking = true; |
| 53 | + $this->expectException(JsonMapper_Exception::class); |
| 54 | + $this->expectExceptionMessage('JSON property "datetime" must be an object, string given'); |
| 55 | + $sn = $jm->map( |
| 56 | + json_decode("{\"datetime\":\"$value\"}"), |
| 57 | + new JsonMapperTest_Object() |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + public function testMapArrayValidFormat() |
| 62 | + { |
| 63 | + $jm = new JsonMapper(); |
| 64 | + $jm->bStrictObjectTypeChecking = true; |
| 65 | + $sn = $jm->map( |
| 66 | + json_decode("{\"typedSimpleArray\":[\"{$this->validFormatData()['date only'][0]}\",\"{$this->validFormatData()['space separator'][0]}\"]}"), |
| 67 | + new JsonMapperTest_Array() |
| 68 | + ); |
| 69 | + $this->assertIsArray($sn->typedSimpleArray); |
| 70 | + $this->assertCount(2, $sn->typedSimpleArray); |
| 71 | + $this->assertInstanceOf(DateTime::class, $sn->typedSimpleArray[0]); |
| 72 | + $this->assertInstanceOf(DateTime::class, $sn->typedSimpleArray[1]); |
| 73 | + } |
| 74 | + |
| 75 | + public function testMapArrayInvalidFormat() |
| 76 | + { |
| 77 | + $jm = new JsonMapper(); |
| 78 | + $jm->bStrictObjectTypeChecking = true; |
| 79 | + $this->expectException(JsonMapper_Exception::class); |
| 80 | + $this->expectExceptionMessage('JSON property "typedSimpleArray[1]" must be an object, string given'); |
| 81 | + $sn = $jm->map( |
| 82 | + json_decode("{\"typedSimpleArray\":[\"{$this->validFormatData()['date only'][0]}\",\"{$this->invalidFormatData()['relative string'][0]}\"]}"), |
| 83 | + new JsonMapperTest_Array() |
| 84 | + ); |
| 85 | + } |
| 86 | +} |
0 commit comments