Passed
Push — master ( 4e7aa8...dae217 )
by Adrien
35:16 queued 30:13
created

ImporterTest::testThrowInvalidCamtFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
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 PHPUnit\Framework\TestCase;
13
14
class ImporterTest extends TestCase
15
{
16
    /**
17
     * @var string
18
     */
19
    private $previousTimeZone;
20
21
    protected function setUp(): void
22
    {
23
        $this->previousTimeZone = date_default_timezone_get();
24
        date_default_timezone_set('Europe/Zurich');
25
    }
26
27
    protected function tearDown(): void
28
    {
29
        date_default_timezone_set($this->previousTimeZone);
30
31
        // Be sure to clear created object from memory
32
        _em()->clear();
33
    }
34
35
    private function import(string $filename): array
36
    {
37
        $importer = new Importer();
38
39
        return $this->extract($importer->import($filename));
40
    }
41
42
    public function testImportMinimal(): void
43
    {
44
        $actual = $this->import('tests/data/importer/minimal.xml');
45
        $expected = require 'tests/data/importer/minimal.php';
46
47
        self::assertSame($expected, $actual);
48
    }
49
50
    public function testImport(): void
51
    {
52
        $actual = $this->import('tests/data/importer/two-transactions.xml');
53
        $expected = require 'tests/data/importer/two-transactions.php';
54
55
        self::assertSame($expected, $actual);
56
    }
57
58
    public function testThrowWhenFileDoesNotExist(): void
59
    {
60
        $this->expectExceptionMessage('/this/surely/is/a/non/existing/file does not exists');
61
        $this->import('/this/surely/is/a/non/existing/file');
62
    }
63
64
    public function testThrowMissingAcctSvcrRef(): void
65
    {
66
        $this->expectExceptionMessage('Cannot import a transaction without an end-to-end ID or an account servicer reference to store a universal identifier.');
67
        $this->import('tests/data/importer/missing-EndToEndId-and-AcctSvcrRef.xml');
68
    }
69
70
    public function testThrowInvalidIban(): void
71
    {
72
        $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.');
73
        $this->import('tests/data/importer/invalid-iban.xml');
74
    }
75
76
    public function testThrowInvalidUser(): void
77
    {
78
        $this->expectExceptionMessage('Could not find a matching user for reference number `800826000000000000000099994`.');
79
        $this->import('tests/data/importer/invalid-user.xml');
80
    }
81
82
    public function testThrowInvalidCamtFormat(): void
83
    {
84
        $this->expectExceptionMessage('The format CAMT 054 is expected, but instead we got: camt.053.001.04');
85
        $this->import('tests/data/importer/invalid-camt-format.xml');
86
    }
87
88
    public function testThrowDuplicatedImport(): void
89
    {
90
        $this->expectExceptionMessage('It looks like this file was already imported. A transaction line with the following `importedId` was already imported once and cannot be imported again: my-unique-imported-id');
91
        $this->import('tests/data/importer/duplicated-importedId.xml');
92
    }
93
94
    public function testThrowInvalidXml(): void
95
    {
96
        $this->expectExceptionMessage('Unsupported format, cannot find message format with xmlns');
97
        $this->import('tests/data/importer/invalid-xml.xml');
98
    }
99
100
    /**
101
     * @param null|Account|User $o
102
     *
103
     * @return string
104
     */
105
    private function nameOrNull($o): ?string
106
    {
107
        return $o ? $o->getName() : null;
108
    }
109
110
    /**
111
     * @param Transaction[] $transactions
112
     */
113
    private function extract(array $transactions): array
114
    {
115
        $result = [];
116
117
        foreach ($transactions as $transaction) {
118
            $result[] = $this->extractTransaction($transaction);
119
        }
120
121
        return $result;
122
    }
123
124
    private function extractTransaction(Transaction $transaction): array
125
    {
126
        $lines = [];
127
        /** @var TransactionLine $line */
128
        foreach ($transaction->getTransactionLines() as $line) {
129
            $lines[] = $this->extractLine($line);
130
        }
131
132
        $result = [
133
            'name' => $transaction->getName(),
134
            'remarks' => $transaction->getRemarks(),
135
            'internalRemarks' => $transaction->getInternalRemarks(),
136
            'datatransRef' => $transaction->getDatatransRef(),
137
            'transactionDate' => $transaction->getTransactionDate()->toIso8601String(),
138
            'owner' => $this->nameOrNull($transaction->getOwner()),
139
            'transactionLines' => $lines,
140
        ];
141
142
        return $result;
143
    }
144
145
    private function extractLine(TransactionLine $line): array
146
    {
147
        $result = [
148
            'importedId' => $line->getImportedId(),
149
            'name' => $line->getName(),
150
            'remarks' => $line->getRemarks(),
151
            'transactionDate' => $line->getTransactionDate()->toIso8601String(),
152
            'balance' => $line->getBalance()->getAmount(),
153
            'owner' => $this->nameOrNull($line->getOwner()),
154
            'debit' => $this->nameOrNull($line->getDebit()),
155
            'credit' => $this->nameOrNull($line->getCredit()),
156
        ];
157
158
        return $result;
159
    }
160
}
161