Failed Conditions
Push — master ( 76f788...13b360 )
by Sylvain
08:55
created

AccountingTest::testClose()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 26
rs 9.7
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApplicationTest\Service;
6
7
use Application\DBAL\Types\AccountTypeType;
8
use Application\Model\Account;
9
use Application\Model\User;
10
use Application\Repository\AccountRepository;
11
use Application\Service\Accounting;
12
use ApplicationTest\Traits\TestWithTransactionAndUser;
13
use Cake\Chronos\Chronos;
14
use Cake\Chronos\Date;
15
use Money\Money;
16
use PHPUnit\Framework\TestCase;
17
18
class AccountingTest extends TestCase
19
{
20
    use TestWithTransactionAndUser {
21
        setUp as setupWithTransaction;
22
    }
23
24
    private Accounting $accounting;
25
26
    protected function setUp(): void
27
    {
28
        $this->setupWithTransaction();
29
30
        /** @var User $user */
31
        $user = _em()->getRepository(User::class)->getOneByLogin('administrator');
32
        User::setCurrent($user);
33
34
        global $container;
35
        $this->accounting = $container->get(Accounting::class);
36
    }
37
38
    public function testCheck(): void
39
    {
40
        $this->expectOutputString(<<<STRING
41
42
            Produits  : 240.00
43
            Charges   : 112.50
44
            Bénéfice  : 127.50
45
            Actifs    : 35187.50
46
            Passifs   : 60.00
47
            Capital   : 35000.00
48
            Écart     : 0.00
49
            Création du compte 20300010 pour l'utilisateur 1003...
50
            Création du compte 20300011 pour l'utilisateur 1004...
51
            Création du compte 20300012 pour l'utilisateur 1005...
52
            Création du compte 20300013 pour l'utilisateur 1006...
53
54
            STRING
55
        );
56
57
        self::assertFalse($this->accounting->check(), 'fixture data should not produce any errors');
58
    }
59
60
    public function testClose(): void
61
    {
62
        /** @var AccountRepository $accountRepository */
63
        $accountRepository = _em()->getRepository(Account::class);
64
        $closingDate = Date::createFromDate(2019, 12, 31);
65
66
        $expectedLog = [
67
            'Bouclement au 2019-12-31',
68
            'Bénéfice : 127.50',
69
        ];
70
        $output = [];
71
        $closingTransaction = $this->accounting->close($closingDate, $output);
72
        self::assertSame($expectedLog, $output);
73
74
        $actualDate = $closingTransaction->getTransactionDate();
75
        $expectedDate = (new Chronos($closingDate))->endOfDay();
76
        self::assertTrue($actualDate->eq($expectedDate), 'Closing transaction was not created on ' . $closingDate);
77
78
        $accounts = $accountRepository->findByType([AccountTypeType::REVENUE, AccountTypeType::EXPENSE]);
0 ignored issues
show
Bug introduced by
The method findByType() does not exist on Application\Repository\AccountRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
        /** @scrutinizer ignore-call */ 
79
        $accounts = $accountRepository->findByType([AccountTypeType::REVENUE, AccountTypeType::EXPENSE]);
Loading history...
79
        $openingDate = $closingDate->addDay();
80
        foreach ($accounts as $account) {
81
            self::assertTrue(Money::CHF(0)->equals($account->getBalanceAtDate($openingDate)));
82
        }
83
84
        $this->expectExceptionMessage('Le bouclement a déjà été fait au 2019-12-31 23:59:59');
85
        $this->accounting->close($closingDate, $output);
86
    }
87
}
88