RegistryTest.php 6.8 KB

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