Skip to content

Commit ab924b2

Browse files
committed
updated test to accommodate MMDB data change
1 parent fd28fe7 commit ab924b2

9 files changed

+431
-197
lines changed

test/e2e/RedisAuthTest.php

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,53 @@ private static function getTestData() {
2929
return json_decode(file_get_contents(__DIR__ . '/../ip-test-data.json'), true);
3030
}
3131

32+
private function assertIpDetails($data) {
33+
$this->assertArrayHasKey('country', $data);
34+
$this->assertArrayHasKey('isEU', $data);
35+
$this->assertArrayHasKey('city', $data);
36+
$this->assertArrayHasKey('postal', $data);
37+
$this->assertArrayHasKey('div', $data);
38+
$this->assertArrayHasKey('divIso', $data);
39+
$this->assertArrayHasKey('accuracy', $data);
40+
$this->assertArrayHasKey('lat', $data);
41+
$this->assertArrayHasKey('long', $data);
42+
$this->assertArrayHasKey('timezone', $data);
43+
44+
$this->assertStringMatchesFormat('%s', $data['country']);
45+
$this->assertIsBool($data['isEU']);
46+
$this->assertStringMatchesFormat('%s', $data['city']);
47+
$this->assertStringMatchesFormat('%S', $data['postal']);
48+
$this->assertStringMatchesFormat('%s', $data['div']);
49+
$this->assertStringMatchesFormat('%s', $data['divIso']);
50+
$this->assertIsNumeric($data['accuracy']);
51+
$this->assertIsFloat($data['lat']);
52+
$this->assertIsFloat($data['long']);
53+
$this->assertMatchesRegularExpression('/[a-zA-Z]+\/[a-zA-Z]+/', $data['timezone']);
54+
}
55+
56+
private function assertIpData($input, $expected) {
57+
if (array_key_exists('status', $expected)) {
58+
$this->assertArrayHasKey('status', $input);
59+
$this->assertArrayHasKey('data', $input);
60+
$this->assertArrayHasKey('message', $input);
61+
62+
$this->assertEquals($expected['status'], $input['status']);
63+
$this->assertIsArray($input['data']);
64+
$this->assertEquals($expected['message'], $input['message']);
65+
66+
if (array_key_exists('country', $expected['data'])) {
67+
$data = $input['data'];
68+
$this->assertIpDetails($data);
69+
} else {
70+
foreach ($input['data'] as $ip => $data) {
71+
$this->assertIpDetails($data);
72+
}
73+
}
74+
} else {
75+
$this->assertIpDetails($input);
76+
}
77+
}
78+
3279
public static function setUpBeforeClass(): void {
3380
static::$redis = new \Redis();
3481
static::$redis->connect('tcp://127.0.0.1', 6379, 0, null, 100, 10);
@@ -181,10 +228,7 @@ public function testPingResponseIsValid(): void {
181228
* @dataProvider ipDataProvider
182229
*/
183230
public function testCanConnectAndAnalyzeIp($input, array $expected): void {
184-
$this->assertSame(
185-
$expected,
186-
self::connect($input)
187-
);
231+
$this->assertIpData(self::connect($input), $expected);
188232
}
189233

190234
public function ipDataProvider(): array {
@@ -205,10 +249,7 @@ public function ipDataProvider(): array {
205249
* @dataProvider cacheDataProvider
206250
*/
207251
public function testCanCacheAndExpireResult($input, array $expected): void {
208-
$this->assertSame(
209-
$expected,
210-
static::$redis->get("result:{$input}")
211-
);
252+
$this->assertIpData(static::$redis->get("result:{$input}"), $expected);
212253

213254
$this->assertLessThanOrEqual(
214255
3600,
@@ -234,10 +275,7 @@ public function cacheDataProvider(): array {
234275
* @dataProvider ipListDataProvider
235276
*/
236277
public function testCanConnectAndAnalyzeIpList($input, array $expected): void {
237-
$this->assertSame(
238-
$expected,
239-
self::connect($input)
240-
);
278+
$this->assertIpData(self::connect($input), $expected);
241279
}
242280

243281
public function ipListDataProvider(): array {

test/e2e/RedisBasicTest.php

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,53 @@ private static function getTestData() {
2929
return json_decode(file_get_contents(__DIR__ . '/../ip-test-data.json'), true);
3030
}
3131

32+
private function assertIpDetails($data) {
33+
$this->assertArrayHasKey('country', $data);
34+
$this->assertArrayHasKey('isEU', $data);
35+
$this->assertArrayHasKey('city', $data);
36+
$this->assertArrayHasKey('postal', $data);
37+
$this->assertArrayHasKey('div', $data);
38+
$this->assertArrayHasKey('divIso', $data);
39+
$this->assertArrayHasKey('accuracy', $data);
40+
$this->assertArrayHasKey('lat', $data);
41+
$this->assertArrayHasKey('long', $data);
42+
$this->assertArrayHasKey('timezone', $data);
43+
44+
$this->assertStringMatchesFormat('%s', $data['country']);
45+
$this->assertIsBool($data['isEU']);
46+
$this->assertStringMatchesFormat('%s', $data['city']);
47+
$this->assertStringMatchesFormat('%S', $data['postal']);
48+
$this->assertStringMatchesFormat('%s', $data['div']);
49+
$this->assertStringMatchesFormat('%s', $data['divIso']);
50+
$this->assertIsNumeric($data['accuracy']);
51+
$this->assertIsFloat($data['lat']);
52+
$this->assertIsFloat($data['long']);
53+
$this->assertMatchesRegularExpression('/[a-zA-Z]+\/[a-zA-Z]+/', $data['timezone']);
54+
}
55+
56+
private function assertIpData($input, $expected) {
57+
if (array_key_exists('status', $expected)) {
58+
$this->assertArrayHasKey('status', $input);
59+
$this->assertArrayHasKey('data', $input);
60+
$this->assertArrayHasKey('message', $input);
61+
62+
$this->assertEquals($expected['status'], $input['status']);
63+
$this->assertIsArray($input['data']);
64+
$this->assertEquals($expected['message'], $input['message']);
65+
66+
if (array_key_exists('country', $expected['data'])) {
67+
$data = $input['data'];
68+
$this->assertIpDetails($data);
69+
} else {
70+
foreach ($input['data'] as $ip => $data) {
71+
$this->assertIpDetails($data);
72+
}
73+
}
74+
} else {
75+
$this->assertIpDetails($input);
76+
}
77+
}
78+
3279
public static function setUpBeforeClass(): void {
3380
static::$redis = new \Redis();
3481
static::$redis->connect('tcp://127.0.0.1', 6379, 0, null, 100, 10);
@@ -132,10 +179,7 @@ public function testPingResponseIsValid(): void {
132179
* @dataProvider ipDataProvider
133180
*/
134181
public function testCanConnectAndAnalyzeIp($input, array $expected): void {
135-
$this->assertSame(
136-
$expected,
137-
self::connect($input)
138-
);
182+
$this->assertIpData(self::connect($input), $expected);
139183
}
140184

141185
public function ipDataProvider(): array {
@@ -156,10 +200,7 @@ public function ipDataProvider(): array {
156200
* @dataProvider cacheDataProvider
157201
*/
158202
public function testCanCacheAndExpireResult($input, array $expected): void {
159-
$this->assertSame(
160-
$expected,
161-
static::$redis->get("result:{$input}")
162-
);
203+
$this->assertIpData(static::$redis->get("result:{$input}"), $expected);
163204

164205
$this->assertLessThanOrEqual(
165206
3600,
@@ -185,10 +226,7 @@ public function cacheDataProvider(): array {
185226
* @dataProvider ipListDataProvider
186227
*/
187228
public function testCanConnectAndAnalyzeIpList($input, array $expected): void {
188-
$this->assertSame(
189-
$expected,
190-
self::connect($input)
191-
);
229+
$this->assertIpData(self::connect($input), $expected);
192230
}
193231

194232
public function ipListDataProvider(): array {

test/e2e/RedisListTest.php

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,53 @@ private static function getTestData() {
4141
return json_decode(file_get_contents(__DIR__ . '/../ip-test-data.json'), true);
4242
}
4343

44+
private function assertIpDetails($data) {
45+
$this->assertArrayHasKey('country', $data);
46+
$this->assertArrayHasKey('isEU', $data);
47+
$this->assertArrayHasKey('city', $data);
48+
$this->assertArrayHasKey('postal', $data);
49+
$this->assertArrayHasKey('div', $data);
50+
$this->assertArrayHasKey('divIso', $data);
51+
$this->assertArrayHasKey('accuracy', $data);
52+
$this->assertArrayHasKey('lat', $data);
53+
$this->assertArrayHasKey('long', $data);
54+
$this->assertArrayHasKey('timezone', $data);
55+
56+
$this->assertStringMatchesFormat('%s', $data['country']);
57+
$this->assertIsBool($data['isEU']);
58+
$this->assertStringMatchesFormat('%s', $data['city']);
59+
$this->assertStringMatchesFormat('%S', $data['postal']);
60+
$this->assertStringMatchesFormat('%s', $data['div']);
61+
$this->assertStringMatchesFormat('%s', $data['divIso']);
62+
$this->assertIsNumeric($data['accuracy']);
63+
$this->assertIsFloat($data['lat']);
64+
$this->assertIsFloat($data['long']);
65+
$this->assertMatchesRegularExpression('/[a-zA-Z]+\/[a-zA-Z]+/', $data['timezone']);
66+
}
67+
68+
private function assertIpData($input, $expected) {
69+
if (array_key_exists('status', $expected)) {
70+
$this->assertArrayHasKey('status', $input);
71+
$this->assertArrayHasKey('data', $input);
72+
$this->assertArrayHasKey('message', $input);
73+
74+
$this->assertEquals($expected['status'], $input['status']);
75+
$this->assertIsArray($input['data']);
76+
$this->assertEquals($expected['message'], $input['message']);
77+
78+
if (array_key_exists('country', $expected['data'])) {
79+
$data = $input['data'];
80+
$this->assertIpDetails($data);
81+
} else {
82+
foreach ($input['data'] as $ip => $data) {
83+
$this->assertIpDetails($data);
84+
}
85+
}
86+
} else {
87+
$this->assertIpDetails($input);
88+
}
89+
}
90+
4491
public static function setUpBeforeClass(): void {
4592
static::$redis = new \Redis();
4693
static::$redis->connect('tcp://127.0.0.1', 6379, 0, null, 100, 10);
@@ -144,10 +191,7 @@ public function testPingResponseIsValid(): void {
144191
* @dataProvider ipDataProvider
145192
*/
146193
public function testCanConnectAndAnalyzeIp($input, array $expected): void {
147-
$this->assertSame(
148-
$expected,
149-
self::connect($input)
150-
);
194+
$this->assertIpData(self::connect($input), $expected);
151195
}
152196

153197
public function ipDataProvider(): array {
@@ -168,10 +212,7 @@ public function ipDataProvider(): array {
168212
* @dataProvider cacheDataProvider
169213
*/
170214
public function testCanCacheResult($input, array $expected): void {
171-
$this->assertSame(
172-
$expected,
173-
static::$redis->get("result:{$input}")
174-
);
215+
$this->assertIpData(static::$redis->get("result:{$input}"), $expected);
175216
}
176217

177218
public function cacheDataProvider(): array {
@@ -192,10 +233,7 @@ public function cacheDataProvider(): array {
192233
* @dataProvider ipListDataProvider
193234
*/
194235
public function testCanConnectAndAnalyzeIpList($input, array $expected): void {
195-
$this->assertSame(
196-
$expected,
197-
self::connect($input)
198-
);
236+
$this->assertIpData(self::connect($input), $expected);
199237
}
200238

201239
public function ipListDataProvider(): array {

test/e2e/ServiceAuthSslTest.php

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,49 @@ private static function getTestData() {
1818
return json_decode(file_get_contents(__DIR__ . '/../ip-test-data.json'), true);
1919
}
2020

21+
private function assertIpDetails($data) {
22+
$this->assertArrayHasKey('country', $data);
23+
$this->assertArrayHasKey('isEU', $data);
24+
$this->assertArrayHasKey('city', $data);
25+
$this->assertArrayHasKey('postal', $data);
26+
$this->assertArrayHasKey('div', $data);
27+
$this->assertArrayHasKey('divIso', $data);
28+
$this->assertArrayHasKey('accuracy', $data);
29+
$this->assertArrayHasKey('lat', $data);
30+
$this->assertArrayHasKey('long', $data);
31+
$this->assertArrayHasKey('timezone', $data);
32+
33+
$this->assertStringMatchesFormat('%s', $data['country']);
34+
$this->assertIsBool($data['isEU']);
35+
$this->assertStringMatchesFormat('%s', $data['city']);
36+
$this->assertStringMatchesFormat('%S', $data['postal']);
37+
$this->assertStringMatchesFormat('%s', $data['div']);
38+
$this->assertStringMatchesFormat('%s', $data['divIso']);
39+
$this->assertIsNumeric($data['accuracy']);
40+
$this->assertIsFloat($data['lat']);
41+
$this->assertIsFloat($data['long']);
42+
$this->assertMatchesRegularExpression('/[a-zA-Z]+\/[a-zA-Z]+/', $data['timezone']);
43+
}
44+
45+
private function assertIpData($input, $expected) {
46+
$this->assertArrayHasKey('status', $input);
47+
$this->assertArrayHasKey('data', $input);
48+
$this->assertArrayHasKey('message', $input);
49+
50+
$this->assertEquals($expected['status'], $input['status']);
51+
$this->assertIsArray($input['data']);
52+
$this->assertEquals($expected['message'], $input['message']);
53+
54+
if (array_key_exists('country', $expected['data'])) {
55+
$data = $input['data'];
56+
$this->assertIpDetails($data);
57+
} else {
58+
foreach ($input['data'] as $ip => $data) {
59+
$this->assertIpDetails($data);
60+
}
61+
}
62+
}
63+
2164
public static function setUpBeforeClass(): void {
2265
// TODO
2366
}
@@ -170,10 +213,7 @@ public function testPingResponseIsValid(): void {
170213
* @dataProvider ipDataProvider
171214
*/
172215
public function testCanConnectAndAnalyzeIp($input, array $expected): void {
173-
$this->assertSame(
174-
$expected,
175-
self::connect($input)
176-
);
216+
$this->assertIpData(self::connect($input), $expected);
177217
}
178218

179219
public function ipDataProvider(): array {
@@ -194,10 +234,7 @@ public function ipDataProvider(): array {
194234
* @dataProvider ipListDataProvider
195235
*/
196236
public function testCanConnectAndAnalyzeIpList($input, array $expected): void {
197-
$this->assertSame(
198-
$expected,
199-
self::connect($input)
200-
);
237+
$this->assertIpData(self::connect($input), $expected);
201238
}
202239

203240
public function ipListDataProvider(): array {

0 commit comments

Comments
 (0)