RegistryTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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_Registry
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2008 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. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../TestHelper.php';
  26. /**
  27. * @see Zend_Registry
  28. */
  29. require_once 'Zend/Registry.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Registry
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_RegistryTest extends PHPUnit_Framework_TestCase
  38. {
  39. public function setUp()
  40. {
  41. Zend_Registry::_unsetInstance();
  42. }
  43. public function tearDown()
  44. {
  45. Zend_Registry::_unsetInstance();
  46. }
  47. public function testRegistryUninitIsRegistered()
  48. {
  49. // checking entry is set returns false,
  50. // but does not initialize instance
  51. $this->assertFalse(Zend_Registry::isRegistered('objectname'));
  52. }
  53. public function testRegistryUninitGetInstance()
  54. {
  55. // getting instance initializes instance
  56. $registry = Zend_Registry::getInstance();
  57. $this->assertType('Zend_Registry', $registry);
  58. }
  59. public function testRegistryUninitSet()
  60. {
  61. // setting value initializes instance
  62. Zend_Registry::set('foo', 'bar');
  63. $registry = Zend_Registry::getInstance();
  64. $this->assertType('Zend_Registry', $registry);
  65. }
  66. public function testRegistryUninitGet()
  67. {
  68. // getting value initializes instance
  69. // but throws different exception because
  70. // entry is not registered
  71. try {
  72. Zend_Registry::get('foo');
  73. $this->fail('Expected exception when trying to fetch a non-existent key.');
  74. } catch (Zend_Exception $e) {
  75. $this->assertContains('No entry is registered for key', $e->getMessage());
  76. }
  77. $registry = Zend_Registry::getInstance();
  78. $this->assertType('Zend_Registry', $registry);
  79. }
  80. public function testRegistrySingletonSameness()
  81. {
  82. $registry1 = Zend_Registry::getInstance();
  83. $registry2 = Zend_Registry::getInstance();
  84. $this->assertType('Zend_Registry', $registry1);
  85. $this->assertType('Zend_Registry', $registry2);
  86. $this->assertEquals($registry1, $registry2);
  87. $this->assertSame($registry1, $registry2);
  88. }
  89. public function testRegistryEqualContents()
  90. {
  91. Zend_Registry::set('foo', 'bar');
  92. $registry1 = Zend_Registry::getInstance();
  93. $registry2 = new Zend_Registry(array('foo' => 'bar'));
  94. $this->assertEquals($registry1, $registry2);
  95. $this->assertNotSame($registry1, $registry2);
  96. }
  97. public function testRegistryUnequalContents()
  98. {
  99. $registry1 = Zend_Registry::getInstance();
  100. $registry2 = new Zend_Registry(array('foo' => 'bar'));
  101. $this->assertNotEquals($registry1, $registry2);
  102. $this->assertNotSame($registry1, $registry2);
  103. }
  104. public function testRegistrySetAndIsRegistered()
  105. {
  106. $this->assertFalse(Zend_Registry::isRegistered('foo'));
  107. Zend_Registry::set('foo', 'bar');
  108. $this->assertTrue(Zend_Registry::isRegistered('foo'));
  109. }
  110. public function testRegistryGet()
  111. {
  112. Zend_Registry::set('foo', 'bar');
  113. $bar = Zend_Registry::get('foo');
  114. $this->assertEquals('bar', $bar);
  115. }
  116. public function testRegistryArrayObject()
  117. {
  118. $registry = Zend_Registry::getInstance();
  119. $registry['emptyArray'] = array();
  120. $registry['null'] = null;
  121. $this->assertTrue(isset($registry['emptyArray']));
  122. $this->assertTrue(isset($registry['null']));
  123. $this->assertFalse(isset($registry['noIndex']));
  124. $this->assertTrue(Zend_Registry::isRegistered('emptyArray'));
  125. $this->assertTrue(Zend_Registry::isRegistered('null'));
  126. $this->assertFalse(Zend_Registry::isRegistered('noIndex'));
  127. }
  128. public function testRegistryArrayAsProps()
  129. {
  130. $registry = new Zend_Registry(array(), ArrayObject::ARRAY_AS_PROPS);
  131. $registry->foo = 'bar';
  132. $this->assertTrue(isset($registry->foo));
  133. $this->assertEquals('bar', $registry->foo);
  134. }
  135. public function testRegistryExceptionInvalidClassname()
  136. {
  137. try {
  138. $registry = Zend_Registry::setClassName(new StdClass());
  139. $this->fail('Expected exception, because setClassName() wants a string');
  140. } catch (Zend_Exception $e) {
  141. $this->assertContains('Argument is not a class name', $e->getMessage());
  142. }
  143. }
  144. /**
  145. * NB: We cannot make a unit test for the class not Zend_Registry or
  146. * a subclass, because that is enforced by type-hinting in the
  147. * Zend_Registry::setInstance() method. Type-hinting violations throw
  148. * an error, not an exception, so it cannot be caught in a unit test.
  149. */
  150. public function testRegistryExceptionNoEntry()
  151. {
  152. try {
  153. $foo = Zend_Registry::get('foo');
  154. $this->fail('Expected exception when trying to fetch a non-existent key.');
  155. } catch (Zend_Exception $e) {
  156. $this->assertContains('No entry is registered for key', $e->getMessage());
  157. }
  158. }
  159. public function testRegistryExceptionAlreadyInitialized()
  160. {
  161. $registry = Zend_Registry::getInstance();
  162. try {
  163. Zend_Registry::setClassName('anyclass');
  164. $this->fail('Expected exception, because we cannot initialize the registry if it is already initialized.');
  165. } catch (Zend_Exception $e) {
  166. $this->assertContains('Registry is already initialized', $e->getMessage());
  167. }
  168. try {
  169. Zend_Registry::setInstance(new Zend_Registry());
  170. $this->fail('Expected exception, because we cannot initialize the registry if it is already initialized.');
  171. } catch (Zend_Exception $e) {
  172. $this->assertContains('Registry is already initialized', $e->getMessage());
  173. }
  174. }
  175. public function testRegistryExceptionClassNotFound()
  176. {
  177. try {
  178. $registry = @Zend_Registry::setClassName('classdoesnotexist');
  179. $this->fail('Expected exception, because we cannot initialize the registry using a non-existent class.');
  180. } catch (Zend_Exception $e) {
  181. $this->assertRegExp('/file .* does not exist or .*/i', $e->getMessage());
  182. }
  183. }
  184. public function testDefaultRegistryArrayAsPropsZF4654()
  185. {
  186. $registry = Zend_Registry::getInstance();
  187. $registry->bar = "baz";
  188. $this->assertEquals('baz', Zend_Registry::get('bar'));
  189. }
  190. }