Failed Conditions
Push — master ( f87f81...988ef6 )
by Adrien
10:33
created

ImporterTest::testThrowDuplicatedImport()   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
    private string $previousTimeZone;
17
18
    protected function setUp(): void
19
    {
20
        $this->previousTimeZone = date_default_timezone_get();
21
        date_default_timezone_set('Europe/Zurich');
22
    }
23
24
    protected function tearDown(): void
25
    {
26
        date_default_timezone_set($this->previousTimeZone);
27
    }
28
29
    private function import(string $filename): array
30
    {
31
        $importer = new Importer();
32
33
        return $this->extract($importer->import($filename));
34
    }
35
36
    /**
37
     * @dataProvider providerImport
38
     */
39
    public function testImport(string $xml, string $php): void
40
    {
41
        $actual = $this->import($xml);
42
        $expected = require $php;
43
44
        self::assertSame($expected, $actual);
45
    }
46
47
    public function testThrowWhenFileDoesNotExist(): void
48
    {
49
        $this->expectExceptionMessage('/this/surely/is/a/non/existing/file does not exists');
50
        $this->import('/this/surely/is/a/non/existing/file');
51
    }
52
53
    public function testThrowMissingAcctSvcrRef(): void
54
    {
55
        $this->expectExceptionMessage('Cannot import a transaction without unique universal identifier (<EndToEndId>, <AcctSvcrRef> or <MsgId>).');
56
        $this->import('tests/data/importer/missing-EndToEndId-and-AcctSvcrRef.xml');
57
    }
58
59
    public function testThrowInvalidIban(): void
60
    {
61
        $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.');
62
        $this->import('tests/data/importer/invalid-iban.xml');
63
    }
64
65
    public function testThrowInvalidUser(): void
66
    {
67
        $this->expectExceptionMessage('Could not find a matching user for reference number `800826000000000000000099994` and user ID `9999`.');
68
        $this->import('tests/data/importer/invalid-user.xml');
69
    }
70
71
    public function testThrowInvalidCamtFormat(): void
72
    {
73
        $this->expectExceptionMessage('The format CAMT 054 is expected, but instead we got: camt.053.001.04');
74
        $this->import('tests/data/importer/invalid-camt-format.xml');
75
    }
76
77
    public function testThrowInvalidXml(): void
78
    {
79
        $this->expectExceptionMessage('Unsupported format, cannot find message format with xmlns');
80
        $this->import('tests/data/importer/invalid-xml.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
    public function providerImport(): array
145
    {
146
        // Return all couples of xml/php files
147
        $files = [];
148
        foreach (glob('tests/data/importer/*.xml') as $xml) {
149
            $php = preg_replace('~xml$~', 'php', $xml);
150
            if (file_exists($php)) {
151
                $name = str_replace('-', ' ', basename($xml, '.xml'));
152
153
                $files[$name] = [$xml, $php];
154
            }
155
        }
156
157
        return $files;
158
    }
159
}
160