Skip to content

Commit

Permalink
refactor: tests (#39)
Browse files Browse the repository at this point in the history
* refactor: tests

* Fix styling

* add test

* add test

* more improovements

* Fix styling

* format

* test

* update

* disable codecov on older tests
  • Loading branch information
pulkitjalan committed Oct 7, 2020
1 parent 621f0cd commit 9783966
Show file tree
Hide file tree
Showing 17 changed files with 486 additions and 354 deletions.
9 changes: 2 additions & 7 deletions .github/workflows/run-tests-L7.yml
Expand Up @@ -30,17 +30,12 @@ jobs:
with:
php-version: ${{ matrix.php }}
extensions: curl, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, iconv
coverage: xdebug
coverage: none

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
- name: Execute tests
run: vendor/bin/phpunit tests

- uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
directory: build
run: vendor/bin/phpunit tests
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -36,7 +36,7 @@
},
"autoload-dev": {
"psr-4": {
"PulkitJalan\\GeoIP\\Test\\": "tests"
"PulkitJalan\\GeoIP\\Tests\\": "tests"
}
},
"extra": {
Expand Down
62 changes: 27 additions & 35 deletions phpunit.xml.dist
@@ -1,36 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="GeoIP Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
<exclude>
<file>src/GeoIPServiceProvider.php</file>
<directory suffix=".php">src/config/</directory>
<directory suffix=".php">src/Facades/</directory>
<directory suffix=".php">src/Console/</directory>
</exclude>
</whitelist>
</filter>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-xml" target="build/coverage.xml"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>
<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">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
<exclude>
<file>src/GeoIPServiceProvider.php</file>
<directory suffix=".php">src/config/</directory>
<directory suffix=".php">src/Facades/</directory>
<directory suffix=".php">src/Console/</directory>
</exclude>
<report>
<clover outputFile="build/logs/clover.xml"/>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<xml outputDirectory="build/coverage.xml"/>
</report>
</coverage>
<testsuites>
<testsuite name="GeoIP Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
</phpunit>
4 changes: 2 additions & 2 deletions src/Drivers/AbstractGeoIPDriver.php
Expand Up @@ -19,11 +19,11 @@ abstract class AbstractGeoIPDriver
/**
* @param array $config
*/
public function __construct(array $config)
public function __construct(array $config, GuzzleClient $guzzle = null)
{
$this->config = $config;

$this->guzzle = new GuzzleClient();
$this->guzzle = $guzzle ?? new GuzzleClient();
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/Drivers/IpStackDriver.php
Expand Up @@ -3,17 +3,17 @@
namespace PulkitJalan\GeoIP\Drivers;

use Illuminate\Support\Arr;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Client as GuzzleClient;
use PulkitJalan\GeoIP\Exceptions\InvalidCredentialsException;

class IpStackDriver extends AbstractGeoIPDriver
{
/**
* @param array $config
*/
public function __construct(array $config)
public function __construct(array $config, GuzzleClient $guzzle = null)
{
parent::__construct($config);
parent::__construct($config, $guzzle);

if (! Arr::get($this->config, 'key')) {
throw new InvalidCredentialsException();
Expand Down Expand Up @@ -57,13 +57,13 @@ public function get($ip)
*/
public function getRaw($ip)
{
try {
return json_decode($this->guzzle->get($this->getUrl($ip))->getBody(), true);
} catch (RequestException $e) {
// ignore
$data = json_decode($this->guzzle->get($this->getUrl($ip))->getBody(), true);

if (Arr::get($data, 'success') === false && Arr::get($data, 'error.type' === 'invalid_access_key')) {
throw new InvalidCredentialsException();
}

return [];
return $data;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Drivers/MaxmindDriver.php
Expand Up @@ -2,6 +2,7 @@

namespace PulkitJalan\GeoIP\Drivers;

use GuzzleHttp\Client as GuzzleClient;
use GeoIp2\Exception\AddressNotFoundException;

abstract class MaxmindDriver extends AbstractGeoIPDriver
Expand All @@ -14,9 +15,9 @@ abstract class MaxmindDriver extends AbstractGeoIPDriver
/**
* @param array $config
*/
public function __construct(array $config)
public function __construct(array $config, GuzzleClient $guzzle = null)
{
parent::__construct($config);
parent::__construct($config, $guzzle);

$this->maxmind = $this->create();
}
Expand Down
24 changes: 9 additions & 15 deletions src/Drivers/TelizeDriver.php
Expand Up @@ -3,17 +3,17 @@
namespace PulkitJalan\GeoIP\Drivers;

use Illuminate\Support\Arr;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Client as GuzzleClient;
use PulkitJalan\GeoIP\Exceptions\InvalidCredentialsException;

class TelizeDriver extends AbstractGeoIPDriver
{
/**
* @param array $config
*/
public function __construct(array $config)
public function __construct(array $config, GuzzleClient $guzzle = null)
{
parent::__construct($config);
parent::__construct($config, $guzzle);

if (! Arr::get($this->config, 'key')) {
throw new InvalidCredentialsException();
Expand Down Expand Up @@ -57,18 +57,12 @@ public function get($ip)
*/
public function getRaw($ip)
{
try {
return json_decode($this->guzzle->get($this->getUrl($ip), [
'headers' => [
'X-Mashape-Key' => Arr::get($this->config, 'key'),
'Accept' => 'application/json',
],
])->getBody(), true);
} catch (RequestException $e) {
// ignore
}

return [];
return json_decode($this->guzzle->get($this->getUrl($ip), [
'headers' => [
'X-Mashape-Key' => Arr::get($this->config, 'key'),
'Accept' => 'application/json',
],
])->getBody(), true);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/GeoIP.php
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use GuzzleHttp\Client as GuzzleClient;
use PulkitJalan\GeoIP\Exceptions\GeoIPException;

class GeoIP
Expand Down Expand Up @@ -36,9 +37,9 @@ class GeoIP
/**
* @var array
*/
public function __construct(array $config = ['driver' => 'ip-api'])
public function __construct(array $config = ['driver' => 'ip-api'], GuzzleClient $guzzle = null)
{
$this->driver = with(new GeoIPManager($config))->getDriver();
$this->driver = with(new GeoIPManager($config, $guzzle))->getDriver();
$this->random = Arr::get($config, 'random', false);
}

Expand Down
19 changes: 13 additions & 6 deletions src/GeoIPManager.php
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use GuzzleHttp\Client as GuzzleClient;
use PulkitJalan\GeoIP\Drivers\IPApiDriver;
use PulkitJalan\GeoIP\Drivers\TelizeDriver;
use PulkitJalan\GeoIP\Drivers\IpStackDriver;
Expand All @@ -19,12 +20,18 @@ class GeoIPManager
*/
protected $config;

/**
* @var \GuzzleHttp\Client
*/
protected $guzzle;

/**
* @param array $config
*/
public function __construct(array $config)
public function __construct(array $config, GuzzleClient $guzzle = null)
{
$this->config = $config;
$this->guzzle = $guzzle;
}

/**
Expand Down Expand Up @@ -52,7 +59,7 @@ public function getDriver($driver = null): AbstractGeoIPDriver
*/
protected function createIpStackDriver(array $data): IpStackDriver
{
return new IpStackDriver($data);
return new IpStackDriver($data, $this->guzzle);
}

/**
Expand All @@ -62,7 +69,7 @@ protected function createIpStackDriver(array $data): IpStackDriver
*/
protected function createIpApiDriver(array $data): IPApiDriver
{
return new IPApiDriver($data);
return new IPApiDriver($data, $this->guzzle);
}

/**
Expand All @@ -72,7 +79,7 @@ protected function createIpApiDriver(array $data): IPApiDriver
*/
protected function createMaxmindDatabaseDriver(array $data): MaxmindDatabaseDriver
{
return new MaxmindDatabaseDriver($data);
return new MaxmindDatabaseDriver($data, $this->guzzle);
}

/**
Expand All @@ -82,7 +89,7 @@ protected function createMaxmindDatabaseDriver(array $data): MaxmindDatabaseDriv
*/
protected function createMaxmindApiDriver(array $data): MaxmindApiDriver
{
return new MaxmindApiDriver($data);
return new MaxmindApiDriver($data, $this->guzzle);
}

/**
Expand All @@ -92,6 +99,6 @@ protected function createMaxmindApiDriver(array $data): MaxmindApiDriver
*/
protected function createTelizeDriver(array $data): TelizeDriver
{
return new TelizeDriver($data);
return new TelizeDriver($data, $this->guzzle);
}
}
18 changes: 18 additions & 0 deletions tests/AbstractTestCase.php
@@ -0,0 +1,18 @@
<?php

namespace PulkitJalan\GeoIP\Tests;

use Mockery;
use PHPUnit\Framework\TestCase;

abstract class AbstractTestCase extends TestCase
{
protected $multipleIps = '81.2.69.160,127.0.0.1';
protected $validIp = '81.2.69.160';
protected $invalidIp = '127.0.0.1';

public function tearDown(): void
{
Mockery::close();
}
}

0 comments on commit 9783966

Please sign in to comment.