2016-07-30 18:45:48 +03:00
|
|
|
<?php
|
2017-10-12 11:13:20 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-11-11 14:44:57 +03:00
|
|
|
namespace ShlinkioTest\Shlink\Common\IpGeolocation;
|
2016-07-30 18:45:48 +03:00
|
|
|
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use GuzzleHttp\Exception\TransferException;
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-07-30 18:45:48 +03:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2018-11-11 14:44:57 +03:00
|
|
|
use Shlinkio\Shlink\Common\IpGeolocation\IpApiLocationResolver;
|
2018-10-28 10:24:06 +03:00
|
|
|
use function json_encode;
|
2016-07-30 18:45:48 +03:00
|
|
|
|
2018-07-31 21:24:13 +03:00
|
|
|
class IpApiLocationResolverTest extends TestCase
|
2016-07-30 18:45:48 +03:00
|
|
|
{
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var IpApiLocationResolver */
|
2018-11-20 21:37:22 +03:00
|
|
|
private $ipResolver;
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-20 21:37:22 +03:00
|
|
|
private $client;
|
2016-07-30 18:45:48 +03:00
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->client = $this->prophesize(Client::class);
|
2018-07-31 21:24:13 +03:00
|
|
|
$this->ipResolver = new IpApiLocationResolver($this->client->reveal());
|
2016-07-30 18:45:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function correctIpReturnsDecodedInfo()
|
|
|
|
{
|
2018-07-31 21:24:13 +03:00
|
|
|
$actual = [
|
|
|
|
'countryCode' => 'bar',
|
|
|
|
'lat' => 5,
|
|
|
|
'lon' => 10,
|
|
|
|
];
|
2016-07-30 18:45:48 +03:00
|
|
|
$expected = [
|
2018-07-31 21:24:13 +03:00
|
|
|
'country_code' => 'bar',
|
|
|
|
'country_name' => '',
|
|
|
|
'region_name' => '',
|
|
|
|
'city' => '',
|
|
|
|
'latitude' => 5,
|
|
|
|
'longitude' => 10,
|
|
|
|
'time_zone' => '',
|
2016-07-30 18:45:48 +03:00
|
|
|
];
|
|
|
|
$response = new Response();
|
2018-10-28 10:24:06 +03:00
|
|
|
$response->getBody()->write(json_encode($actual));
|
2016-07-30 18:45:48 +03:00
|
|
|
$response->getBody()->rewind();
|
|
|
|
|
2018-07-31 21:24:13 +03:00
|
|
|
$this->client->get('http://ip-api.com/json/1.2.3.4')->willReturn($response)
|
2018-11-11 15:18:21 +03:00
|
|
|
->shouldBeCalledOnce();
|
2016-07-30 18:45:48 +03:00
|
|
|
$this->assertEquals($expected, $this->ipResolver->resolveIpLocation('1.2.3.4'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @expectedException \Shlinkio\Shlink\Common\Exception\WrongIpException
|
|
|
|
*/
|
|
|
|
public function guzzleExceptionThrowsShlinkException()
|
|
|
|
{
|
2018-07-31 21:24:13 +03:00
|
|
|
$this->client->get('http://ip-api.com/json/1.2.3.4')->willThrow(new TransferException())
|
2018-11-11 15:18:21 +03:00
|
|
|
->shouldBeCalledOnce();
|
2016-07-30 18:45:48 +03:00
|
|
|
$this->ipResolver->resolveIpLocation('1.2.3.4');
|
|
|
|
}
|
|
|
|
}
|