NavigationTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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_Application
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 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. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Application_Resource_NavigationTest::main');
  24. }
  25. /**
  26. * Zend_Loader_Autoloader
  27. */
  28. require_once 'Zend/Loader/Autoloader.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Application
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @group Zend_Application
  36. */
  37. class Zend_Application_Resource_NavigationTest extends PHPUnit_Framework_TestCase
  38. {
  39. public static function main()
  40. {
  41. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  42. $result = PHPUnit_TextUI_TestRunner::run($suite);
  43. }
  44. public function setUp()
  45. {
  46. // Store original autoloaders
  47. $this->loaders = spl_autoload_functions();
  48. if (!is_array($this->loaders)) {
  49. // spl_autoload_functions does not return empty array when no
  50. // autoloaders registered...
  51. $this->loaders = array();
  52. }
  53. Zend_Loader_Autoloader::resetInstance();
  54. $this->autoloader = Zend_Loader_Autoloader::getInstance();
  55. $this->application = new Zend_Application('testing');
  56. $this->bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
  57. Zend_Registry::_unsetInstance();
  58. }
  59. public function tearDown()
  60. {
  61. Zend_Navigation_Page::setDefaultPageType();
  62. // Restore original autoloaders
  63. $loaders = spl_autoload_functions();
  64. foreach ($loaders as $loader) {
  65. spl_autoload_unregister($loader);
  66. }
  67. foreach ($this->loaders as $loader) {
  68. spl_autoload_register($loader);
  69. }
  70. // Reset autoloader instance so it doesn't affect other tests
  71. Zend_Loader_Autoloader::resetInstance();
  72. }
  73. public function testInitializationInitializesNavigationObject()
  74. {
  75. $this->bootstrap->registerPluginResource('view');
  76. $resource = new Zend_Application_Resource_Navigation(array());
  77. $resource->setBootstrap($this->bootstrap);
  78. $resource->init();
  79. $this->assertTrue($resource->getContainer() instanceof Zend_Navigation_Container);
  80. $this->bootstrap->unregisterPluginResource('view');
  81. }
  82. public function testInitializationReturnsNavigationObject()
  83. {
  84. $this->bootstrap->registerPluginResource('view');
  85. $resource = new Zend_Application_Resource_Navigation(array());
  86. $resource->setBootstrap($this->bootstrap);
  87. $test = $resource->init();
  88. $this->assertTrue($test instanceof Zend_Navigation);
  89. $this->bootstrap->unregisterPluginResource('view');
  90. }
  91. public function testContainerIsStoredInViewhelper()
  92. {
  93. $options = array('pages'=> array(new Zend_Navigation_Page_Mvc(array(
  94. 'action' => 'index',
  95. 'controller' => 'index'))));
  96. $this->bootstrap->registerPluginResource('view');
  97. $resource = new Zend_Application_Resource_Navigation($options);
  98. $resource->setBootstrap($this->bootstrap)->init();
  99. $view = $this->bootstrap->getPluginResource('view')->getView();
  100. $number = $view->getHelper('navigation')->navigation()->count();
  101. $this->assertEquals($number,1);
  102. $this->bootstrap->unregisterPluginResource('view');
  103. }
  104. public function testContainerIsStoredInRegistry()
  105. {
  106. $options = array('pages'=> array(new Zend_Navigation_Page_Mvc(array(
  107. 'action' => 'index',
  108. 'controller' => 'index'))), 'storage' => array('registry' => true));
  109. $resource = new Zend_Application_Resource_Navigation($options);
  110. $resource->setBootstrap($this->bootstrap)->init();
  111. $key = Zend_Application_Resource_Navigation::DEFAULT_REGISTRY_KEY;
  112. $this->assertEquals(Zend_Registry::isRegistered($key),true);
  113. $container = Zend_Registry::get($key);
  114. $number = $container->count();
  115. $this->assertEquals($number,1);
  116. }
  117. /**
  118. * @group ZF-6747
  119. */
  120. public function testViewMethodIsUsedWhenAvailableInsteadOfResourcePlugin()
  121. {
  122. require_once '_files/ZfAppBootstrapCustomView.php';
  123. $bootstrap = new ZfAppBootstrapCustomView($this->application);
  124. $bootstrap->registerPluginResource('view');
  125. $view = $bootstrap->bootstrap('view')->view;
  126. $this->assertEquals($view->setInMethodByTest,true);
  127. }
  128. /**
  129. * @group ZF-10959
  130. */
  131. public function testDefaultPageTypeIsSet()
  132. {
  133. $this->bootstrap->registerPluginResource('view');
  134. $this->bootstrap->getPluginResource('view')->getView();
  135. $options = array('defaultPageType' => 'foobar',
  136. 'pages'=> array(array(
  137. 'action' => 'index',
  138. 'controller' => 'index')));
  139. $results = array();
  140. $resource = new Zend_Application_Resource_Navigation($options);
  141. try {
  142. $resource->setBootstrap($this->bootstrap)->init();
  143. $this->fail('An exception should have been thrown but wasn\'t');
  144. } catch(Zend_Exception $e) {
  145. $this->assertTrue(true);
  146. }
  147. $this->bootstrap->unregisterPluginResource('view');
  148. }
  149. /**
  150. * @group ZF-7461
  151. */
  152. public function testRegistryIsUsedWhenNumericRegistryValueIsGiven()
  153. {
  154. // Register view for cases where registry should/is not (be) used
  155. $this->bootstrap->registerPluginResource('view');
  156. $this->bootstrap->getPluginResource('view')->getView();
  157. $options1 = array(
  158. 'pages'=> array(new Zend_Navigation_Page_Mvc(array(
  159. 'action' => 'index',
  160. 'controller' => 'index'))),
  161. 'storage' => array('registry' => true));
  162. $options = array($options1,
  163. array_merge($options1, array('storage' => array('registry' => '1'))), // Original culprit here
  164. array_merge($options1, array('storage' => array('registry' => 1))),
  165. array_merge($options1, array('storage' => array('registry' => false))));
  166. $results = array();
  167. $key = Zend_Application_Resource_Navigation::DEFAULT_REGISTRY_KEY;
  168. foreach($options as $option) {
  169. $resource = new Zend_Application_Resource_Navigation($option);
  170. $resource->setBootstrap($this->bootstrap)->init();
  171. $results[] = Zend_Registry::get($key) instanceof Zend_Navigation;;
  172. Zend_Registry::set($key,null);
  173. }
  174. $this->assertEquals(array(true,true,true,false),$results);
  175. $this->bootstrap->unregisterPluginResource('view');
  176. }
  177. }
  178. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_NavigationTest::main') {
  179. Zend_Application_Resource_NavigationTest::main();
  180. }