PropertyPathTest.php 8.8 KB

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