NavigationTest.php 6.5 KB

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