1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ApplicationTest\Model; |
6
|
|
|
|
7
|
|
|
use Application\Model\Account; |
8
|
|
|
use Application\Model\User; |
9
|
|
|
use Application\Repository\AccountRepository; |
10
|
|
|
use Cake\Chronos\Date; |
11
|
|
|
use Doctrine\DBAL\Exception\InvalidArgumentException; |
12
|
|
|
use Money\Money; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
|
15
|
|
|
class AccountTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function testUserRelation(): void |
18
|
|
|
{ |
19
|
|
|
$account = new Account(); |
20
|
|
|
self::assertNull($account->getOwner()); |
21
|
|
|
|
22
|
|
|
$user = new User(); |
23
|
|
|
User::setCurrent($user); |
24
|
|
|
$account->setOwner($user); |
25
|
|
|
|
26
|
|
|
self::assertSame($account->getOwner(), $user); |
27
|
|
|
self::assertSame($account, $user->getAccount()); |
28
|
|
|
|
29
|
|
|
$otherUser = new User(); |
30
|
|
|
$account->setOwner($otherUser); |
31
|
|
|
self::assertSame($otherUser, $account->getOwner(), 'Account must have second user'); |
32
|
|
|
self::assertNull($user->getAccount(), 'First user must have no account'); |
33
|
|
|
self::assertSame($account, $otherUser->getAccount(), 'second user must have account'); |
34
|
|
|
|
35
|
|
|
User::setCurrent($otherUser); |
36
|
|
|
$account->setOwner(null); |
37
|
|
|
self::assertNull($account->getOwner(), 'Account must have no user'); |
38
|
|
|
self::assertNull($user->getAccount(), 'First user must have no account'); |
39
|
|
|
self::assertNull($otherUser->getAccount(), 'Second user must have no account'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testTree(): void |
43
|
|
|
{ |
44
|
|
|
$a = new Account(); |
45
|
|
|
$b = new Account(); |
46
|
|
|
$c = new Account(); |
47
|
|
|
|
48
|
|
|
$b->setParent($a); |
49
|
|
|
$c->setParent($a); |
50
|
|
|
|
51
|
|
|
self::assertCount(2, $a->getChildren()); |
52
|
|
|
|
53
|
|
|
$c->setParent(null); |
54
|
|
|
|
55
|
|
|
self::assertCount(1, $a->getChildren()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testIban(): void |
59
|
|
|
{ |
60
|
|
|
$account = new Account(); |
61
|
|
|
|
62
|
|
|
self::assertEmpty($account->getIban()); |
63
|
|
|
|
64
|
|
|
$iban = 'CH6303714697192579556'; |
65
|
|
|
$account->setIban($iban); |
66
|
|
|
self::assertSame($iban, $account->getIban()); |
67
|
|
|
|
68
|
|
|
$germanIban = 'DE75512108001245126199'; |
69
|
|
|
$account->setIban($germanIban); |
70
|
|
|
self::assertSame($germanIban, $account->getIban()); |
71
|
|
|
|
72
|
|
|
$this->expectException(InvalidArgumentException::class); |
73
|
|
|
$invalidIban = 'CH123456789012345678'; |
74
|
|
|
$account->setIban($invalidIban); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testBalanceAtDate(): void |
78
|
|
|
{ |
79
|
|
|
/** @var AccountRepository $accountRepository */ |
80
|
|
|
$accountRepository = _em()->getRepository(Account::class); |
81
|
|
|
|
82
|
|
|
// Past balance from an asset account |
83
|
|
|
// 10201: PostFinance |
84
|
|
|
$bank = $accountRepository->getOneById(10025); |
85
|
|
|
|
86
|
|
|
$balance = $bank->getBalanceAtDate(new Date('2019-03-01')); |
87
|
|
|
self::assertTrue(Money::CHF(800000)->equals($balance)); |
88
|
|
|
|
89
|
|
|
$balance = $bank->getBalanceAtDate(new Date('2019-05-01')); |
90
|
|
|
self::assertTrue(Money::CHF(818750)->equals($balance)); |
91
|
|
|
|
92
|
|
|
// Past balance from a group account |
93
|
|
|
// 6: Autres charges exploitation |
94
|
|
|
$otherExpenses = $accountRepository->getOneById(10005); |
95
|
|
|
|
96
|
|
|
$balance = $otherExpenses->getBalanceAtDate(new Date('2019-03-01')); |
97
|
|
|
self::assertTrue(Money::CHF(0)->equals($balance)); |
98
|
|
|
|
99
|
|
|
$balance = $otherExpenses->getBalanceAtDate(new Date('2019-03-12')); |
100
|
|
|
self::assertTrue(Money::CHF(1250)->equals($balance)); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|