|
6 | 6 |
|
7 | 7 | use Cake\Chronos\Date; |
8 | 8 | use Ecodev\Felix\Api\Scalar\DateType; |
| 9 | +use GraphQL\Language\AST\IntValueNode; |
| 10 | +use GraphQL\Language\AST\StringValueNode; |
9 | 11 | use PHPUnit\Framework\TestCase; |
10 | 12 |
|
11 | 13 | final class DateTypeTest extends TestCase |
12 | 14 | { |
13 | | - public function testTimezoneIsIgnored(): void |
| 15 | + /** |
| 16 | + * @var string |
| 17 | + */ |
| 18 | + private $timezone; |
| 19 | + |
| 20 | + protected function setUp(): void |
14 | 21 | { |
15 | | - $date = new Date(); |
16 | | - $offsetFromGmt = $date->dst ? $date->offsetHours - 1 : $date->offsetHours; |
17 | | - $timezone = sprintf('+%02u:00', $offsetFromGmt); |
| 22 | + $this->timezone = date_default_timezone_get(); |
| 23 | + date_default_timezone_set('Europe/Zurich'); |
| 24 | + } |
18 | 25 |
|
| 26 | + protected function tearDown(): void |
| 27 | + { |
| 28 | + date_default_timezone_set($this->timezone); |
| 29 | + } |
| 30 | + |
| 31 | + public function testSerialize(): void |
| 32 | + { |
19 | 33 | $type = new DateType(); |
20 | | - $actual = $type->parseValue('2010-02-09'); |
21 | | - self::assertInstanceOf(Date::class, $actual); |
22 | | - self::assertSame('2010-02-09T00:00:00' . $timezone, $actual->format('c')); |
| 34 | + $date = new Date('2010-02-03'); |
| 35 | + $actual = $type->serialize($date); |
| 36 | + self::assertSame('2010-02-03', $actual); |
| 37 | + } |
23 | 38 |
|
24 | | - $actual = $type->parseValue('2010-02-09T23:00:00'); |
| 39 | + /** |
| 40 | + * @dataProvider providerValues |
| 41 | + */ |
| 42 | + public function testParseValue(string $input, string $expected): void |
| 43 | + { |
| 44 | + $type = new DateType(); |
| 45 | + $actual = $type->parseValue($input); |
25 | 46 | self::assertInstanceOf(Date::class, $actual); |
26 | | - self::assertSame('2010-02-09T00:00:00' . $timezone, $actual->format('c')); |
| 47 | + self::assertSame($expected, $actual->format('c')); |
| 48 | + } |
27 | 49 |
|
28 | | - $actual = $type->parseValue('2010-02-09T02:00:00+08:00'); |
| 50 | + /** |
| 51 | + * @dataProvider providerValues |
| 52 | + */ |
| 53 | + public function testParseLiteral(string $input, string $expected): void |
| 54 | + { |
| 55 | + $type = new DateType(); |
| 56 | + $ast = new StringValueNode(['value' => $input]); |
| 57 | + |
| 58 | + $actual = $type->parseLiteral($ast); |
29 | 59 | self::assertInstanceOf(Date::class, $actual); |
30 | | - self::assertSame('2010-02-09T00:00:00' . $timezone, $actual->format('c'), 'timezone should be ignored'); |
| 60 | + self::assertSame($expected, $actual->format('c')); |
| 61 | + } |
31 | 62 |
|
32 | | - $date = new Date('2010-02-03'); |
33 | | - $actual = $type->serialize($date); |
34 | | - self::assertSame('2010-02-03', $actual); |
| 63 | + public function testParseLiteralAsInt(): void |
| 64 | + { |
| 65 | + $type = new DateType(); |
| 66 | + $ast = new IntValueNode(['value' => 123]); |
35 | 67 |
|
36 | | - $actual = $type->parseValue('2020-03-24T23:30:00+04.5:0-30'); |
37 | | - self::assertInstanceOf(Date::class, $actual); |
38 | | - self::assertSame('2020-03-24T00:00:00' . $timezone, $actual->format('c'), 'timezone should be ignored'); |
| 68 | + $this->expectExceptionMessage('Query error: Can only parse strings got: IntValue'); |
| 69 | + $type->parseLiteral($ast); |
| 70 | + } |
| 71 | + |
| 72 | + public function providerValues(): array |
| 73 | + { |
| 74 | + return [ |
| 75 | + 'normal' => ['2010-06-09', '2010-06-09T00:00:00+02:00'], |
| 76 | + 'time should be ignored' => ['2010-06-09T23:00:00', '2010-06-09T00:00:00+02:00'], |
| 77 | + 'timezone should be ignored' => ['2010-06-09T02:00:00+08:00', '2010-06-09T00:00:00+02:00'], |
| 78 | + 'unusual timezone should be ignored' => ['2020-06-24T23:30:00+04.5:0-30', '2020-06-24T00:00:00+02:00'], |
| 79 | + ]; |
39 | 80 | } |
40 | 81 | } |
0 commit comments