|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ApplicationTest\Repository; |
|
6
|
|
|
|
|
7
|
|
|
use Application\Model\Transaction; |
|
8
|
|
|
use Application\Model\TransactionLine; |
|
9
|
|
|
use Application\Model\User; |
|
10
|
|
|
use Application\Repository\TransactionRepository; |
|
11
|
|
|
use ApplicationTest\Traits\LimitedAccessSubQuery; |
|
12
|
|
|
use Cake\Chronos\Date; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @group Repository |
|
16
|
|
|
*/ |
|
17
|
|
|
class TransactionRepositoryTest extends AbstractRepositoryTest |
|
18
|
|
|
{ |
|
19
|
|
|
use LimitedAccessSubQuery; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var TransactionRepository |
|
23
|
|
|
*/ |
|
24
|
|
|
private $repository; |
|
25
|
|
|
|
|
26
|
|
|
public function setUp(): void |
|
27
|
|
|
{ |
|
28
|
|
|
parent::setUp(); |
|
29
|
|
|
$this->repository = _em()->getRepository(Transaction::class); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function providerGetAccessibleSubQuery(): array |
|
33
|
|
|
{ |
|
34
|
|
|
$all = [8000, 8001, 8002, 8003, 8004, 8005]; |
|
35
|
|
|
|
|
36
|
|
|
return [ |
|
37
|
|
|
['anonymous', []], |
|
38
|
|
|
['bookingonly', []], |
|
39
|
|
|
['individual', []], |
|
40
|
|
|
['member', [8000, 8002, 8003, 8004]], |
|
41
|
|
|
['responsible', $all], |
|
42
|
|
|
['administrator', $all], |
|
43
|
|
|
]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testHydrateLinesAndFlush(): void |
|
47
|
|
|
{ |
|
48
|
|
|
/** @var User $user */ |
|
49
|
|
|
$user = _em()->getRepository(User::class)->getByLogin('administrator'); |
|
50
|
|
|
User::setCurrent($user); |
|
51
|
|
|
|
|
52
|
|
|
$account = $user->getAccount(); |
|
53
|
|
|
|
|
54
|
|
|
$transaction = new Transaction(); |
|
55
|
|
|
$transaction->setName('foo'); |
|
56
|
|
|
$transaction->setTransactionDate(Date::today()); |
|
57
|
|
|
$line = new TransactionLine(); |
|
58
|
|
|
$line->setTransaction($transaction); |
|
59
|
|
|
|
|
60
|
|
|
self::assertTrue($transaction->getTransactionLines()->contains($line)); |
|
61
|
|
|
$lines = [ |
|
62
|
|
|
[ |
|
63
|
|
|
'balance' => '5', |
|
64
|
|
|
'transactionDate' => Date::today(), |
|
65
|
|
|
'credit' => $account, |
|
66
|
|
|
], |
|
67
|
|
|
]; |
|
68
|
|
|
|
|
69
|
|
|
$this->repository->hydrateLinesAndFlush($transaction, $lines); |
|
70
|
|
|
|
|
71
|
|
|
self::assertSame('5.00', $account->getBalance(), 'account balance must have been refreshed from DB'); |
|
72
|
|
|
self::assertFalse($transaction->getTransactionLines()->contains($line), 'original line must have been deleted'); |
|
73
|
|
|
self::assertCount(1, $transaction->getTransactionLines(), 'one single new line'); |
|
74
|
|
|
|
|
75
|
|
|
$line = $transaction->getTransactionLines()->first(); |
|
76
|
|
|
self::assertSame('5', $line->getBalance()); |
|
77
|
|
|
self::assertSame($account, $line->getCredit()); |
|
78
|
|
|
self::assertNull($line->getDebit()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testHydrateLinesAndFlushMustThrowWithoutAnyLines(): void |
|
82
|
|
|
{ |
|
83
|
|
|
$transaction = new Transaction(); |
|
84
|
|
|
|
|
85
|
|
|
$this->expectExceptionMessage('A Transaction must have at least one TransactionLine'); |
|
86
|
|
|
$this->repository->hydrateLinesAndFlush($transaction, []); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testHydrateLinesAndFlushMustThrowWithALineWithoutAnyAccount(): void |
|
90
|
|
|
{ |
|
91
|
|
|
$transaction = new Transaction(); |
|
92
|
|
|
$this->expectExceptionMessage('Cannot create a TransactionLine without any account'); |
|
93
|
|
|
$this->repository->hydrateLinesAndFlush($transaction, [[]]); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|