PropertyPathTest.php 8.5 KB

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