From ab924b2a17c52041046c0ab955df68ba0ce9b904 Mon Sep 17 00:00:00 2001 From: Eric Chow <88537011+ecmchow@users.noreply.github.com> Date: Fri, 2 Sep 2022 16:40:46 +0800 Subject: [PATCH] updated test to accommodate MMDB data change --- test/e2e/RedisAuthTest.php | 62 ++++++++--- test/e2e/RedisBasicTest.php | 62 ++++++++--- test/e2e/RedisListTest.php | 62 ++++++++--- test/e2e/ServiceAuthSslTest.php | 53 ++++++++-- test/e2e/ServiceAuthTest.php | 53 ++++++++-- test/e2e/ServiceBasicTest.php | 53 ++++++++-- test/e2e/ServiceIPSumTest.php | 53 ++++++++-- test/e2e/ServiceSslTest.php | 53 ++++++++-- test/unit/AnalyzerTest.php | 177 ++++++++++---------------------- 9 files changed, 431 insertions(+), 197 deletions(-) diff --git a/test/e2e/RedisAuthTest.php b/test/e2e/RedisAuthTest.php index 3fd7939..e142239 100644 --- a/test/e2e/RedisAuthTest.php +++ b/test/e2e/RedisAuthTest.php @@ -29,6 +29,53 @@ private static function getTestData() { return json_decode(file_get_contents(__DIR__ . '/../ip-test-data.json'), true); } + private function assertIpDetails($data) { + $this->assertArrayHasKey('country', $data); + $this->assertArrayHasKey('isEU', $data); + $this->assertArrayHasKey('city', $data); + $this->assertArrayHasKey('postal', $data); + $this->assertArrayHasKey('div', $data); + $this->assertArrayHasKey('divIso', $data); + $this->assertArrayHasKey('accuracy', $data); + $this->assertArrayHasKey('lat', $data); + $this->assertArrayHasKey('long', $data); + $this->assertArrayHasKey('timezone', $data); + + $this->assertStringMatchesFormat('%s', $data['country']); + $this->assertIsBool($data['isEU']); + $this->assertStringMatchesFormat('%s', $data['city']); + $this->assertStringMatchesFormat('%S', $data['postal']); + $this->assertStringMatchesFormat('%s', $data['div']); + $this->assertStringMatchesFormat('%s', $data['divIso']); + $this->assertIsNumeric($data['accuracy']); + $this->assertIsFloat($data['lat']); + $this->assertIsFloat($data['long']); + $this->assertMatchesRegularExpression('/[a-zA-Z]+\/[a-zA-Z]+/', $data['timezone']); + } + + private function assertIpData($input, $expected) { + if (array_key_exists('status', $expected)) { + $this->assertArrayHasKey('status', $input); + $this->assertArrayHasKey('data', $input); + $this->assertArrayHasKey('message', $input); + + $this->assertEquals($expected['status'], $input['status']); + $this->assertIsArray($input['data']); + $this->assertEquals($expected['message'], $input['message']); + + if (array_key_exists('country', $expected['data'])) { + $data = $input['data']; + $this->assertIpDetails($data); + } else { + foreach ($input['data'] as $ip => $data) { + $this->assertIpDetails($data); + } + } + } else { + $this->assertIpDetails($input); + } + } + public static function setUpBeforeClass(): void { static::$redis = new \Redis(); static::$redis->connect('tcp://127.0.0.1', 6379, 0, null, 100, 10); @@ -181,10 +228,7 @@ public function testPingResponseIsValid(): void { * @dataProvider ipDataProvider */ public function testCanConnectAndAnalyzeIp($input, array $expected): void { - $this->assertSame( - $expected, - self::connect($input) - ); + $this->assertIpData(self::connect($input), $expected); } public function ipDataProvider(): array { @@ -205,10 +249,7 @@ public function ipDataProvider(): array { * @dataProvider cacheDataProvider */ public function testCanCacheAndExpireResult($input, array $expected): void { - $this->assertSame( - $expected, - static::$redis->get("result:{$input}") - ); + $this->assertIpData(static::$redis->get("result:{$input}"), $expected); $this->assertLessThanOrEqual( 3600, @@ -234,10 +275,7 @@ public function cacheDataProvider(): array { * @dataProvider ipListDataProvider */ public function testCanConnectAndAnalyzeIpList($input, array $expected): void { - $this->assertSame( - $expected, - self::connect($input) - ); + $this->assertIpData(self::connect($input), $expected); } public function ipListDataProvider(): array { diff --git a/test/e2e/RedisBasicTest.php b/test/e2e/RedisBasicTest.php index d668383..54743ae 100644 --- a/test/e2e/RedisBasicTest.php +++ b/test/e2e/RedisBasicTest.php @@ -29,6 +29,53 @@ private static function getTestData() { return json_decode(file_get_contents(__DIR__ . '/../ip-test-data.json'), true); } + private function assertIpDetails($data) { + $this->assertArrayHasKey('country', $data); + $this->assertArrayHasKey('isEU', $data); + $this->assertArrayHasKey('city', $data); + $this->assertArrayHasKey('postal', $data); + $this->assertArrayHasKey('div', $data); + $this->assertArrayHasKey('divIso', $data); + $this->assertArrayHasKey('accuracy', $data); + $this->assertArrayHasKey('lat', $data); + $this->assertArrayHasKey('long', $data); + $this->assertArrayHasKey('timezone', $data); + + $this->assertStringMatchesFormat('%s', $data['country']); + $this->assertIsBool($data['isEU']); + $this->assertStringMatchesFormat('%s', $data['city']); + $this->assertStringMatchesFormat('%S', $data['postal']); + $this->assertStringMatchesFormat('%s', $data['div']); + $this->assertStringMatchesFormat('%s', $data['divIso']); + $this->assertIsNumeric($data['accuracy']); + $this->assertIsFloat($data['lat']); + $this->assertIsFloat($data['long']); + $this->assertMatchesRegularExpression('/[a-zA-Z]+\/[a-zA-Z]+/', $data['timezone']); + } + + private function assertIpData($input, $expected) { + if (array_key_exists('status', $expected)) { + $this->assertArrayHasKey('status', $input); + $this->assertArrayHasKey('data', $input); + $this->assertArrayHasKey('message', $input); + + $this->assertEquals($expected['status'], $input['status']); + $this->assertIsArray($input['data']); + $this->assertEquals($expected['message'], $input['message']); + + if (array_key_exists('country', $expected['data'])) { + $data = $input['data']; + $this->assertIpDetails($data); + } else { + foreach ($input['data'] as $ip => $data) { + $this->assertIpDetails($data); + } + } + } else { + $this->assertIpDetails($input); + } + } + public static function setUpBeforeClass(): void { static::$redis = new \Redis(); static::$redis->connect('tcp://127.0.0.1', 6379, 0, null, 100, 10); @@ -132,10 +179,7 @@ public function testPingResponseIsValid(): void { * @dataProvider ipDataProvider */ public function testCanConnectAndAnalyzeIp($input, array $expected): void { - $this->assertSame( - $expected, - self::connect($input) - ); + $this->assertIpData(self::connect($input), $expected); } public function ipDataProvider(): array { @@ -156,10 +200,7 @@ public function ipDataProvider(): array { * @dataProvider cacheDataProvider */ public function testCanCacheAndExpireResult($input, array $expected): void { - $this->assertSame( - $expected, - static::$redis->get("result:{$input}") - ); + $this->assertIpData(static::$redis->get("result:{$input}"), $expected); $this->assertLessThanOrEqual( 3600, @@ -185,10 +226,7 @@ public function cacheDataProvider(): array { * @dataProvider ipListDataProvider */ public function testCanConnectAndAnalyzeIpList($input, array $expected): void { - $this->assertSame( - $expected, - self::connect($input) - ); + $this->assertIpData(self::connect($input), $expected); } public function ipListDataProvider(): array { diff --git a/test/e2e/RedisListTest.php b/test/e2e/RedisListTest.php index 32a973e..07e0d52 100644 --- a/test/e2e/RedisListTest.php +++ b/test/e2e/RedisListTest.php @@ -41,6 +41,53 @@ private static function getTestData() { return json_decode(file_get_contents(__DIR__ . '/../ip-test-data.json'), true); } + private function assertIpDetails($data) { + $this->assertArrayHasKey('country', $data); + $this->assertArrayHasKey('isEU', $data); + $this->assertArrayHasKey('city', $data); + $this->assertArrayHasKey('postal', $data); + $this->assertArrayHasKey('div', $data); + $this->assertArrayHasKey('divIso', $data); + $this->assertArrayHasKey('accuracy', $data); + $this->assertArrayHasKey('lat', $data); + $this->assertArrayHasKey('long', $data); + $this->assertArrayHasKey('timezone', $data); + + $this->assertStringMatchesFormat('%s', $data['country']); + $this->assertIsBool($data['isEU']); + $this->assertStringMatchesFormat('%s', $data['city']); + $this->assertStringMatchesFormat('%S', $data['postal']); + $this->assertStringMatchesFormat('%s', $data['div']); + $this->assertStringMatchesFormat('%s', $data['divIso']); + $this->assertIsNumeric($data['accuracy']); + $this->assertIsFloat($data['lat']); + $this->assertIsFloat($data['long']); + $this->assertMatchesRegularExpression('/[a-zA-Z]+\/[a-zA-Z]+/', $data['timezone']); + } + + private function assertIpData($input, $expected) { + if (array_key_exists('status', $expected)) { + $this->assertArrayHasKey('status', $input); + $this->assertArrayHasKey('data', $input); + $this->assertArrayHasKey('message', $input); + + $this->assertEquals($expected['status'], $input['status']); + $this->assertIsArray($input['data']); + $this->assertEquals($expected['message'], $input['message']); + + if (array_key_exists('country', $expected['data'])) { + $data = $input['data']; + $this->assertIpDetails($data); + } else { + foreach ($input['data'] as $ip => $data) { + $this->assertIpDetails($data); + } + } + } else { + $this->assertIpDetails($input); + } + } + public static function setUpBeforeClass(): void { static::$redis = new \Redis(); static::$redis->connect('tcp://127.0.0.1', 6379, 0, null, 100, 10); @@ -144,10 +191,7 @@ public function testPingResponseIsValid(): void { * @dataProvider ipDataProvider */ public function testCanConnectAndAnalyzeIp($input, array $expected): void { - $this->assertSame( - $expected, - self::connect($input) - ); + $this->assertIpData(self::connect($input), $expected); } public function ipDataProvider(): array { @@ -168,10 +212,7 @@ public function ipDataProvider(): array { * @dataProvider cacheDataProvider */ public function testCanCacheResult($input, array $expected): void { - $this->assertSame( - $expected, - static::$redis->get("result:{$input}") - ); + $this->assertIpData(static::$redis->get("result:{$input}"), $expected); } public function cacheDataProvider(): array { @@ -192,10 +233,7 @@ public function cacheDataProvider(): array { * @dataProvider ipListDataProvider */ public function testCanConnectAndAnalyzeIpList($input, array $expected): void { - $this->assertSame( - $expected, - self::connect($input) - ); + $this->assertIpData(self::connect($input), $expected); } public function ipListDataProvider(): array { diff --git a/test/e2e/ServiceAuthSslTest.php b/test/e2e/ServiceAuthSslTest.php index c1756cb..270a226 100644 --- a/test/e2e/ServiceAuthSslTest.php +++ b/test/e2e/ServiceAuthSslTest.php @@ -18,6 +18,49 @@ private static function getTestData() { return json_decode(file_get_contents(__DIR__ . '/../ip-test-data.json'), true); } + private function assertIpDetails($data) { + $this->assertArrayHasKey('country', $data); + $this->assertArrayHasKey('isEU', $data); + $this->assertArrayHasKey('city', $data); + $this->assertArrayHasKey('postal', $data); + $this->assertArrayHasKey('div', $data); + $this->assertArrayHasKey('divIso', $data); + $this->assertArrayHasKey('accuracy', $data); + $this->assertArrayHasKey('lat', $data); + $this->assertArrayHasKey('long', $data); + $this->assertArrayHasKey('timezone', $data); + + $this->assertStringMatchesFormat('%s', $data['country']); + $this->assertIsBool($data['isEU']); + $this->assertStringMatchesFormat('%s', $data['city']); + $this->assertStringMatchesFormat('%S', $data['postal']); + $this->assertStringMatchesFormat('%s', $data['div']); + $this->assertStringMatchesFormat('%s', $data['divIso']); + $this->assertIsNumeric($data['accuracy']); + $this->assertIsFloat($data['lat']); + $this->assertIsFloat($data['long']); + $this->assertMatchesRegularExpression('/[a-zA-Z]+\/[a-zA-Z]+/', $data['timezone']); + } + + private function assertIpData($input, $expected) { + $this->assertArrayHasKey('status', $input); + $this->assertArrayHasKey('data', $input); + $this->assertArrayHasKey('message', $input); + + $this->assertEquals($expected['status'], $input['status']); + $this->assertIsArray($input['data']); + $this->assertEquals($expected['message'], $input['message']); + + if (array_key_exists('country', $expected['data'])) { + $data = $input['data']; + $this->assertIpDetails($data); + } else { + foreach ($input['data'] as $ip => $data) { + $this->assertIpDetails($data); + } + } + } + public static function setUpBeforeClass(): void { // TODO } @@ -170,10 +213,7 @@ public function testPingResponseIsValid(): void { * @dataProvider ipDataProvider */ public function testCanConnectAndAnalyzeIp($input, array $expected): void { - $this->assertSame( - $expected, - self::connect($input) - ); + $this->assertIpData(self::connect($input), $expected); } public function ipDataProvider(): array { @@ -194,10 +234,7 @@ public function ipDataProvider(): array { * @dataProvider ipListDataProvider */ public function testCanConnectAndAnalyzeIpList($input, array $expected): void { - $this->assertSame( - $expected, - self::connect($input) - ); + $this->assertIpData(self::connect($input), $expected); } public function ipListDataProvider(): array { diff --git a/test/e2e/ServiceAuthTest.php b/test/e2e/ServiceAuthTest.php index 53147b0..66355f0 100644 --- a/test/e2e/ServiceAuthTest.php +++ b/test/e2e/ServiceAuthTest.php @@ -18,6 +18,49 @@ private static function getTestData() { return json_decode(file_get_contents(__DIR__ . '/../ip-test-data.json'), true); } + private function assertIpDetails($data) { + $this->assertArrayHasKey('country', $data); + $this->assertArrayHasKey('isEU', $data); + $this->assertArrayHasKey('city', $data); + $this->assertArrayHasKey('postal', $data); + $this->assertArrayHasKey('div', $data); + $this->assertArrayHasKey('divIso', $data); + $this->assertArrayHasKey('accuracy', $data); + $this->assertArrayHasKey('lat', $data); + $this->assertArrayHasKey('long', $data); + $this->assertArrayHasKey('timezone', $data); + + $this->assertStringMatchesFormat('%s', $data['country']); + $this->assertIsBool($data['isEU']); + $this->assertStringMatchesFormat('%s', $data['city']); + $this->assertStringMatchesFormat('%S', $data['postal']); + $this->assertStringMatchesFormat('%s', $data['div']); + $this->assertStringMatchesFormat('%s', $data['divIso']); + $this->assertIsNumeric($data['accuracy']); + $this->assertIsFloat($data['lat']); + $this->assertIsFloat($data['long']); + $this->assertMatchesRegularExpression('/[a-zA-Z]+\/[a-zA-Z]+/', $data['timezone']); + } + + private function assertIpData($input, $expected) { + $this->assertArrayHasKey('status', $input); + $this->assertArrayHasKey('data', $input); + $this->assertArrayHasKey('message', $input); + + $this->assertEquals($expected['status'], $input['status']); + $this->assertIsArray($input['data']); + $this->assertEquals($expected['message'], $input['message']); + + if (array_key_exists('country', $expected['data'])) { + $data = $input['data']; + $this->assertIpDetails($data); + } else { + foreach ($input['data'] as $ip => $data) { + $this->assertIpDetails($data); + } + } + } + public static function setUpBeforeClass(): void { // TODO } @@ -164,10 +207,7 @@ public function testPingResponseIsValid(): void { * @dataProvider ipDataProvider */ public function testCanConnectAndAnalyzeIp($input, array $expected): void { - $this->assertSame( - $expected, - self::connect($input) - ); + $this->assertIpData(self::connect($input), $expected); } public function ipDataProvider(): array { @@ -188,10 +228,7 @@ public function ipDataProvider(): array { * @dataProvider ipListDataProvider */ public function testCanConnectAndAnalyzeIpList($input, array $expected): void { - $this->assertSame( - $expected, - self::connect($input) - ); + $this->assertIpData(self::connect($input), $expected); } public function ipListDataProvider(): array { diff --git a/test/e2e/ServiceBasicTest.php b/test/e2e/ServiceBasicTest.php index 03ac54d..794370c 100644 --- a/test/e2e/ServiceBasicTest.php +++ b/test/e2e/ServiceBasicTest.php @@ -18,6 +18,49 @@ private static function getTestData() { return json_decode(file_get_contents(__DIR__ . '/../ip-test-data.json'), true); } + private function assertIpDetails($data) { + $this->assertArrayHasKey('country', $data); + $this->assertArrayHasKey('isEU', $data); + $this->assertArrayHasKey('city', $data); + $this->assertArrayHasKey('postal', $data); + $this->assertArrayHasKey('div', $data); + $this->assertArrayHasKey('divIso', $data); + $this->assertArrayHasKey('accuracy', $data); + $this->assertArrayHasKey('lat', $data); + $this->assertArrayHasKey('long', $data); + $this->assertArrayHasKey('timezone', $data); + + $this->assertStringMatchesFormat('%s', $data['country']); + $this->assertIsBool($data['isEU']); + $this->assertStringMatchesFormat('%s', $data['city']); + $this->assertStringMatchesFormat('%S', $data['postal']); + $this->assertStringMatchesFormat('%s', $data['div']); + $this->assertStringMatchesFormat('%s', $data['divIso']); + $this->assertIsNumeric($data['accuracy']); + $this->assertIsFloat($data['lat']); + $this->assertIsFloat($data['long']); + $this->assertMatchesRegularExpression('/[a-zA-Z]+\/[a-zA-Z]+/', $data['timezone']); + } + + private function assertIpData($input, $expected) { + $this->assertArrayHasKey('status', $input); + $this->assertArrayHasKey('data', $input); + $this->assertArrayHasKey('message', $input); + + $this->assertEquals($expected['status'], $input['status']); + $this->assertIsArray($input['data']); + $this->assertEquals($expected['message'], $input['message']); + + if (array_key_exists('country', $expected['data'])) { + $data = $input['data']; + $this->assertIpDetails($data); + } else { + foreach ($input['data'] as $ip => $data) { + $this->assertIpDetails($data); + } + } + } + public static function setUpBeforeClass(): void { // TODO } @@ -115,10 +158,7 @@ public function testPingResponseIsValid(): void { * @dataProvider ipDataProvider */ public function testCanConnectAndAnalyzeIp($input, array $expected): void { - $this->assertSame( - $expected, - self::connect($input) - ); + $this->assertIpData(self::connect($input), $expected); } public function ipDataProvider(): array { @@ -139,10 +179,7 @@ public function ipDataProvider(): array { * @dataProvider ipListDataProvider */ public function testCanConnectAndAnalyzeIpList($input, array $expected): void { - $this->assertSame( - $expected, - self::connect($input) - ); + $this->assertIpData(self::connect($input), $expected); } public function ipListDataProvider(): array { diff --git a/test/e2e/ServiceIPSumTest.php b/test/e2e/ServiceIPSumTest.php index 544fa16..5402b15 100644 --- a/test/e2e/ServiceIPSumTest.php +++ b/test/e2e/ServiceIPSumTest.php @@ -18,6 +18,49 @@ private static function getTestData() { return json_decode(file_get_contents(__DIR__ . '/../ip-test-data.json'), true); } + private function assertIpDetails($data) { + $this->assertArrayHasKey('country', $data); + $this->assertArrayHasKey('isEU', $data); + $this->assertArrayHasKey('city', $data); + $this->assertArrayHasKey('postal', $data); + $this->assertArrayHasKey('div', $data); + $this->assertArrayHasKey('divIso', $data); + $this->assertArrayHasKey('accuracy', $data); + $this->assertArrayHasKey('lat', $data); + $this->assertArrayHasKey('long', $data); + $this->assertArrayHasKey('timezone', $data); + + $this->assertStringMatchesFormat('%s', $data['country']); + $this->assertIsBool($data['isEU']); + $this->assertStringMatchesFormat('%s', $data['city']); + $this->assertStringMatchesFormat('%S', $data['postal']); + $this->assertStringMatchesFormat('%s', $data['div']); + $this->assertStringMatchesFormat('%s', $data['divIso']); + $this->assertIsNumeric($data['accuracy']); + $this->assertIsFloat($data['lat']); + $this->assertIsFloat($data['long']); + $this->assertMatchesRegularExpression('/[a-zA-Z]+\/[a-zA-Z]+/', $data['timezone']); + } + + private function assertIpData($input, $expected) { + $this->assertArrayHasKey('status', $input); + $this->assertArrayHasKey('data', $input); + $this->assertArrayHasKey('message', $input); + + $this->assertEquals($expected['status'], $input['status']); + $this->assertIsArray($input['data']); + $this->assertEquals($expected['message'], $input['message']); + + if (array_key_exists('country', $expected['data'])) { + $data = $input['data']; + $this->assertIpDetails($data); + } else { + foreach ($input['data'] as $ip => $data) { + $this->assertIpDetails($data); + } + } + } + public static function setUpBeforeClass(): void { // TODO } @@ -115,10 +158,7 @@ public function testPingResponseIsValid(): void { * @dataProvider ipDataProvider */ public function testCanConnectAndAnalyzeIp($input, array $expected): void { - $this->assertSame( - $expected, - self::connect($input) - ); + $this->assertIpData(self::connect($input), $expected); } public function ipDataProvider(): array { @@ -140,10 +180,7 @@ public function ipDataProvider(): array { * @dataProvider ipListDataProvider */ public function testCanConnectAndAnalyzeIpList($input, array $expected): void { - $this->assertSame( - $expected, - self::connect($input) - ); + $this->assertIpData(self::connect($input), $expected); } public function ipListDataProvider(): array { diff --git a/test/e2e/ServiceSslTest.php b/test/e2e/ServiceSslTest.php index 49b8632..d0c044d 100644 --- a/test/e2e/ServiceSslTest.php +++ b/test/e2e/ServiceSslTest.php @@ -18,6 +18,49 @@ private static function getTestData() { return json_decode(file_get_contents(__DIR__ . '/../ip-test-data.json'), true); } + private function assertIpDetails($data) { + $this->assertArrayHasKey('country', $data); + $this->assertArrayHasKey('isEU', $data); + $this->assertArrayHasKey('city', $data); + $this->assertArrayHasKey('postal', $data); + $this->assertArrayHasKey('div', $data); + $this->assertArrayHasKey('divIso', $data); + $this->assertArrayHasKey('accuracy', $data); + $this->assertArrayHasKey('lat', $data); + $this->assertArrayHasKey('long', $data); + $this->assertArrayHasKey('timezone', $data); + + $this->assertStringMatchesFormat('%s', $data['country']); + $this->assertIsBool($data['isEU']); + $this->assertStringMatchesFormat('%s', $data['city']); + $this->assertStringMatchesFormat('%S', $data['postal']); + $this->assertStringMatchesFormat('%s', $data['div']); + $this->assertStringMatchesFormat('%s', $data['divIso']); + $this->assertIsNumeric($data['accuracy']); + $this->assertIsFloat($data['lat']); + $this->assertIsFloat($data['long']); + $this->assertMatchesRegularExpression('/[a-zA-Z]+\/[a-zA-Z]+/', $data['timezone']); + } + + private function assertIpData($input, $expected) { + $this->assertArrayHasKey('status', $input); + $this->assertArrayHasKey('data', $input); + $this->assertArrayHasKey('message', $input); + + $this->assertEquals($expected['status'], $input['status']); + $this->assertIsArray($input['data']); + $this->assertEquals($expected['message'], $input['message']); + + if (array_key_exists('country', $expected['data'])) { + $data = $input['data']; + $this->assertIpDetails($data); + } else { + foreach ($input['data'] as $ip => $data) { + $this->assertIpDetails($data); + } + } + } + public static function setUpBeforeClass(): void { // TODO } @@ -121,10 +164,7 @@ public function testPingResponseIsValid(): void { * @dataProvider ipDataProvider */ public function testCanConnectAndAnalyzeIp($input, array $expected): void { - $this->assertSame( - $expected, - self::connect($input) - ); + $this->assertIpData(self::connect($input), $expected); } public function ipDataProvider(): array { @@ -145,10 +185,7 @@ public function ipDataProvider(): array { * @dataProvider ipListDataProvider */ public function testCanConnectAndAnalyzeIpList($input, array $expected): void { - $this->assertSame( - $expected, - self::connect($input) - ); + $this->assertIpData(self::connect($input), $expected); } public function ipListDataProvider(): array { diff --git a/test/unit/AnalyzerTest.php b/test/unit/AnalyzerTest.php index a22cb6a..2861e8d 100644 --- a/test/unit/AnalyzerTest.php +++ b/test/unit/AnalyzerTest.php @@ -16,114 +16,6 @@ final class AnalyzerTest extends TestCase { private static $capture = null; private static $currentLogSetting = null; - private static $ipTestData = [ - '128.101.101.101' => [ - 'code' => 'NA', - 'continent' => 'North America', - 'iso' => 'US', - 'country' => 'United States', - 'isEU' => false, - 'city' => 'Minneapolis', - 'postal' => '55423', - 'div' => 'Minnesota', - 'divIso' => 'MN', - 'accuracy' => 20, - 'lat' => 44.8769, - 'long' => -93.2535, - 'timezone' => 'America/Chicago' - ], - '::ffff:8065:6565' => [ - 'code' => 'NA', - 'continent' => 'North America', - 'iso' => 'US', - 'country' => 'United States', - 'isEU' => false, - 'city' => 'Minneapolis', - 'postal' => '55423', - 'div' => 'Minnesota', - 'divIso' => 'MN', - 'accuracy' => 20, - 'lat' => 44.8769, - 'long' => -93.2535, - 'timezone' => 'America/Chicago' - ], - '156.33.241.5' => [ - 'code' => 'NA', - 'continent' => 'North America', - 'iso' => 'US', - 'country' => 'United States', - 'isEU' => false, - 'city' => 'Washington', - 'postal' => '20002', - 'div' => 'District of Columbia', - 'divIso' => 'DC', - 'accuracy' => 50, - 'lat' => 38.9034, - 'long' => -76.9882, - 'timezone' => 'America/New_York' - ], - '2601:481:8600:b3c0:bd63:8f82:1022:1c5b' => [ - 'code' => 'NA', - 'continent' => 'North America', - 'iso' => 'US', - 'country' => 'United States', - 'isEU' => false, - 'city' => 'White House', - 'postal' => '37188', - 'div' => 'Tennessee', - 'divIso' => 'TN', - 'accuracy' => 20, - 'lat' => 36.4566, - 'long' => -86.6638, - 'timezone' => 'America/Chicago' - ], - '161.23.77.210' => [ - 'code' => 'EU', - 'continent' => 'Europe', - 'iso' => 'GB', - 'country' => 'United Kingdom', - 'isEU' => false, - 'city' => 'London', - 'postal' => 'EC4N', - 'div' => 'England', - 'divIso' => 'ENG', - 'accuracy' => 5, - 'lat' => 51.5095, - 'long' => -0.0955, - 'timezone' => 'Europe/London' - ], - '133.11.93.255' => [ - 'code' => 'AS', - 'continent' => 'Asia', - 'iso' => 'JP', - 'country' => 'Japan', - 'isEU' => false, - 'city' => 'Bunkyo-ku', - 'postal' => '112-8001', - 'div' => 'Tokyo', - 'divIso' => '13', - 'accuracy' => 5, - 'lat' => 35.7201, - 'long' => 139.7439, - 'timezone' => 'Asia/Tokyo' - ], - '203.198.7.66' => [ - 'code' => 'AS', - 'continent' => 'Asia', - 'iso' => 'HK', - 'country' => 'Hong Kong', - 'isEU' => false, - 'city' => 'Central', - 'postal' => '', - 'div' => 'Central and Western District', - 'divIso' => 'HCW', - 'accuracy' => 5, - 'lat' => 22.2908, - 'long' => 114.1501, - 'timezone' => 'Asia/Hong_Kong' - ] - ]; - private static function expectedResponse(string $status, $data, $message): array { return [ 'status' => $status, @@ -132,6 +24,53 @@ private static function expectedResponse(string $status, $data, $message): array ]; } + private static function getTestData() { + return json_decode(file_get_contents(__DIR__ . '/../ip-test-data.json'), true); + } + + private function assertIpDetails($data) { + $this->assertArrayHasKey('country', $data); + $this->assertArrayHasKey('isEU', $data); + $this->assertArrayHasKey('city', $data); + $this->assertArrayHasKey('postal', $data); + $this->assertArrayHasKey('div', $data); + $this->assertArrayHasKey('divIso', $data); + $this->assertArrayHasKey('accuracy', $data); + $this->assertArrayHasKey('lat', $data); + $this->assertArrayHasKey('long', $data); + $this->assertArrayHasKey('timezone', $data); + + $this->assertStringMatchesFormat('%s', $data['country']); + $this->assertIsBool($data['isEU']); + $this->assertStringMatchesFormat('%s', $data['city']); + $this->assertStringMatchesFormat('%S', $data['postal']); + $this->assertStringMatchesFormat('%s', $data['div']); + $this->assertStringMatchesFormat('%s', $data['divIso']); + $this->assertIsNumeric($data['accuracy']); + $this->assertIsFloat($data['lat']); + $this->assertIsFloat($data['long']); + $this->assertMatchesRegularExpression('/[a-zA-Z]+\/[a-zA-Z]+/', $data['timezone']); + } + + private function assertIpData($input, $expected) { + $this->assertArrayHasKey('status', $input); + $this->assertArrayHasKey('data', $input); + $this->assertArrayHasKey('message', $input); + + $this->assertEquals($expected['status'], $input['status']); + $this->assertIsArray($input['data']); + $this->assertEquals($expected['message'], $input['message']); + + if (array_key_exists('country', $expected['data'])) { + $data = $input['data']; + $this->assertIpDetails($data); + } else { + foreach ($input['data'] as $ip => $data) { + $this->assertIpDetails($data); + } + } + } + public static function setUpBeforeClass(): void { self::$currentLogSetting = ini_get('error_log'); @@ -265,16 +204,13 @@ public function testPingResponseIsValid(): void { * @dataProvider ipDataProvider */ public function testCanAnalyzeIp($input, array $expected): void { - $this->assertSame( - $expected, - Analyzer::authenticateRequest($input, self::$geoIP, null, []) - ); + $this->assertIpData(Analyzer::authenticateRequest($input, self::$geoIP, null, []), $expected); } public function ipDataProvider(): array { $ipData = []; - foreach (static::$ipTestData as $ip => $data) { + foreach (self::getTestData() as $ip => $data) { // case $ipData[] = [ ['ip' => $ip], @@ -289,21 +225,20 @@ public function ipDataProvider(): array { * @dataProvider ipListDataProvider */ public function testCanAnalyzeIpList($input, array $expected): void { - $this->assertSame( - $expected, - Analyzer::authenticateRequest($input, self::$geoIP, null, []) - ); + $this->assertIpData(Analyzer::authenticateRequest($input, self::$geoIP, null, []), $expected); } public function ipListDataProvider(): array { + $ipTestData = self::getTestData(); + return [ [ - ['iplist' => array_keys(static::$ipTestData)], - self::expectedResponse('success', static::$ipTestData, null) // expected + ['iplist' => array_keys($ipTestData)], + self::expectedResponse('success', $ipTestData, null) // expected ], [ - ['iplist' => array_reverse(array_keys(static::$ipTestData))], - self::expectedResponse('success', array_reverse(static::$ipTestData), null) // expected + ['iplist' => array_reverse(array_keys($ipTestData))], + self::expectedResponse('success', array_reverse($ipTestData), null) // expected ] ]; }