From 095b79a5eb033dc49b7228bbb57d594e044d59ec Mon Sep 17 00:00:00 2001 From: pablodip Date: Sat, 4 Jan 2014 17:22:30 +0100 Subject: [PATCH] adding to extract logic from twig pagerfanta extension --- Resources/config/pagerfanta.xml | 9 ++- Routing/PagerfantaRouteGenerator.php | 31 ++++++++++ Routing/PagerfantaRouteGeneratorCreator.php | 29 +++++++++ .../PagerfantaRouteGeneratorCreatorTest.php | 35 +++++++++++ .../Routing/PagerfantaRouteGeneratorTest.php | 27 +++++++++ Tests/Twig/PagerfantaExtensionTest.php | 60 +++++++++++++++++++ Twig/PagerfantaExtension.php | 54 ++++++++--------- composer.json | 3 +- 8 files changed, 216 insertions(+), 32 deletions(-) create mode 100644 Routing/PagerfantaRouteGenerator.php create mode 100644 Routing/PagerfantaRouteGeneratorCreator.php create mode 100644 Tests/Routing/PagerfantaRouteGeneratorCreatorTest.php create mode 100644 Tests/Routing/PagerfantaRouteGeneratorTest.php create mode 100644 Tests/Twig/PagerfantaExtensionTest.php diff --git a/Resources/config/pagerfanta.xml b/Resources/config/pagerfanta.xml index 35b0e1c..5d672a9 100644 --- a/Resources/config/pagerfanta.xml +++ b/Resources/config/pagerfanta.xml @@ -52,10 +52,17 @@ + + + + - + + + %white_october_pagerfanta.default_view% + diff --git a/Routing/PagerfantaRouteGenerator.php b/Routing/PagerfantaRouteGenerator.php new file mode 100644 index 0000000..d8fcc85 --- /dev/null +++ b/Routing/PagerfantaRouteGenerator.php @@ -0,0 +1,31 @@ +urlGenerator = $urlGenerator; + $this->routeName = $routeName; + $this->routeParams = $routeParams; + $this->pageParam = $pageParam; + } + + public function __invoke($page) + { + $propertyAccessor = PropertyAccess::getPropertyAccessor(); + $propertyAccessor->setValue($this->routeParams, $this->pageParam, $page); + + return $this->urlGenerator->generate($this->routeName, $this->routeParams); + } +} diff --git a/Routing/PagerfantaRouteGeneratorCreator.php b/Routing/PagerfantaRouteGeneratorCreator.php new file mode 100644 index 0000000..a6d46b6 --- /dev/null +++ b/Routing/PagerfantaRouteGeneratorCreator.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace WhiteOctober\PagerfantaBundle\Routing; + +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; + +class PagerfantaRouteGeneratorCreator +{ + private $urlGenerator; + + public function __construct(UrlGeneratorInterface $urlGenerator) + { + $this->urlGenerator = $urlGenerator; + } + + public function create($routeName, $routeParams, $pageParam) + { + return new PagerfantaRouteGenerator($this->urlGenerator, $routeName, $routeParams, $pageParam); + } +} diff --git a/Tests/Routing/PagerfantaRouteGeneratorCreatorTest.php b/Tests/Routing/PagerfantaRouteGeneratorCreatorTest.php new file mode 100644 index 0000000..d5f6200 --- /dev/null +++ b/Tests/Routing/PagerfantaRouteGeneratorCreatorTest.php @@ -0,0 +1,35 @@ +urlGenerator = Mockery::mock('Symfony\Component\Routing\Generator\UrlGeneratorInterface'); + + $this->creator = new PagerfantaRouteGeneratorCreator($this->urlGenerator); + } + + public function testGenerate() + { + $routeName = 'foo'; + $routeParams = array('a' => '1'); + $pageParameter = '[page]'; + + $result = new PagerfantaRouteGenerator($this->urlGenerator, $routeName, $routeParams, $pageParameter); + + $this->assertEquals($result, $this->creator->create($routeName, $routeParams, $pageParameter)); + } +} diff --git a/Tests/Routing/PagerfantaRouteGeneratorTest.php b/Tests/Routing/PagerfantaRouteGeneratorTest.php new file mode 100644 index 0000000..92a4f32 --- /dev/null +++ b/Tests/Routing/PagerfantaRouteGeneratorTest.php @@ -0,0 +1,27 @@ + '1'); + $pageParameter = '[page]'; + + $page = 2; + $routeParamsWithPage = array_merge($routeParams, array('page' => $page)); + + $generator = new PagerfantaRouteGenerator($urlGenerator, $routeName, $routeParams, $pageParameter); + + $result = 'blahblahblah'; + $urlGenerator->shouldReceive('generate')->with($routeName, $routeParamsWithPage)->once()->andReturn($result); + + $this->assertSame($result, $generator($page)); + } +} diff --git a/Tests/Twig/PagerfantaExtensionTest.php b/Tests/Twig/PagerfantaExtensionTest.php new file mode 100644 index 0000000..b7a35b0 --- /dev/null +++ b/Tests/Twig/PagerfantaExtensionTest.php @@ -0,0 +1,60 @@ +routeGeneratorCreator = Mockery::mock('WhiteOctober\PagerfantaBundle\Routing\PagerfantaRouteGeneratorCreator'); + $this->viewFactory = Mockery::mock('Pagerfanta\View\ViewFactoryInterface'); + $this->defaultView = 'foo'; + $this->container = new Container(); + + $this->extension = new PagerfantaExtension($this->routeGeneratorCreator, $this->viewFactory, $this->defaultView, $this->container); + } + + public function testRenderPagerfanta() + { + $routeName = 'ups'; + $routeParams = array('a' => 1); + $pageParam = '[page]'; + $result = 'foobar'; + + $pagerfanta = Mockery::mock('Pagerfanta\Pagerfanta'); + + $request = new Request(); + $request->attributes->set('_route', $routeName); + $request->attributes->set('_route_params', $routeParams); + $this->container->set('request', $request); + + $routeGenerator = new \stdClass(); + $this->routeGeneratorCreator->shouldReceive('create')->with($routeName, $routeParams, $pageParam)->once()->andReturn($routeGenerator); + + $view = Mockery::mock('Pagerfanta\View\ViewInterface'); + $view->shouldReceive('render')->with($pagerfanta, $routeGenerator, array())->once()->andReturn($result); + + $this->viewFactory->shouldReceive('get')->with($this->defaultView)->once()->andReturn($view); + + $this->assertSame($result, $this->extension->renderPagerfanta($pagerfanta)); + } +} diff --git a/Twig/PagerfantaExtension.php b/Twig/PagerfantaExtension.php index 140771a..588141f 100644 --- a/Twig/PagerfantaExtension.php +++ b/Twig/PagerfantaExtension.php @@ -11,10 +11,11 @@ namespace WhiteOctober\PagerfantaBundle\Twig; +use Pagerfanta\View\ViewFactoryInterface; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\PropertyAccess\PropertyPath; -use Symfony\Component\PropertyAccess\PropertyAccess; +use Symfony\Component\HttpFoundation\Request; use Pagerfanta\PagerfantaInterface; +use WhiteOctober\PagerfantaBundle\Routing\PagerfantaRouteGeneratorCreator; /** * PagerfantaExtension. @@ -23,16 +24,17 @@ */ class PagerfantaExtension extends \Twig_Extension { + private $routeGeneratorCreator; + private $viewFactory; + private $defaultView; private $container; - /** - * Constructor. - * - * @param ContainerInterface $container A container. - */ - public function __construct(ContainerInterface $container) + public function __construct(PagerfantaRouteGeneratorCreator $routeGeneratorCreator, ViewFactoryInterface $viewFactory, $defaultView, ContainerInterface $container) { - $this->container = $container; + $this->routeGeneratorCreator = $routeGeneratorCreator; + $this->viewFactory = $viewFactory; + $this->defaultView = $defaultView; + $this->container = $container; // hack for not being able to inject the request } /** @@ -57,13 +59,10 @@ public function getFunctions() */ public function renderPagerfanta(PagerfantaInterface $pagerfanta, $viewName = null, array $options = array()) { - if (null === $viewName) { - $viewName = $this->container->getParameter('white_october_pagerfanta.default_view'); - } - + $view = $viewName ?: $this->defaultView; $routeGenerator = $this->createRouteGenerator($options); - return $this->container->get('white_october_pagerfanta.view_factory')->get($viewName)->render($pagerfanta, $routeGenerator, $options); + return $this->viewFactory->get($view)->render($pagerfanta, $routeGenerator, $options); } /** @@ -100,16 +99,14 @@ public function getPageUrl(PagerfantaInterface $pagerfanta, $page, array $option private function createRouteGenerator($options = array()) { $options = array_replace(array( - 'routeName' => null, - 'routeParams' => array(), - 'pageParameter' => '[page]', - ), $options); + 'routeName' => null, + 'routeParams' => array(), + 'pageParameter' => '[page]', + ), $options); - $router = $this->container->get('router'); + $request = $this->getRequest(); if (null === $options['routeName']) { - $request = $this->container->get('request'); - $options['routeName'] = $request->attributes->get('_route'); if ('_internal' === $options['routeName']) { throw new \Exception('PagerfantaBundle can not guess the route when used in a subrequest'); @@ -118,16 +115,13 @@ private function createRouteGenerator($options = array()) $options['routeParams'] = array_merge($request->query->all(), $request->attributes->get('_route_params')); } - $routeName = $options['routeName']; - $routeParams = $options['routeParams']; - $pagePropertyPath = new PropertyPath($options['pageParameter']); - - return function($page) use($router, $routeName, $routeParams, $pagePropertyPath) { - $propertyAccessor = PropertyAccess::getPropertyAccessor(); - $propertyAccessor->setValue($routeParams, $pagePropertyPath, $page); + return $this->routeGeneratorCreator->create($options['routeName'], $options['routeParams'], $options['pageParameter']); + } - return $router->generate($routeName, $routeParams); - }; + /** @return Request */ + private function getRequest() + { + return $this->container->get('request'); } /** diff --git a/composer.json b/composer.json index b2f7fb7..dbeb5ba 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ }, "require-dev": { "symfony/symfony": "~2.2", - "phpunit/phpunit": "~3.7" + "phpunit/phpunit": "~3.7", + "mockery/mockery": "~0.8" }, "autoload": { "psr-0": { "WhiteOctober\\PagerfantaBundle": "" }