Skip to content

Commit 38c21fa

Browse files
AndreyDemenenkoPostScripton
authored andcommitted
feat: add compare method for return int
1 parent b156a5c commit 38c21fa

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/Money.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,15 @@ public function equals(Money $money, bool $strict = true): bool
219219
return app(Calculator::class)->compare($this->amount, $money->amount) === 0;
220220
}
221221

222+
public function compare(Money $money, bool $strict = true): int
223+
{
224+
if ($strict && $this->isDifferentCurrency($money)) {
225+
return false;
226+
}
227+
228+
return app(Calculator::class)->compare($this->amount, $money->amount);
229+
}
230+
222231
public function difference(Money $money): Money
223232
{
224233
if ($this->isDifferentCurrency($money)) {

tests/Unit/MoneyTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,15 @@ public function testCeil(): void
193193
$this->assertEquals('$ 11', $money->toString());
194194
}
195195

196+
/** @dataProvider compareDataProvider */
197+
public function testCompare(string $value1, string $value2, int $result): void
198+
{
199+
$money1 = money_parse($value1);
200+
$money2 = money_parse($value2);
201+
202+
$this->assertEquals($result, $money2->compare($money1));
203+
}
204+
196205
public function testNegativeCeil(): void
197206
{
198207
$money = money('-102500');
@@ -401,6 +410,27 @@ protected function absoluteDataProvider(): array
401410
];
402411
}
403412

413+
protected function compareDataProvider(): array
414+
{
415+
return [
416+
[
417+
'value1' => '0',
418+
'value2' => '10',
419+
'result' => 1,
420+
],
421+
[
422+
'value1' => '10',
423+
'value2' => '0',
424+
'result' => -1,
425+
],
426+
[
427+
'value1' => '0',
428+
'value2' => '0',
429+
'result' => 0,
430+
],
431+
];
432+
}
433+
404434
protected function creatingMoneyWithExceptionDataProvider(): array
405435
{
406436
return [

0 commit comments

Comments
 (0)