PropertyPathTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 testGetValueThrowsExceptionIfGetterIsNotPublic()
  69. {
  70. $path = new PropertyPath('privateGetter');
  71. $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
  72. $path->getValue(new Author());
  73. }
  74. public function testGetValueReadsIssers()
  75. {
  76. $path = new PropertyPath('australian');
  77. $object = new Author();
  78. $object->setAustralian(false);
  79. $this->assertSame(false, $path->getValue($object));
  80. }
  81. public function testGetValueThrowsExceptionIfIsserIsNotPublic()
  82. {
  83. $path = new PropertyPath('privateIsser');
  84. $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
  85. $path->getValue(new Author());
  86. }
  87. public function testGetValueThrowsExceptionIfPropertyDoesNotExist()
  88. {
  89. $path = new PropertyPath('foobar');
  90. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyException');
  91. $path->getValue(new Author());
  92. }
  93. public function testSetValueUpdatesArrays()
  94. {
  95. $array = array();
  96. $path = new PropertyPath('firstName');
  97. $path->setValue($array, 'Bernhard');
  98. $this->assertEquals(array('firstName' => 'Bernhard'), $array);
  99. }
  100. public function testSetValueUpdatesArraysWithCustomPropertyPath()
  101. {
  102. $array = array();
  103. $path = new PropertyPath('child[index].firstName');
  104. $path->setValue($array, 'Bernhard');
  105. $this->assertEquals(array('child' => array('index' => array('firstName' => 'Bernhard'))), $array);
  106. }
  107. public function testSetValueUpdatesProperties()
  108. {
  109. $object = new Author();
  110. $path = new PropertyPath('firstName');
  111. $path->setValue($object, 'Bernhard');
  112. $this->assertEquals('Bernhard', $object->firstName);
  113. }
  114. public function testSetValueUpdatesPropertiesWithCustomPropertyPath()
  115. {
  116. $object = new Author();
  117. $object->child = array();
  118. $object->child['index'] = new Author();
  119. $path = new PropertyPath('child[index].firstName');
  120. $path->setValue($object, 'Bernhard');
  121. $this->assertEquals('Bernhard', $object->child['index']->firstName);
  122. }
  123. public function testSetValueUpdatesArrayAccess()
  124. {
  125. $object = new \ArrayObject();
  126. $path = new PropertyPath('[firstName]');
  127. $path->setValue($object, 'Bernhard');
  128. $this->assertEquals('Bernhard', $object['firstName']);
  129. }
  130. public function testSetValueThrowsExceptionIfArrayAccessExpected()
  131. {
  132. $path = new PropertyPath('[firstName]');
  133. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyException');
  134. $path->setValue(new Author(), 'Bernhard');
  135. }
  136. public function testSetValueUpdatesSetters()
  137. {
  138. $object = new Author();
  139. $path = new PropertyPath('lastName');
  140. $path->setValue($object, 'Schussek');
  141. $this->assertEquals('Schussek', $object->getLastName());
  142. }
  143. public function testSetValueThrowsExceptionIfGetterIsNotPublic()
  144. {
  145. $path = new PropertyPath('privateSetter');
  146. $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
  147. $path->setValue(new Author(), 'foobar');
  148. }
  149. public function testToString()
  150. {
  151. $path = new PropertyPath('reference.traversable[index].property');
  152. $this->assertEquals('reference.traversable[index].property', $path->__toString());
  153. }
  154. public function testInvalidPropertyPath_noDotBeforeProperty()
  155. {
  156. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  157. new PropertyPath('[index]property');
  158. }
  159. public function testInvalidPropertyPath_dotAtTheBeginning()
  160. {
  161. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  162. new PropertyPath('.property');
  163. }
  164. public function testInvalidPropertyPath_unexpectedCharacters()
  165. {
  166. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  167. new PropertyPath('property.$field');
  168. }
  169. public function testInvalidPropertyPath_empty()
  170. {
  171. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  172. new PropertyPath('');
  173. }
  174. public function testInvalidPropertyPath_null()
  175. {
  176. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  177. new PropertyPath(null);
  178. }
  179. }