Last active
August 29, 2015 14:05
-
-
Save saro0h/b00ef8e525f4e7bccf7b to your computer and use it in GitHub Desktop.
Test of a method doing a generation of URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Canopy\CoreBundle\Service; | |
use Doctrine\ORM\EntityManager; | |
use Symfony\Bundle\FrameworkBundle\Routing\Router; | |
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |
//… | |
class Paginator | |
{ | |
//… | |
/** | |
* Gets the the previous page's url according to the current page | |
* and the filters provided, or null if there is no previous page. | |
* | |
* @param integer $page Current page | |
* @param string $repository Namespace of the the repository of the resource. | |
* @param array | null $filters Array of filters given to restrict the results. | |
* | |
* @return string | null | |
*/ | |
public function getPreviousUrl($page, $repository, array $filters = null) | |
{ | |
if (1 == $page || 1 === $this->getTotalPages($repository, $filters)) { | |
return null; | |
} | |
$parameters = array('page' => $this->getPreviousPage($page)); | |
if ($filters) { | |
$parameters = array_merge($parameters, array('category' => $filters['category'])); | |
} | |
return $this->router->generate( | |
'canopy_get_notifications', | |
$parameters, | |
UrlGeneratorInterface::ABSOLUTE_URL | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Canopy\CoreBundle\Tests\Service; | |
use Canopy\CoreBundle\Service\Paginator; | |
use Canopy\CoreBundle\Tests\Fake\FakeRepository; | |
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |
class PaginatorTest extends \PHPUnit_Framework_TestCase | |
{ | |
private $em; | |
private $router; | |
private $fakeRepository; | |
private $paginator; | |
public function testGetPreviousPageT | |
{ | |
$this->fakeRepository->expects($this->any()) | |
->method('getTotalCount') | |
->will($this->returnValue(30) | |
); | |
// I make sure that the method is called one time with the right parameters, instead of | |
// testing that the router workds well by generating the URL. | |
$this->router->expects($this->once()) | |
->method('generate') | |
->with('canopy_get_notifications', array('page' => 1), UrlGeneratorInterface::ABSOLUTE_URL) | |
; | |
$this->paginator->getPreviousUrl(2, $this->fakeRepository); | |
} | |
public function setUp() | |
{ | |
$this->em = $this->getMockBuilder('\Doctrine\ORM\EntityManager') | |
->disableOriginalConstructor() | |
->getMock(array('getRepository')) | |
; | |
$this->router = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Routing\Router') | |
->disableOriginalConstructor() | |
->getMock(array('generate')) | |
; | |
$this->fakeRepository = $this->getMockBuilder('Canopy\CoreBundle\Tests\Fake\FakeRepository') | |
->disableOriginalConstructor() | |
->getMock(array('getTotalCount')) | |
; | |
$this->em->expects($this->any()) | |
->method('getRepository') | |
->will($this->returnValue($this->fakeRepository) | |
); | |
$this->paginator = new Paginator($this->em, $this->router, 10); | |
} | |
public function tearDown() | |
{ | |
$this->em = null; | |
$this->router = null; | |
$this->fakeRepository = null; | |
$this->paginator = null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment