|
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\PhoneNumber; |
|
17
|
|
|
|
|
18
|
|
|
use Iterator; |
|
19
|
|
|
use Numverify\Exception\NumverifyApiResponseException; |
|
20
|
|
|
use Numverify\PhoneNumber\ValidPhoneNumber; |
|
21
|
|
|
use PHPUnit\Framework\Attributes\CoversClass; |
|
22
|
|
|
use PHPUnit\Framework\Attributes\DataProvider; |
|
23
|
|
|
use PHPUnit\Framework\Attributes\TestDox; |
|
24
|
|
|
use PHPUnit\Framework\TestCase; |
|
25
|
|
|
use stdClass; |
|
26
|
|
|
|
|
27
|
|
|
use function json_decode; |
|
28
|
|
|
use function json_encode; |
|
29
|
|
|
use function print_r; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @internal |
|
33
|
|
|
*/ |
|
34
|
|
|
#[CoversClass(ValidPhoneNumber::class)] |
|
35
|
|
|
class ValidPhoneNumberTest extends TestCase |
|
36
|
|
|
{ |
|
37
|
|
|
private const CARRIER = 'AT&T Mobility LLC'; |
|
38
|
|
|
|
|
39
|
|
|
private const COUNTRY_CODE = 'US'; |
|
40
|
|
|
|
|
41
|
|
|
private const COUNTRY_NAME = 'United States of America'; |
|
42
|
|
|
|
|
43
|
|
|
private const COUNTRY_PREFIX = '+1'; |
|
44
|
|
|
|
|
45
|
|
|
private const INTERNATIONAL_FORMAT = '+14158586273'; |
|
46
|
|
|
|
|
47
|
|
|
private const LINE_TYPE = 'mobile'; |
|
48
|
|
|
|
|
49
|
|
|
private const LOCAL_FORMAT = '4158586273'; |
|
50
|
|
|
|
|
51
|
|
|
private const LOCATION = 'Novato'; |
|
52
|
|
|
|
|
53
|
|
|
private const NUMBER = '14158586273'; |
|
54
|
|
|
private const VALID = true; |
|
55
|
|
|
|
|
56
|
|
|
private stdClass $validatedPhoneNumberData; |
|
57
|
|
|
|
|
58
|
|
|
protected function setUp(): void |
|
59
|
|
|
{ |
|
60
|
|
|
$this->validatedPhoneNumberData = new stdClass(); |
|
61
|
|
|
$this->validatedPhoneNumberData->valid = self::VALID; |
|
62
|
|
|
$this->validatedPhoneNumberData->number = self::NUMBER; |
|
63
|
|
|
$this->validatedPhoneNumberData->local_format = self::LOCAL_FORMAT; |
|
64
|
|
|
$this->validatedPhoneNumberData->international_format = self::INTERNATIONAL_FORMAT; |
|
65
|
|
|
$this->validatedPhoneNumberData->country_prefix = self::COUNTRY_PREFIX; |
|
66
|
|
|
$this->validatedPhoneNumberData->country_code = self::COUNTRY_CODE; |
|
67
|
|
|
$this->validatedPhoneNumberData->country_name = self::COUNTRY_NAME; |
|
68
|
|
|
$this->validatedPhoneNumberData->location = self::LOCATION; |
|
69
|
|
|
$this->validatedPhoneNumberData->carrier = self::CARRIER; |
|
70
|
|
|
$this->validatedPhoneNumberData->line_type = self::LINE_TYPE; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
#[TestDox('ValidPhoneNumber sets __debugInfo for var_dump to return number data as array.')] |
|
74
|
|
|
public function testDebugInfo(): void |
|
75
|
|
|
{ |
|
76
|
|
|
$validPhoneNumber = new ValidPhoneNumber($this->validatedPhoneNumberData); |
|
77
|
|
|
|
|
78
|
|
|
$debugInfo = print_r($validPhoneNumber, true); |
|
79
|
|
|
|
|
80
|
|
|
self::assertStringContainsString('valid', $debugInfo); |
|
81
|
|
|
self::assertStringContainsString('number', $debugInfo); |
|
82
|
|
|
self::assertStringContainsString('localFormat', $debugInfo); |
|
83
|
|
|
self::assertStringContainsString('internationalFormat', $debugInfo); |
|
84
|
|
|
self::assertStringContainsString('countryPrefix', $debugInfo); |
|
85
|
|
|
self::assertStringContainsString('countryCode', $debugInfo); |
|
86
|
|
|
self::assertStringContainsString('countryName', $debugInfo); |
|
87
|
|
|
self::assertStringContainsString('location', $debugInfo); |
|
88
|
|
|
self::assertStringContainsString('carrier', $debugInfo); |
|
89
|
|
|
self::assertStringContainsString('lineType', $debugInfo); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
#[TestDox('ValidPhoneNumber \'getters\' returns appropriate data.')] |
|
93
|
|
|
public function testGetters(): void |
|
94
|
|
|
{ |
|
95
|
|
|
$validPhoneNumber = new ValidPhoneNumber($this->validatedPhoneNumberData); |
|
96
|
|
|
|
|
97
|
|
|
$number = $validPhoneNumber->getNumber(); |
|
98
|
|
|
$localFormat = $validPhoneNumber->getLocalFormat(); |
|
99
|
|
|
$internationalFormat = $validPhoneNumber->getInternationalFormat(); |
|
100
|
|
|
$countryPrefix = $validPhoneNumber->getCountryPrefix(); |
|
101
|
|
|
$countryCode = $validPhoneNumber->getCountryCode(); |
|
102
|
|
|
$countryName = $validPhoneNumber->getCountryName(); |
|
103
|
|
|
$location = $validPhoneNumber->getLocation(); |
|
104
|
|
|
$carrier = $validPhoneNumber->getCarrier(); |
|
105
|
|
|
$lineType = $validPhoneNumber->getLineType(); |
|
106
|
|
|
|
|
107
|
|
|
self::assertSame(self::NUMBER, $number); |
|
108
|
|
|
self::assertSame(self::LOCAL_FORMAT, $localFormat); |
|
109
|
|
|
self::assertSame(self::INTERNATIONAL_FORMAT, $internationalFormat); |
|
110
|
|
|
self::assertSame(self::COUNTRY_PREFIX, $countryPrefix); |
|
111
|
|
|
self::assertSame(self::COUNTRY_CODE, $countryCode); |
|
112
|
|
|
self::assertSame(self::COUNTRY_NAME, $countryName); |
|
113
|
|
|
self::assertSame(self::LOCATION, $location); |
|
114
|
|
|
self::assertSame(self::CARRIER, $carrier); |
|
115
|
|
|
self::assertSame(self::LINE_TYPE, $lineType); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
#[TestDox('ValidPhoneNumber returns true on isValid.')] |
|
119
|
|
|
public function testIsValid(): void |
|
120
|
|
|
{ |
|
121
|
|
|
$validPhoneNumber = new ValidPhoneNumber($this->validatedPhoneNumberData); |
|
122
|
|
|
|
|
123
|
|
|
$isValid = $validPhoneNumber->isValid(); |
|
124
|
|
|
self::assertTrue($isValid); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
#[TestDox('ValidPhoneNumber uses JsonSerializable interface to return number data as array.')] |
|
128
|
|
|
public function testJsonSerialize(): void |
|
129
|
|
|
{ |
|
130
|
|
|
$validPhoneNumber = new ValidPhoneNumber($this->validatedPhoneNumberData); |
|
131
|
|
|
|
|
132
|
|
|
/** @var non-empty-string $json */ |
|
133
|
|
|
$json = json_encode($validPhoneNumber); |
|
134
|
|
|
|
|
135
|
|
|
/** @var stdClass $object */ |
|
136
|
|
|
$object = json_decode($json); |
|
137
|
|
|
self::assertSame(self::VALID, $object->valid); |
|
138
|
|
|
self::assertSame(self::NUMBER, $object->number); |
|
139
|
|
|
self::assertSame(self::LOCAL_FORMAT, $object->localFormat); |
|
140
|
|
|
self::assertSame(self::INTERNATIONAL_FORMAT, $object->internationalFormat); |
|
141
|
|
|
self::assertSame(self::COUNTRY_PREFIX, $object->countryPrefix); |
|
142
|
|
|
self::assertSame(self::COUNTRY_CODE, $object->countryCode); |
|
143
|
|
|
self::assertSame(self::COUNTRY_NAME, $object->countryName); |
|
144
|
|
|
self::assertSame(self::LOCATION, $object->location); |
|
145
|
|
|
self::assertSame(self::CARRIER, $object->carrier); |
|
146
|
|
|
self::assertSame(self::LINE_TYPE, $object->lineType); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
#[DataProvider('dataProviderForFields')] |
|
150
|
|
|
#[TestDox('ValidPhoneNumber throws a NumverifyApiResponseException exception if missing data. Using field: $missingField')] |
|
151
|
|
|
public function testPhoneNumberDataValidation(string $missingField): void |
|
152
|
|
|
{ |
|
153
|
|
|
unset($this->validatedPhoneNumberData->$missingField); |
|
154
|
|
|
|
|
155
|
|
|
$this->expectException(NumverifyApiResponseException::class); |
|
156
|
|
|
new ValidPhoneNumber($this->validatedPhoneNumberData); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
#[TestDox('ValidPhoneNumber uses Stringable interface to return proper string representation of number data.')] |
|
160
|
|
|
public function testToString(): void |
|
161
|
|
|
{ |
|
162
|
|
|
$validPhoneNumber = new ValidPhoneNumber($this->validatedPhoneNumberData); |
|
163
|
|
|
|
|
164
|
|
|
$stringRepresentation = (string) $validPhoneNumber; |
|
165
|
|
|
self::assertSame(self::NUMBER, $stringRepresentation); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @psalm-suppress PossiblyUnusedMethod |
|
170
|
|
|
*/ |
|
171
|
|
|
public static function dataProviderForFields(): Iterator |
|
172
|
|
|
{ |
|
173
|
|
|
yield ['valid']; |
|
174
|
|
|
yield ['number']; |
|
175
|
|
|
yield ['local_format']; |
|
176
|
|
|
yield ['international_format']; |
|
177
|
|
|
yield ['country_prefix']; |
|
178
|
|
|
yield ['country_code']; |
|
179
|
|
|
yield ['country_name']; |
|
180
|
|
|
yield ['location']; |
|
181
|
|
|
yield ['carrier']; |
|
182
|
|
|
yield ['line_type']; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|