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