forked from ivmos/iso3166
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ISO3166Test.php
249 lines (221 loc) · 6.56 KB
/
ISO3166Test.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
/*
* (c) Rob Bast <rob.bast@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Alcohol\ISO3166;
class ISO3166Test extends \PHPUnit_Framework_TestCase
{
/**
* @testdox Calling getByAlpha2 with an invalid alpha2 throws a DomainException.
* @dataProvider invalidAlpha2Provider
* @expectedException \DomainException
* @expectedExceptionMessageRegExp /^Not a valid alpha2: .*$/
*
* @param string $alpha2
*/
public function testGetByAlpha2Invalid($alpha2)
{
$iso3166 = new ISO3166();
$iso3166->getByAlpha2($alpha2);
}
/**
* @testdox Calling getByAlpha2 with an unknown alpha2 throws a OutOfBoundsException.
* @expectedException \OutOfBoundsException
* @expectedExceptionMessage ISO 3166-1 does not contain: ZZ
*/
public function testGetByAlpha2Unknown()
{
$iso3166 = new ISO3166();
$iso3166->getByAlpha2('ZZ');
}
/**
* @testdox Calling getByAlpha2 with a known alpha2 returns an associative array with the data.
* @dataProvider alpha2Provider
*
* @param string $alpha2
* @param array $expected
*/
public function testGetByAlpha2($alpha2, array $expected)
{
$iso3166 = new ISO3166();
$this->assertEquals($expected, $iso3166->getByAlpha2($alpha2));
}
/**
* @testdox Calling getByAlpha3 with an invalid alpha3 throws a DomainException.
* @dataProvider invalidAlpha3Provider
* @expectedException \DomainException
* @expectedExceptionMessageRegExp /^Not a valid alpha3: .*$/
*
* @param string $alpha3
*/
public function testGetByAlpha3Invalid($alpha3)
{
$iso3166 = new ISO3166();
$iso3166->getByAlpha3($alpha3);
}
/**
* @testdox Calling getByAlpha3 with an unknown alpha3 throws a OutOfBoundsException.
* @expectedException \OutOfBoundsException
* @expectedExceptionMessage ISO 3166-1 does not contain: ZZZ
*/
public function testGetByAlpha3Unknown()
{
$iso3166 = new ISO3166();
$iso3166->getByAlpha3('ZZZ');
}
/**
* @testdox Calling getByAlpha3 with a known alpha3 returns an associative array with the data.
* @dataProvider alpha3Provider
*
* @param string $alpha3
* @param array $expected
*/
public function testGetByAlpha3($alpha3, array $expected)
{
$iso3166 = new ISO3166();
$this->assertEquals($expected, $iso3166->getByAlpha3($alpha3));
}
/**
* @testdox Calling getByNumeric with an invalid numeric throws a DomainException.
* @dataProvider invalidNumericProvider
* @expectedException \DomainException
* @expectedExceptionMessageRegExp /^Not a valid numeric: .*$/
*
* @param string $numeric
*/
public function testGetByNumericInvalid($numeric)
{
$iso3166 = new ISO3166();
$iso3166->getByNumeric($numeric);
}
/**
* @testdox Calling getByNumeric with an unknown numeric throws a OutOfBoundsException.
* @expectedException \OutOfBoundsException
* @expectedExceptionMessage ISO 3166-1 does not contain: 000
*/
public function testGetByNumericUnknown()
{
$iso3166 = new ISO3166();
$iso3166->getByNumeric('000');
}
/**
* @testdox Calling getByNumeric with a known numeric returns an associative array with the data.
* @dataProvider numericProvider
*
* @param string $numeric
* @param array $expected
*/
public function testGetByNumeric($numeric, $expected)
{
$iso3166 = new ISO3166();
$this->assertEquals($expected, $iso3166->getByNumeric($numeric));
}
/**
* @testdox Calling getAll returns an array with all elements.
*/
public function testGetAll()
{
$iso3166 = new ISO3166();
$this->assertInternalType('array', $iso3166->getAll());
$this->assertCount(249, $iso3166->getAll());
}
/**
* @testdox Iterating over $instance should behave as expected.
*/
public function testIterator()
{
$iso3166 = new ISO3166();
$i = 0;
foreach ($iso3166 as $key => $value) {
++$i;
}
$this->assertEquals(count($iso3166->getAll()), $i, 'Compare iterated count to count(getAll()).');
}
/**
* @testdox Iterating over $instance->listBy() should behave as expected.
*/
public function testListBy()
{
$iso3166 = new ISO3166();
try {
foreach ($iso3166->listBy('foo') as $key => $value) {
// void
}
} catch (\Exception $e) {
$this->assertInstanceOf('DomainException', $e);
$this->assertRegExp('{Invalid value for \$indexBy, got "\w++", expected one of:(?: \w++,?)+}', $e->getMessage());
} finally {
$this->assertTrue(isset($e));
}
$i = 0;
foreach ($iso3166->listBy(ISO3166::KEY_ALPHA3) as $key => $value) {
++$i;
}
$this->assertEquals(count($iso3166->getAll()), $i, 'Compare iterated count to count(getAll()).');
}
/**
* @return array
*/
public function invalidAlpha2Provider()
{
return [['Z'], ['ZZZ'], [1], [123]];
}
/**
* @return array
*/
public function alpha2Provider()
{
return $this->getCountries('alpha2');
}
/**
* @return array
*/
public function invalidAlpha3Provider()
{
return [['ZZ'], ['ZZZZ'], [12], [1234]];
}
/**
* @return array
*/
public function alpha3Provider()
{
return $this->getCountries('alpha3');
}
/**
* @return array
*/
public function invalidNumericProvider()
{
return [['00'], ['0000'], ['ZZ'], ['ZZZZ']];
}
/**
* @return array
*/
public function numericProvider()
{
return $this->getCountries('numeric');
}
/**
* @param string $indexedBy
*
* @return array
*/
private function getCountries($indexedBy)
{
$reflected = new \ReflectionClass('Alcohol\ISO3166\ISO3166');
$countries = $reflected->getProperty('countries');
$countries->setAccessible(true);
$countries = $countries->getValue(new ISO3166());
return array_reduce(
$countries,
function (array $carry, array $country) use ($indexedBy) {
$carry[] = [$country[$indexedBy], $country];
return $carry;
},
[]
);
}
}