PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Navigation/Page/MvcTest.php

https://github.com/Exercise/zf2
PHP | 367 lines | 276 code | 58 blank | 33 comment | 4 complexity | e6fc524f382ee2707f754d2dcb9897ef MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Navigation
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @namespace
  24. */
  25. namespace ZendTest\Navigation\Page;
  26. use Zend\Controller\Request;
  27. use Zend\Navigation\Page;
  28. use Zend\Navigation;
  29. /**
  30. * Tests the class Zend_Navigation_Page_Mvc
  31. *
  32. * @category Zend
  33. * @package Zend_Navigation
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_Navigation
  38. */
  39. class MvcTest extends \PHPUnit_Framework_TestCase
  40. {
  41. protected $_front;
  42. protected $_oldRequest;
  43. protected $_oldRouter;
  44. protected function setUp()
  45. {
  46. $this->_front = \Zend\Controller\Front::getInstance();
  47. $this->_oldRequest = $this->_front->getRequest();
  48. $this->_oldRouter = $this->_front->getRouter();
  49. $this->_front->resetInstance();
  50. $this->_front->setRequest(new Request\Http());
  51. $this->_front->getRouter()->addDefaultRoutes();
  52. }
  53. protected function tearDown()
  54. {
  55. if (null !== $this->_oldRequest) {
  56. $this->_front->setRequest($this->_oldRequest);
  57. } else {
  58. $this->_front->setRequest(new Request\Http());
  59. }
  60. $this->_front->setRouter($this->_oldRouter);
  61. }
  62. public function testHrefGeneratedByUrlHelperRequiresNoRoute()
  63. {
  64. $page = new Page\Mvc(array(
  65. 'label' => 'foo',
  66. 'action' => 'index',
  67. 'controller' => 'index'
  68. ));
  69. $page->setAction('view');
  70. $page->setController('news');
  71. $this->assertEquals('/news/view', $page->getHref());
  72. }
  73. public function testHrefGeneratedIsRouteAware()
  74. {
  75. $page = new Page\Mvc(array(
  76. 'label' => 'foo',
  77. 'action' => 'myaction',
  78. 'controller' => 'mycontroller',
  79. 'route' => 'myroute',
  80. 'params' => array(
  81. 'page' => 1337
  82. )
  83. ));
  84. $this->_front->getRouter()->addRoute(
  85. 'myroute',
  86. new \Zend\Controller\Router\Route\Route(
  87. 'lolcat/:action/:page',
  88. array(
  89. 'module' => 'default',
  90. 'controller' => 'foobar',
  91. 'action' => 'bazbat',
  92. 'page' => 1
  93. )
  94. )
  95. );
  96. $this->assertEquals('/lolcat/myaction/1337', $page->getHref());
  97. }
  98. public function testIsActiveReturnsTrueOnIdenticalModuleControllerAction()
  99. {
  100. $page = new Page\Mvc(array(
  101. 'label' => 'foo',
  102. 'action' => 'index',
  103. 'controller' => 'index'
  104. ));
  105. $this->_front->getRequest()->setParams(array(
  106. 'module' => 'default',
  107. 'controller' => 'index',
  108. 'action' => 'index'
  109. ));
  110. $this->assertEquals(true, $page->isActive());
  111. }
  112. public function testIsActiveReturnsFalseOnDifferentModuleControllerAction()
  113. {
  114. $page = new Page\Mvc(array(
  115. 'label' => 'foo',
  116. 'action' => 'bar',
  117. 'controller' => 'index'
  118. ));
  119. $this->_front->getRequest()->setParams(array(
  120. 'module' => 'default',
  121. 'controller' => 'index',
  122. 'action' => 'index'
  123. ));
  124. $this->assertEquals(false, $page->isActive());
  125. }
  126. public function testIsActiveReturnsTrueOnIdenticalIncludingPageParams()
  127. {
  128. $page = new Page\Mvc(array(
  129. 'label' => 'foo',
  130. 'action' => 'view',
  131. 'controller' => 'post',
  132. 'module' => 'blog',
  133. 'params' => array(
  134. 'id' => '1337'
  135. )
  136. ));
  137. $this->_front->getRequest()->setParams(array(
  138. 'module' => 'blog',
  139. 'controller' => 'post',
  140. 'action' => 'view',
  141. 'id' => '1337'
  142. ));
  143. $this->assertEquals(true, $page->isActive());
  144. }
  145. public function testIsActiveReturnsTrueWhenRequestHasMoreParams()
  146. {
  147. $page = new Page\Mvc(array(
  148. 'label' => 'foo',
  149. 'action' => 'view',
  150. 'controller' => 'post',
  151. 'module' => 'blog'
  152. ));
  153. $this->_front->getRequest()->setParams(array(
  154. 'module' => 'blog',
  155. 'controller' => 'post',
  156. 'action' => 'view',
  157. 'id' => '1337'
  158. ));
  159. $this->assertEquals(true, $page->isActive());
  160. }
  161. public function testIsActiveReturnsFalseWhenRequestHasLessParams()
  162. {
  163. $page = new Page\Mvc(array(
  164. 'label' => 'foo',
  165. 'action' => 'view',
  166. 'controller' => 'post',
  167. 'module' => 'blog',
  168. 'params' => array(
  169. 'id' => '1337'
  170. )
  171. ));
  172. $this->_front->getRequest()->setParams(array(
  173. 'module' => 'blog',
  174. 'controller' => 'post',
  175. 'action' => 'view',
  176. 'id' => null
  177. ));
  178. $this->assertEquals(false, $page->isActive());
  179. }
  180. public function testActionAndControllerAccessors()
  181. {
  182. $page = new Page\Mvc(array(
  183. 'label' => 'foo',
  184. 'action' => 'index',
  185. 'controller' => 'index'
  186. ));
  187. $props = array('Action', 'Controller');
  188. $valids = array('index', 'help', 'home', 'default', '1', ' ', '', null);
  189. $invalids = array(42, (object) null);
  190. foreach ($props as $prop) {
  191. $setter = "set$prop";
  192. $getter = "get$prop";
  193. foreach ($valids as $valid) {
  194. $page->$setter($valid);
  195. $this->assertEquals($valid, $page->$getter());
  196. }
  197. foreach ($invalids as $invalid) {
  198. try {
  199. $page->$setter($invalid);
  200. $msg = "'$invalid' is invalid for $setter(), but no ";
  201. $msg .= 'Zend_Navigation_Exception was thrown';
  202. $this->fail($msg);
  203. } catch (Navigation\Exception $e) {
  204. }
  205. }
  206. }
  207. }
  208. public function testModuleAndRouteAccessors()
  209. {
  210. $page = new Page\Mvc(array(
  211. 'label' => 'foo',
  212. 'action' => 'index',
  213. 'controller' => 'index'
  214. ));
  215. $props = array('Module', 'Route');
  216. $valids = array('index', 'help', 'home', 'default', '1', ' ', null);
  217. $invalids = array(42, (object) null);
  218. foreach ($props as $prop) {
  219. $setter = "set$prop";
  220. $getter = "get$prop";
  221. foreach ($valids as $valid) {
  222. $page->$setter($valid);
  223. $this->assertEquals($valid, $page->$getter());
  224. }
  225. foreach ($invalids as $invalid) {
  226. try {
  227. $page->$setter($invalid);
  228. $msg = "'$invalid' is invalid for $setter(), but no ";
  229. $msg .= 'Zend_Navigation_Exception was thrown';
  230. $this->fail($msg);
  231. } catch (Navigation\Exception $e) {
  232. }
  233. }
  234. }
  235. }
  236. public function testSetAndGetResetParams()
  237. {
  238. $page = new Page\Mvc(array(
  239. 'label' => 'foo',
  240. 'action' => 'index',
  241. 'controller' => 'index'
  242. ));
  243. $valids = array(true, 1, '1', 3.14, 'true', 'yes');
  244. foreach ($valids as $valid) {
  245. $page->setResetParams($valid);
  246. $this->assertEquals(true, $page->getResetParams());
  247. }
  248. $invalids = array(false, 0, '0', 0.0, array());
  249. foreach ($invalids as $invalid) {
  250. $page->setResetParams($invalid);
  251. $this->assertEquals(false, $page->getResetParams());
  252. }
  253. }
  254. public function testSetAndGetParams()
  255. {
  256. $page = new Page\Mvc(array(
  257. 'label' => 'foo',
  258. 'action' => 'index',
  259. 'controller' => 'index'
  260. ));
  261. $params = array('foo' => 'bar', 'baz' => 'bat');
  262. $page->setParams($params);
  263. $this->assertEquals($params, $page->getParams());
  264. $page->setParams();
  265. $this->assertEquals(array(), $page->getParams());
  266. $page->setParams($params);
  267. $this->assertEquals($params, $page->getParams());
  268. $page->setParams(array());
  269. $this->assertEquals(array(), $page->getParams());
  270. }
  271. public function testToArrayMethod()
  272. {
  273. $options = array(
  274. 'label' => 'foo',
  275. 'action' => 'index',
  276. 'controller' => 'index',
  277. 'module' => 'test',
  278. 'id' => 'my-id',
  279. 'class' => 'my-class',
  280. 'title' => 'my-title',
  281. 'target' => 'my-target',
  282. 'order' => 100,
  283. 'active' => true,
  284. 'visible' => false,
  285. 'foo' => 'bar',
  286. 'meaning' => 42
  287. );
  288. $page = new Page\Mvc($options);
  289. $toArray = $page->toArray();
  290. $options['reset_params'] = true;
  291. $options['route'] = null;
  292. $options['params'] = array();
  293. $options['rel'] = array();
  294. $options['rev'] = array();
  295. $this->assertEquals(array(),
  296. array_diff_assoc($options, $page->toArray()));
  297. }
  298. public function testSpecifyingAnotherUrlHelperToGenerateHrefs()
  299. {
  300. $newHelper = new \ZendTest\Navigation\TestAsset\UrlHelper();
  301. Page\Mvc::setUrlHelper($newHelper);
  302. $page = new Page\Mvc();
  303. $expected = \ZendTest\Navigation\TestAsset\UrlHelper::RETURN_URL;
  304. $actual = $page->getHref();
  305. $old = \Zend\Controller\Action\HelperBroker::getStaticHelper('URL');
  306. Page\Mvc::setUrlHelper($old);
  307. $this->assertEquals($expected, $actual);
  308. }
  309. }