Total Complexity | 12 |
Total Lines | 134 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
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)); |
||
1 ignored issue
–
show
|
|||
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)); |
||
1 ignored issue
–
show
|
|||
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)); |
||
1 ignored issue
–
show
|
|||
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 |
||
139 | ]; |
||
140 | } |
||
141 | |||
142 | public function providerInvalidValues(): array |
||
143 | { |
||
144 | return [ |
||
145 | 'non numeric' => ['foo'], |
||
146 | 'too many decimals' => ['1.123'], |
||
147 | 'exponential' => ['1e10'], |
||
149 | ]; |
||
150 | } |
||
151 | } |
||
152 |