Added more tests

This commit is contained in:
Alejandro Celaya 2016-05-01 17:54:56 +02:00
parent bb11269146
commit 83352f0af9
3 changed files with 77 additions and 1 deletions

View File

@ -42,7 +42,7 @@ class VisitsTracker implements VisitsTrackerInterface
$visit = new Visit();
$visit->setShortUrl($shortUrl)
->setUserAgent($this->getArrayValue($visitorData, 'HTTP_USER_AGENT'))
->setReferer($this->getArrayValue($visitorData, 'REFERER'))
->setReferer($this->getArrayValue($visitorData, 'HTTP_REFERER'))
->setRemoteAddr($this->getArrayValue($visitorData, 'REMOTE_ADDR'));
$this->em->persist($visit);
$this->em->flush();

View File

@ -0,0 +1,46 @@
<?php
namespace AcelayaTest\UrlShortener\Factory;
use Acelaya\UrlShortener\Factory\CacheFactory;
use Doctrine\Common\Cache\ApcuCache;
use Doctrine\Common\Cache\ArrayCache;
use PHPUnit_Framework_TestCase as TestCase;
use Zend\ServiceManager\ServiceManager;
class CacheFactoryTest extends TestCase
{
/**
* @var CacheFactory
*/
protected $factory;
public function setUp()
{
$this->factory = new CacheFactory();
}
public static function tearDownAfterClass()
{
putenv('APP_ENV');
}
/**
* @test
*/
public function productionReturnsApcAdapter()
{
putenv('APP_ENV=pro');
$instance = $this->factory->__invoke(new ServiceManager(), '');
$this->assertInstanceOf(ApcuCache::class, $instance);
}
/**
* @test
*/
public function developmentReturnsArrayAdapter()
{
putenv('APP_ENV=dev');
$instance = $this->factory->__invoke(new ServiceManager(), '');
$this->assertInstanceOf(ArrayCache::class, $instance);
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace AcelayaTest\UrlShortener\Service;
use Acelaya\UrlShortener\Entity\ShortUrl;
use Acelaya\UrlShortener\Service\VisitsTracker;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use PHPUnit_Framework_TestCase as TestCase;
use Prophecy\Argument;
class VisitsTrackerTest extends TestCase
{
/**
* @test
*/
public function trackPersistsVisit()
{
$shortCode = '123ABC';
$repo = $this->prophesize(EntityRepository::class);
$repo->findOneBy(['shortCode' => $shortCode])->willReturn(new ShortUrl());
$em = $this->prophesize(EntityManager::class);
$em->getRepository(ShortUrl::class)->willReturn($repo->reveal())->shouldBeCalledTimes(1);
$em->persist(Argument::any())->shouldBeCalledTimes(1);
$em->flush()->shouldBeCalledTimes(1);
$visitsTracker = new VisitsTracker($em->reveal());
$visitsTracker->track($shortCode);
}
}