|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace EcodevTests\Felix\Api\Scalar; |
|
6
|
|
|
|
|
7
|
|
|
use Cake\Chronos\ChronosTime; |
|
8
|
|
|
use Ecodev\Felix\Api\Scalar\TimeType; |
|
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 TimeTypeTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
public function testSerialize(): void |
|
17
|
|
|
{ |
|
18
|
|
|
$type = new TimeType(); |
|
19
|
|
|
$time = new ChronosTime('14:30:25'); |
|
20
|
|
|
$actual = $type->serialize($time); |
|
21
|
|
|
self::assertSame('14:30', $actual); |
|
22
|
|
|
|
|
23
|
|
|
// Test serialize with microseconds |
|
24
|
|
|
$time = new ChronosTime('23:59:59.1254'); |
|
25
|
|
|
$actual = $type->serialize($time); |
|
26
|
|
|
self::assertSame('23:59', $actual); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @dataProvider providerValues |
|
31
|
|
|
*/ |
|
32
|
|
|
public function testParseValue(string $input, ?string $expected): void |
|
33
|
|
|
{ |
|
34
|
|
|
$type = new TimeType(); |
|
35
|
|
|
$actual = $type->parseValue($input); |
|
36
|
|
|
if ($actual) { |
|
37
|
|
|
$actual = $actual->__toString(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
self::assertSame($expected, $actual); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @dataProvider providerValues |
|
45
|
|
|
*/ |
|
46
|
|
|
public function testParseLiteral(string $input, ?string $expected): void |
|
47
|
|
|
{ |
|
48
|
|
|
$type = new TimeType(); |
|
49
|
|
|
$ast = new StringValueNode(['value' => $input]); |
|
50
|
|
|
|
|
51
|
|
|
$actual = $type->parseLiteral($ast); |
|
52
|
|
|
if ($actual) { |
|
53
|
|
|
$actual = $actual->__toString(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
self::assertSame($expected, $actual); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testParseLiteralAsInt(): void |
|
60
|
|
|
{ |
|
61
|
|
|
$type = new TimeType(); |
|
62
|
|
|
$ast = new IntValueNode(['value' => '123']); |
|
63
|
|
|
|
|
64
|
|
|
$this->expectException(Error::class); |
|
65
|
|
|
$this->expectExceptionMessage('Query error: Can only parse strings got: IntValue'); |
|
66
|
|
|
$type->parseLiteral($ast); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testParseValueAsInt(): void |
|
70
|
|
|
{ |
|
71
|
|
|
$type = new TimeType(); |
|
72
|
|
|
|
|
73
|
|
|
$this->expectException(Error::class); |
|
74
|
|
|
$this->expectExceptionMessage('Cannot represent value as ChronosTime: 123'); |
|
75
|
|
|
$type->parseValue(123); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testParseValueInvalidFormat(): void |
|
79
|
|
|
{ |
|
80
|
|
|
$type = new TimeType(); |
|
81
|
|
|
|
|
82
|
|
|
$this->expectException(Error::class); |
|
83
|
|
|
$this->expectExceptionMessage('Invalid format for ChronosTime. Expected "14h35", "14:35" or "14h", but got: "123"'); |
|
84
|
|
|
$type->parseValue('123'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public static function providerValues(): array |
|
88
|
|
|
{ |
|
89
|
|
|
return [ |
|
90
|
|
|
'empty string' => ['', null], |
|
91
|
|
|
'normal time' => ['14:30', '14:30:00'], |
|
92
|
|
|
'alternative separator' => ['14h30', '14:30:00'], |
|
93
|
|
|
'only hour' => ['14h', '14:00:00'], |
|
94
|
|
|
'only hour alternative' => ['14:', '14:00:00'], |
|
95
|
|
|
'even shorter' => ['9', '09:00:00'], |
|
96
|
|
|
'spaces are fines' => [' 14h00 ', '14:00:00'], |
|
97
|
|
|
'a bit weird, but why not' => [' 14h6 ', '14:06:00'], |
|
98
|
|
|
]; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|