Passed
Push — master ( e3c27b...b957a6 )
by Adrien
17:10
created

ImporterTest::nameOrNull()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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 Transaction[] $transactions
85
     */
86
    private function extract(array $transactions): array
87
    {
88
        $result = [];
89
90
        foreach ($transactions as $transaction) {
91
            $result[] = $this->extractTransaction($transaction);
92
        }
93
94
        return $result;
95
    }
96
97
    private function extractTransaction(Transaction $transaction): array
98
    {
99
        $lines = [];
100
        /** @var TransactionLine $line */
101
        foreach ($transaction->getTransactionLines() as $line) {
102
            $lines[] = $this->extractLine($line);
103
        }
104
105
        $result = [
106
            'name' => $transaction->getName(),
107
            'remarks' => $transaction->getRemarks(),
108
            'internalRemarks' => $transaction->getInternalRemarks(),
109
            'datatransRef' => $transaction->getDatatransRef(),
110
            'transactionDate' => $transaction->getTransactionDate()->toIso8601String(),
111
            'owner' => $transaction->getOwner()?->getName(),
112
            'transactionLines' => $lines,
113
        ];
114
115
        return $result;
116
    }
117
118
    private function extractLine(TransactionLine $line): array
119
    {
120
        $result = [
121
            'importedId' => $line->getImportedId(),
122
            'name' => $line->getName(),
123
            'remarks' => $line->getRemarks(),
124
            'transactionDate' => $line->getTransactionDate()->toIso8601String(),
125
            'balance' => $line->getBalance()->getAmount(),
126
            'owner' => $line->getOwner()?->getName(),
127
            'debit' => $line->getDebit()?->getName(),
128
            'credit' => $line->getCredit()?->getName(),
129
        ];
130
131
        return $result;
132
    }
133
134
    public function providerImport(): array
135
    {
136
        // Return all couples of xml/php files
137
        $files = [];
138
        foreach (glob('tests/data/importer/*.xml') as $xml) {
139
            $php = preg_replace('~xml$~', 'php', $xml);
140
            if (file_exists($php)) {
141
                $name = str_replace('-', ' ', basename($xml, '.xml'));
142
143
                $files[$name] = [$xml, $php];
144
            }
145
        }
146
147
        return $files;
148
    }
149
}
150