|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the daikon-cqrs/money-interop project. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Daikon\Tests\Money\ValueObject; |
|
10
|
|
|
|
|
11
|
|
|
use Daikon\Interop\InvalidArgumentException; |
|
12
|
|
|
use Daikon\Money\ValueObject\Money; |
|
13
|
|
|
use Daikon\Money\ValueObject\Wallet; |
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
|
|
16
|
|
|
final class WalletTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
1 |
|
public function testMakeEmpty(): void |
|
19
|
|
|
{ |
|
20
|
1 |
|
$wallet = Wallet::makeEmpty(); |
|
21
|
1 |
|
$this->assertCount(0, $wallet->unwrap()); |
|
22
|
1 |
|
} |
|
23
|
|
|
|
|
24
|
1 |
|
public function testIsEmpty(): void |
|
25
|
|
|
{ |
|
26
|
1 |
|
$wallet = Wallet::makeEmpty(); |
|
27
|
1 |
|
$this->assertTrue($wallet->isEmpty()); |
|
28
|
|
|
|
|
29
|
1 |
|
$wallet = Wallet::makeEmpty()->credit(Money::fromNative('0SAT')); |
|
30
|
1 |
|
$this->assertTrue($wallet->isEmpty()); |
|
31
|
|
|
|
|
32
|
1 |
|
$wallet = Wallet::makeEmpty() |
|
33
|
1 |
|
->credit(Money::fromNative('0USD')) |
|
34
|
1 |
|
->credit(Money::fromNative('1MSAT')); |
|
35
|
1 |
|
$this->assertFalse($wallet->isEmpty()); |
|
36
|
1 |
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
public function testCredit(): void |
|
39
|
|
|
{ |
|
40
|
1 |
|
$wallet = Wallet::makeEmpty(); |
|
41
|
1 |
|
$creditedWallet = $wallet->credit(Money::fromNative('100SAT')); |
|
42
|
1 |
|
$this->assertEquals(['SAT' => '100SAT'], $creditedWallet->unwrap()); |
|
43
|
|
|
|
|
44
|
1 |
|
$wallet = Wallet::makeEmpty(); |
|
45
|
1 |
|
$creditedWallet = $wallet->credit(Money::fromNative('-100SAT')); |
|
46
|
1 |
|
$this->assertEquals(['SAT' => '-100SAT'], $creditedWallet->unwrap()); |
|
47
|
1 |
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
public function testDebit(): void |
|
50
|
|
|
{ |
|
51
|
1 |
|
$wallet = Wallet::makeEmpty(); |
|
52
|
1 |
|
$debitedWallet = $wallet->debit(Money::fromNative('100SAT')); |
|
53
|
1 |
|
$this->assertEquals(['SAT' => '-100SAT'], $debitedWallet->unwrap()); |
|
54
|
|
|
|
|
55
|
1 |
|
$wallet = Wallet::makeEmpty(); |
|
56
|
1 |
|
$debitedWallet = $wallet->debit(Money::fromNative('-100SAT')); |
|
57
|
1 |
|
$this->assertEquals(['SAT' => '100SAT'], $debitedWallet->unwrap()); |
|
58
|
1 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testGetBalance(): void |
|
61
|
|
|
{ |
|
62
|
|
|
$wallet = Wallet::makeEmpty(); |
|
63
|
|
|
$balance = $wallet->getBalance('X1'); |
|
64
|
|
|
$this->assertInstanceOf(Money::class, $balance); |
|
65
|
|
|
$this->assertEquals(0, $balance->getAmount()); |
|
66
|
|
|
$this->assertEquals('X1', $balance->getCurrency()); |
|
67
|
|
|
|
|
68
|
|
|
$wallet = Wallet::fromNative(['SAT' => '-100SAT']); |
|
69
|
|
|
$balance = $wallet->getBalance('SAT'); |
|
70
|
|
|
$this->assertEquals('-100SAT', (string)$balance); |
|
71
|
|
|
|
|
72
|
|
|
$wallet = Wallet::makeEmpty(); |
|
73
|
|
|
$credit = Money::fromNative('100SAT'); |
|
74
|
|
|
$creditedWallet = $wallet->credit($credit); |
|
75
|
|
|
$balance = $creditedWallet->getBalance('SAT'); |
|
76
|
|
|
$this->assertNotSame($credit, $balance); |
|
77
|
|
|
$this->assertEquals($credit, $balance); |
|
78
|
|
|
|
|
79
|
|
|
$this->markTestSkipped('Assert wallet key matches money currency.'); |
|
80
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
81
|
|
|
Wallet::fromNative(['sat' => '100SAT']); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
public function testHasBalance(): void |
|
85
|
|
|
{ |
|
86
|
1 |
|
$amount = Money::fromNative('100SAT'); |
|
87
|
1 |
|
$wallet = Wallet::makeEmpty(); |
|
88
|
1 |
|
$this->assertFalse($wallet->hasBalance($amount)); |
|
89
|
|
|
|
|
90
|
1 |
|
$wallet = Wallet::fromNative(['SAT' => '100SAT']); |
|
91
|
1 |
|
$this->assertTrue($wallet->hasBalance($amount)); |
|
92
|
|
|
|
|
93
|
1 |
|
$amount = Money::fromNative('100MSAT'); |
|
94
|
1 |
|
$this->assertFalse($wallet->hasBalance($amount)); |
|
95
|
1 |
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|