forked from moneyphp/money
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoneySpec.php
339 lines (261 loc) · 12.5 KB
/
MoneySpec.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
namespace spec\Money;
use Money\Calculator;
use Money\Currency;
use Money\Money;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
final class MoneySpec extends ObjectBehavior
{
const AMOUNT = 10;
const OTHER_AMOUNT = 5;
const CURRENCY = 'EUR';
const OTHER_CURRENCY = 'USD';
function let(Calculator $calculator)
{
// Override the calculator for testing
$reflection = new \ReflectionProperty(Money::class, 'calculator');
$reflection->setAccessible(true);
$reflection->setValue(null, $calculator->getWrappedObject());
$this->beConstructedWith(self::AMOUNT, new Currency(self::CURRENCY));
}
function it_is_initializable()
{
$this->shouldHaveType(Money::class);
}
function it_is_json_serializable()
{
$this->shouldImplement(\JsonSerializable::class);
}
function it_has_an_amount()
{
$this->getAmount()->shouldBeLike(self::AMOUNT);
}
function it_has_a_currency()
{
$currency = $this->getCurrency();
$currency->shouldHaveType(Currency::class);
$currency->equals(new Currency(self::CURRENCY))->shouldReturn(true);
}
function it_throws_an_exception_when_amount_is_not_numeric()
{
$this->beConstructedWith('ONE', new Currency(self::CURRENCY));
$this->shouldThrow(\InvalidArgumentException::class)->duringInstantiation();
}
function it_constructs_integer()
{
$this->beConstructedWith(5, new Currency(self::CURRENCY));
}
function it_constructs_string()
{
$this->beConstructedWith('5', new Currency(self::CURRENCY));
}
function it_constructs_integer_with_decimals_of_zero()
{
$this->beConstructedWith('5.00', new Currency(self::CURRENCY));
}
function it_tests_currency_equality()
{
$this->isSameCurrency(new Money(self::AMOUNT, new Currency(self::CURRENCY)))->shouldReturn(true);
$this->isSameCurrency(new Money(self::AMOUNT, new Currency(self::OTHER_CURRENCY)))->shouldReturn(false);
}
function it_equals_to_another_money()
{
$this->equals(new Money(self::AMOUNT, new Currency(self::CURRENCY)))->shouldReturn(true);
}
function it_compares_two_amounts(Calculator $calculator)
{
$calculator->compare((string) self::AMOUNT, (string) self::AMOUNT)->willReturn(0);
$money = new Money(self::AMOUNT, new Currency(self::CURRENCY));
$this->compare($money)->shouldReturn(0);
$this->greaterThan($money)->shouldReturn(false);
$this->greaterThanOrEqual($money)->shouldReturn(true);
$this->lessThan($money)->shouldReturn(false);
$this->lessThanOrEqual($money)->shouldReturn(true);
}
function it_throws_an_exception_when_currency_is_different_during_comparison(Calculator $calculator)
{
$calculator->compare(Argument::type('string'), Argument::type('string'))->shouldNotBeCalled();
$money = new Money(self::AMOUNT + 1, new Currency(self::OTHER_CURRENCY));
$this->shouldThrow(\InvalidArgumentException::class)->duringCompare($money);
$this->shouldThrow(\InvalidArgumentException::class)->duringGreaterThan($money);
$this->shouldThrow(\InvalidArgumentException::class)->duringGreaterThanOrEqual($money);
$this->shouldThrow(\InvalidArgumentException::class)->duringLessThan($money);
$this->shouldThrow(\InvalidArgumentException::class)->duringLessThanOrEqual($money);
}
function it_adds_an_other_money(Calculator $calculator)
{
$result = self::AMOUNT + self::OTHER_AMOUNT;
$calculator->add((string) self::AMOUNT, (string) self::OTHER_AMOUNT)->willReturn((string) $result);
$money = $this->add(new Money(self::OTHER_AMOUNT, new Currency(self::CURRENCY)));
$money->shouldHaveType(Money::class);
$money->getAmount()->shouldBe((string) $result);
}
function it_throws_an_exception_when_currency_is_different_during_addition(Calculator $calculator)
{
$calculator->add((string) self::AMOUNT, (string) self::AMOUNT)->shouldNotBeCalled();
$this->shouldThrow(\InvalidArgumentException::class)->duringAdd(new Money(self::AMOUNT, new Currency(self::OTHER_CURRENCY)));
}
function it_subtracts_an_other_money(Calculator $calculator)
{
$result = self::AMOUNT - self::OTHER_AMOUNT;
$calculator->subtract((string) self::AMOUNT, (string) self::OTHER_AMOUNT)->willReturn((string) $result);
$money = $this->subtract(new Money(self::OTHER_AMOUNT, new Currency(self::CURRENCY)));
$money->shouldHaveType(Money::class);
$money->getAmount()->shouldBe((string) $result);
}
function it_throws_an_exception_if_currency_is_different_during_subtractition(Calculator $calculator)
{
$calculator->subtract((string) self::AMOUNT, (string) self::AMOUNT)->shouldNotBeCalled();
$this->shouldThrow(\InvalidArgumentException::class)->duringSubtract(new Money(self::AMOUNT, new Currency(self::OTHER_CURRENCY)));
}
function it_multiplies_the_amount(Calculator $calculator)
{
$this->beConstructedWith(1, new Currency(self::CURRENCY));
$calculator->multiply('1', 5)->willReturn(5);
$calculator->round(5, Money::ROUND_HALF_UP)->willReturn(5);
$money = $this->multiply(5);
$money->shouldHaveType(Money::class);
$money->getAmount()->shouldBe('5');
}
public function it_throws_an_exception_when_operand_is_invalid_during_multiplication(Calculator $calculator)
{
$calculator->multiply(Argument::type('string'), Argument::type('numeric'))->shouldNotBeCalled();
$calculator->round(Argument::type('string'), Argument::type('integer'))->shouldNotBeCalled();
$this->shouldThrow(\InvalidArgumentException::class)->duringMultiply('INVALID_OPERAND');
}
public function it_throws_an_exception_when_rounding_mode_is_invalid_during_multiplication(Calculator $calculator)
{
$calculator->multiply(Argument::type('string'), Argument::type('numeric'))->shouldNotBeCalled();
$calculator->round(Argument::type('string'), Argument::type('integer'))->shouldNotBeCalled();
$this->shouldThrow(\InvalidArgumentException::class)->duringMultiply(1.0, 'INVALID_ROUNDING_MODE');
}
function it_divides_the_amount(Calculator $calculator)
{
$this->beConstructedWith(4, new Currency(self::CURRENCY));
$calculator->compare((string) (1 / 2), '0')->willReturn(1 / 2 > 1);
$calculator->divide('4', 1 / 2)->willReturn(2);
$calculator->round(2, Money::ROUND_HALF_UP)->willReturn(2);
$money = $this->divide(1 / 2, Money::ROUND_HALF_UP);
$money->shouldHaveType(Money::class);
$money->getAmount()->shouldBeLike(2);
}
public function it_throws_an_exception_when_operand_is_invalid_during_division(Calculator $calculator)
{
$calculator->compare(Argument::type('string'), Argument::type('string'))->shouldNotBeCalled();
$calculator->divide(Argument::type('string'), Argument::type('numeric'))->shouldNotBeCalled();
$calculator->round(Argument::type('string'), Argument::type('integer'))->shouldNotBeCalled();
$this->shouldThrow(\InvalidArgumentException::class)->duringDivide('INVALID_OPERAND');
}
public function it_throws_an_exception_when_rounding_mode_is_invalid_during_division(Calculator $calculator)
{
$calculator->compare('1.0', '0')->shouldNotBeCalled();
$calculator->divide(Argument::type('string'), Argument::type('numeric'))->shouldNotBeCalled();
$calculator->round(Argument::type('string'), Argument::type('integer'))->shouldNotBeCalled();
$this->shouldThrow(\InvalidArgumentException::class)->duringDivide(1.0, 'INVALID_ROUNDING_MODE');
}
function it_throws_an_exception_when_divisor_is_zero(Calculator $calculator)
{
$calculator->compare(0, '0')->willThrow(\InvalidArgumentException::class);
$calculator->divide(Argument::type('string'), Argument::type('numeric'))->shouldNotBeCalled();
$calculator->round(Argument::type('string'), Argument::type('integer'))->shouldNotBeCalled();
$this->shouldThrow(\InvalidArgumentException::class)->duringDivide(0);
}
function it_allocates_amount(Calculator $calculator)
{
$this->beConstructedWith(100, new Currency(self::CURRENCY));
$calculator->share(Argument::type('numeric'), Argument::type('int'), Argument::type('int'))->will(function($args) {
return (int) floor($args[0] * $args[1] / $args[2]);
});
$calculator->subtract(Argument::type('numeric'), Argument::type('int'))->will(function($args) {
return (string) $args[0] - $args[1];
});
$calculator->add(Argument::type('numeric'), Argument::type('int'))->will(function($args) {
return (string) ($args[0] + $args[1]);
});
$calculator->compare(Argument::type('numeric'), Argument::type('int'))->will(function($args) {
return ($args[0] < $args[1]) ? -1 : (($args[0] > $args[1]) ? 1 : 0);
});
$calculator->absolute(Argument::type('numeric'))->will(function($args) {
return ltrim($args[0], '-');
});
$calculator->multiply(Argument::type('numeric'), Argument::type('int'))->will(function($args) {
return (string) $args[0] * $args[1];
});
$allocated = $this->allocate([1, 1, 1]);
$allocated->shouldBeArray();
$allocated->shouldEqualAllocation([34, 33, 33]);
}
function it_allocates_amount_to_n_targets(Calculator $calculator)
{
$this->beConstructedWith(15, new Currency(self::CURRENCY));
$calculator->share(Argument::type('numeric'), Argument::type('int'), Argument::type('int'))->will(function($args) {
return (int) floor($args[0] * $args[1] / $args[2]);
});
$calculator->subtract(Argument::type('numeric'), Argument::type('int'))->will(function($args) {
return $args[0] - $args[1];
});
$calculator->add(Argument::type('numeric'), Argument::type('int'))->will(function($args) {
return $args[0] + $args[1];
});
$calculator->compare(Argument::type('numeric'), Argument::type('int'))->will(function($args) {
return ($args[0] < $args[1]) ? -1 : (($args[0] > $args[1]) ? 1 : 0);
});
$allocated = $this->allocateTo(2);
$allocated->shouldBeArray();
$allocated->shouldEqualAllocation([8, 7]);
}
function it_throws_an_exception_when_allocation_target_is_not_integer()
{
$this->shouldThrow(\InvalidArgumentException::class)->duringAllocateTo('two');
}
function it_throws_an_exception_when_allocation_target_is_empty()
{
$this->shouldThrow(\InvalidArgumentException::class)->duringAllocate([]);
}
function it_throws_an_exception_when_allocation_ratio_is_negative()
{
$this->shouldThrow(\InvalidArgumentException::class)->duringAllocate([-1]);
}
function it_throws_an_exception_when_allocation_total_is_zero()
{
$this->shouldThrow(\InvalidArgumentException::class)->duringAllocate([0, 0]);
}
function it_throws_an_exception_when_allocate_to_target_is_less_than_or_equals_zero()
{
$this->shouldThrow(\InvalidArgumentException::class)->duringAllocateTo(-1);
}
function it_has_comparators(Calculator $calculator)
{
$this->beConstructedWith(1, new Currency(self::CURRENCY));
$calculator->compare(Argument::type('numeric'), Argument::type('int'))->will(function($args) {
return ($args[0] < $args[1]) ? -1 : (($args[0] > $args[1]) ? 1 : 0);
});
$this->isZero()->shouldReturn(false);
$this->isPositive()->shouldReturn(true);
$this->isNegative()->shouldReturn(false);
}
function it_calculates_the_absolute_amount(Calculator $calculator)
{
$this->beConstructedWith(-1, new Currency(self::CURRENCY));
$calculator->absolute(-1)->willReturn(1);
$money = $this->absolute();
$money->shouldHaveType(Money::class);
$money->getAmount()->shouldBeLike(1);
}
public function getMatchers()
{
return [
'equalAllocation' => function ($subject, $value) {
/** @var Money $money */
foreach ($subject as $key => $money) {
$compareTo = new Money($value[$key], $money->getCurrency());
if ($money->equals($compareTo) === false) {
return false;
}
}
return true;
},
];
}
}