1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace hiqdev\php\billing\tests\unit\price; |
6
|
|
|
|
7
|
|
|
use hiqdev\php\billing\price\ProgressivePriceThreshold; |
8
|
|
|
use hiqdev\php\billing\price\ProgressivePriceThresholdList; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ProgressivePriceThresholdsTest |
14
|
|
|
* |
15
|
|
|
* @author Dmytro Naumenko <[email protected]> |
16
|
|
|
* @covers \hiqdev\php\billing\price\ProgressivePriceThresholdList |
17
|
|
|
*/ |
18
|
|
|
class ProgressivePriceThresholdsTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
public function testCreateFromScalarsArray(): void |
21
|
|
|
{ |
22
|
|
|
$thresholds = ProgressivePriceThresholdList::fromScalarsArray([ |
23
|
|
|
[ |
24
|
|
|
'price' => '10.22', |
25
|
|
|
'currency' => 'USD', |
26
|
|
|
'quantity' => '2', |
27
|
|
|
'unit' => 'items', |
28
|
|
|
], |
29
|
|
|
[ |
30
|
|
|
'price' => '9.0994', |
31
|
|
|
'currency' => 'USD', |
32
|
|
|
'quantity' => '10', |
33
|
|
|
'unit' => 'items', |
34
|
|
|
], |
35
|
|
|
]); |
36
|
|
|
|
37
|
|
|
$this->assertCount(2, $thresholds->get()); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testWithAdded(): void |
41
|
|
|
{ |
42
|
|
|
$thresholds = new ProgressivePriceThresholdList([ |
43
|
|
|
ProgressivePriceThreshold::createFromScalar( |
44
|
|
|
'10.22', |
45
|
|
|
'USD', |
46
|
|
|
'2', |
47
|
|
|
'items' |
48
|
|
|
), |
49
|
|
|
ProgressivePriceThreshold::createFromScalar( |
50
|
|
|
'9.0994', |
51
|
|
|
'USD', |
52
|
|
|
'10', |
53
|
|
|
'items' |
54
|
|
|
), |
55
|
|
|
]); |
56
|
|
|
|
57
|
|
|
$newThreshold = ProgressivePriceThreshold::createFromScalar( |
58
|
|
|
'8.0994', |
59
|
|
|
'USD', |
60
|
|
|
'20', |
61
|
|
|
'items' |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$newThresholds = $thresholds->withAdded($newThreshold); |
65
|
|
|
|
66
|
|
|
$this->assertCount(3, $newThresholds->get()); |
67
|
|
|
$this->assertNotSame($thresholds, $newThresholds); |
|
|
|
|
68
|
|
|
$this->assertNotContains($newThreshold, $thresholds->get()); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testDifferentCurrency(): void |
72
|
|
|
{ |
73
|
|
|
$this->expectException(InvalidArgumentException::class); |
74
|
|
|
$this->expectExceptionMessage('Progressive price thresholds must have the same currency'); |
75
|
|
|
new ProgressivePriceThresholdList([ |
76
|
|
|
ProgressivePriceThreshold::createFromScalar( |
77
|
|
|
'10.22', |
78
|
|
|
'USD', |
79
|
|
|
'2', |
80
|
|
|
'items' |
81
|
|
|
), |
82
|
|
|
ProgressivePriceThreshold::createFromScalar( |
83
|
|
|
'9.0994', |
84
|
|
|
'EUR', |
85
|
|
|
'10', |
86
|
|
|
'items' |
87
|
|
|
), |
88
|
|
|
]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testUnitsConvertabilityIsChecked(): void |
92
|
|
|
{ |
93
|
|
|
$this->expectException(InvalidArgumentException::class); |
94
|
|
|
$this->expectExceptionMessage('Progressive price thresholds must be of the same unit family'); |
95
|
|
|
new ProgressivePriceThresholdList([ |
96
|
|
|
ProgressivePriceThreshold::createFromScalar( |
97
|
|
|
'10.22', |
98
|
|
|
'USD', |
99
|
|
|
'2', |
100
|
|
|
'items' |
101
|
|
|
), |
102
|
|
|
ProgressivePriceThreshold::createFromScalar( |
103
|
|
|
'9.0994', |
104
|
|
|
'USD', |
105
|
|
|
'10', |
106
|
|
|
'kg' |
107
|
|
|
), |
108
|
|
|
]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testToArray(): void |
112
|
|
|
{ |
113
|
|
|
$thresholds = new ProgressivePriceThresholdList([ |
114
|
|
|
ProgressivePriceThreshold::createFromScalar( |
115
|
|
|
'10.22', |
116
|
|
|
'USD', |
117
|
|
|
'2', |
118
|
|
|
'items' |
119
|
|
|
), |
120
|
|
|
ProgressivePriceThreshold::createFromScalar( |
121
|
|
|
'9.0994', |
122
|
|
|
'USD', |
123
|
|
|
'10', |
124
|
|
|
'items' |
125
|
|
|
), |
126
|
|
|
]); |
127
|
|
|
|
128
|
|
|
$this->assertSame([ |
|
|
|
|
129
|
|
|
[ |
130
|
|
|
'price' => '10.22', |
131
|
|
|
'currency' => 'USD', |
132
|
|
|
'quantity' => '2', |
133
|
|
|
'unit' => 'items', |
134
|
|
|
], |
135
|
|
|
[ |
136
|
|
|
'price' => '9.0994', |
137
|
|
|
'currency' => 'USD', |
138
|
|
|
'quantity' => '10', |
139
|
|
|
'unit' => 'items', |
140
|
|
|
], |
141
|
|
|
], $thresholds->__toArray()); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function testJsonSerializable(): void |
145
|
|
|
{ |
146
|
|
|
$thresholds = new ProgressivePriceThresholdList([ |
147
|
|
|
ProgressivePriceThreshold::createFromScalar( |
148
|
|
|
'10.22', |
149
|
|
|
'USD', |
150
|
|
|
'2', |
151
|
|
|
'items' |
152
|
|
|
), |
153
|
|
|
ProgressivePriceThreshold::createFromScalar( |
154
|
|
|
'9.0994', |
155
|
|
|
'USD', |
156
|
|
|
'10', |
157
|
|
|
'items' |
158
|
|
|
), |
159
|
|
|
]); |
160
|
|
|
|
161
|
|
|
$this->assertSame([ |
162
|
|
|
[ |
163
|
|
|
'price' => '10.22', |
164
|
|
|
'currency' => 'USD', |
165
|
|
|
'quantity' => '2', |
166
|
|
|
'unit' => 'items', |
167
|
|
|
], |
168
|
|
|
[ |
169
|
|
|
'price' => '9.0994', |
170
|
|
|
'currency' => 'USD', |
171
|
|
|
'quantity' => '10', |
172
|
|
|
'unit' => 'items', |
173
|
|
|
], |
174
|
|
|
], json_decode(json_encode($thresholds), true)); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.