PropertyPathTest.php 7.7 KB

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