RegistryTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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_View
  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. // Call Zend_View_Helper_Placeholder_RegistryTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_Placeholder_RegistryTest::main");
  25. }
  26. require_once dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/TestHelper.php';
  27. require_once "PHPUnit/Framework/TestCase.php";
  28. require_once "PHPUnit/Framework/TestSuite.php";
  29. /** Zend_View_Helper_Placeholder_Registry */
  30. require_once 'Zend/View/Helper/Placeholder/Registry.php';
  31. /**
  32. * Test class for Zend_View_Helper_Placeholder_Registry.
  33. *
  34. * @category Zend
  35. * @package Zend_View
  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_View
  40. * @group Zend_View_Helper
  41. */
  42. class Zend_View_Helper_Placeholder_RegistryTest extends PHPUnit_Framework_TestCase
  43. {
  44. /**
  45. * @var Zend_View_Helper_Placeholder_Registry
  46. */
  47. public $registry;
  48. /**
  49. * Runs the test methods of this class.
  50. *
  51. * @return void
  52. */
  53. public static function main()
  54. {
  55. require_once "PHPUnit/TextUI/TestRunner.php";
  56. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_Placeholder_RegistryTest");
  57. $result = PHPUnit_TextUI_TestRunner::run($suite);
  58. }
  59. /**
  60. * Sets up the fixture, for example, open a network connection.
  61. * This method is called before a test is executed.
  62. *
  63. * @return void
  64. */
  65. public function setUp()
  66. {
  67. $registry = Zend_Registry::getInstance();
  68. if (isset($registry[Zend_View_Helper_Placeholder_Registry::REGISTRY_KEY])) {
  69. unset($registry[Zend_View_Helper_Placeholder_Registry::REGISTRY_KEY]);
  70. }
  71. $this->registry = new Zend_View_Helper_Placeholder_Registry();
  72. }
  73. /**
  74. * Tears down the fixture, for example, close a network connection.
  75. * This method is called after a test is executed.
  76. *
  77. * @return void
  78. */
  79. public function tearDown()
  80. {
  81. unset($this->registry);
  82. }
  83. /**
  84. * @return void
  85. */
  86. public function testCreateContainer()
  87. {
  88. $this->assertFalse($this->registry->containerExists('foo'));
  89. $this->registry->createContainer('foo');
  90. $this->assertTrue($this->registry->containerExists('foo'));
  91. }
  92. /**
  93. * @return void
  94. */
  95. public function testCreateContainerCreatesDefaultContainerClass()
  96. {
  97. $this->assertFalse($this->registry->containerExists('foo'));
  98. $container = $this->registry->createContainer('foo');
  99. $this->assertTrue($container instanceof Zend_View_Helper_Placeholder_Container);
  100. }
  101. /**
  102. * @return void
  103. */
  104. public function testGetContainerCreatesContainerIfNonExistent()
  105. {
  106. $this->assertFalse($this->registry->containerExists('foo'));
  107. $container = $this->registry->getContainer('foo');
  108. $this->assertTrue($container instanceof Zend_View_Helper_Placeholder_Container_Abstract);
  109. $this->assertTrue($this->registry->containerExists('foo'));
  110. }
  111. /**
  112. * @return void
  113. */
  114. public function testSetContainerCreatesRegistryEntry()
  115. {
  116. $foo = new Zend_View_Helper_Placeholder_Container(array('foo', 'bar'));
  117. $this->assertFalse($this->registry->containerExists('foo'));
  118. $this->registry->setContainer('foo', $foo);
  119. $this->assertTrue($this->registry->containerExists('foo'));
  120. }
  121. /**
  122. * @return void
  123. */
  124. public function testSetContainerCreatesRegistersContainerInstance()
  125. {
  126. $foo = new Zend_View_Helper_Placeholder_Container(array('foo', 'bar'));
  127. $this->assertFalse($this->registry->containerExists('foo'));
  128. $this->registry->setContainer('foo', $foo);
  129. $container = $this->registry->getContainer('foo');
  130. $this->assertSame($foo, $container);
  131. }
  132. /**
  133. * @return void
  134. */
  135. public function testContainerClassAccessorsSetState()
  136. {
  137. $this->assertEquals('Zend_View_Helper_Placeholder_Container', $this->registry->getContainerClass());
  138. $this->registry->setContainerClass('Zend_View_Helper_Placeholder_RegistryTest_Container');
  139. $this->assertEquals('Zend_View_Helper_Placeholder_RegistryTest_Container', $this->registry->getContainerClass());
  140. }
  141. /**
  142. * @return void
  143. */
  144. public function testSetContainerClassThrowsExceptionWithInvalidContainerClass()
  145. {
  146. try {
  147. $this->registry->setContainerClass('Zend_View_Helper_Placeholder_RegistryTest_BogusContainer');
  148. $this->fail('Invalid container classes should not be accepted');
  149. } catch (Exception $e) {
  150. }
  151. }
  152. public function testDeletingContainerRemovesFromRegistry()
  153. {
  154. $this->registry->createContainer('foo');
  155. $this->assertTrue($this->registry->containerExists('foo'));
  156. $result = $this->registry->deleteContainer('foo');
  157. $this->assertFalse($this->registry->containerExists('foo'));
  158. $this->assertTrue($result);
  159. }
  160. public function testDeleteContainerReturnsFalseIfContainerDoesNotExist()
  161. {
  162. $result = $this->registry->deleteContainer('foo');
  163. $this->assertFalse($result);
  164. }
  165. /**
  166. * @return void
  167. */
  168. public function testUsingCustomContainerClassCreatesContainersOfCustomClass()
  169. {
  170. $this->registry->setContainerClass('Zend_View_Helper_Placeholder_RegistryTest_Container');
  171. $container = $this->registry->createContainer('foo');
  172. $this->assertTrue($container instanceof Zend_View_Helper_Placeholder_RegistryTest_Container);
  173. }
  174. public function testGetRegistryReturnsRegistryInstance()
  175. {
  176. $registry = Zend_View_Helper_Placeholder_Registry::getRegistry();
  177. $this->assertTrue($registry instanceof Zend_View_Helper_Placeholder_Registry);
  178. }
  179. public function testGetRegistrySubsequentTimesReturnsSameInstance()
  180. {
  181. $registry1 = Zend_View_Helper_Placeholder_Registry::getRegistry();
  182. $registry2 = Zend_View_Helper_Placeholder_Registry::getRegistry();
  183. $this->assertSame($registry1, $registry2);
  184. }
  185. public function testGetRegistryRegistersWithGlobalRegistry()
  186. {
  187. $this->assertFalse(Zend_Registry::isRegistered(Zend_View_Helper_Placeholder_Registry::REGISTRY_KEY));
  188. $registry = Zend_View_Helper_Placeholder_Registry::getRegistry();
  189. $this->assertTrue(Zend_Registry::isRegistered(Zend_View_Helper_Placeholder_Registry::REGISTRY_KEY));
  190. $registered = Zend_Registry::get(Zend_View_Helper_Placeholder_Registry::REGISTRY_KEY);
  191. $this->assertSame($registry, $registered);
  192. }
  193. }
  194. class Zend_View_Helper_Placeholder_RegistryTest_Container extends Zend_View_Helper_Placeholder_Container_Abstract
  195. {
  196. }
  197. class Zend_View_Helper_Placeholder_RegistryTest_BogusContainer
  198. {
  199. }
  200. // Call Zend_View_Helper_Placeholder_RegistryTest::main() if this source file is executed directly.
  201. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_Placeholder_RegistryTest::main") {
  202. Zend_View_Helper_Placeholder_RegistryTest::main();
  203. }