shlink/module/Common/test/Service/IpApiLocationResolverTest.php

69 lines
1.9 KiB
PHP
Raw Normal View History

2016-07-30 18:45:48 +03:00
<?php
2017-10-12 11:13:20 +03:00
declare(strict_types=1);
2016-07-30 18:45:48 +03:00
namespace ShlinkioTest\Shlink\Common\Service;
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;
use Shlinkio\Shlink\Common\Service\IpApiLocationResolver;
2016-07-30 18:45:48 +03:00
class IpApiLocationResolverTest extends TestCase
2016-07-30 18:45:48 +03:00
{
/**
* @var IpApiLocationResolver
2016-07-30 18:45:48 +03:00
*/
protected $ipResolver;
/**
* @var ObjectProphecy
*/
protected $client;
public function setUp()
{
$this->client = $this->prophesize(Client::class);
$this->ipResolver = new IpApiLocationResolver($this->client->reveal());
2016-07-30 18:45:48 +03:00
}
/**
* @test
*/
public function correctIpReturnsDecodedInfo()
{
$actual = [
'countryCode' => 'bar',
'lat' => 5,
'lon' => 10,
];
2016-07-30 18:45:48 +03:00
$expected = [
'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();
$response->getBody()->write(\json_encode($actual));
2016-07-30 18:45:48 +03:00
$response->getBody()->rewind();
$this->client->get('http://ip-api.com/json/1.2.3.4')->willReturn($response)
->shouldBeCalledTimes(1);
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()
{
$this->client->get('http://ip-api.com/json/1.2.3.4')->willThrow(new TransferException())
->shouldBeCalledTimes(1);
2016-07-30 18:45:48 +03:00
$this->ipResolver->resolveIpLocation('1.2.3.4');
}
}