|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace EcodevTests\Felix\Service; |
|
6
|
|
|
|
|
7
|
|
|
use Ecodev\Felix\Service\Bvr; |
|
8
|
|
|
use Money\Money; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
|
|
11
|
|
|
final class BvrTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @dataProvider providerGetReferenceNumber |
|
15
|
|
|
*/ |
|
16
|
|
|
public function testGetReferenceNumber(string $bankAccount, string $referenceNumber, string $expected): void |
|
17
|
|
|
{ |
|
18
|
|
|
$actual = Bvr::getReferenceNumber($bankAccount, $referenceNumber); |
|
19
|
|
|
self::assertSame($expected, $actual); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function providerGetReferenceNumber(): array |
|
23
|
|
|
{ |
|
24
|
|
|
return [ |
|
25
|
|
|
['123456', '', '123456000000000000000000006'], |
|
26
|
|
|
['123456', '789', '123456000000000000000007891'], |
|
27
|
|
|
]; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testGetReferenceNumberMustThrowIfTooLongBankAccount(): void |
|
31
|
|
|
{ |
|
32
|
|
|
$this->expectExceptionMessage('Invalid bank account. It must be exactly 6 digits, but got: `1234567`'); |
|
33
|
|
|
Bvr::getReferenceNumber('1234567', '123'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testGetReferenceNumberMustThrowIfTooLongReferenceNumber(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$this->expectExceptionMessage('Invalid custom ID. It must be 20 or less digits, but got: `000000000000000000000`'); |
|
39
|
|
|
Bvr::getReferenceNumber('123456', str_repeat('0', 21)); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testGetReferenceNumberMustThrowIfInvalidReferenceNumber(): void |
|
43
|
|
|
{ |
|
44
|
|
|
$this->expectExceptionMessage('Invalid custom ID. It must be 20 or less digits, but got: `1.5`'); |
|
45
|
|
|
Bvr::getReferenceNumber('123456', '1.5'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @dataProvider providerModulo10 |
|
50
|
|
|
*/ |
|
51
|
|
|
public function testModulo10(string $number, int $expected): void |
|
52
|
|
|
{ |
|
53
|
|
|
$actual = Bvr::modulo10($number); |
|
54
|
|
|
self::assertSame($expected, $actual); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function providerModulo10(): array |
|
58
|
|
|
{ |
|
59
|
|
|
return [ |
|
60
|
|
|
['', 0], |
|
61
|
|
|
['0', 0], |
|
62
|
|
|
['04', 2], |
|
63
|
|
|
['010000394975', 3], |
|
64
|
|
|
['313947143000901', 8], |
|
65
|
|
|
['80082600000000000000000201', 6], |
|
66
|
|
|
['80082600000000000000000001', 2], |
|
67
|
|
|
['80082600000000000000000002', 8], |
|
68
|
|
|
['80082600000000000000000003', 3], |
|
69
|
|
|
['80082600000000000000000004', 9], |
|
70
|
|
|
['80082600000000000000000005', 7], |
|
71
|
|
|
['80082600000000000000000006', 5], |
|
72
|
|
|
['80082600000000000000000007', 0], |
|
73
|
|
|
['80082600000000000000000008', 1], |
|
74
|
|
|
['80082600000000000000000009', 6], |
|
75
|
|
|
['80082600000000000000000010', 8], |
|
76
|
|
|
]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @dataProvider providerGetEncodingLine |
|
81
|
|
|
*/ |
|
82
|
|
|
public function testGetEncodingLine(string $bankAccount, string $referenceNumber, string $postalAccount, ?Money $amount, string $expected): void |
|
83
|
|
|
{ |
|
84
|
|
|
$actual = Bvr::getEncodingLine($bankAccount, $referenceNumber, $postalAccount, $amount); |
|
85
|
|
|
self::assertSame($expected, $actual); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function providerGetEncodingLine(): array |
|
89
|
|
|
{ |
|
90
|
|
|
return [ |
|
91
|
|
|
['800826', '00000000000000000201', '01-4567-0', null, '042>800826000000000000000002016+ 010045670>'], |
|
92
|
|
|
['000000', '', '1-2-3', null, '042>000000000000000000000000000+ 010000023>'], |
|
93
|
|
|
['000000', '123', '01-4567-0', Money::CHF(145), '0100000001453>000000000000000000000001236+ 010045670>'], |
|
94
|
|
|
]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function testGetEncodingLineMustThrowIfTooLongReference(): void |
|
98
|
|
|
{ |
|
99
|
|
|
$this->expectExceptionMessage('Invalid custom ID. It must be 20 or less digits, but got: `000000000000000000000000000`'); |
|
100
|
|
|
Bvr::getEncodingLine('123456', str_repeat('0', 27), '01-4567-0'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function testGetEncodingLineMustThrowIfInvalidReference(): void |
|
104
|
|
|
{ |
|
105
|
|
|
$this->expectExceptionMessage('Invalid custom ID. It must be 20 or less digits, but got: `0.0`'); |
|
106
|
|
|
Bvr::getEncodingLine('123456', '0.0', '01-4567-0'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function testGetEncodingLineMustThrowIfInvalidPostAccount(): void |
|
110
|
|
|
{ |
|
111
|
|
|
$this->expectExceptionMessage('Invalid post account number'); |
|
112
|
|
|
Bvr::getEncodingLine('123456', '0', '0145670'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function testGetEncodingLineMustThrowIfTooLongPostAccount(): void |
|
116
|
|
|
{ |
|
117
|
|
|
$this->expectExceptionMessage('The post account number is too long'); |
|
118
|
|
|
Bvr::getEncodingLine('123456', '0', '0123-456789-0'); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function testGetEncodingLineMustThrowIfInvalidAmount(): void |
|
122
|
|
|
{ |
|
123
|
|
|
$this->expectExceptionMessage('Invalid amount. Must be positive, but got: `-100`'); |
|
124
|
|
|
Bvr::getEncodingLine('123456', '0', '01-4567-0', Money::CHF(-100)); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @dataProvider providerExtractCustomId |
|
129
|
|
|
*/ |
|
130
|
|
|
public function testExtractCustomId(string $referenceNumber, string $expected): void |
|
131
|
|
|
{ |
|
132
|
|
|
$actual = Bvr::extractCustomId($referenceNumber); |
|
133
|
|
|
self::assertSame($expected, $actual); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function providerExtractCustomId(): array |
|
137
|
|
|
{ |
|
138
|
|
|
return [ |
|
139
|
|
|
['800826000000000000000002016', '00000000000000000201'], |
|
140
|
|
|
['000000000000000000000000000', '00000000000000000000'], |
|
141
|
|
|
['000000000000000000000001236', '00000000000000000123'], |
|
142
|
|
|
]; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function testExtractCustomIdMustThrowIfInvalidReferenceNumber(): void |
|
146
|
|
|
{ |
|
147
|
|
|
$this->expectExceptionMessage('Invalid reference number. It must be exactly 27 digits, but got: `foo`'); |
|
148
|
|
|
Bvr::extractCustomId('foo'); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function testExtractCustomIdMustThrowIfInvalidVerificationDigit(): void |
|
152
|
|
|
{ |
|
153
|
|
|
$this->expectExceptionMessage('Invalid reference number. The verification digit does not match. Expected `0`, but got `6`'); |
|
154
|
|
|
Bvr::extractCustomId('800826000000000000000002010'); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function providerIban(): array |
|
158
|
|
|
{ |
|
159
|
|
|
return [ |
|
160
|
|
|
['30-12465-5', false], |
|
161
|
|
|
['', false], |
|
162
|
|
|
['CH2208390037471510005', false], |
|
163
|
|
|
['CH7030123036078110002', true], |
|
164
|
|
|
]; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @dataProvider providerIban |
|
169
|
|
|
*/ |
|
170
|
|
|
public function testQrIban(string $iban, bool $expected): void |
|
171
|
|
|
{ |
|
172
|
|
|
$actual = Bvr::isQrIban($iban); |
|
173
|
|
|
self::assertSame($expected, $actual); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|