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
|
|
|
public function setUp(): void |
23
|
|
|
{ |
24
|
|
|
$this->previousTimeZone = date_default_timezone_get(); |
25
|
|
|
date_default_timezone_set('Europe/Zurich'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public 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
|
|
|
/** |
37
|
|
|
* @param string $filename |
38
|
|
|
* |
39
|
|
|
* @return Transaction[] |
40
|
|
|
*/ |
41
|
|
|
private function import(string $filename): array |
42
|
|
|
{ |
43
|
|
|
$importer = new Importer(); |
44
|
|
|
|
45
|
|
|
return $this->extract($importer->import($filename)); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testImportMinimal(): void |
49
|
|
|
{ |
50
|
|
|
$actual = $this->import('tests/data/importer/minimal.xml'); |
51
|
|
|
$expected = require 'tests/data/importer/minimal.php'; |
52
|
|
|
|
53
|
|
|
self::assertSame($expected, $actual); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testImport(): void |
57
|
|
|
{ |
58
|
|
|
$actual = $this->import('tests/data/importer/two-transactions.xml'); |
59
|
|
|
$expected = require 'tests/data/importer/two-transactions.php'; |
60
|
|
|
|
61
|
|
|
self::assertSame($expected, $actual); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testThrowWhenFileDoesNotExist(): void |
65
|
|
|
{ |
66
|
|
|
$this->expectException(ReaderException::class); |
67
|
|
|
$this->import('/this/surely/is/a/non/existing/file'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testThrowMissingAcctSvcrRef(): void |
71
|
|
|
{ |
72
|
|
|
$this->expectExceptionMessage('Cannot import a transaction without a account servicer reference to store a universal identifier.'); |
73
|
|
|
$this->import('tests/data/importer/missing-AcctSvcrRef.xml'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testThrowInvalidIban(): void |
77
|
|
|
{ |
78
|
|
|
$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.'); |
79
|
|
|
$this->import('tests/data/importer/invalid-iban.xml'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testThrowInvalidUser(): void |
83
|
|
|
{ |
84
|
|
|
$this->expectExceptionMessage('Could not find a matching user for reference number `800826000000000000000099994`.'); |
85
|
|
|
$this->import('tests/data/importer/invalid-user.xml'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param Account|User $o |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
private function nameOrNull($o): ?string |
94
|
|
|
{ |
95
|
|
|
return $o ? $o->getName() : null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Transaction[] $transactions |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
private function extract(array $transactions): array |
104
|
|
|
{ |
105
|
|
|
$result = []; |
106
|
|
|
|
107
|
|
|
foreach ($transactions as $transaction) { |
108
|
|
|
$result[] = $this->extractTransaction($transaction); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $result; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private function extractTransaction(Transaction $transaction): array |
115
|
|
|
{ |
116
|
|
|
$lines = []; |
117
|
|
|
/** @var TransactionLine $line */ |
118
|
|
|
foreach ($transaction->getTransactionLines() as $line) { |
119
|
|
|
$lines[] = $this->extractLine($line); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$result = [ |
123
|
|
|
'name' => $transaction->getName(), |
124
|
|
|
'remarks' => $transaction->getRemarks(), |
125
|
|
|
'internalRemarks' => $transaction->getInternalRemarks(), |
126
|
|
|
'datatransRef' => $transaction->getDatatransRef(), |
127
|
|
|
'transactionDate' => $transaction->getTransactionDate()->toIso8601String(), |
128
|
|
|
'owner' => $this->nameOrNull($transaction->getOwner()), |
129
|
|
|
'transactionLines' => $lines, |
130
|
|
|
]; |
131
|
|
|
|
132
|
|
|
return $result; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
private function extractLine(TransactionLine $line): array |
136
|
|
|
{ |
137
|
|
|
$result = [ |
138
|
|
|
'importedId' => $line->getImportedId(), |
139
|
|
|
'name' => $line->getName(), |
140
|
|
|
'remarks' => $line->getRemarks(), |
141
|
|
|
'transactionDate' => $line->getTransactionDate()->toIso8601String(), |
142
|
|
|
'balance' => $line->getBalance(), |
143
|
|
|
'owner' => $this->nameOrNull($line->getOwner()), |
144
|
|
|
'debit' => $this->nameOrNull($line->getDebit()), |
145
|
|
|
'credit' => $this->nameOrNull($line->getCredit()), |
146
|
|
|
]; |
147
|
|
|
|
148
|
|
|
return $result; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|