|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* This file is part of the Numverify API Client for PHP. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) 2024 Eric Sizemore <[email protected]> |
|
9
|
|
|
* (c) 2018-2021 Mark Rogoyski <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* This source file is subject to the MIT license. For the full copyright, |
|
12
|
|
|
* license information, and credits/acknowledgements, please view the LICENSE |
|
13
|
|
|
* and README files that were distributed with this source code. |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Numverify\Tests\Country; |
|
17
|
|
|
|
|
18
|
|
|
use Iterator; |
|
19
|
|
|
use LogicException; |
|
20
|
|
|
use Numverify\Country\Collection; |
|
21
|
|
|
use Numverify\Country\Country; |
|
22
|
|
|
use PHPUnit\Framework\Attributes\CoversClass; |
|
23
|
|
|
use PHPUnit\Framework\Attributes\DataProvider; |
|
24
|
|
|
use PHPUnit\Framework\Attributes\TestDox; |
|
25
|
|
|
use PHPUnit\Framework\Attributes\UsesClass; |
|
26
|
|
|
use PHPUnit\Framework\TestCase; |
|
27
|
|
|
use stdClass; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @internal |
|
31
|
|
|
*/ |
|
32
|
|
|
#[CoversClass(Collection::class)] |
|
33
|
|
|
#[UsesClass(Country::class)] |
|
34
|
|
|
class CollectionTest extends TestCase |
|
35
|
|
|
{ |
|
36
|
|
|
private Country $countryGb; |
|
37
|
|
|
|
|
38
|
|
|
private Country $countryJp; |
|
39
|
|
|
private Country $countryUs; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Set up countries. |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function setUp(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$this->countryUs = new Country('US', 'United States', '+1'); |
|
47
|
|
|
$this->countryGb = new Country('GB', 'United Kingdom', '+44'); |
|
48
|
|
|
$this->countryJp = new Country('JP', 'Japan', '+81'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param Country[] $countries |
|
53
|
|
|
*/ |
|
54
|
|
|
#[DataProvider('dataProviderForCountryCounts')] |
|
55
|
|
|
#[TestDox('Collection uses the Countable interface to implement count. Using $countries results in $expectedCount.')] |
|
56
|
|
|
public function testCount(array $countries, int $expectedCount): void |
|
57
|
|
|
{ |
|
58
|
|
|
$collection = new Collection(...$countries); |
|
59
|
|
|
|
|
60
|
|
|
self::assertSame($expectedCount, $collection->count()); |
|
61
|
|
|
self::assertCount($expectedCount, $collection); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
#[TestDox('Collection is able to find countries by country code.')] |
|
65
|
|
|
public function testFindByCountryCode(): void |
|
66
|
|
|
{ |
|
67
|
|
|
$collection = new Collection(...[$this->countryUs, $this->countryGb, $this->countryJp]); |
|
68
|
|
|
|
|
69
|
|
|
$countryUs = $collection->findByCountryCode($this->countryUs->getCountryCode()); |
|
70
|
|
|
$countryGb = $collection->findByCountryCode($this->countryGb->getCountryCode()); |
|
71
|
|
|
$countryJp = $collection->findByCountryCode($this->countryJp->getCountryCode()); |
|
72
|
|
|
|
|
73
|
|
|
self::assertSame($this->countryUs, $countryUs); |
|
74
|
|
|
self::assertSame($this->countryGb, $countryGb); |
|
75
|
|
|
self::assertSame($this->countryJp, $countryJp); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
#[TestDox('Collection is able to find countries by country name.')] |
|
79
|
|
|
public function testFindByCountryName(): void |
|
80
|
|
|
{ |
|
81
|
|
|
$collection = new Collection(...[$this->countryUs, $this->countryGb, $this->countryJp]); |
|
82
|
|
|
|
|
83
|
|
|
$countryUs = $collection->findByCountryName($this->countryUs->getCountryName()); |
|
84
|
|
|
$countryGb = $collection->findByCountryName($this->countryGb->getCountryName()); |
|
85
|
|
|
$countryJp = $collection->findByCountryName($this->countryJp->getCountryName()); |
|
86
|
|
|
|
|
87
|
|
|
self::assertSame($this->countryUs, $countryUs); |
|
88
|
|
|
self::assertSame($this->countryGb, $countryGb); |
|
89
|
|
|
self::assertSame($this->countryJp, $countryJp); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @psalm-suppress NoValue |
|
94
|
|
|
*/ |
|
95
|
|
|
#[TestDox('Collection iteration failure if manually manipulating the iterator (no elements).')] |
|
96
|
|
|
public function testIterationCurrentFailureNoElements(): void |
|
97
|
|
|
{ |
|
98
|
|
|
$collection = new Collection(...[]); |
|
99
|
|
|
|
|
100
|
|
|
$this->expectException(LogicException::class); |
|
101
|
|
|
$collection->current(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
#[TestDox('Collection iteration failure if manually manipulating the iterator (with elements).')] |
|
105
|
|
|
public function testIterationCurrentFailureWithElements(): void |
|
106
|
|
|
{ |
|
107
|
|
|
$collection = new Collection(...[$this->countryUs]); |
|
108
|
|
|
$collection->next(); |
|
109
|
|
|
|
|
110
|
|
|
$this->expectException(LogicException::class); |
|
111
|
|
|
$collection->current(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
#[TestDox('Collection can be iterated due to implementing the Iterator interface.')] |
|
115
|
|
|
public function testIterator(): void |
|
116
|
|
|
{ |
|
117
|
|
|
$collection = new Collection(...[$this->countryUs, $this->countryGb, $this->countryJp]); |
|
118
|
|
|
$expectedCountries = ['US' => false, 'GB' => false, 'JP' => false]; |
|
119
|
|
|
|
|
120
|
|
|
foreach ($collection as $countryCode => $country) { |
|
121
|
|
|
/** @var string $countryCode */ |
|
122
|
|
|
$expectedCountries[$countryCode] = true; |
|
123
|
|
|
self::assertInstanceOf(Country::class, $country); // @phpstan-ignore-line |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
foreach ($expectedCountries as $expectedCountry) { |
|
127
|
|
|
self::assertTrue($expectedCountry); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
#[TestDox('Collection uses the JsonSerialize interface to return country information as an array.')] |
|
132
|
|
|
public function testJsonSerialize(): void |
|
133
|
|
|
{ |
|
134
|
|
|
$collection = new Collection(...[$this->countryUs, $this->countryGb, $this->countryJp]); |
|
135
|
|
|
|
|
136
|
|
|
/** @var non-empty-string $json */ |
|
137
|
|
|
$json = json_encode($collection); |
|
138
|
|
|
|
|
139
|
|
|
/** @var stdClass $object */ |
|
140
|
|
|
$object = json_decode($json); |
|
141
|
|
|
self::assertObjectHasProperty('US', $object); |
|
142
|
|
|
self::assertObjectHasProperty('GB', $object); |
|
143
|
|
|
self::assertObjectHasProperty('JP', $object); |
|
144
|
|
|
|
|
145
|
|
|
/** @var stdClass $countryUs */ |
|
146
|
|
|
$countryUs = $object->US; |
|
147
|
|
|
/** @var stdClass $countryGb */ |
|
148
|
|
|
$countryGb = $object->GB; |
|
149
|
|
|
/** @var stdClass $countryJp */ |
|
150
|
|
|
$countryJp = $object->JP; |
|
151
|
|
|
|
|
152
|
|
|
self::assertSame('US', $countryUs->countryCode); |
|
153
|
|
|
self::assertSame('GB', $countryGb->countryCode); |
|
154
|
|
|
self::assertSame('JP', $countryJp->countryCode); |
|
155
|
|
|
self::assertSame('United States', $countryUs->countryName); |
|
156
|
|
|
self::assertSame('United Kingdom', $countryGb->countryName); |
|
157
|
|
|
self::assertSame('Japan', $countryJp->countryName); |
|
158
|
|
|
self::assertSame('+1', $countryUs->diallingCode); |
|
159
|
|
|
self::assertSame('+44', $countryGb->diallingCode); |
|
160
|
|
|
self::assertSame('+81', $countryJp->diallingCode); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @psalm-suppress PossiblyUnusedMethod |
|
165
|
|
|
*/ |
|
166
|
|
|
public static function dataProviderForCountryCounts(): Iterator |
|
167
|
|
|
{ |
|
168
|
|
|
yield 'zero' => [ |
|
169
|
|
|
[], |
|
170
|
|
|
0, |
|
171
|
|
|
]; |
|
172
|
|
|
yield 'one' => [ |
|
173
|
|
|
[new Country('US', 'United States', '+1')], |
|
174
|
|
|
1, |
|
175
|
|
|
]; |
|
176
|
|
|
yield 'two' => [ |
|
177
|
|
|
[ |
|
178
|
|
|
new Country('US', 'United States', '+1'), |
|
179
|
|
|
new Country('GB', 'United Kingdom', '+44'), |
|
180
|
|
|
], |
|
181
|
|
|
2, |
|
182
|
|
|
]; |
|
183
|
|
|
yield 'three' => [ |
|
184
|
|
|
[ |
|
185
|
|
|
new Country('US', 'United States', '+1'), |
|
186
|
|
|
new Country('GB', 'United Kingdom', '+44'), |
|
187
|
|
|
new Country('JP', 'Japan', '+81'), |
|
188
|
|
|
], |
|
189
|
|
|
3, |
|
190
|
|
|
]; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|