Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of git://github.com/zendframework/zf2
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 15 deletions.
60 changes: 57 additions & 3 deletions src/Page/Mvc.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ class Mvc extends AbstractPage
*/
protected $controller;

/**
* URL query part to use when assembling URL
*
* @var array|string
*/
protected $query;

/**
* Params to use when assembling URL
*
Expand Down Expand Up @@ -183,7 +190,23 @@ public function getHref()
);
}

$params = $this->getParams();
if ($this->getRouteMatch() !== null) {
$rmParams = $this->getRouteMatch()->getParams();

if (isset($rmParams[ModuleRouteListener::ORIGINAL_CONTROLLER])) {
$rmParams['controller'] = $rmParams[ModuleRouteListener::ORIGINAL_CONTROLLER];
unset($rmParams[ModuleRouteListener::ORIGINAL_CONTROLLER]);
}

if (isset($rmParams[ModuleRouteListener::MODULE_NAMESPACE])) {
unset($rmParams[ModuleRouteListener::MODULE_NAMESPACE]);
}

$params = array_merge($rmParams, $this->getParams());
} else {
$params = $this->getParams();
}


if (($param = $this->getController()) != null) {
$params['controller'] = $param;
Expand All @@ -205,14 +228,19 @@ public function getHref()
}

$options = array('name' => $name);
$url = $router->assemble($params, $options);

// Add the fragment identifier if it is set
$fragment = $this->getFragment();
if (null !== $fragment) {
$url .= '#' . $fragment;
$options['fragment'] = $fragment;
}

if (null !== ($query = $this->getQuery())) {
$options['query'] = $query;
}

$url = $router->assemble($params, $options);

return $this->hrefCache = $url;
}

Expand Down Expand Up @@ -284,6 +312,32 @@ public function getController()
return $this->controller;
}

/**
* Sets URL query part to use when assembling URL
*
* @see getHref()
* @param array|string|null $query URL query part
* @return self fluent interface, returns self
*/
public function setQuery($query)
{
$this->query = $query;
$this->hrefCache = null;
return $this;
}

/**
* Returns URL query part to use when assembling URL
*
* @see getHref()
*
* @return array|string|null URL query part (as an array or string) or null
*/
public function getQuery()
{
return $this->query;
}

/**
* Sets params to use when assembling URL
*
Expand Down
20 changes: 15 additions & 5 deletions src/Service/AbstractNavigationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,26 @@ protected function getPages(ServiceLocatorInterface $serviceLocator)
));
}

$application = $serviceLocator->get('Application');
$routeMatch = $application->getMvcEvent()->getRouteMatch();
$router = $application->getMvcEvent()->getRouter();
$pages = $this->getPagesFromConfig($configuration['navigation'][$this->getName()]);

$this->pages = $this->injectComponents($pages, $routeMatch, $router);
$this->pages = $this->preparePages($serviceLocator, $pages);
}
return $this->pages;
}

/**
* @param ServiceLocatorInterface $serviceLocator
* @param array|\Zend\Config\Config $pages
* @throws \Zend\Navigation\Exception\InvalidArgumentException
*/
protected function preparePages(ServiceLocatorInterface $serviceLocator, $pages)
{
$application = $serviceLocator->get('Application');
$routeMatch = $application->getMvcEvent()->getRouteMatch();
$router = $application->getMvcEvent()->getRouter();

return $this->injectComponents($pages, $routeMatch, $router);
}

