1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EcodevTests\Felix\Api\Scalar; |
6
|
|
|
|
7
|
|
|
use Ecodev\Felix\Api\Scalar\CHFType; |
8
|
|
|
use GraphQL\Error\Error; |
9
|
|
|
use GraphQL\Language\AST\FloatValueNode; |
10
|
|
|
use GraphQL\Language\AST\IntValueNode; |
11
|
|
|
use GraphQL\Language\AST\StringValueNode; |
12
|
|
|
use Money\Money; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use stdClass; |
15
|
|
|
|
16
|
|
|
final class CHFTypeTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
public function testSerialize(): void |
19
|
|
|
{ |
20
|
|
|
$type = new CHFType(); |
21
|
|
|
|
22
|
|
|
$money = Money::CHF('995'); |
23
|
|
|
$actual = $type->serialize($money); |
24
|
|
|
self::assertSame('9.95', $actual); |
25
|
|
|
|
26
|
|
|
$actual = $type->serialize(995); |
27
|
|
|
self::assertSame('9.95', $actual, 'should also accept raw value when aggregated from DB'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @dataProvider providerValues |
32
|
|
|
*/ |
33
|
|
|
public function testParseValue(string $input, Money $expected): void |
34
|
|
|
{ |
35
|
|
|
$type = new CHFType(); |
36
|
|
|
|
37
|
|
|
$actual = $type->parseValue($input); |
38
|
|
|
self::assertInstanceOf(Money::class, $actual); |
39
|
|
|
self::assertTrue($expected->equals($actual)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @dataProvider providerValues |
44
|
|
|
*/ |
45
|
|
|
public function testParseValueAsFloat(string $input, Money $expected): void |
46
|
|
|
{ |
47
|
|
|
$type = new CHFType(); |
48
|
|
|
|
49
|
|
|
$actual = $type->parseValue((float) $input); |
50
|
|
|
self::assertInstanceOf(Money::class, $actual); |
51
|
|
|
self::assertTrue($expected->equals($actual)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @dataProvider providerIntValues |
56
|
|
|
*/ |
57
|
|
|
public function testParseValueAsInt(int $input, Money $expected): void |
58
|
|
|
{ |
59
|
|
|
$type = new CHFType(); |
60
|
|
|
$actual = $type->parseValue($input); |
61
|
|
|
self::assertInstanceOf(Money::class, $actual); |
62
|
|
|
self::assertSame((int) $expected->getAmount(), (int) $actual->getAmount()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @dataProvider providerValues |
67
|
|
|
*/ |
68
|
|
|
public function testParseLiteral(string $input, Money $expected): void |
69
|
|
|
{ |
70
|
|
|
$type = new CHFType(); |
71
|
|
|
$ast = new StringValueNode(['value' => $input]); |
72
|
|
|
|
73
|
|
|
$actual = $type->parseLiteral($ast); |
74
|
|
|
self::assertInstanceOf(Money::class, $actual); |
75
|
|
|
self::assertTrue($expected->equals($actual)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @dataProvider providerValues |
80
|
|
|
*/ |
81
|
|
|
public function testParseLiteralAsFloat(string $input, Money $expected): void |
82
|
|
|
{ |
83
|
|
|
$type = new CHFType(); |
84
|
|
|
$ast = new FloatValueNode(['value' => $input]); |
85
|
|
|
|
86
|
|
|
$actual = $type->parseLiteral($ast); |
87
|
|
|
self::assertInstanceOf(Money::class, $actual); |
88
|
|
|
self::assertTrue($expected->equals($actual)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @dataProvider providerValues |
93
|
|
|
*/ |
94
|
|
|
public function testParseLiteralAsInt(string $input, Money $expected): void |
95
|
|
|
{ |
96
|
|
|
$type = new CHFType(); |
97
|
|
|
$ast = new IntValueNode(['value' => $input]); |
98
|
|
|
|
99
|
|
|
$actual = $type->parseLiteral($ast); |
100
|
|
|
self::assertInstanceOf(Money::class, $actual); |
101
|
|
|
self::assertTrue($expected->equals($actual)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @dataProvider providerInvalidValues |
106
|
|
|
*/ |
107
|
|
|
public function testParseValueThrowsWithInvalidValue(string $invalidValue): void |
108
|
|
|
{ |
109
|
|
|
$type = new CHFType(); |
110
|
|
|
|
111
|
|
|
$this->expectException(Error::class); |
112
|
|
|
$this->expectExceptionMessage('Query error: Not a valid Money: '); |
113
|
|
|
$type->parseValue($invalidValue); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testParseValueThrowsWithInvalidValueASD(): void |
117
|
|
|
{ |
118
|
|
|
$type = new CHFType(); |
119
|
|
|
|
120
|
|
|
$this->expectException(Error::class); |
121
|
|
|
$this->expectExceptionMessage('Cannot represent value as Money: instance of stdClass'); |
122
|
|
|
$type->parseValue(new stdClass()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @dataProvider providerInvalidValues |
127
|
|
|
*/ |
128
|
|
|
public function testParseLiteralThrowsWithInvalidValue(string $invalidValue): void |
129
|
|
|
{ |
130
|
|
|
$type = new CHFType(); |
131
|
|
|
$ast = new StringValueNode(['value' => $invalidValue]); |
132
|
|
|
|
133
|
|
|
$this->expectException(Error::class); |
134
|
|
|
$this->expectExceptionMessage('Query error: Not a valid Money: '); |
135
|
|
|
$type->parseLiteral($ast); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public static function providerValues(): array |
139
|
|
|
{ |
140
|
|
|
return [ |
141
|
|
|
['2', Money::CHF(200)], |
142
|
|
|
['2.95', Money::CHF(295)], |
143
|
|
|
['0', Money::CHF(0)], |
144
|
|
|
['9.00', Money::CHF(900)], |
145
|
|
|
['-9.00', Money::CHF(-900)], |
146
|
|
|
['-0.00', Money::CHF(0)], |
147
|
|
|
]; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public static function providerIntValues(): array |
151
|
|
|
{ |
152
|
|
|
return [ |
153
|
|
|
[2, Money::CHF(200)], |
154
|
|
|
[0, Money::CHF(0)], |
155
|
|
|
[9, Money::CHF(900)], |
156
|
|
|
[-9, Money::CHF(-900)], |
157
|
|
|
]; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public static function providerInvalidValues(): array |
161
|
|
|
{ |
162
|
|
|
return [ |
163
|
|
|
'non numeric' => ['foo'], |
164
|
|
|
'too many decimals' => ['1.123'], |
165
|
|
|
'exponential' => ['1e10'], |
166
|
|
|
'empty string' => [''], |
167
|
|
|
'only negative sign' => ['-'], |
168
|
|
|
]; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|