1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ApplicationTest\Repository; |
6
|
|
|
|
7
|
|
|
use Application\Model\Account; |
8
|
|
|
use Application\Model\Transaction; |
9
|
|
|
use Application\Model\TransactionLine; |
10
|
|
|
use Application\Model\User; |
11
|
|
|
use Application\Repository\TransactionRepository; |
12
|
|
|
use ApplicationTest\Traits\LimitedAccessSubQuery; |
13
|
|
|
use Cake\Chronos\Date; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @group Repository |
17
|
|
|
*/ |
18
|
|
|
class TransactionRepositoryTest extends AbstractRepositoryTest |
19
|
|
|
{ |
20
|
|
|
use LimitedAccessSubQuery; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var TransactionRepository |
24
|
|
|
*/ |
25
|
|
|
private $repository; |
26
|
|
|
|
27
|
|
|
public function setUp(): void |
28
|
|
|
{ |
29
|
|
|
parent::setUp(); |
30
|
|
|
$this->repository = _em()->getRepository(Transaction::class); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function providerGetAccessibleSubQuery(): array |
34
|
|
|
{ |
35
|
|
|
$all = [8000, 8001, 8002, 8003, 8004, 8005, 8006, 8007]; |
36
|
|
|
|
37
|
|
|
return [ |
38
|
|
|
['anonymous', []], |
39
|
|
|
['bookingonly', []], |
40
|
|
|
['individual', []], |
41
|
|
|
['member', [8000, 8002, 8002, 8003, 8004, 8006]], |
42
|
|
|
['responsible', $all], |
43
|
|
|
['administrator', $all], |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testHydrateLinesAndFlush(): void |
48
|
|
|
{ |
49
|
|
|
/** @var User $user */ |
50
|
|
|
$user = _em()->getRepository(User::class)->getOneByLogin('administrator'); |
51
|
|
|
User::setCurrent($user); |
52
|
|
|
|
53
|
|
|
$credit = $user->getAccount(); |
54
|
|
|
$debit = _em()->getRepository(Account::class)->findOneBy(['code' => 1000]); |
55
|
|
|
|
56
|
|
|
$transaction = new Transaction(); |
57
|
|
|
$transaction->setName('foo'); |
58
|
|
|
$transaction->setTransactionDate(Date::today()); |
59
|
|
|
$line = new TransactionLine(); |
60
|
|
|
$line->setTransaction($transaction); |
61
|
|
|
|
62
|
|
|
self::assertTrue($transaction->getTransactionLines()->contains($line)); |
63
|
|
|
$lines = [ |
64
|
|
|
[ |
65
|
|
|
'balance' => '5', |
66
|
|
|
'transactionDate' => Date::today(), |
67
|
|
|
'credit' => $credit, |
68
|
|
|
'debit' => $debit, |
69
|
|
|
], |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
$this->repository->hydrateLinesAndFlush($transaction, $lines); |
73
|
|
|
|
74
|
|
|
self::assertSame('5.00', $credit->getBalance(), 'credit account balance must have been refreshed from DB'); |
75
|
|
|
self::assertSame('5.00', $debit->getBalance(), 'debit account balance must have been refreshed from DB'); |
76
|
|
|
self::assertFalse($transaction->getTransactionLines()->contains($line), 'original line must have been deleted'); |
77
|
|
|
self::assertCount(1, $transaction->getTransactionLines(), 'one line'); |
78
|
|
|
|
79
|
|
|
$line = $transaction->getTransactionLines()->first(); |
80
|
|
|
self::assertSame('5', $line->getBalance()); |
81
|
|
|
self::assertSame($credit, $line->getCredit()); |
82
|
|
|
self::assertSame($debit, $line->getDebit()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testHydrateLinesAndFlushMustThrowWithoutAnyLines(): void |
86
|
|
|
{ |
87
|
|
|
$transaction = new Transaction(); |
88
|
|
|
|
89
|
|
|
$this->expectExceptionMessage('A Transaction must have at least one TransactionLine'); |
90
|
|
|
$this->repository->hydrateLinesAndFlush($transaction, []); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testHydrateLinesAndFlushMustThrowWithALineWithoutAnyAccount(): void |
94
|
|
|
{ |
95
|
|
|
$transaction = new Transaction(); |
96
|
|
|
$this->expectExceptionMessage('Cannot create a TransactionLine without any account'); |
97
|
|
|
$this->repository->hydrateLinesAndFlush($transaction, [[]]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testHydrateLinesAndFlushMustThrowWithUnbalancedLines(): void |
101
|
|
|
{ |
102
|
|
|
/** @var User $user */ |
103
|
|
|
$user = _em()->getRepository(User::class)->getOneByLogin('administrator'); |
104
|
|
|
User::setCurrent($user); |
105
|
|
|
|
106
|
|
|
$debit = _em()->getRepository(Account::class)->findOneBy(['code' => 10201]); |
107
|
|
|
$credit = _em()->getRepository(Account::class)->findOneBy(['code' => 1000]); |
108
|
|
|
|
109
|
|
|
$transaction = new Transaction(); |
110
|
|
|
$transaction->setName('caisse à poste'); |
111
|
|
|
$transaction->setRemarks('montants erronés'); |
112
|
|
|
$transaction->setTransactionDate(Date::today()); |
113
|
|
|
$line = new TransactionLine(); |
114
|
|
|
$line->setTransaction($transaction); |
115
|
|
|
|
116
|
|
|
$lines = [ |
117
|
|
|
[ |
118
|
|
|
'balance' => '1000', |
119
|
|
|
'transactionDate' => Date::today(), |
120
|
|
|
'debit' => $debit, |
121
|
|
|
], |
122
|
|
|
[ |
123
|
|
|
'balance' => '900', |
124
|
|
|
'transactionDate' => Date::today(), |
125
|
|
|
'credit' => $credit, |
126
|
|
|
], |
127
|
|
|
]; |
128
|
|
|
|
129
|
|
|
$this->expectExceptionMessage('Transaction NEW non-équilibrée, débits: 1000.00, crédits: 900.00'); |
130
|
|
|
$this->repository->hydrateLinesAndFlush($transaction, $lines); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|