PropertyPathTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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\Util\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 testGetValueReadsElementWithSpecialCharsExceptDOt()
  37. {
  38. $array = array('#!@$' => 'Bernhard');
  39. $path = new PropertyPath('#!@$');
  40. $this->assertEquals('Bernhard', $path->getValue($array));
  41. }
  42. public function testGetValueReadsNestedIndexWithSpecialChars()
  43. {
  44. $array = array('root' => array('#!@$.' => 'Bernhard'));
  45. $path = new PropertyPath('root[#!@$.]');
  46. $this->assertEquals('Bernhard', $path->getValue($array));
  47. }
  48. public function testGetValueReadsArrayWithCustomPropertyPath()
  49. {
  50. $array = array('child' => array('index' => array('firstName' => 'Bernhard')));
  51. $path = new PropertyPath('child[index].firstName');
  52. $this->assertEquals('Bernhard', $path->getValue($array));
  53. }
  54. public function testGetValueReadsArrayWithMissingIndexForCustomPropertyPath()
  55. {
  56. $array = array('child' => array('index' => array()));
  57. $path = new PropertyPath('child[index].firstName');
  58. $this->assertNull($path->getValue($array));
  59. }
  60. public function testGetValueReadsProperty()
  61. {
  62. $object = new Author();
  63. $object->firstName = 'Bernhard';
  64. $path = new PropertyPath('firstName');
  65. $this->assertEquals('Bernhard', $path->getValue($object));
  66. }
  67. public function testGetValueReadsPropertyWithCustomPropertyPath()
  68. {
  69. $object = new Author();
  70. $object->child = array();
  71. $object->child['index'] = new Author();
  72. $object->child['index']->firstName = 'Bernhard';
  73. $path = new PropertyPath('child[index].firstName');
  74. $this->assertEquals('Bernhard', $path->getValue($object));
  75. }
  76. public function testGetValueReadsArrayAccess()
  77. {
  78. $object = new \ArrayObject();
  79. $object['firstName'] = 'Bernhard';
  80. $path = new PropertyPath('[firstName]');
  81. $this->assertEquals('Bernhard', $path->getValue($object));
  82. }
  83. public function testGetValueThrowsExceptionIfArrayAccessExpected()
  84. {
  85. $path = new PropertyPath('[firstName]');
  86. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyException');
  87. $path->getValue(new Author());
  88. }
  89. public function testGetValueThrowsExceptionIfPropertyIsNotPublic()
  90. {
  91. $path = new PropertyPath('privateProperty');
  92. $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
  93. $path->getValue(new Author());
  94. }
  95. public function testGetValueReadsGetters()
  96. {
  97. $path = new PropertyPath('lastName');
  98. $object = new Author();
  99. $object->setLastName('Schussek');
  100. $this->assertEquals('Schussek', $path->getValue($object));
  101. }
  102. public function testGetValueCamelizesGetterNames()
  103. {
  104. $path = new PropertyPath('last_name');
  105. $object = new Author();
  106. $object->setLastName('Schussek');
  107. $this->assertEquals('Schussek', $path->getValue($object));
  108. }
  109. public function testGetValueThrowsExceptionIfGetterIsNotPublic()
  110. {
  111. $path = new PropertyPath('privateGetter');
  112. $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
  113. $path->getValue(new Author());
  114. }
  115. public function testGetValueReadsIssers()
  116. {
  117. $path = new PropertyPath('australian');
  118. $object = new Author();
  119. $object->setAustralian(false);
  120. $this->assertSame(false, $path->getValue($object));
  121. }
  122. public function testGetValueReadsMagicGet()
  123. {
  124. $path = new PropertyPath('magicProperty');
  125. $object = new Magician();
  126. $object->__set('magicProperty', 'foobar');
  127. $this->assertSame('foobar', $path->getValue($object));
  128. }
  129. public function testGetValueThrowsExceptionIfIsserIsNotPublic()
  130. {
  131. $path = new PropertyPath('privateIsser');
  132. $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
  133. $path->getValue(new Author());
  134. }
  135. public function testGetValueThrowsExceptionIfPropertyDoesNotExist()
  136. {
  137. $path = new PropertyPath('foobar');
  138. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyException');
  139. $path->getValue(new Author());
  140. }
  141. public function testGetValueThrowsExceptionIfNotObjectOrArray()
  142. {
  143. $path = new PropertyPath('foobar');
  144. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  145. $path->getValue('baz');
  146. }
  147. public function testGetValueThrowsExceptionIfNull()
  148. {
  149. $path = new PropertyPath('foobar');
  150. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  151. $path->getValue(null);
  152. }
  153. public function testGetValueThrowsExceptionIfEmpty()
  154. {
  155. $path = new PropertyPath('foobar');
  156. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  157. $path->getValue('');
  158. }
  159. public function testSetValueUpdatesArrays()
  160. {
  161. $array = array();
  162. $path = new PropertyPath('firstName');
  163. $path->setValue($array, 'Bernhard');
  164. $this->assertEquals(array('firstName' => 'Bernhard'), $array);
  165. }
  166. public function testSetValueUpdatesArraysWithCustomPropertyPath()
  167. {
  168. $array = array();
  169. $path = new PropertyPath('child[index].firstName');
  170. $path->setValue($array, 'Bernhard');
  171. $this->assertEquals(array('child' => array('index' => array('firstName' => 'Bernhard'))), $array);
  172. }
  173. public function testSetValueUpdatesProperties()
  174. {
  175. $object = new Author();
  176. $path = new PropertyPath('firstName');
  177. $path->setValue($object, 'Bernhard');
  178. $this->assertEquals('Bernhard', $object->firstName);
  179. }
  180. public function testSetValueUpdatesPropertiesWithCustomPropertyPath()
  181. {
  182. $object = new Author();
  183. $object->child = array();
  184. $object->child['index'] = new Author();
  185. $path = new PropertyPath('child[index].firstName');
  186. $path->setValue($object, 'Bernhard');
  187. $this->assertEquals('Bernhard', $object->child['index']->firstName);
  188. }
  189. public function testSetValueUpdatesArrayAccess()
  190. {
  191. $object = new \ArrayObject();
  192. $path = new PropertyPath('[firstName]');
  193. $path->setValue($object, 'Bernhard');
  194. $this->assertEquals('Bernhard', $object['firstName']);
  195. }
  196. public function testSetValueUpdateMagicSet()
  197. {
  198. $object = new Magician();
  199. $path = new PropertyPath('magicProperty');
  200. $path->setValue($object, 'foobar');
  201. $this->assertEquals('foobar', $object->__get('magicProperty'));
  202. }
  203. public function testSetValueThrowsExceptionIfArrayAccessExpected()
  204. {
  205. $path = new PropertyPath('[firstName]');
  206. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyException');
  207. $path->setValue(new Author(), 'Bernhard');
  208. }
  209. public function testSetValueUpdatesSetters()
  210. {
  211. $object = new Author();
  212. $path = new PropertyPath('lastName');
  213. $path->setValue($object, 'Schussek');
  214. $this->assertEquals('Schussek', $object->getLastName());
  215. }
  216. public function testSetValueCamelizesSetterNames()
  217. {
  218. $object = new Author();
  219. $path = new PropertyPath('last_name');
  220. $path->setValue($object, 'Schussek');
  221. $this->assertEquals('Schussek', $object->getLastName());
  222. }
  223. public function testSetValueThrowsExceptionIfGetterIsNotPublic()
  224. {
  225. $path = new PropertyPath('privateSetter');
  226. $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
  227. $path->setValue(new Author(), 'foobar');
  228. }
  229. public function testSetValueThrowsExceptionIfNotObjectOrArray()
  230. {
  231. $path = new PropertyPath('foobar');
  232. $value = 'baz';
  233. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  234. $path->setValue($value, 'bam');
  235. }
  236. public function testSetValueThrowsExceptionIfNull()
  237. {
  238. $path = new PropertyPath('foobar');
  239. $value = null;
  240. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  241. $path->setValue($value, 'bam');
  242. }
  243. public function testSetValueThrowsExceptionIfEmpty()
  244. {
  245. $path = new PropertyPath('foobar');
  246. $value = '';
  247. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  248. $path->setValue($value, 'bam');
  249. }
  250. public function testToString()
  251. {
  252. $path = new PropertyPath('reference.traversable[index].property');
  253. $this->assertEquals('reference.traversable[index].property', $path->__toString());
  254. }
  255. public function testInvalidPropertyPath_noDotBeforeProperty()
  256. {
  257. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  258. new PropertyPath('[index]property');
  259. }
  260. public function testInvalidPropertyPath_dotAtTheBeginning()
  261. {
  262. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  263. new PropertyPath('.property');
  264. }
  265. public function testInvalidPropertyPath_unexpectedCharacters()
  266. {
  267. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  268. new PropertyPath('property.$form');
  269. }
  270. public function testInvalidPropertyPath_null()
  271. {
  272. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  273. new PropertyPath(null);
  274. }
  275. }