Skip to content

Commit

Permalink
updated test to accommodate MMDB data change
Browse files Browse the repository at this point in the history
  • Loading branch information
ecmchow committed Sep 2, 2022
1 parent fd28fe7 commit ab924b2
Show file tree
Hide file tree
Showing 9 changed files with 431 additions and 197 deletions.
62 changes: 50 additions & 12 deletions test/e2e/RedisAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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 {
Expand Down
62 changes: 50 additions & 12 deletions test/e2e/RedisBasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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 {
Expand Down
62 changes: 50 additions & 12 deletions test/e2e/RedisListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
53 changes: 45 additions & 8 deletions test/e2e/ServiceAuthSslTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
Loading

0 comments on commit ab924b2

Please sign in to comment.