-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
ISO4217Test.php
151 lines (129 loc) · 4.17 KB
/
ISO4217Test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
declare(strict_types=1);
/*
* (c) Rob Bast <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Alcohol;
use PHPUnit\Framework\TestCase;
class ISO4217Test extends TestCase
{
/**
* @testdox Calling getByAlpha3 with an invalid alpha3 throws a DomainException.
*
* @dataProvider invalidAlpha3Provider
*/
public function testGetByAlpha3Invalid($alpha3, $expectException): void
{
$this->expectException($expectException);
$iso4217 = new ISO4217();
$iso4217->getByAlpha3($alpha3);
}
/**
* @testdox Calling getByAlpha3 with an unknown alpha3 throws a OutOfBoundsException.
*/
public function testGetByAlpha3Unknown(): void
{
$this->expectException(\OutOfBoundsException::class);
$this->expectExceptionMessage('ISO 4217 does not contain: ZZZ');
$iso4217 = new ISO4217();
$iso4217->getByAlpha3('ZZZ');
}
/**
* @testdox Calling getByAlpha3 with a known alpha3 returns an associative array with the data.
*
* @dataProvider alpha3Provider
*/
public function testGetByAlpha3(string $alpha3, array $expected): void
{
$iso4217 = new ISO4217();
$this->assertEquals($expected, $iso4217->getByAlpha3($alpha3));
}
/**
* @testdox Calling getByNumeric with an invalid numeric throws a DomainException.
*
* @dataProvider invalidNumericProvider
*/
public function testGetByNumericInvalid($numeric, $expectException): void
{
$this->expectException($expectException);
$iso4217 = new ISO4217();
$iso4217->getByNumeric($numeric);
}
/**
* @testdox Calling getByNumeric with an unknown numeric throws a OutOfBoundsException.
*/
public function testGetByNumericUnknown(): void
{
$this->expectException(\OutOfBoundsException::class);
$this->expectExceptionMessage('ISO 4217 does not contain: 000');
$iso4217 = new ISO4217();
$iso4217->getByNumeric('000');
}
/**
* @testdox Calling getByNumeric with a known numeric returns an associative array with the data.
*
* @dataProvider numericProvider
*/
public function testGetByNumeric(string $numeric, array $expected): void
{
$iso4217 = new ISO4217();
$this->assertEquals($expected, $iso4217->getByNumeric($numeric));
}
/**
* @testdox Calling getAll returns an array with all elements.
*/
public function testGetAll(): void
{
$iso4217 = new ISO4217();
$this->assertIsArray($iso4217->getAll());
$this->assertCount(158, $iso4217->getAll());
}
public function invalidAlpha3Provider(): array
{
return [
['00', \DomainException::class],
['0000', \DomainException::class],
['ZZ', \DomainException::class],
['ZZZZ', \DomainException::class],
[12, \TypeError::class],
[1234, \TypeError::class],
];
}
public function alpha3Provider(): array
{
return $this->getCurrencies('alpha3');
}
public function invalidNumericProvider(): array
{
return [
['00', \DomainException::class],
['0000', \DomainException::class],
['ZZ', \DomainException::class],
['ZZZZ', \DomainException::class],
[12, \TypeError::class],
[1234, \TypeError::class],
];
}
public function numericProvider(): array
{
return $this->getCurrencies('numeric');
}
private function getCurrencies(string $indexedBy): array
{
$reflected = new \ReflectionClass('Alcohol\ISO4217');
$currencies = $reflected->getProperty('currencies');
$currencies->setAccessible(true);
$currencies = $currencies->getValue(new ISO4217());
return array_reduce(
$currencies,
static function (array $carry, array $currency) use ($indexedBy) {
$carry[] = [$currency[$indexedBy], $currency];
return $carry;
},
[]
);
}
}