PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/urlcatcher.org/htdocs/lib/vendor/symfony/test/unit/util/sfNamespacedParameterHolderTest.php

https://github.com/mrwabu/urlcatcher
PHP | 224 lines | 150 code | 51 blank | 23 comment | 1 complexity | d1f871a80b4bfd1a59d6c0a2e506ed8c MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. require_once(dirname(__FILE__).'/../../bootstrap/unit.php');
  10. $t = new lime_test(54);
  11. // ->clear()
  12. $t->diag('->clear()');
  13. $ph = new sfNamespacedParameterHolder();
  14. $ph->clear();
  15. $t->is($ph->getAll(), null, '->clear() clears all parameters');
  16. $ph->set('foo', 'bar');
  17. $ph->clear();
  18. $t->is($ph->getAll(), null, '->clear() clears all parameters');
  19. // ->get()
  20. $t->diag('->get()');
  21. $ph = new sfNamespacedParameterHolder();
  22. $ph->set('foo', 'bar');
  23. $t->is($ph->get('foo'), 'bar', '->get() returns the parameter value for the given key');
  24. $t->is($ph->get('bar'), null, '->get() returns null if the key does not exist');
  25. $ph = new sfNamespacedParameterHolder();
  26. $t->is('default_value', $ph->get('foo1', 'default_value'), '->get() takes the default value as its second argument');
  27. $ph = new sfNamespacedParameterHolder();
  28. $ph->set('myfoo', 'bar', 'symfony/mynamespace');
  29. $t->is('bar', $ph->get('myfoo', null, 'symfony/mynamespace'), '->get() takes an optional namespace as its third argument');
  30. $t->is(null, $ph->get('myfoo'), '->get() can have the same key for several namespaces');
  31. // ->getNames()
  32. $t->diag('->getNames()');
  33. $ph = new sfNamespacedParameterHolder();
  34. $ph->set('foo', 'bar');
  35. $ph->set('yourfoo', 'bar');
  36. $ph->set('myfoo', 'bar', 'symfony/mynamespace');
  37. $t->is($ph->getNames(), array('foo', 'yourfoo'), '->getNames() returns all key names for the default namespace');
  38. $t->is($ph->getNames('symfony/mynamespace'), array('myfoo'), '->getNames() takes a namepace as its first argument');
  39. // ->getNamespaces()
  40. $t->diag('->getNamespaces()');
  41. $ph = new sfNamespacedParameterHolder();
  42. $ph->set('foo', 'bar');
  43. $ph->set('yourfoo', 'bar');
  44. $ph->set('myfoo', 'bar', 'symfony/mynamespace');
  45. $t->is($ph->getNamespaces(), array($ph->getDefaultNamespace(), 'symfony/mynamespace'), '->getNamespaces() returns all non empty namespaces');
  46. // ->setDefaultNamespace()
  47. $t->diag('->setDefaultNamespace()');
  48. $ph = new sfNamespacedParameterHolder('symfony/mynamespace');
  49. $ph->setDefaultNamespace('othernamespace');
  50. $t->is($ph->getDefaultNamespace(), 'othernamespace', '->setDefaultNamespace() sets the default namespace');
  51. $ph->set('foo', 'bar');
  52. $ph->setDefaultNamespace('foonamespace');
  53. $t->is($ph->get('foo'), 'bar', '->setDefaultNamespace() moves values from the old namespace to the new');
  54. $t->is($ph->get('foo', null, 'othernamespace'), null, '->setDefaultNamespace() moves values from the old namespace to the new');
  55. $ph->set('foo', 'bar');
  56. $ph->setDefaultNamespace('barnamespace', false);
  57. $t->is($ph->get('foo'), null, '->setDefaultNamespace() does not move old values to the new namespace if the second argument is false');
  58. $t->is($ph->get('foo', null, 'foonamespace'), 'bar', '->setDefaultNamespace() does not move old values to the new namespace if the second argument is false');
  59. // ->getAll()
  60. $t->diag('->getAll()');
  61. $parameters = array('foo' => 'bar', 'myfoo' => 'bar');
  62. $ph = new sfNamespacedParameterHolder();
  63. $ph->add($parameters);
  64. $ph->set('myfoo', 'bar', 'symfony/mynamespace');
  65. $t->is($ph->getAll(), $parameters, '->getAll() returns all parameters from the default namespace');
  66. // ->has()
  67. $t->diag('->has()');
  68. $ph = new sfNamespacedParameterHolder();
  69. $ph->set('foo', 'bar');
  70. $ph->set('myfoo', 'bar', 'symfony/mynamespace');
  71. $t->is($ph->has('foo'), true, '->has() returns true if the key exists');
  72. $t->is($ph->has('bar'), false, '->has() returns false if the key does not exist');
  73. $t->is($ph->has('myfoo'), false, '->has() returns false if the key exists but in another namespace');
  74. $t->is($ph->has('myfoo', 'symfony/mynamespace'), true, '->has() returns true if the key exists in the namespace given as its second argument');
  75. // ->hasNamespace()
  76. $t->diag('->hasNamespace()');
  77. $ph = new sfNamespacedParameterHolder();
  78. $ph->set('foo', 'bar');
  79. $ph->set('myfoo', 'bar', 'symfony/mynamespace');
  80. $t->is($ph->hasNamespace($ph->getDefaultNamespace()), true, '->hasNamespace() returns true for the default namespace');
  81. $t->is($ph->hasNamespace('symfony/mynamespace'), true, '->hasNamespace() returns true if the namespace exists');
  82. $t->is($ph->hasNamespace('symfony/nonexistant'), false, '->hasNamespace() returns false if the namespace does not exist');
  83. // ->remove()
  84. $t->diag('->remove()');
  85. $ph = new sfNamespacedParameterHolder();
  86. $ph->set('foo', 'bar');
  87. $ph->set('myfoo', 'bar');
  88. $ph->set('myfoo', 'bar', 'symfony/mynamespace');
  89. $ph->remove('foo');
  90. $t->is($ph->has('foo'), false, '->remove() removes the key from parameters');
  91. $ph->remove('myfoo');
  92. $t->is($ph->has('myfoo'), false, '->remove() removes the key from parameters');
  93. $t->is($ph->has('myfoo', 'symfony/mynamespace'), true, '->remove() removes the key from parameters for a given namespace');
  94. $ph->remove('myfoo', null, 'symfony/mynamespace');
  95. $t->is($ph->has('myfoo', 'symfony/mynamespace'), false, '->remove() takes a namespace as its third argument');
  96. $t->is($ph->remove('nonexistant', 'foobar', 'symfony/mynamespace'), 'foobar', '->remove() takes a default value as its second argument');
  97. $t->is($ph->getAll(), null, '->remove() removes the key from parameters');
  98. // ->removeNamespace()
  99. $t->diag('->removeNamespace()');
  100. $ph = new sfNamespacedParameterHolder();
  101. $ph->set('foo', 'bar');
  102. $ph->set('myfoo', 'bar');
  103. $ph->set('myfoo', 'bar', 'symfony/mynamespace');
  104. $ph->removeNamespace($ph->getDefaultNamespace());
  105. $t->is($ph->has('foo'), false, '->removeNamespace() removes all keys and values from a namespace');
  106. $t->is($ph->has('myfoo'), false, '->removeNamespace() removes all keys and values from a namespace');
  107. $t->is($ph->has('myfoo', 'symfony/mynamespace'), true, '->removeNamespace() does not remove keys in other namepaces');
  108. $ph->set('foo', 'bar');
  109. $ph->set('myfoo', 'bar');
  110. $ph->set('myfoo', 'bar', 'symfony/mynamespace');
  111. $ph->removeNamespace();
  112. $t->is($ph->has('foo'), false, '->removeNamespace() removes all keys and values from the default namespace by default');
  113. $t->is($ph->has('myfoo'), false, '->removeNamespace() removes all keys and values from the default namespace by default');
  114. $t->is($ph->has('myfoo', 'symfony/mynamespace'), true, '->removeNamespace() does not remove keys in other namepaces');
  115. $ph->removeNamespace('symfony/mynamespace');
  116. $t->is($ph->has('myfoo', 'symfony/mynamespace'), false, '->removeNamespace() takes a namespace as its first parameter');
  117. $t->is(null, $ph->getAll(), '->removeNamespace() removes all the keys from parameters');
  118. // ->set()
  119. $t->diag('->set()');
  120. $foo = 'bar';
  121. $ph = new sfNamespacedParameterHolder();
  122. $ph->set('foo', $foo);
  123. $t->is($ph->get('foo'), $foo, '->set() sets the value for a key');
  124. $foo = 'foo';
  125. $t->is($ph->get('foo'), 'bar', '->set() sets the value for a key, not a reference');
  126. $ph->set('myfoo', 'bar', 'symfony/mynamespace');
  127. $t->is($ph->get('myfoo', null, 'symfony/mynamespace'), 'bar', '->set() takes a namespace as its third parameter');
  128. // ->setByRef()
  129. $t->diag('->setByRef()');
  130. $foo = 'bar';
  131. $ph = new sfNamespacedParameterHolder();
  132. $ph->setByRef('foo', $foo);
  133. $t->is($ph->get('foo'), $foo, '->setByRef() sets the value for a key');
  134. $foo = 'foo';
  135. $t->is($ph->get('foo'), $foo, '->setByRef() sets the value for a key as a reference');
  136. $myfoo = 'bar';
  137. $ph->setByRef('myfoo', $myfoo, 'symfony/mynamespace');
  138. $t->is($ph->get('myfoo', null, 'symfony/mynamespace'), $myfoo, '->setByRef() takes a namespace as its third parameter');
  139. // ->add()
  140. $t->diag('->add()');
  141. $foo = 'bar';
  142. $parameters = array('foo' => $foo, 'bar' => 'bar');
  143. $myparameters = array('myfoo' => 'bar', 'mybar' => 'bar');
  144. $ph = new sfNamespacedParameterHolder();
  145. $ph->add($parameters);
  146. $ph->add($myparameters, 'symfony/mynamespace');
  147. $t->is($ph->getAll(), $parameters, '->add() adds an array of parameters');
  148. $t->is($ph->getAll('symfony/mynamespace'), $myparameters, '->add() takes a namespace as its second argument');
  149. $foo = 'mybar';
  150. $t->is($ph->getAll(), $parameters, '->add() adds an array of parameters, not a reference');
  151. // ->addByRef()
  152. $t->diag('->addByRef()');
  153. $foo = 'bar';
  154. $parameters = array('foo' => &$foo, 'bar' => 'bar');
  155. $myparameters = array('myfoo' => 'bar', 'mybar' => 'bar');
  156. $ph = new sfNamespacedParameterHolder();
  157. $ph->addByRef($parameters);
  158. $ph->addByRef($myparameters, 'symfony/mynamespace');
  159. $t->is($parameters, $ph->getAll(), '->add() adds an array of parameters');
  160. $t->is($myparameters, $ph->getAll('symfony/mynamespace'), '->add() takes a namespace as its second argument');
  161. $foo = 'mybar';
  162. $t->is($parameters, $ph->getAll(), '->add() adds a reference of an array of parameters');
  163. // ->serialize() ->unserialize()
  164. $t->diag('->serialize() ->unserialize()');
  165. $t->ok($ph == unserialize(serialize($ph)), 'sfNamespacedParameterHolder implements the Serializable interface');
  166. // Array path as a key
  167. $t->diag('Array path as a key');
  168. $ph = new sfNamespacedParameterHolder();
  169. $ph->add(array('foo' => array('bar' => 'foo')));
  170. $t->is($ph->has('foo[bar]'), true, '->has() can takes a multi-array key');
  171. $t->is($ph->get('foo[bar]'), 'foo', '->has() can takes a multi-array key');
  172. $t->is($ph->remove('foo[bar]'), 'foo', '->remove() can takes a multi-array key');
  173. $t->is($ph->getAll(), array('foo' => array()), '->remove() can takes a multi-array key');