Added missing tests for Common module

This commit is contained in:
Alejandro Celaya 2016-07-30 17:45:48 +02:00
parent 2ce6c1f44b
commit fcdcfde04f
5 changed files with 232 additions and 0 deletions

View file

@ -0,0 +1,31 @@
<?php
namespace ShlinkioTest\Shlink\Common;
use PHPUnit_Framework_TestCase as TestCase;
use Shlinkio\Shlink\Common\ConfigProvider;
class ConfigProviderTest extends TestCase
{
/**
* @var ConfigProvider
*/
protected $configProvider;
public function setUp()
{
$this->configProvider = new ConfigProvider();
}
/**
* @test
*/
public function configIsReturned()
{
$config = $this->configProvider->__invoke();
$this->assertArrayHasKey('error_handler', $config);
$this->assertArrayHasKey('middleware_pipeline', $config);
$this->assertArrayHasKey('services', $config);
$this->assertArrayHasKey('twig', $config);
}
}

View file

@ -0,0 +1,43 @@
<?php
namespace ShlinkioTest\Shlink\Common\Paginator;
use PHPUnit_Framework_TestCase as TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Common\Paginator\Adapter\PaginableRepositoryAdapter;
use Shlinkio\Shlink\Common\Repository\PaginableRepositoryInterface;
class PaginableRepositoryAdapterTest extends TestCase
{
/**
* @var PaginableRepositoryAdapter
*/
protected $adapter;
/**
* @var ObjectProphecy
*/
protected $repo;
public function setUp()
{
$this->repo = $this->prophesize(PaginableRepositoryInterface::class);
$this->adapter = new PaginableRepositoryAdapter($this->repo->reveal(), 'search', 'order');
}
/**
* @test
*/
public function getItemsFallbacksToFindList()
{
$this->repo->findList(10, 5, 'search', 'order')->shouldBeCalledTimes(1);
$this->adapter->getItems(5, 10);
}
/**
* @test
*/
public function countFallbacksToCountList()
{
$this->repo->countList('search')->shouldBeCalledTimes(1);
$this->adapter->count();
}
}

View file

@ -0,0 +1,56 @@
<?php
namespace ShlinkioTest\Shlink\Common\Service;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\Psr7\Response;
use PHPUnit_Framework_TestCase as TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Common\Service\IpLocationResolver;
class IpLocationResolverTest extends TestCase
{
/**
* @var IpLocationResolver
*/
protected $ipResolver;
/**
* @var ObjectProphecy
*/
protected $client;
public function setUp()
{
$this->client = $this->prophesize(Client::class);
$this->ipResolver = new IpLocationResolver($this->client->reveal());
}
/**
* @test
*/
public function correctIpReturnsDecodedInfo()
{
$expected = [
'foo' => 'bar',
'baz' => 'foo',
];
$response = new Response();
$response->getBody()->write(json_encode($expected));
$response->getBody()->rewind();
$this->client->get('http://freegeoip.net/json/1.2.3.4')->willReturn($response)
->shouldBeCalledTimes(1);
$this->assertEquals($expected, $this->ipResolver->resolveIpLocation('1.2.3.4'));
}
/**
* @test
* @expectedException \Shlinkio\Shlink\Common\Exception\WrongIpException
*/
public function guzzleExceptionThrowsShlinkException()
{
$this->client->get('http://freegeoip.net/json/1.2.3.4')->willThrow(new TransferException())
->shouldBeCalledTimes(1);
$this->ipResolver->resolveIpLocation('1.2.3.4');
}
}

View file

@ -0,0 +1,70 @@
<?php
namespace ShlinkioTest\Shlink\Common\Twig\Extension;
use PHPUnit_Framework_TestCase as TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Common\Twig\Extension\TranslatorExtension;
use Zend\I18n\Translator\Translator;
class TranslatorExtensionTest extends TestCase
{
/**
* @var TranslatorExtension
*/
protected $extension;
/**
* @var ObjectProphecy
*/
protected $translator;
public function setUp()
{
$this->translator = $this->prophesize(Translator::class);
$this->extension = new TranslatorExtension($this->translator->reveal());
}
/**
* @test
*/
public function extensionNameIsClassName()
{
$this->assertEquals(TranslatorExtension::class, $this->extension->getName());
}
/**
* @test
*/
public function properFunctionsAreReturned()
{
$funcs = $this->extension->getFunctions();
$this->assertCount(2, $funcs);
foreach ($funcs as $func) {
$this->assertInstanceOf(\Twig_SimpleFunction::class, $func);
}
}
/**
* @test
*/
public function translateFallbacksToTranslator()
{
$this->translator->translate('foo', 'default', null)->shouldBeCalledTimes(1);
$this->extension->translate('foo');
$this->translator->translate('bar', 'baz', 'en')->shouldBeCalledTimes(1);
$this->extension->translate('bar', 'baz', 'en');
}
/**
* @test
*/
public function translatePluralFallbacksToTranslator()
{
$this->translator->translatePlural('foo', 'bar', 'baz', 'default', null)->shouldBeCalledTimes(1);
$this->extension->translatePlural('foo', 'bar', 'baz');
$this->translator->translatePlural('foo', 'bar', 'baz', 'another', 'en')->shouldBeCalledTimes(1);
$this->extension->translatePlural('foo', 'bar', 'baz', 'another', 'en');
}
}

View file

@ -0,0 +1,32 @@
<?php
namespace ShlinkioTest\Shlink\Common\Util;
use PHPUnit_Framework_TestCase as TestCase;
use Shlinkio\Shlink\Common\Util\DateRange;
class DateRangeTest extends TestCase
{
/**
* @test
*/
public function defaultConstructorSetDatesToNull()
{
$range = new DateRange();
$this->assertNull($range->getStartDate());
$this->assertNull($range->getEndDate());
$this->assertTrue($range->isEmpty());
}
/**
* @test
*/
public function providedDatesAreSet()
{
$startDate = new \DateTime();
$endDate = new \DateTime();
$range = new DateRange($startDate, $endDate);
$this->assertSame($startDate, $range->getStartDate());
$this->assertSame($endDate, $range->getEndDate());
$this->assertFalse($range->isEmpty());
}
}