PropertyPathTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\Form;
  11. require_once __DIR__ . '/Fixtures/Author.php';
  12. require_once __DIR__ . '/Fixtures/Magician.php';
  13. use Symfony\Component\Form\PropertyPath;
  14. use Symfony\Tests\Component\Form\Fixtures\Author;
  15. use Symfony\Tests\Component\Form\Fixtures\Magician;
  16. class PropertyPathTest extends \PHPUnit_Framework_TestCase
  17. {
  18. public function testGetValueReadsArray()
  19. {
  20. $array = array('firstName' => 'Bernhard');
  21. $path = new PropertyPath('firstName');
  22. $this->assertEquals('Bernhard', $path->getValue($array));
  23. }
  24. public function testGetValueReadsZeroIndex()
  25. {
  26. $array = array('Bernhard');
  27. $path = new PropertyPath('0');
  28. $this->assertEquals('Bernhard', $path->getValue($array));
  29. }
  30. public function testGetValueReadsIndexWithSpecialChars()
  31. {
  32. $array = array('#!@$.' => 'Bernhard');
  33. $path = new PropertyPath('[#!@$.]');
  34. $this->assertEquals('Bernhard', $path->getValue($array));
  35. }
  36. public function testGetValueReadsNestedIndexWithSpecialChars()
  37. {
  38. $array = array('root' => array('#!@$.' => 'Bernhard'));
  39. $path = new PropertyPath('root[#!@$.]');
  40. $this->assertEquals('Bernhard', $path->getValue($array));
  41. }
  42. public function testGetValueReadsArrayWithCustomPropertyPath()
  43. {
  44. $array = array('child' => array('index' => array('firstName' => 'Bernhard')));
  45. $path = new PropertyPath('child[index].firstName');
  46. $this->assertEquals('Bernhard', $path->getValue($array));
  47. }
  48. public function testGetValueReadsArrayWithMissingIndexForCustomPropertyPath()
  49. {
  50. $array = array('child' => array('index' => array()));
  51. $path = new PropertyPath('child[index].firstName');
  52. $this->assertNull($path->getValue($array));
  53. }
  54. public function testGetValueReadsProperty()
  55. {
  56. $object = new Author();
  57. $object->firstName = 'Bernhard';
  58. $path = new PropertyPath('firstName');
  59. $this->assertEquals('Bernhard', $path->getValue($object));
  60. }
  61. public function testGetValueReadsPropertyWithCustomPropertyPath()
  62. {
  63. $object = new Author();
  64. $object->child = array();
  65. $object->child['index'] = new Author();
  66. $object->child['index']->firstName = 'Bernhard';
  67. $path = new PropertyPath('child[index].firstName');
  68. $this->assertEquals('Bernhard', $path->getValue($object));
  69. }
  70. public function testGetValueReadsArrayAccess()
  71. {
  72. $object = new \ArrayObject();
  73. $object['firstName'] = 'Bernhard';
  74. $path = new PropertyPath('[firstName]');
  75. $this->assertEquals('Bernhard', $path->getValue($object));
  76. }
  77. public function testGetValueThrowsExceptionIfArrayAccessExpected()
  78. {
  79. $path = new PropertyPath('[firstName]');
  80. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyException');
  81. $path->getValue(new Author());
  82. }
  83. public function testGetValueThrowsExceptionIfPropertyIsNotPublic()
  84. {
  85. $path = new PropertyPath('privateProperty');
  86. $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
  87. $path->getValue(new Author());
  88. }
  89. public function testGetValueReadsGetters()
  90. {
  91. $path = new PropertyPath('lastName');
  92. $object = new Author();
  93. $object->setLastName('Schussek');
  94. $this->assertEquals('Schussek', $path->getValue($object));
  95. }
  96. public function testGetValueCamelizesGetterNames()
  97. {
  98. $path = new PropertyPath('last_name');
  99. $object = new Author();
  100. $object->setLastName('Schussek');
  101. $this->assertEquals('Schussek', $path->getValue($object));
  102. }
  103. public function testGetValueThrowsExceptionIfGetterIsNotPublic()
  104. {
  105. $path = new PropertyPath('privateGetter');
  106. $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
  107. $path->getValue(new Author());
  108. }
  109. public function testGetValueReadsIssers()
  110. {
  111. $path = new PropertyPath('australian');
  112. $object = new Author();
  113. $object->setAustralian(false);
  114. $this->assertSame(false, $path->getValue($object));
  115. }
  116. public function testGetValueReadsMagicGet()
  117. {
  118. $path = new PropertyPath('magicProperty');
  119. $object = new Magician();
  120. $object->__set('magicProperty', 'foobar');
  121. $this->assertSame('foobar', $path->getValue($object));
  122. }
  123. public function testGetValueThrowsExceptionIfIsserIsNotPublic()
  124. {
  125. $path = new PropertyPath('privateIsser');
  126. $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
  127. $path->getValue(new Author());
  128. }
  129. public function testGetValueThrowsExceptionIfPropertyDoesNotExist()
  130. {
  131. $path = new PropertyPath('foobar');
  132. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyException');
  133. $path->getValue(new Author());
  134. }
  135. public function testSetValueUpdatesArrays()
  136. {
  137. $array = array();
  138. $path = new PropertyPath('firstName');
  139. $path->setValue($array, 'Bernhard');
  140. $this->assertEquals(array('firstName' => 'Bernhard'), $array);
  141. }
  142. public function testSetValueUpdatesArraysWithCustomPropertyPath()
  143. {
  144. $array = array();
  145. $path = new PropertyPath('child[index].firstName');
  146. $path->setValue($array, 'Bernhard');
  147. $this->assertEquals(array('child' => array('index' => array('firstName' => 'Bernhard'))), $array);
  148. }
  149. public function testSetValueUpdatesProperties()
  150. {
  151. $object = new Author();
  152. $path = new PropertyPath('firstName');
  153. $path->setValue($object, 'Bernhard');
  154. $this->assertEquals('Bernhard', $object->firstName);
  155. }
  156. public function testSetValueUpdatesPropertiesWithCustomPropertyPath()
  157. {
  158. $object = new Author();
  159. $object->child = array();
  160. $object->child['index'] = new Author();
  161. $path = new PropertyPath('child[index].firstName');
  162. $path->setValue($object, 'Bernhard');
  163. $this->assertEquals('Bernhard', $object->child['index']->firstName);
  164. }
  165. public function testSetValueUpdatesArrayAccess()
  166. {
  167. $object = new \ArrayObject();
  168. $path = new PropertyPath('[firstName]');
  169. $path->setValue($object, 'Bernhard');
  170. $this->assertEquals('Bernhard', $object['firstName']);
  171. }
  172. public function testSetValueUpdateMagicSet()
  173. {
  174. $object = new Magician();
  175. $path = new PropertyPath('magicProperty');
  176. $path->setValue($object, 'foobar');
  177. $this->assertEquals('foobar', $object->__get('magicProperty'));
  178. }
  179. public function testSetValueThrowsExceptionIfArrayAccessExpected()
  180. {
  181. $path = new PropertyPath('[firstName]');
  182. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyException');
  183. $path->setValue(new Author(), 'Bernhard');
  184. }
  185. public function testSetValueUpdatesSetters()
  186. {
  187. $object = new Author();
  188. $path = new PropertyPath('lastName');
  189. $path->setValue($object, 'Schussek');
  190. $this->assertEquals('Schussek', $object->getLastName());
  191. }
  192. public function testSetValueCamelizesSetterNames()
  193. {
  194. $object = new Author();
  195. $path = new PropertyPath('last_name');
  196. $path->setValue($object, 'Schussek');
  197. $this->assertEquals('Schussek', $object->getLastName());
  198. }
  199. public function testSetValueThrowsExceptionIfGetterIsNotPublic()
  200. {
  201. $path = new PropertyPath('privateSetter');
  202. $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
  203. $path->setValue(new Author(), 'foobar');
  204. }
  205. public function testToString()
  206. {
  207. $path = new PropertyPath('reference.traversable[index].property');
  208. $this->assertEquals('reference.traversable[index].property', $path->__toString());
  209. }
  210. public function testInvalidPropertyPath_noDotBeforeProperty()
  211. {
  212. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  213. new PropertyPath('[index]property');
  214. }
  215. public function testInvalidPropertyPath_dotAtTheBeginning()
  216. {
  217. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  218. new PropertyPath('.property');
  219. }
  220. public function testInvalidPropertyPath_unexpectedCharacters()
  221. {
  222. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  223. new PropertyPath('property.$field');
  224. }
  225. public function testInvalidPropertyPath_empty()
  226. {
  227. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  228. new PropertyPath('');
  229. }
  230. public function testInvalidPropertyPath_null()
  231. {
  232. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  233. new PropertyPath(null);
  234. }
  235. }