PropertyPathTest.php 7.9 KB

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