1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EcodevTests\Felix\Api\Scalar; |
6
|
|
|
|
7
|
|
|
use Ecodev\Felix\Api\Scalar\AbstractDecimalType; |
8
|
|
|
use GraphQL\Language\AST\FloatValueNode; |
9
|
|
|
use GraphQL\Language\AST\IntValueNode; |
10
|
|
|
use GraphQL\Language\AST\StringValueNode; |
11
|
|
|
use GraphQL\Utils\Utils; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
|
14
|
|
|
final class AbstractDecimalTypeTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
private function createType(int $decimal, ?string $minimum, ?string $maximum): AbstractDecimalType |
17
|
|
|
{ |
18
|
|
|
return new class($decimal, $minimum, $maximum) extends AbstractDecimalType { |
19
|
|
|
public $name = 'TestDecimal'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
private $decimal; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var null|string |
28
|
|
|
*/ |
29
|
|
|
private $minimum; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var null|string |
33
|
|
|
*/ |
34
|
|
|
private $maximum; |
35
|
|
|
|
36
|
|
|
public function __construct(int $decimal, ?string $minimum, ?string $maximum) |
37
|
|
|
{ |
38
|
|
|
parent::__construct([]); |
39
|
|
|
$this->decimal = $decimal; |
40
|
|
|
$this->minimum = $minimum; |
41
|
|
|
$this->maximum = $maximum; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function getScale(): int |
45
|
|
|
{ |
46
|
|
|
return $this->decimal; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function getMinimum(): ?string |
50
|
|
|
{ |
51
|
|
|
return $this->minimum; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function getMaximum(): ?string |
55
|
|
|
{ |
56
|
|
|
return $this->maximum; |
57
|
|
|
} |
58
|
|
|
}; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @dataProvider providerInputs |
63
|
|
|
* |
64
|
|
|
* @param null|float|int|string $input |
65
|
|
|
*/ |
66
|
|
|
public function testSerialize(int $decimal, ?string $minimum, ?string $maximum, $input, ?string $expected): void |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$type = $this->createType($decimal, $minimum, $maximum); |
69
|
|
|
$actual = $type->serialize($input); |
70
|
|
|
self::assertSame($input, $actual); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @dataProvider providerInputs |
75
|
|
|
* |
76
|
|
|
* @param null|float|int|string $input |
77
|
|
|
*/ |
78
|
|
|
public function testParseValue(int $decimal, ?string $minimum, ?string $maximum, $input, ?string $expected): void |
79
|
|
|
{ |
80
|
|
|
$type = $this->createType($decimal, $minimum, $maximum); |
81
|
|
|
|
82
|
|
|
if ($expected === null) { |
83
|
|
|
$this->expectExceptionMessage('Query error: Not a valid TestDecimal' . ': ' . Utils::printSafe($input)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$actual = $type->parseValue($input); |
87
|
|
|
|
88
|
|
|
self::assertSame($expected, $actual); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @dataProvider providerInputs |
93
|
|
|
* |
94
|
|
|
* @param null|float|int|string $input |
95
|
|
|
*/ |
96
|
|
|
public function testParseLiteral(int $decimal, ?string $minimum, ?string $maximum, $input, ?string $expected): void |
97
|
|
|
{ |
98
|
|
|
$type = $this->createType($decimal, $minimum, $maximum); |
99
|
|
|
|
100
|
|
|
if (is_string($input)) { |
101
|
|
|
$ast = new StringValueNode(['value' => $input]); |
102
|
|
|
} elseif (is_float($input)) { |
103
|
|
|
$ast = new FloatValueNode(['value' => $input]); |
104
|
|
|
} else { |
105
|
|
|
$ast = new IntValueNode(['value' => $input]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($expected === null) { |
109
|
|
|
$this->expectExceptionMessage('Query error: Not a valid TestDecimal'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$actual = $type->parseLiteral($ast); |
113
|
|
|
|
114
|
|
|
self::assertSame($expected, $actual); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function providerInputs(): array |
118
|
|
|
{ |
119
|
|
|
return [ |
120
|
|
|
[3, null, null, null, null], |
121
|
|
|
[3, null, null, '', null], |
122
|
|
|
[3, null, null, ' ', null], |
123
|
|
|
[3, null, null, '0', '0'], |
124
|
|
|
[3, null, null, '2', '2'], |
125
|
|
|
[3, null, null, '0.1', '0.1'], |
126
|
|
|
[3, null, null, '0.12', '0.12'], |
127
|
|
|
[3, null, null, '0.123', '0.123'], |
128
|
|
|
[3, null, null, '0.1234', null], |
129
|
|
|
[3, null, null, '-0', '-0'], |
130
|
|
|
[3, null, null, '-0.123', '-0.123'], |
131
|
|
|
[3, null, null, '-0.1234', null], |
132
|
|
|
[3, null, null, 0, '0'], |
133
|
|
|
[3, null, null, 2, '2'], |
134
|
|
|
[3, null, null, 0.1, '0.1'], |
135
|
|
|
[3, null, null, 0.12, '0.12'], |
136
|
|
|
[3, null, null, 0.123, '0.123'], |
137
|
|
|
[3, null, null, 0.1234, null], |
138
|
|
|
[3, null, null, -0, '0'], |
139
|
|
|
[3, null, null, -0.123, '-0.123'], |
140
|
|
|
[3, null, null, -0.1234, null], |
141
|
|
|
[0, null, null, '0', '0'], |
142
|
|
|
[0, null, null, '1', '1'], |
143
|
|
|
[0, null, null, '1.1', null], |
144
|
|
|
[0, null, null, '-1', '-1'], |
145
|
|
|
[0, null, null, 0, '0'], |
146
|
|
|
[0, null, null, 1, '1'], |
147
|
|
|
[0, null, null, 1.1, null], |
148
|
|
|
[0, null, null, -1, '-1'], |
149
|
|
|
[2, '0.00', '1.00', '-0.01', null], |
150
|
|
|
[2, '0.00', '1.00', '0.00', '0.00'], |
151
|
|
|
[2, '0.00', '1.00', '1.00', '1.00'], |
152
|
|
|
[2, '0.00', '1.00', '1.01', null], |
153
|
|
|
[2, '0.00', '1.00', '0.000', null], |
154
|
|
|
]; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.