diff --git a/library/Zend/Navigation/Page/Mvc.php b/library/Zend/Navigation/Page/Mvc.php index b5dbefc764d..26dc0d49deb 100644 --- a/library/Zend/Navigation/Page/Mvc.php +++ b/library/Zend/Navigation/Page/Mvc.php @@ -21,9 +21,9 @@ namespace Zend\Navigation\Page; -use Zend\Mvc\Router\RouteMatch, - Zend\Navigation\Exception, - Zend\View\Helper\Url as UrlHelper; +use Zend\Mvc\Router\RouteMatch; +use Zend\Mvc\Router\RouteStackInterface; +use Zend\Navigation\Exception; /** * Represents a page that is defined using controller, action, route @@ -86,21 +86,21 @@ class Mvc extends AbstractPage protected $routeMatch; /** - * View helper for assembling URLs + * Router for assembling URLs * * @see getHref() - * @var UrlHelper + * @var RouteStackInterface */ - protected $urlHelper = null; + protected $router = null; /** - * Default urlHelper to be used if urlHelper is not given. + * Default router to be used if router is not given. * * @see getHref() * - * @var UrlHelper + * @var RouteStackInterface */ - protected static $defaultUrlHelper = null; + protected static $defaultRouter= null; // Accessors: @@ -165,12 +165,12 @@ public function isActive($recursive = false) /** * Returns href for this page * - * This method uses {@link UrlHelper} to assemble + * This method uses {@link RouteStackInterface} to assemble * the href based on the page's properties. * - * @see UrlHelper + * @see RouteStackInterface * @return string page href - * @throws Exception\DomainException if no UrlHelper is set + * @throws Exception\DomainException if no router is set */ public function getHref() { @@ -178,15 +178,15 @@ public function getHref() return $this->hrefCache; } - $helper = $this->urlHelper; - if (null === $helper) { - $helper = self::$defaultUrlHelper; + $router = $this->router; + if (null === $router) { + $router = self::$defaultRouter; } - if (!$helper instanceof UrlHelper) { + if (!$router instanceof RouteStackInterface) { throw new Exception\DomainException( __METHOD__ - . ' cannot execute as no Zend\View\Helper\Url instance is composed' + . ' cannot execute as no Zend\Mvc\Router\RouteStackInterface instance is composed' ); } @@ -200,10 +200,19 @@ public function getHref() $params['action'] = $param; } - $url = $helper( - $this->getRoute(), - $params - ); + switch (true) { + case ($this->getRoute() !== null): + $name = $this->getRoute(); + break; + case ($this->getRouteMatch() !== null): + $name = $this->getRouteMatch()->getMatchedRouteName(); + break; + default: + throw new Exception\DomainException('No route name could be found'); + } + + $options = array('name' => $name); + $url = $router->assemble($params, $options); // Add the fragment identifier if it is set $fragment = $this->getFragment(); @@ -372,49 +381,49 @@ public function setRouteMatch(RouteMatch $matches) } /** - * Get the url helper. + * Get the router. * - * @return null|\Zend\View\Helper\Url + * @return null|RouteStackInterface */ - public function getUrlHelper() + public function getRouter() { - return $this->urlHelper; + return $this->router; } /** - * Sets action helper for assembling URLs + * Sets router for assembling URLs * * @see getHref() * - * @param UrlHelper $helper URL helper plugin + * @param RouteStackInterface $router Router * @return Mvc fluent interface, returns self */ - public function setUrlHelper(UrlHelper $helper) + public function setRouter(RouteStackInterface $router) { - $this->urlHelper = $helper; + $this->router = $router; return $this; } /** - * Sets the default view helper for assembling URLs. + * Sets the default router for assembling URLs. * * @see getHref() - * @param null|UrlHelper $helper URL helper + * @param RouteStackInterface $router Router * @return void */ - public static function setDefaultUrlHelper($helper) + public static function setDefaultRouter($router) { - self::$defaultUrlHelper = $helper; + self::$defaultRouter = $router; } /** - * Gets the default view helper for assembling URLs. + * Gets the default router for assembling URLs. * - * @return UrlHelper + * @return RouteStackInterface */ - public static function getDefaultUrlHelper() + public static function getDefaultRouter() { - return self::$defaultUrlHelper; + return self::$defaultRouter; } // Public methods: diff --git a/library/Zend/Navigation/Service/AbstractNavigationFactory.php b/library/Zend/Navigation/Service/AbstractNavigationFactory.php index 5040bf7ac12..1ae56e13734 100644 --- a/library/Zend/Navigation/Service/AbstractNavigationFactory.php +++ b/library/Zend/Navigation/Service/AbstractNavigationFactory.php @@ -26,9 +26,9 @@ use Zend\Navigation\Navigation; use Zend\Navigation\Page\Mvc as MvcPage; use Zend\Mvc\Router\RouteMatch; +use Zend\Mvc\Router\RouteStackInterface as Router; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; -use Zend\View\Helper\Url as UrlHelper; /** * Abstract navigation factory @@ -82,11 +82,11 @@ protected function getPages(ServiceLocatorInterface $serviceLocator) } $application = $serviceLocator->get('Application'); - $urlHelper = $serviceLocator->get('ViewHelperManager')->get('url'); $routeMatch = $application->getMvcEvent()->getRouteMatch(); + $router = $application->getMvcEvent()->getRouter(); $pages = $this->getPagesFromConfig($configuration['navigation'][$this->getName()]); - $this->pages = $this->injectComponents($pages, $routeMatch, $urlHelper); + $this->pages = $this->injectComponents($pages, $routeMatch, $router); } return $this->pages; } @@ -124,7 +124,7 @@ protected function getPagesFromConfig($config = null) * @param UrlHelper $urlHelper * @return mixed */ - protected function injectComponents(array $pages, RouteMatch $routeMatch = null, UrlHelper $urlHelper = null) + protected function injectComponents(array $pages, RouteMatch $routeMatch = null, Router $router = null) { foreach($pages as &$page) { $hasMvc = isset($page['action']) || isset($page['controller']) || isset($page['route']); @@ -132,13 +132,13 @@ protected function injectComponents(array $pages, RouteMatch $routeMatch = null, if (!isset($page['routeMatch']) && $routeMatch) { $page['routeMatch'] = $routeMatch; } - if (!isset($page['urlHelper']) && $urlHelper) { - $page['urlHelper'] = $urlHelper; + if (!isset($page['router'])) { + $page['router'] = $router; } } if (isset($page['pages'])) { - $page['pages'] = $this->injectComponents($page['pages'], $routeMatch, $urlHelper); + $page['pages'] = $this->injectComponents($page['pages'], $routeMatch, $router); } } return $pages; diff --git a/tests/Zend/Navigation/NavigationTest.php b/tests/Zend/Navigation/NavigationTest.php index 16154079a8e..320b637b46a 100644 --- a/tests/Zend/Navigation/NavigationTest.php +++ b/tests/Zend/Navigation/NavigationTest.php @@ -40,13 +40,13 @@ class NavigationTest extends \PHPUnit_Framework_TestCase * @var Zend_Navigation */ private $_navigation; - + protected function setUp() { parent::setUp(); $this->_navigation = new \Zend\Navigation\Navigation(); } - + protected function tearDown() { $this->_navigation = null; @@ -55,7 +55,7 @@ protected function tearDown() /** * Testing that navigation order is done correctly - * + * * @group ZF-8337 * @group ZF-8313 */ @@ -78,5 +78,5 @@ public function testNavigationArraySortsCorrectly() $this->assertEquals('page1', $pages[1]['uri']); $this->assertEquals('page2', $pages[2]['uri']); } - + } diff --git a/tests/Zend/Navigation/Page/MvcTest.php b/tests/Zend/Navigation/Page/MvcTest.php index 9505056b9b9..dc53be4e83a 100644 --- a/tests/Zend/Navigation/Page/MvcTest.php +++ b/tests/Zend/Navigation/Page/MvcTest.php @@ -21,15 +21,14 @@ namespace ZendTest\Navigation\Page; -use PHPUnit_Framework_TestCase as TestCase, - Zend\View\Helper\Url as UrlHelper, - Zend\Mvc\Router\RouteMatch, - Zend\Mvc\Router\Http\Regex as RegexRoute, - Zend\Mvc\Router\Http\Literal as LiteralRoute, - Zend\Mvc\Router\Http\TreeRouteStack, - Zend\Navigation\Page, - Zend\Navigation, - ZendTest\Navigation\TestAsset; +use PHPUnit_Framework_TestCase as TestCase; +use Zend\Mvc\Router\RouteMatch; +use Zend\Mvc\Router\Http\Regex as RegexRoute; +use Zend\Mvc\Router\Http\Literal as LiteralRoute; +use Zend\Mvc\Router\Http\TreeRouteStack; +use Zend\Navigation\Page; +use Zend\Navigation; +use ZendTest\Navigation\TestAsset; /** * Tests the class Zend_Navigation_Page_Mvc @@ -43,10 +42,6 @@ */ class MvcTest extends TestCase { - protected $_front; - protected $_oldRequest; - protected $_oldRouter; - protected function setUp() { $this->route = new RegexRoute( @@ -62,17 +57,13 @@ protected function setUp() $this->routeMatch = new RouteMatch(array()); $this->routeMatch->setMatchedRouteName('default'); - - $this->urlHelper = new UrlHelper(); - $this->urlHelper->setRouter($this->router); - $this->urlHelper->setRouteMatch($this->routeMatch); } protected function tearDown() { } - public function testHrefGeneratedByUrlHelperRequiresNoRoute() + public function testHrefGeneratedByRouterRequiresNoRoute() { $page = new Page\Mvc(array( 'label' => 'foo', @@ -80,7 +71,7 @@ public function testHrefGeneratedByUrlHelperRequiresNoRoute() 'controller' => 'index' )); $page->setRouteMatch($this->routeMatch); - $page->setUrlHelper($this->urlHelper); + $page->setRouter($this->router); $page->setAction('view'); $page->setController('news'); @@ -117,11 +108,7 @@ public function testHrefGeneratedIsRouteAware() 'page' => 1, )); - $urlHelper = new UrlHelper(); - $urlHelper->setRouter($router); - $urlHelper->setRouteMatch($routeMatch); - - $page->setUrlHelper($urlHelper); + $page->setRouter($router); $page->setRouteMatch($routeMatch); $this->assertEquals('/lolcat/myaction/1337', $page->getHref()); @@ -142,11 +129,7 @@ public function testIsActiveReturnsTrueWhenMatchingRoute() $routeMatch = new RouteMatch(array()); $routeMatch->setMatchedRouteName('lolfish'); - $urlHelper = new UrlHelper; - $urlHelper->setRouter($router); - $urlHelper->setRouteMatch($routeMatch); - - $page->setUrlHelper($urlHelper); + $page->setRouter($router); $page->setRouteMatch($routeMatch); $this->assertEquals(true, $page->isActive()); @@ -157,8 +140,6 @@ public function testIsActiveReturnsFalseWhenNoRouteAndNoMatchedRouteNameIsSet() $page = new Page\Mvc(); $routeMatch = new RouteMatch(array()); - $this->urlHelper->setRouteMatch($routeMatch); - $page->setRouteMatch($routeMatch); $this->assertFalse($page->isActive()); @@ -193,7 +174,7 @@ public function testGetHrefWithFragmentIdentifier() $this->routeMatch->setMatchedRouteName('myroute'); $page->setRouteMatch($this->routeMatch); - $page->setUrlHelper($this->urlHelper); + $page->setRouter($this->router); $this->assertEquals('/lolcat/myaction/1337#qux', $page->getHref()); } @@ -210,8 +191,6 @@ public function testIsActiveReturnsTrueOnIdenticalControllerAction() 'action' => 'index', )); - $this->urlHelper->setRouteMatch($routeMatch); - $page->setRouteMatch($routeMatch); $this->assertTrue($page->isActive()); @@ -229,8 +208,6 @@ public function testIsActiveReturnsFalseOnDifferentControllerAction() 'action' => 'index', )); - $this->urlHelper->setRouteMatch($routeMatch); - $page->setRouteMatch($routeMatch); $this->assertFalse($page->isActive()); @@ -253,8 +230,6 @@ public function testIsActiveReturnsTrueOnIdenticalIncludingPageParams() 'id' => '1337' )); - $this->urlHelper->setRouteMatch($routeMatch); - $page->setRouteMatch($routeMatch); $this->assertTrue($page->isActive()); @@ -274,8 +249,6 @@ public function testIsActiveReturnsTrueWhenRequestHasMoreParams() 'id' => '1337', )); - $this->urlHelper->setRouteMatch($routeMatch); - $page->setRouteMatch($routeMatch); $this->assertTrue($page->isActive()); @@ -298,8 +271,6 @@ public function testIsActiveReturnsFalseWhenRequestHasLessParams() 'id' => null )); - $this->urlHelper->setRouteMatch($routeMatch); - $page->setRouteMatch($routeMatch); $this->assertFalse($page->isActive()); @@ -435,55 +406,58 @@ public function testToArrayMethod() public function testSpecifyingAnotherUrlHelperToGenerateHrefs() { - $newHelper = new TestAsset\UrlHelper(); + $newRouter = new TestAsset\Router(); - $page = new Page\Mvc(); - $page->setUrlHelper($newHelper); + $page = new Page\Mvc(array( + 'route' => 'default' + )); + $page->setRouter($newRouter); - $expected = TestAsset\UrlHelper::RETURN_URL; + $expected = TestAsset\Router::RETURN_URL; $actual = $page->getHref(); $this->assertEquals($expected, $actual); } - public function testDefaultUrlHelperCanBeSetWithConstructor() + public function testDefaultRouterCanBeSetWithConstructor() { $page = new Page\Mvc(array( - 'label' => 'foo', - 'action' => 'index', - 'controller' => 'index', - 'defaultUrlHelper' => $this->urlHelper + 'label' => 'foo', + 'action' => 'index', + 'controller' => 'index', + 'defaultRouter' => $this->router )); - $this->assertEquals($this->urlHelper, $page->getDefaultUrlHelper()); - $page->setDefaultUrlHelper(null); + $this->assertEquals($this->router, $page->getDefaultRouter()); + $page->setDefaultRouter(null); } - public function testDefaultUrlHelperCanBeSetWithGetter() + public function testDefaultRouterCanBeSetWithGetter() { $page = new Page\Mvc(array( 'label' => 'foo', 'action' => 'index', 'controller' => 'index', )); - $page->setDefaultUrlHelper($this->urlHelper); + $page->setDefaultRouter($this->router); - $this->assertEquals($this->urlHelper, $page->getDefaultUrlHelper()); - $page->setDefaultUrlHelper(null); + $this->assertEquals($this->router, $page->getDefaultRouter()); + $page->setDefaultRouter(null); } - public function testNoExceptionForGetHrefIfDefaultUrlHelperIsSet() + public function testNoExceptionForGetHrefIfDefaultRouterIsSet() { $page = new Page\Mvc(array( 'label' => 'foo', 'action' => 'index', 'controller' => 'index', - 'defaultUrlHelper' => $this->urlHelper + 'route' => 'default', + 'defaultRouter' => $this->router )); - // If the default url helper is not used an exception will be thrown. + // If the default router is not used an exception will be thrown. // This method intentionally has no assertion. $page->getHref(); - $page->setDefaultUrlHelper(null); + $page->setDefaultRouter(null); } } diff --git a/tests/Zend/Navigation/ServiceFactoryTest.php b/tests/Zend/Navigation/ServiceFactoryTest.php index 74952ef3205..58634124313 100644 --- a/tests/Zend/Navigation/ServiceFactoryTest.php +++ b/tests/Zend/Navigation/ServiceFactoryTest.php @@ -133,7 +133,7 @@ public function testMvcPagesGetInjectedWithComponents() $recursive = function($that, $pages) use (&$recursive) { foreach($pages as $page) { if ($page instanceof MvcPage) { - $that->assertInstanceOf('Zend\View\Helper\Url', $page->getUrlHelper()); + $that->assertInstanceOf('Zend\Mvc\Router\RouteStackInterface', $page->getRouter()); $that->assertInstanceOf('Zend\Mvc\Router\RouteMatch', $page->getRouteMatch()); } diff --git a/tests/Zend/Navigation/TestAsset/UrlHelper.php b/tests/Zend/Navigation/TestAsset/Router.php similarity index 86% rename from tests/Zend/Navigation/TestAsset/UrlHelper.php rename to tests/Zend/Navigation/TestAsset/Router.php index 0880d3bd3c7..f393d2db628 100644 --- a/tests/Zend/Navigation/TestAsset/UrlHelper.php +++ b/tests/Zend/Navigation/TestAsset/Router.php @@ -28,11 +28,11 @@ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ -class UrlHelper extends \Zend\View\Helper\Url +class Router extends \Zend\Mvc\Router\Http\TreeRouteStack { const RETURN_URL = 'spotify:track:2nd6CTjR9zjHGT0QtpfLHe'; - public function __invoke($name = null, array $params = array(), array $options = array(), $reuseMatchedParams = false) + public function assemble(array $params = array(), array $options = array()) { return self::RETURN_URL; }