/**
* @param string|\Zend\Config\Config|array $config
* @return array|null|\Zend\Config\Config
Expand Down
10 changes: 9 additions & 1 deletion src/Service/ConstructedNavigationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@
*/
class ConstructedNavigationFactory extends AbstractNavigationFactory
{
/**
* @var string|\Zend\Config\Config|array
*/
protected $config;

/**
* @param string|\Zend\Config\Config|array $config
*/
public function __construct($config)
{
$this->pages = $this->getPagesFromConfig($config);
$this->config = $config;
}

/**
Expand All @@ -30,6 +35,9 @@ public function __construct($config)
*/
public function getPages(ServiceLocatorInterface $serviceLocator)
{
if (null === $this->pages) {
$this->pages = $this->preparePages($serviceLocator, $this->getPagesFromConfig($this->config));
}
return $this->pages;
}

Expand Down
93 changes: 93 additions & 0 deletions test/Page/MvcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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\Segment as SegmentRoute;
use Zend\Mvc\Router\Http\TreeRouteStack;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
Expand Down Expand Up @@ -215,6 +216,44 @@ public function testGetHrefWithFragmentIdentifier()
$this->assertEquals('/lolcat/myaction/1337#qux', $page->getHref());
}

public function testGetHrefPassesQueryPartToRouter()
{
$page = new Page\Mvc(array(
'label' => 'foo',
'query' => 'foo=bar&baz=qux',
'controller' => 'mycontroller',
'action' => 'myaction',
'route' => 'myroute',
'params' => array(
'page' => 1337
)
));

$route = new RegexRoute(
'(lolcat/(?<action>[^/]+)/(?<page>\d+))',
'/lolcat/%action%/%page%',
array(
'controller' => 'foobar',
'action' => 'bazbat',
'page' => 1,
)
);
$this->router->addRoute('myroute', $route);
$this->routeMatch->setMatchedRouteName('myroute');

$page->setRouteMatch($this->routeMatch);
$page->setRouter($this->router);

$this->assertEquals('/lolcat/myaction/1337?foo=bar&baz=qux', $page->getHref());

// Test with array notation
$page->setQuery(array(
'foo' => 'bar',
'baz' => 'qux',
));
$this->assertEquals('/lolcat/myaction/1337?foo=bar&baz=qux', $page->getHref());
}

public function testIsActiveReturnsTrueOnIdenticalControllerAction()
{
$page = new Page\Mvc(array(
Expand Down Expand Up @@ -498,4 +537,58 @@ public function testNoExceptionForGetHrefIfDefaultRouterIsSet()
$page->getHref();
$page->setDefaultRouter(null);
}

public function testMvcPageParamsInheritRouteMatchParams()
{
$page = new Page\Mvc(array(
'label' => 'lollerblades',
'route' => 'lollerblades'
));

$route = new SegmentRoute('/lollerblades/view/:serialNumber');

$router = new TreeRouteStack;
$router->addRoute('lollerblades', $route);

$routeMatch = new RouteMatch(array(
'serialNumber' => 23,
));
$routeMatch->setMatchedRouteName('lollerblades');

$page->setRouter($router);
$page->setRouteMatch($routeMatch);

$this->assertEquals('/lollerblades/view/23', $page->getHref());
}

public function testInheritedRouteMatchParamsWorkWithModuleRouteListener()
{
$page = new Page\Mvc(array(
'label' => 'mpinkstonwashere',
'route' => 'lmaoplane'
));

$route = new SegmentRoute('/lmaoplane/:controller');

$router = new TreeRouteStack;
$router->addRoute('lmaoplane', $route);

$routeMatch = new RouteMatch(array(
ModuleRouteListener::MODULE_NAMESPACE => 'Application\Controller',
'controller' => 'index'
));
$routeMatch->setMatchedRouteName('lmaoplane');

$event = new MvcEvent();
$event->setRouter($router)
->setRouteMatch($routeMatch);

$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->onRoute($event);

$page->setRouter($event->getRouter());
$page->setRouteMatch($event->getRouteMatch());

$this->assertEquals('/lmaoplane/index', $page->getHref());
}
}
61 changes: 55 additions & 6 deletions test/ServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected function tearDown()
}

