1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ApplicationTest\Repository; |
6
|
|
|
|
7
|
|
|
use Application\DBAL\Types\AccountTypeType; |
8
|
|
|
use Application\Model\Account; |
9
|
|
|
use Application\Model\User; |
10
|
|
|
use Application\Repository\AccountRepository; |
11
|
|
|
use ApplicationTest\Traits\LimitedAccessSubQuery; |
12
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @group Repository |
16
|
|
|
*/ |
17
|
|
|
class AccountRepositoryTest extends AbstractRepositoryTest |
18
|
|
|
{ |
19
|
|
|
use LimitedAccessSubQuery; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var AccountRepository |
23
|
|
|
*/ |
24
|
|
|
private $repository; |
25
|
|
|
|
26
|
|
|
public function setUp(): void |
27
|
|
|
{ |
28
|
|
|
parent::setUp(); |
29
|
|
|
$this->repository = _em()->getRepository(Account::class); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function providerGetAccessibleSubQuery(): array |
33
|
|
|
{ |
34
|
|
|
$all = range(10000, 10106); |
35
|
|
|
|
36
|
|
|
return [ |
37
|
|
|
['anonymous', []], |
38
|
|
|
['bookingonly', []], |
39
|
|
|
['individual', [10097]], |
40
|
|
|
['member', [10096]], |
41
|
|
|
['responsible', $all], |
42
|
|
|
['administrator', $all], |
43
|
|
|
]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testOneUserCanHaveOnlyOneAccount(): void |
47
|
|
|
{ |
48
|
|
|
$this->expectException(UniqueConstraintViolationException::class); |
49
|
|
|
$this->getEntityManager()->getConnection()->insert('account', ['owner_id' => -1000, 'iban' => uniqid()]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testGetOrCreate(): void |
53
|
|
|
{ |
54
|
|
|
$user = new User(); |
55
|
|
|
$user->setFirstName('Foo'); |
56
|
|
|
$user->setLastName('Bar'); |
57
|
|
|
|
58
|
|
|
$account = $this->repository->getOrCreate($user); |
59
|
|
|
|
60
|
|
|
self::assertSame($user, $account->getOwner()); |
61
|
|
|
self::assertSame('Foo Bar', $account->getName()); |
62
|
|
|
self::assertSame(AccountTypeType::LIABILITY, $account->getType()); |
63
|
|
|
self::assertSame('20300009', $account->getCode()); |
64
|
|
|
self::assertSame('Acomptes de clients', $account->getParent()->getName()); |
65
|
|
|
self::assertSame($account, $user->getAccount()); |
66
|
|
|
|
67
|
|
|
$account2 = $this->repository->getOrCreate($user); |
68
|
|
|
self::assertSame($account, $account2, 'should return the same one if called more than once'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testGetOrCreateInMemory(): void |
72
|
|
|
{ |
73
|
|
|
$user = new User(); |
74
|
|
|
$account = new Account(); |
75
|
|
|
$account->setOwner($user); |
76
|
|
|
|
77
|
|
|
$actualAccount = $this->repository->getOrCreate($user); |
78
|
|
|
|
79
|
|
|
self::assertSame($account, $actualAccount, 'should return the in-memory account if existing'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testTotalBalance(): void |
83
|
|
|
{ |
84
|
|
|
$totalAssets = $this->repository->totalBalanceByType(AccountTypeType::ASSET); |
85
|
|
|
$totalLiabilities = $this->repository->totalBalanceByType(AccountTypeType::LIABILITY); |
86
|
|
|
$totalRevenue = $this->repository->totalBalanceByType(AccountTypeType::REVENUE); |
87
|
|
|
$totalExpense = $this->repository->totalBalanceByType(AccountTypeType::EXPENSE); |
88
|
|
|
$totalEquity = $this->repository->totalBalanceByType(AccountTypeType::EQUITY); |
89
|
|
|
|
90
|
|
|
self::assertEquals(35187.50, $totalAssets); |
91
|
|
|
self::assertEquals(60, $totalLiabilities); |
92
|
|
|
self::assertEquals(240, $totalRevenue); |
93
|
|
|
self::assertEquals(112.50, $totalExpense); |
94
|
|
|
self::assertEquals(35000, $totalEquity); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|