PropertyPathTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 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 testGetValueThrowsExceptionIfNotObjectOrArray()
  136. {
  137. $path = new PropertyPath('foobar');
  138. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  139. $path->getValue('baz');
  140. }
  141. public function testGetValueThrowsExceptionIfNull()
  142. {
  143. $path = new PropertyPath('foobar');
  144. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  145. $path->getValue(null);
  146. }
  147. public function testGetValueThrowsExceptionIfEmpty()
  148. {
  149. $path = new PropertyPath('foobar');
  150. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  151. $path->getValue('');
  152. }
  153. public function testSetValueUpdatesArrays()
  154. {
  155. $array = array();
  156. $path = new PropertyPath('firstName');
  157. $path->setValue($array, 'Bernhard');
  158. $this->assertEquals(array('firstName' => 'Bernhard'), $array);
  159. }
  160. public function testSetValueUpdatesArraysWithCustomPropertyPath()
  161. {
  162. $array = array();
  163. $path = new PropertyPath('child[index].firstName');
  164. $path->setValue($array, 'Bernhard');
  165. $this->assertEquals(array('child' => array('index' => array('firstName' => 'Bernhard'))), $array);
  166. }
  167. public function testSetValueUpdatesProperties()
  168. {
  169. $object = new Author();
  170. $path = new PropertyPath('firstName');
  171. $path->setValue($object, 'Bernhard');
  172. $this->assertEquals('Bernhard', $object->firstName);
  173. }
  174. public function testSetValueUpdatesPropertiesWithCustomPropertyPath()
  175. {
  176. $object = new Author();
  177. $object->child = array();
  178. $object->child['index'] = new Author();
  179. $path = new PropertyPath('child[index].firstName');
  180. $path->setValue($object, 'Bernhard');
  181. $this->assertEquals('Bernhard', $object->child['index']->firstName);
  182. }
  183. public function testSetValueUpdatesArrayAccess()
  184. {
  185. $object = new \ArrayObject();
  186. $path = new PropertyPath('[firstName]');
  187. $path->setValue($object, 'Bernhard');
  188. $this->assertEquals('Bernhard', $object['firstName']);
  189. }
  190. public function testSetValueUpdateMagicSet()
  191. {
  192. $object = new Magician();
  193. $path = new PropertyPath('magicProperty');
  194. $path->setValue($object, 'foobar');
  195. $this->assertEquals('foobar', $object->__get('magicProperty'));
  196. }
  197. public function testSetValueThrowsExceptionIfArrayAccessExpected()
  198. {
  199. $path = new PropertyPath('[firstName]');
  200. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyException');
  201. $path->setValue(new Author(), 'Bernhard');
  202. }
  203. public function testSetValueUpdatesSetters()
  204. {
  205. $object = new Author();
  206. $path = new PropertyPath('lastName');
  207. $path->setValue($object, 'Schussek');
  208. $this->assertEquals('Schussek', $object->getLastName());
  209. }
  210. public function testSetValueCamelizesSetterNames()
  211. {
  212. $object = new Author();
  213. $path = new PropertyPath('last_name');
  214. $path->setValue($object, 'Schussek');
  215. $this->assertEquals('Schussek', $object->getLastName());
  216. }
  217. public function testSetValueThrowsExceptionIfGetterIsNotPublic()
  218. {
  219. $path = new PropertyPath('privateSetter');
  220. $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
  221. $path->setValue(new Author(), 'foobar');
  222. }
  223. public function testSetValueThrowsExceptionIfNotObjectOrArray()
  224. {
  225. $path = new PropertyPath('foobar');
  226. $value = 'baz';
  227. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  228. $path->setValue($value, 'bam');
  229. }
  230. public function testSetValueThrowsExceptionIfNull()
  231. {
  232. $path = new PropertyPath('foobar');
  233. $value = null;
  234. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  235. $path->setValue($value, 'bam');
  236. }
  237. public function testSetValueThrowsExceptionIfEmpty()
  238. {
  239. $path = new PropertyPath('foobar');
  240. $value = '';
  241. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  242. $path->setValue($value, 'bam');
  243. }
  244. public function testToString()
  245. {
  246. $path = new PropertyPath('reference.traversable[index].property');
  247. $this->assertEquals('reference.traversable[index].property', $path->__toString());
  248. }
  249. public function testInvalidPropertyPath_noDotBeforeProperty()
  250. {
  251. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  252. new PropertyPath('[index]property');
  253. }
  254. public function testInvalidPropertyPath_dotAtTheBeginning()
  255. {
  256. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  257. new PropertyPath('.property');
  258. }
  259. public function testInvalidPropertyPath_unexpectedCharacters()
  260. {
  261. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  262. new PropertyPath('property.$form');
  263. }
  264. public function testInvalidPropertyPath_empty()
  265. {
  266. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  267. new PropertyPath('');
  268. }
  269. public function testInvalidPropertyPath_null()
  270. {
  271. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  272. new PropertyPath(null);
  273. }
  274. }