RegistryTest.php 8.0 KB

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