RegistryTest.php 7.1 KB

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