1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ApplicationTest\Repository; |
6
|
|
|
|
7
|
|
|
use Application\Model\Account; |
8
|
|
|
use Application\Model\TransactionLine; |
9
|
|
|
use Application\Repository\AccountRepository; |
10
|
|
|
use Application\Repository\TransactionLineRepository; |
11
|
|
|
use ApplicationTest\Traits\LimitedAccessSubQuery; |
12
|
|
|
use Cake\Chronos\Date; |
13
|
|
|
use Money\Money; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @group Repository |
17
|
|
|
*/ |
18
|
|
|
class TransactionLineRepositoryTest extends AbstractRepositoryTest |
19
|
|
|
{ |
20
|
|
|
use LimitedAccessSubQuery; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var TransactionLineRepository |
24
|
|
|
*/ |
25
|
|
|
private $repository; |
26
|
|
|
|
27
|
|
|
protected function setUp(): void |
28
|
|
|
{ |
29
|
|
|
parent::setUp(); |
30
|
|
|
$this->repository = $this->getEntityManager()->getRepository(TransactionLine::class); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function providerGetAccessibleSubQuery(): array |
34
|
|
|
{ |
35
|
|
|
$all = range(14000, 14011); |
36
|
|
|
|
37
|
|
|
$family = [14000, 14002, 14003, 14004, 14008, 14011]; |
38
|
|
|
|
39
|
|
|
return [ |
40
|
|
|
['anonymous', []], |
41
|
|
|
['bookingonly', []], |
42
|
|
|
['individual', $family], |
43
|
|
|
['member', $family], |
44
|
|
|
['responsible', $all], |
45
|
|
|
['administrator', $all], |
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testTriggers(): void |
50
|
|
|
{ |
51
|
|
|
$account1 = 10096; |
52
|
|
|
$account2 = 10037; |
53
|
|
|
$account3 = 10085; |
54
|
|
|
$account4 = 10025; |
55
|
|
|
|
56
|
|
|
$this->assertAccountBalance($account1, 5000, 'initial balance'); |
57
|
|
|
$this->assertAccountBalance($account2, 10000, 'initial balance'); |
58
|
|
|
$this->assertAccountBalance($account3, 1250, 'initial balance'); |
59
|
|
|
$this->assertAccountBalance($account4, 818750, 'initial balance'); |
60
|
|
|
|
61
|
|
|
$connection = $this->getEntityManager()->getConnection(); |
62
|
|
|
$connection->insert('transaction_line', [ |
63
|
|
|
'transaction_id' => 8000, |
64
|
|
|
'debit_id' => $account1, |
65
|
|
|
'credit_id' => $account2, |
66
|
|
|
'balance' => 500, |
67
|
|
|
]); |
68
|
|
|
|
69
|
|
|
$id = $connection->lastInsertId(); |
70
|
|
|
|
71
|
|
|
$this->assertAccountBalance($account1, 4500, 'balance should be reduced when line is inserted'); |
72
|
|
|
$this->assertAccountBalance($account2, 10500, 'balance should be increased when line is inserted'); |
73
|
|
|
|
74
|
|
|
$count = $connection->update('transaction_line', |
75
|
|
|
[ |
76
|
|
|
'balance' => 4000, |
77
|
|
|
], |
78
|
|
|
[ |
79
|
|
|
'id' => $id, |
80
|
|
|
] |
81
|
|
|
); |
82
|
|
|
self::assertSame(1, $count); |
83
|
|
|
$this->assertAccountBalance($account1, 1000, 'balance should be reduced even more after update'); |
84
|
|
|
$this->assertAccountBalance($account2, 14000, 'balance should be increased even more after update'); |
85
|
|
|
|
86
|
|
|
$count = $connection->update('transaction_line', |
87
|
|
|
[ |
88
|
|
|
'debit_id' => $account3, |
89
|
|
|
'credit_id' => $account4, |
90
|
|
|
], |
91
|
|
|
[ |
92
|
|
|
'id' => $id, |
93
|
|
|
] |
94
|
|
|
); |
95
|
|
|
self::assertSame(1, $count); |
96
|
|
|
$this->assertAccountBalance($account1, 5000, 'balance should be restored to its original value after deletion'); |
97
|
|
|
$this->assertAccountBalance($account2, 10000, 'balance should be restored to its original value after deletion'); |
98
|
|
|
$this->assertAccountBalance($account3, 5250, 'balance should be increased after swapped account'); |
99
|
|
|
$this->assertAccountBalance($account4, 814750, 'balance should be reduced after swapped account'); |
100
|
|
|
|
101
|
|
|
$count = $connection->delete('transaction_line', ['id' => $id]); |
102
|
|
|
self::assertSame(1, $count); |
103
|
|
|
$this->assertAccountBalance($account1, 5000, 'balance should be restored to its original value after deletion'); |
104
|
|
|
$this->assertAccountBalance($account2, 10000, 'balance should be restored to its original value after deletion'); |
105
|
|
|
$this->assertAccountBalance($account3, 1250, 'balance should be restored to its original value after deletion'); |
106
|
|
|
$this->assertAccountBalance($account4, 818750, 'balance should be restored to its original value after deletion'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testTotalBalance(): void |
110
|
|
|
{ |
111
|
|
|
/** @var AccountRepository $accountRepository */ |
112
|
|
|
$accountRepository = _em()->getRepository(Account::class); |
113
|
|
|
|
114
|
|
|
/** @var Account $poste */ |
115
|
|
|
$poste = $accountRepository->getOneById(10025); |
116
|
|
|
|
117
|
|
|
$totalDebit = $this->repository->totalBalance($poste, null); |
118
|
|
|
$totalCredit = $this->repository->totalBalance(null, $poste); |
119
|
|
|
self::assertTrue(Money::CHF(1520000)->equals($totalDebit)); |
120
|
|
|
self::assertTrue(Money::CHF(701250)->equals($totalCredit)); |
121
|
|
|
|
122
|
|
|
$totalDebitFromDate = $this->repository->totalBalance($poste, null, new Date('2019-02-01')); |
123
|
|
|
$totalDebitUntilDate = $this->repository->totalBalance($poste, null, null, new Date('2019-01-01')); |
124
|
|
|
self::assertTrue(Money::CHF(20000)->equals($totalDebitFromDate)); |
125
|
|
|
self::assertTrue(Money::CHF(1500000)->equals($totalDebitUntilDate)); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|