1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EcodevTests\Felix\Api\Scalar; |
6
|
|
|
|
7
|
|
|
use Cake\Chronos\Chronos; |
8
|
|
|
use Ecodev\Felix\Api\Scalar\ChronosType; |
9
|
|
|
use GraphQL\Error\Error; |
10
|
|
|
use GraphQL\Language\AST\IntValueNode; |
11
|
|
|
use GraphQL\Language\AST\StringValueNode; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
|
14
|
|
|
final class ChronosTypeTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
private string $timezone; |
17
|
|
|
|
18
|
|
|
protected function setUp(): void |
19
|
|
|
{ |
20
|
|
|
$this->timezone = date_default_timezone_get(); |
21
|
|
|
date_default_timezone_set('Europe/Zurich'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
protected function tearDown(): void |
25
|
|
|
{ |
26
|
|
|
date_default_timezone_set($this->timezone); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testSerialize(): void |
30
|
|
|
{ |
31
|
|
|
$type = new ChronosType(); |
32
|
|
|
$date = new Chronos('2018-09-15T00:00:00+02:00'); |
33
|
|
|
$actual = $type->serialize($date); |
34
|
|
|
self::assertSame('2018-09-15T00:00:00+02:00', $actual); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @dataProvider providerValues |
39
|
|
|
*/ |
40
|
|
|
public function testParseValue(string $input, ?string $expected): void |
41
|
|
|
{ |
42
|
|
|
$type = new ChronosType(); |
43
|
|
|
$actual = $type->parseValue($input); |
44
|
|
|
if ($actual) { |
45
|
|
|
$actual = $actual->format('c'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
self::assertSame($expected, $actual); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @dataProvider providerValues |
53
|
|
|
*/ |
54
|
|
|
public function testParseLiteral(string $input, ?string $expected): void |
55
|
|
|
{ |
56
|
|
|
$type = new ChronosType(); |
57
|
|
|
$ast = new StringValueNode(['value' => $input]); |
58
|
|
|
|
59
|
|
|
$actual = $type->parseLiteral($ast); |
60
|
|
|
if ($actual) { |
61
|
|
|
$actual = $actual->format('c'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
self::assertSame($expected, $actual); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testParseLiteralAsInt(): void |
68
|
|
|
{ |
69
|
|
|
$type = new ChronosType(); |
70
|
|
|
$ast = new IntValueNode(['value' => '123']); |
71
|
|
|
|
72
|
|
|
$this->expectException(Error::class); |
73
|
|
|
$this->expectExceptionMessage('Query error: Can only parse strings got: IntValue'); |
74
|
|
|
$type->parseLiteral($ast); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testParseValueAsInt(): void |
78
|
|
|
{ |
79
|
|
|
$type = new ChronosType(); |
80
|
|
|
|
81
|
|
|
$this->expectException(Error::class); |
82
|
|
|
$this->expectExceptionMessage('Cannot represent value as Chronos date: 123'); |
83
|
|
|
$type->parseValue(123); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public static function providerValues(): array |
87
|
|
|
{ |
88
|
|
|
return [ |
89
|
|
|
'UTC' => ['2018-09-14T22:00:00.000Z', '2018-09-15T00:00:00+02:00'], |
90
|
|
|
'local time' => ['2018-09-15T00:00:00+02:00', '2018-09-15T00:00:00+02:00'], |
91
|
|
|
'other time' => ['2018-09-15T02:00:00+04:00', '2018-09-15T00:00:00+02:00'], |
92
|
|
|
'empty string' => ['', null], |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|