Failed Conditions
Push — master ( ab912b...01f77f )
by Adrien
10:12
created

ImporterTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApplicationTest\Service;
6
7
use Application\Model\Account;
8
use Application\Model\Transaction;
9
use Application\Model\TransactionLine;
10
use Application\Model\User;
11
use Application\Service\Importer;
12
use Genkgo\Camt\Exception\ReaderException;
13
use PHPUnit\Framework\TestCase;
14
15
class ImporterTest extends TestCase
16
{
17
    /**
18
     * @var string
19
     */
20
    private $previousTimeZone;
21
22
    protected function setUp(): void
23
    {
24
        $this->previousTimeZone = date_default_timezone_get();
25
        date_default_timezone_set('Europe/Zurich');
26
    }
27
28
    protected function tearDown(): void
29
    {
30
        date_default_timezone_set($this->previousTimeZone);
31
32
        // Be sure to clear created object from memory
33
        _em()->clear();
34
    }
35
36
    private function import(string $filename): array
37
    {
38
        $importer = new Importer();
39
40
        return $this->extract($importer->import($filename));
41
    }
42
43
    public function testImportMinimal(): void
44
    {
45
        $actual = $this->import('tests/data/importer/minimal.xml');
46
        $expected = require 'tests/data/importer/minimal.php';
47
48
        self::assertSame($expected, $actual);
49
    }
50
51
    public function testImport(): void
52
    {
53
        $actual = $this->import('tests/data/importer/two-transactions.xml');
54
        $expected = require 'tests/data/importer/two-transactions.php';
55
56
        self::assertSame($expected, $actual);
57
    }
58
59
    public function testThrowWhenFileDoesNotExist(): void
60
    {
61
        $this->expectException(ReaderException::class);
62
        $this->import('/this/surely/is/a/non/existing/file');
63
    }
64
65
    public function testThrowMissingAcctSvcrRef(): void
66
    {
67
        $this->expectExceptionMessage('Cannot import a transaction without an end-to-end ID or an account servicer reference to store a universal identifier.');
68
        $this->import('tests/data/importer/missing-EndToEndId-and-AcctSvcrRef.xml');
69
    }
70
71
    public function testThrowInvalidIban(): void
72
    {
73
        $this->expectExceptionMessage('The CAMT file contains a statement for account with IBAN `CH2133685416723344187`, but no account exist for that IBAN in the database. Either create/update a corresponding account, or import a different CAMT file.');
74
        $this->import('tests/data/importer/invalid-iban.xml');
75
    }
76
77
    public function testThrowInvalidUser(): void
78
    {
79
        $this->expectExceptionMessage('Could not find a matching user for reference number `800826000000000000000099994`.');
80
        $this->import('tests/data/importer/invalid-user.xml');
81
    }
82
83
    /**
84
     * @param null|Account|User $o
85
     *
86
     * @return string
87
     */
88
    private function nameOrNull($o): ?string
89
    {
90
        return $o ? $o->getName() : null;
91
    }
92
93
    /**
94
     * @param Transaction[] $transactions
95
     */
96
    private function extract(array $transactions): array
97
    {
98
        $result = [];
99
100
        foreach ($transactions as $transaction) {
101
            $result[] = $this->extractTransaction($transaction);
102
        }
103
104
        return $result;
105
    }
106
107
    private function extractTransaction(Transaction $transaction): array
108
    {
109
        $lines = [];
110
        /** @var TransactionLine $line */
111
        foreach ($transaction->getTransactionLines() as $line) {
112
            $lines[] = $this->extractLine($line);
113
        }
114
115
        $result = [
116
            'name' => $transaction->getName(),
117
            'remarks' => $transaction->getRemarks(),
118
            'internalRemarks' => $transaction->getInternalRemarks(),
119
            'datatransRef' => $transaction->getDatatransRef(),
120
            'transactionDate' => $transaction->getTransactionDate()->toIso8601String(),
121
            'owner' => $this->nameOrNull($transaction->getOwner()),
122
            'transactionLines' => $lines,
123
        ];
124
125
        return $result;
126
    }
127
128
    private function extractLine(TransactionLine $line): array
129
    {
130
        $result = [
131
            'importedId' => $line->getImportedId(),
132
            'name' => $line->getName(),
133
            'remarks' => $line->getRemarks(),
134
            'transactionDate' => $line->getTransactionDate()->toIso8601String(),
135
            'balance' => $line->getBalance()->getAmount(),
136
            'owner' => $this->nameOrNull($line->getOwner()),
137
            'debit' => $this->nameOrNull($line->getDebit()),
138
            'credit' => $this->nameOrNull($line->getCredit()),
139
        ];
140
141
        return $result;
142
    }
143
}
144