2
0

RegistryTest.php 7.1 KB

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