RegistryTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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-2015 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-2015 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->assertTrue($registry instanceof Zend_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->assertTrue($registry instanceof Zend_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->assertTrue($registry instanceof Zend_Registry);
  76. }
  77. public function testRegistrySingletonSameness()
  78. {
  79. $registry1 = Zend_Registry::getInstance();
  80. $registry2 = Zend_Registry::getInstance();
  81. $this->assertTrue($registry1 instanceof Zend_Registry);
  82. $this->assertTrue($registry2 instanceof Zend_Registry);
  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. $this->assertTrue(Zend_Registry::getInstance() instanceof ArrayObject);
  116. }
  117. public function testRegistryArrayAsProps()
  118. {
  119. $registry = new Zend_Registry(array(), ArrayObject::ARRAY_AS_PROPS);
  120. $registry->foo = 'bar';
  121. $this->assertTrue(isset($registry->foo));
  122. $this->assertEquals('bar', $registry->foo);
  123. }
  124. public function testRegistryExceptionInvalidClassname()
  125. {
  126. try {
  127. $registry = Zend_Registry::setClassName(new StdClass());
  128. $this->fail('Expected exception, because setClassName() wants a string');
  129. } catch (Zend_Exception $e) {
  130. $this->assertContains('Argument is not a class name', $e->getMessage());
  131. }
  132. }
  133. /**
  134. * NB: We cannot make a unit test for the class not Zend_Registry or
  135. * a subclass, because that is enforced by type-hinting in the
  136. * Zend_Registry::setInstance() method. Type-hinting violations throw
  137. * an error, not an exception, so it cannot be caught in a unit test.
  138. */
  139. public function testRegistryExceptionNoEntry()
  140. {
  141. try {
  142. $foo = Zend_Registry::get('foo');
  143. $this->fail('Expected exception when trying to fetch a non-existent key.');
  144. } catch (Zend_Exception $e) {
  145. $this->assertContains('No entry is registered for key', $e->getMessage());
  146. }
  147. }
  148. public function testRegistryExceptionAlreadyInitialized()
  149. {
  150. $registry = Zend_Registry::getInstance();
  151. try {
  152. Zend_Registry::setClassName('anyclass');
  153. $this->fail('Expected exception, because we cannot initialize the registry if it is already initialized.');
  154. } catch (Zend_Exception $e) {
  155. $this->assertContains('Registry is already initialized', $e->getMessage());
  156. }
  157. try {
  158. Zend_Registry::setInstance(new Zend_Registry());
  159. $this->fail('Expected exception, because we cannot initialize the registry if it is already initialized.');
  160. } catch (Zend_Exception $e) {
  161. $this->assertContains('Registry is already initialized', $e->getMessage());
  162. }
  163. }
  164. public function testRegistryExceptionClassNotFound()
  165. {
  166. try {
  167. $registry = @Zend_Registry::setClassName('classdoesnotexist');
  168. $this->fail('Expected exception, because we cannot initialize the registry using a non-existent class.');
  169. } catch (Zend_Exception $e) {
  170. $this->assertRegExp('/file .* does not exist or .*/i', $e->getMessage());
  171. }
  172. }
  173. public function testDefaultRegistryArrayAsPropsZF4654()
  174. {
  175. $registry = Zend_Registry::getInstance();
  176. $registry->bar = "baz";
  177. $this->assertEquals('baz', Zend_Registry::get('bar'));
  178. }
  179. }