1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ApplicationTest\Api\Scalar; |
6
|
|
|
|
7
|
|
|
use Application\Api\Scalar\MoneyType; |
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
|
|
|
|
15
|
|
|
class MoneyTypeTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function testSerialize(): void |
18
|
|
|
{ |
19
|
|
|
$type = new MoneyType(); |
20
|
|
|
|
21
|
|
|
$money = Money::CHF('995'); |
22
|
|
|
$actual = $type->serialize($money); |
23
|
|
|
self::assertSame('9.95', $actual); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @dataProvider providerValues |
28
|
|
|
*/ |
29
|
|
|
public function testParseValue(string $input, Money $expected): void |
30
|
|
|
{ |
31
|
|
|
$type = new MoneyType(); |
32
|
|
|
|
33
|
|
|
$actual = $type->parseValue($input); |
34
|
|
|
self::assertInstanceOf(Money::class, $actual); |
35
|
|
|
self::assertTrue($expected->equals($actual)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @dataProvider providerValues |
40
|
|
|
*/ |
41
|
|
|
public function testParseValueAsFloat(string $input, Money $expected): void |
42
|
|
|
{ |
43
|
|
|
$type = new MoneyType(); |
44
|
|
|
|
45
|
|
|
$actual = $type->parseValue((float) $input); |
46
|
|
|
self::assertInstanceOf(Money::class, $actual); |
47
|
|
|
self::assertTrue($expected->equals($actual)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @dataProvider providerIntValues |
52
|
|
|
*/ |
53
|
|
|
public function testParseValueAsInt(int $input, Money $expected): void |
54
|
|
|
{ |
55
|
|
|
$type = new MoneyType(); |
56
|
|
|
$actual = $type->parseValue($input); |
57
|
|
|
self::assertInstanceOf(Money::class, $actual); |
58
|
|
|
self::assertSame((int) $expected->getAmount(), (int) $actual->getAmount()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @dataProvider providerValues |
63
|
|
|
*/ |
64
|
|
|
public function testParseLiteral(string $input, Money $expected): void |
65
|
|
|
{ |
66
|
|
|
$type = new MoneyType(); |
67
|
|
|
$ast = new StringValueNode(['value' => $input]); |
68
|
|
|
|
69
|
|
|
$actual = $type->parseLiteral($ast); |
70
|
|
|
self::assertInstanceOf(Money::class, $actual); |
71
|
|
|
self::assertTrue($expected->equals($actual)); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @dataProvider providerValues |
76
|
|
|
*/ |
77
|
|
|
public function testParseLiteralAsFloat(string $input, Money $expected): void |
78
|
|
|
{ |
79
|
|
|
$type = new MoneyType(); |
80
|
|
|
$ast = new FloatValueNode(['value' => $input]); |
81
|
|
|
|
82
|
|
|
$actual = $type->parseLiteral($ast); |
83
|
|
|
self::assertInstanceOf(Money::class, $actual); |
84
|
|
|
self::assertTrue($expected->equals($actual)); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @dataProvider providerIntValues |
89
|
|
|
*/ |
90
|
|
|
public function testParseLiteralAsInt(int $input, Money $expected): void |
91
|
|
|
{ |
92
|
|
|
$type = new MoneyType(); |
93
|
|
|
$ast = new IntValueNode(['value' => $input]); |
94
|
|
|
|
95
|
|
|
$actual = $type->parseLiteral($ast); |
96
|
|
|
self::assertInstanceOf(Money::class, $actual); |
97
|
|
|
self::assertTrue($expected->equals($actual)); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @dataProvider providerInvalidValues |
102
|
|
|
*/ |
103
|
|
|
public function testParseValueThrowsWithInvalidValue(string $invalidValue): void |
104
|
|
|
{ |
105
|
|
|
$type = new MoneyType(); |
106
|
|
|
|
107
|
|
|
$this->expectException(Error::class); |
108
|
|
|
$type->parseValue($invalidValue); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @dataProvider providerInvalidValues |
113
|
|
|
*/ |
114
|
|
|
public function testParseLiteralThrowsWithInvalidValue(string $invalidValue): void |
115
|
|
|
{ |
116
|
|
|
$type = new MoneyType(); |
117
|
|
|
$ast = new StringValueNode(['value' => $invalidValue]); |
118
|
|
|
|
119
|
|
|
$this->expectException(Error::class); |
120
|
|
|
$type->parseLiteral($ast); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function providerValues(): array |
124
|
|
|
{ |
125
|
|
|
return [ |
126
|
|
|
['2', Money::CHF(200)], |
127
|
|
|
['2.95', Money::CHF(295)], |
128
|
|
|
['0', Money::CHF(0)], |
129
|
|
|
['9.00', Money::CHF(900)], |
130
|
|
|
]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function providerIntValues(): array |
134
|
|
|
{ |
135
|
|
|
return [ |
136
|
|
|
[2, Money::CHF(200)], |
137
|
|
|
[0, Money::CHF(0)], |
138
|
|
|
[9, Money::CHF(900)], |
139
|
|
|
]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function providerInvalidValues(): array |
143
|
|
|
{ |
144
|
|
|
return [ |
145
|
|
|
'non numeric' => ['foo'], |
146
|
|
|
'too many decimals' => ['1.123'], |
147
|
|
|
'exponential' => ['1e10'], |
148
|
|
|
'empty string' => [''], |
149
|
|
|
]; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|