Skip to content

Commit 9783966

Browse files
authored
refactor: tests (#39)
* refactor: tests * Fix styling * add test * add test * more improovements * Fix styling * format * test * update * disable codecov on older tests
1 parent 621f0cd commit 9783966

17 files changed

+486
-354
lines changed

.github/workflows/run-tests-L7.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,12 @@ jobs:
3030
with:
3131
php-version: ${{ matrix.php }}
3232
extensions: curl, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, iconv
33-
coverage: xdebug
33+
coverage: none
3434

3535
- name: Install dependencies
3636
run: |
3737
composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update
3838
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
3939
4040
- name: Execute tests
41-
run: vendor/bin/phpunit tests
42-
43-
- uses: codecov/codecov-action@v1
44-
with:
45-
token: ${{ secrets.CODECOV_TOKEN }}
46-
directory: build
41+
run: vendor/bin/phpunit tests

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
},
3737
"autoload-dev": {
3838
"psr-4": {
39-
"PulkitJalan\\GeoIP\\Test\\": "tests"
39+
"PulkitJalan\\GeoIP\\Tests\\": "tests"
4040
}
4141
},
4242
"extra": {

phpunit.xml.dist

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,28 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit bootstrap="vendor/autoload.php"
3-
backupGlobals="false"
4-
backupStaticAttributes="false"
5-
colors="true"
6-
verbose="true"
7-
convertErrorsToExceptions="true"
8-
convertNoticesToExceptions="true"
9-
convertWarningsToExceptions="true"
10-
processIsolation="false"
11-
stopOnFailure="false">
12-
<testsuites>
13-
<testsuite name="GeoIP Test Suite">
14-
<directory>tests</directory>
15-
</testsuite>
16-
</testsuites>
17-
<filter>
18-
<whitelist>
19-
<directory suffix=".php">src/</directory>
20-
<exclude>
21-
<file>src/GeoIPServiceProvider.php</file>
22-
<directory suffix=".php">src/config/</directory>
23-
<directory suffix=".php">src/Facades/</directory>
24-
<directory suffix=".php">src/Console/</directory>
25-
</exclude>
26-
</whitelist>
27-
</filter>
28-
<logging>
29-
<log type="tap" target="build/report.tap"/>
30-
<log type="junit" target="build/report.junit.xml"/>
31-
<log type="coverage-html" target="build/coverage"/>
32-
<log type="coverage-text" target="build/coverage.txt"/>
33-
<log type="coverage-xml" target="build/coverage.xml"/>
34-
<log type="coverage-clover" target="build/logs/clover.xml"/>
35-
</logging>
36-
</phpunit>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">src/</directory>
6+
</include>
7+
<exclude>
8+
<file>src/GeoIPServiceProvider.php</file>
9+
<directory suffix=".php">src/config/</directory>
10+
<directory suffix=".php">src/Facades/</directory>
11+
<directory suffix=".php">src/Console/</directory>
12+
</exclude>
13+
<report>
14+
<clover outputFile="build/logs/clover.xml"/>
15+
<html outputDirectory="build/coverage"/>
16+
<text outputFile="build/coverage.txt"/>
17+
<xml outputDirectory="build/coverage.xml"/>
18+
</report>
19+
</coverage>
20+
<testsuites>
21+
<testsuite name="GeoIP Test Suite">
22+
<directory>tests</directory>
23+
</testsuite>
24+
</testsuites>
25+
<logging>
26+
<junit outputFile="build/report.junit.xml"/>
27+
</logging>
28+
</phpunit>

src/Drivers/AbstractGeoIPDriver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ abstract class AbstractGeoIPDriver
1919
/**
2020
* @param array $config
2121
*/
22-
public function __construct(array $config)
22+
public function __construct(array $config, GuzzleClient $guzzle = null)
2323
{
2424
$this->config = $config;
2525

26-
$this->guzzle = new GuzzleClient();
26+
$this->guzzle = $guzzle ?? new GuzzleClient();
2727
}
2828

2929
/**

src/Drivers/IpStackDriver.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
namespace PulkitJalan\GeoIP\Drivers;
44

55
use Illuminate\Support\Arr;
6-
use GuzzleHttp\Exception\RequestException;
6+
use GuzzleHttp\Client as GuzzleClient;
77
use PulkitJalan\GeoIP\Exceptions\InvalidCredentialsException;
88

99
class IpStackDriver extends AbstractGeoIPDriver
1010
{
1111
/**
1212
* @param array $config
1313
*/
14-
public function __construct(array $config)
14+
public function __construct(array $config, GuzzleClient $guzzle = null)
1515
{
16-
parent::__construct($config);
16+
parent::__construct($config, $guzzle);
1717

1818
if (! Arr::get($this->config, 'key')) {
1919
throw new InvalidCredentialsException();
@@ -57,13 +57,13 @@ public function get($ip)
5757
*/
5858
public function getRaw($ip)
5959
{
60-
try {
61-
return json_decode($this->guzzle->get($this->getUrl($ip))->getBody(), true);
62-
} catch (RequestException $e) {
63-
// ignore
60+
$data = json_decode($this->guzzle->get($this->getUrl($ip))->getBody(), true);
61+
62+
if (Arr::get($data, 'success') === false && Arr::get($data, 'error.type' === 'invalid_access_key')) {
63+
throw new InvalidCredentialsException();
6464
}
6565

66-
return [];
66+
return $data;
6767
}
6868

6969
/**

src/Drivers/MaxmindDriver.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PulkitJalan\GeoIP\Drivers;
44

5+
use GuzzleHttp\Client as GuzzleClient;
56
use GeoIp2\Exception\AddressNotFoundException;
67

78
abstract class MaxmindDriver extends AbstractGeoIPDriver
@@ -14,9 +15,9 @@ abstract class MaxmindDriver extends AbstractGeoIPDriver
1415
/**
1516
* @param array $config
1617
*/
17-
public function __construct(array $config)
18+
public function __construct(array $config, GuzzleClient $guzzle = null)
1819
{
19-
parent::__construct($config);
20+
parent::__construct($config, $guzzle);
2021

2122
$this->maxmind = $this->create();
2223
}

src/Drivers/TelizeDriver.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
namespace PulkitJalan\GeoIP\Drivers;
44

55
use Illuminate\Support\Arr;
6-
use GuzzleHttp\Exception\RequestException;
6+
use GuzzleHttp\Client as GuzzleClient;
77
use PulkitJalan\GeoIP\Exceptions\InvalidCredentialsException;
88

99
class TelizeDriver extends AbstractGeoIPDriver
1010
{
1111
/**
1212
* @param array $config
1313
*/
14-
public function __construct(array $config)
14+
public function __construct(array $config, GuzzleClient $guzzle = null)
1515
{
16-
parent::__construct($config);
16+
parent::__construct($config, $guzzle);
1717

1818
if (! Arr::get($this->config, 'key')) {
1919
throw new InvalidCredentialsException();
@@ -57,18 +57,12 @@ public function get($ip)
5757
*/
5858
public function getRaw($ip)
5959
{
60-
try {
61-
return json_decode($this->guzzle->get($this->getUrl($ip), [
62-
'headers' => [
63-
'X-Mashape-Key' => Arr::get($this->config, 'key'),
64-
'Accept' => 'application/json',
65-
],
66-
])->getBody(), true);
67-
} catch (RequestException $e) {
68-
// ignore
69-
}
70-
71-
return [];
60+
return json_decode($this->guzzle->get($this->getUrl($ip), [
61+
'headers' => [
62+
'X-Mashape-Key' => Arr::get($this->config, 'key'),
63+
'Accept' => 'application/json',
64+
],
65+
])->getBody(), true);
7266
}
7367

7468
/**

src/GeoIP.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Arr;
66
use Illuminate\Support\Str;
7+
use GuzzleHttp\Client as GuzzleClient;
78
use PulkitJalan\GeoIP\Exceptions\GeoIPException;
89

910
class GeoIP
@@ -36,9 +37,9 @@ class GeoIP
3637
/**
3738
* @var array
3839
*/
39-
public function __construct(array $config = ['driver' => 'ip-api'])
40+
public function __construct(array $config = ['driver' => 'ip-api'], GuzzleClient $guzzle = null)
4041
{
41-
$this->driver = with(new GeoIPManager($config))->getDriver();
42+
$this->driver = with(new GeoIPManager($config, $guzzle))->getDriver();
4243
$this->random = Arr::get($config, 'random', false);
4344
}
4445

src/GeoIPManager.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Arr;
66
use Illuminate\Support\Str;
7+
use GuzzleHttp\Client as GuzzleClient;
78
use PulkitJalan\GeoIP\Drivers\IPApiDriver;
89
use PulkitJalan\GeoIP\Drivers\TelizeDriver;
910
use PulkitJalan\GeoIP\Drivers\IpStackDriver;
@@ -19,12 +20,18 @@ class GeoIPManager
1920
*/
2021
protected $config;
2122

23+
/**
24+
* @var \GuzzleHttp\Client
25+
*/
26+
protected $guzzle;
27+
2228
/**
2329
* @param array $config
2430
*/
25-
public function __construct(array $config)
31+
public function __construct(array $config, GuzzleClient $guzzle = null)
2632
{
2733
$this->config = $config;
34+
$this->guzzle = $guzzle;
2835
}
2936

3037
/**
@@ -52,7 +59,7 @@ public function getDriver($driver = null): AbstractGeoIPDriver
5259
*/
5360
protected function createIpStackDriver(array $data): IpStackDriver
5461
{
55-
return new IpStackDriver($data);
62+
return new IpStackDriver($data, $this->guzzle);
5663
}
5764

5865
/**
@@ -62,7 +69,7 @@ protected function createIpStackDriver(array $data): IpStackDriver
6269
*/
6370
protected function createIpApiDriver(array $data): IPApiDriver
6471
{
65-
return new IPApiDriver($data);
72+
return new IPApiDriver($data, $this->guzzle);
6673
}
6774

6875
/**
@@ -72,7 +79,7 @@ protected function createIpApiDriver(array $data): IPApiDriver
7279
*/
7380
protected function createMaxmindDatabaseDriver(array $data): MaxmindDatabaseDriver
7481
{
75-
return new MaxmindDatabaseDriver($data);
82+
return new MaxmindDatabaseDriver($data, $this->guzzle);
7683
}
7784

7885
/**
@@ -82,7 +89,7 @@ protected function createMaxmindDatabaseDriver(array $data): MaxmindDatabaseDriv
8289
*/
8390
protected function createMaxmindApiDriver(array $data): MaxmindApiDriver
8491
{
85-
return new MaxmindApiDriver($data);
92+
return new MaxmindApiDriver($data, $this->guzzle);
8693
}
8794

8895
/**
@@ -92,6 +99,6 @@ protected function createMaxmindApiDriver(array $data): MaxmindApiDriver
9299
*/
93100
protected function createTelizeDriver(array $data): TelizeDriver
94101
{
95-
return new TelizeDriver($data);
102+
return new TelizeDriver($data, $this->guzzle);
96103
}
97104
}

tests/AbstractTestCase.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace PulkitJalan\GeoIP\Tests;
4+
5+
use Mockery;
6+
use PHPUnit\Framework\TestCase;
7+
8+
abstract class AbstractTestCase extends TestCase
9+
{
10+
protected $multipleIps = '81.2.69.160,127.0.0.1';
11+
protected $validIp = '81.2.69.160';
12+
protected $invalidIp = '127.0.0.1';
13+
14+
public function tearDown(): void
15+
{
16+
Mockery::close();
17+
}
18+
}

0 commit comments

Comments
 (0)