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

Commit

Permalink
Merge branch 'feature/navigation-refactor' of https://github.com/fros…
Browse files Browse the repository at this point in the history
…chdesign/zf2 into hotfix/navigation-tweaks
  • Loading branch information
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 63 deletions.
46 changes: 5 additions & 41 deletions src/Page/Mvc.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
Zend\View\Helper\Url as UrlHelper;

/**
* Represents a page that is defined using module, controller, action, route
* Represents a page that is defined using controller, action, route
* name and route params to assemble the href
*
* @category Zend
Expand All @@ -44,11 +44,6 @@ class Mvc extends AbstractPage
*/
protected $action;

/**
* @var bool
*/
protected $active = false;

/**
* Controller name to use when assembling URL
*
Expand Down Expand Up @@ -127,12 +122,15 @@ public function isActive($recursive = false)
if ($this->routeMatch instanceof RouteMatch) {
$reqParams = $this->routeMatch->getParams();

if ($this->routeMatch->getMatchedRouteName() === $this->getRoute()) {
if (null !== $this->getRoute()
&& $this->routeMatch->getMatchedRouteName() === $this->getRoute()
) {
$this->active = true;
return true;
}
}


$myParams = $this->params;

if (null !== $this->controller) {
Expand Down Expand Up @@ -284,40 +282,6 @@ public function getController()
return $this->controller;
}

/**
* Sets module name to use when assembling URL
*
* @see getHref()
*
* @param string|null $module module name
* @return Mvc fluent interface, returns self
* @throws Exception\InvalidArgumentException if invalid module name is given
*/
public function setModule($module)
{
if (null !== $module && !is_string($module)) {
throw new Exception\InvalidArgumentException(
'Invalid argument: $module must be a string or null'
);
}

$this->module = $module;
$this->hrefCache = null;
return $this;
}

/**
* Returns module name to use when assembling URL
*
* @see getHref()
*
* @return string|null module name or null
*/
public function getModule()
{
return $this->module;
}

/**
* Sets params to use when assembling URL
*
Expand Down
39 changes: 20 additions & 19 deletions test/Page/MvcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ public function testIsActiveReturnsTrueWhenMatchingRoute()
$this->assertEquals(true, $page->isActive());
}

public function testIsActiveReturnsFalseWhenNoRouteAndNoMatchedRouteNameIsSet()
{
$page = new Page\Mvc();

$routeMatch = new RouteMatch(array());
$this->urlHelper->setRouteMatch($routeMatch);

$page->setRouteMatch($routeMatch);

$this->assertFalse($page->isActive());
}

/**
* @group ZF-8922
*/
Expand All @@ -175,7 +187,6 @@ public function testGetHrefWithFragmentIdentifier()
'(lolcat/(?<action>[^/]+)/(?<page>\d+))',
'/lolcat/%action%/%page%',
array(
'module' => 'default',
'controller' => 'foobar',
'action' => 'bazbat',
'page' => 1,
Expand All @@ -190,41 +201,37 @@ public function testGetHrefWithFragmentIdentifier()
$this->assertEquals('/lolcat/myaction/1337#qux', $page->getHref());
}

public function testIsActiveReturnsTrueOnIdenticalModuleControllerAction()
public function testIsActiveReturnsTrueOnIdenticalControllerAction()
{
$page = new Page\Mvc(array(
'label' => 'foo',
'action' => 'index',
'controller' => 'index'
));

$routeMatch = new RouteMatch(array(
'module' => 'application',
'controller' => 'index',
'action' => 'index',
));
$routeMatch->setMatchedRouteName('default');

$this->urlHelper->setRouteMatch($routeMatch);

$page->setRouteMatch($routeMatch);

$this->assertTrue($page->isActive());
}

public function testIsActiveReturnsFalseOnDifferentModuleControllerAction()
public function testIsActiveReturnsFalseOnDifferentControllerAction()
{
$page = new Page\Mvc(array(
'label' => 'foo',
'action' => 'bar',
'controller' => 'index'
));

$routeMatch = new RouteMatch(array(
'module' => 'default',
'controller' => 'index',
'action' => 'index',
));
$routeMatch->setMatchedRouteName('default');

$this->urlHelper->setRouteMatch($routeMatch);

$page->setRouteMatch($routeMatch);
Expand All @@ -238,19 +245,17 @@ public function testIsActiveReturnsTrueOnIdenticalIncludingPageParams()
'label' => 'foo',
'action' => 'view',
'controller' => 'post',
'module' => 'blog',
'params' => array(
'id' => '1337'
)
));

$routeMatch = new RouteMatch(array(
'module' => 'blog',
'controller' => 'post',
'action' => 'view',
'id' => '1337'
));
$routeMatch->setMatchedRouteName('default');

$this->urlHelper->setRouteMatch($routeMatch);

$page->setRouteMatch($routeMatch);
Expand All @@ -264,16 +269,14 @@ public function testIsActiveReturnsTrueWhenRequestHasMoreParams()
'label' => 'foo',
'action' => 'view',
'controller' => 'post',
'module' => 'blog'
));

$routeMatch = new RouteMatch(array(
'module' => 'blog',
'controller' => 'post',
'action' => 'view',
'id' => '1337'
'id' => '1337',
));
$routeMatch->setMatchedRouteName('default');

$this->urlHelper->setRouteMatch($routeMatch);

$page->setRouteMatch($routeMatch);
Expand All @@ -287,19 +290,17 @@ public function testIsActiveReturnsFalseWhenRequestHasLessParams()
'label' => 'foo',
'action' => 'view',
'controller' => 'post',
'module' => 'blog',
'params' => array(
'id' => '1337'
)
));

$routeMatch = new RouteMatch(array(
'module' => 'blog',
'controller' => 'post',
'action' => 'view',
'id' => null
));
$routeMatch->setMatchedRouteName('default');

$this->urlHelper->setRouteMatch($routeMatch);

$page->setRouteMatch($routeMatch);
Expand Down
3 changes: 0 additions & 3 deletions test/Page/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,6 @@ public function testSetOptionsShouldTranslateToAccessor()
'label' => 'bar',
'action' => 'baz',
'controller' => 'bat',
'module' => 'test',
'id' => 'foo-test'
);

Expand All @@ -823,15 +822,13 @@ public function testSetOptionsShouldTranslateToAccessor()
'label' => 'bar',
'action' => 'baz',
'controller' => 'bat',
'module' => 'test',
'id' => 'foo-test'
);

$actual = array(
'label' => $page->getLabel(),
'action' => $page->getAction(),
'controller' => $page->getController(),
'module' => $page->getModule(),
'id' => $page->getId()
);

Expand Down

0 comments on commit c31bf4c

Please sign in to comment.