/**
* @covers \Zend\Navigation\MvcNavigationFactory
* @covers \Zend\Navigation\Service\AbstractNavigationFactory
*/
public function testDefaultFactoryAcceptsFileString()
{
Expand All @@ -112,7 +112,7 @@ public function testDefaultFactoryAcceptsFileString()
}

/**
* @covers \Zend\Navigation\MvcNavigationFactory
* @covers \Zend\Navigation\Service\DefaultNavigationFactory
*/
public function testMvcPagesGetInjectedWithComponents()
{
Expand All @@ -133,7 +133,56 @@ public function testMvcPagesGetInjectedWithComponents()
}

/**
* @covers \Zend\Navigation\MvcNavigationFactory
* @covers \Zend\Navigation\Service\ConstructedNavigationFactory
*/
public function testConstructedNavigationFactoryInjectRouterAndMatcher()
{
$builder = $this->getMockBuilder('\Zend\Navigation\Service\ConstructedNavigationFactory');
$builder->setConstructorArgs(array(__DIR__ . '/_files/navigation_mvc.xml'))
->setMethods(array('injectComponents'));

$factory = $builder->getMock();

$factory->expects($this->once())
->method('injectComponents')
->with($this->isType("array"),
$this->isInstanceOf("Zend\Mvc\Router\RouteMatch"),
$this->isInstanceOf("Zend\Mvc\Router\RouteStackInterface"));

$this->serviceManager->setFactory('Navigation', function ($serviceLocator) use ($factory){
return $factory->createService($serviceLocator);
});

$container = $this->serviceManager->get('Navigation');
}

/**
* @covers \Zend\Navigation\Service\ConstructedNavigationFactory
*/
public function testMvcPagesGetInjectedWithComponentsInConstructedNavigationFactory()
{
$this->serviceManager->setFactory('Navigation', function ($serviceLocator) {
$argument = __DIR__ . '/_files/navigation_mvc.xml';
$factory = new \Zend\Navigation\Service\ConstructedNavigationFactory($argument);
return $factory->createService($serviceLocator);
});

$container = $this->serviceManager->get('Navigation');
$recursive = function ($that, $pages) use (&$recursive) {
foreach ($pages as $page) {
if ($page instanceof MvcPage) {
$that->assertInstanceOf('Zend\Mvc\Router\RouteStackInterface', $page->getRouter());
$that->assertInstanceOf('Zend\Mvc\Router\RouteMatch', $page->getRouteMatch());
}

$recursive($that, $page->getPages());
}
};
$recursive($this, $container->getPages());
}

/**
* @covers \Zend\Navigation\Service\DefaultNavigationFactory
*/
public function testDefaultFactory()
{
Expand All @@ -144,7 +193,7 @@ public function testDefaultFactory()
}

/**
* @covers \Zend\Navigation\MvcNavigationFactory
* @covers \Zend\Navigation\Service\ConstructedNavigationFactory
*/
public function testConstructedFromArray()
{
Expand All @@ -171,7 +220,7 @@ public function testConstructedFromArray()
}

/**
* @covers \Zend\Navigation\MvcNavigationFactory
* @covers \Zend\Navigation\Service\ConstructedNavigationFactory
*/
public function testConstructedFromFileString()
{
Expand All @@ -184,7 +233,7 @@ public function testConstructedFromFileString()
}

/**
* @covers \Zend\Navigation\MvcNavigationFactory
* @covers \Zend\Navigation\Service\ConstructedNavigationFactory
*/
public function testConstructedFromConfig()
{
Expand Down
17 changes: 17 additions & 0 deletions test/_files/navigation_mvc.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<configData>
<pageOne>
<label>About</label>
<controller>about</controller>
<action>index</action>
</pageOne>
<pageTwo>
<label>Main</label>
<controller>index</controller>
<action>index</action>
</pageTwo>
<pageThree>
<label>Contacts</label>
<controller>contacts</controller>
<action>index</action>
</pageThree>
</configData>

0 comments on commit 196892e

Please sign in to comment.