PropertyPathBuilderTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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\Component\PropertyAccess\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\PropertyAccess\PropertyPath;
  13. use Symfony\Component\PropertyAccess\PropertyPathBuilder;
  14. /**
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. */
  17. class PropertyPathBuilderTest extends TestCase
  18. {
  19. const PREFIX = 'old1[old2].old3[old4][old5].old6';
  20. /**
  21. * @var PropertyPathBuilder
  22. */
  23. private $builder;
  24. protected function setUp()
  25. {
  26. $this->builder = new PropertyPathBuilder(new PropertyPath(self::PREFIX));
  27. }
  28. public function testCreateEmpty()
  29. {
  30. $builder = new PropertyPathBuilder();
  31. $this->assertNull($builder->getPropertyPath());
  32. }
  33. public function testCreateCopyPath()
  34. {
  35. $this->assertEquals(new PropertyPath(self::PREFIX), $this->builder->getPropertyPath());
  36. }
  37. public function testAppendIndex()
  38. {
  39. $this->builder->appendIndex('new1');
  40. $path = new PropertyPath(self::PREFIX.'[new1]');
  41. $this->assertEquals($path, $this->builder->getPropertyPath());
  42. }
  43. public function testAppendProperty()
  44. {
  45. $this->builder->appendProperty('new1');
  46. $path = new PropertyPath(self::PREFIX.'.new1');
  47. $this->assertEquals($path, $this->builder->getPropertyPath());
  48. }
  49. public function testAppend()
  50. {
  51. $this->builder->append(new PropertyPath('new1[new2]'));
  52. $path = new PropertyPath(self::PREFIX.'.new1[new2]');
  53. $this->assertEquals($path, $this->builder->getPropertyPath());
  54. }
  55. public function testAppendUsingString()
  56. {
  57. $this->builder->append('new1[new2]');
  58. $path = new PropertyPath(self::PREFIX.'.new1[new2]');
  59. $this->assertEquals($path, $this->builder->getPropertyPath());
  60. }
  61. public function testAppendWithOffset()
  62. {
  63. $this->builder->append(new PropertyPath('new1[new2].new3'), 1);
  64. $path = new PropertyPath(self::PREFIX.'[new2].new3');
  65. $this->assertEquals($path, $this->builder->getPropertyPath());
  66. }
  67. public function testAppendWithOffsetAndLength()
  68. {
  69. $this->builder->append(new PropertyPath('new1[new2].new3'), 1, 1);
  70. $path = new PropertyPath(self::PREFIX.'[new2]');
  71. $this->assertEquals($path, $this->builder->getPropertyPath());
  72. }
  73. public function testReplaceByIndex()
  74. {
  75. $this->builder->replaceByIndex(1, 'new1');
  76. $path = new PropertyPath('old1[new1].old3[old4][old5].old6');
  77. $this->assertEquals($path, $this->builder->getPropertyPath());
  78. }
  79. public function testReplaceByIndexWithoutName()
  80. {
  81. $this->builder->replaceByIndex(0);
  82. $path = new PropertyPath('[old1][old2].old3[old4][old5].old6');
  83. $this->assertEquals($path, $this->builder->getPropertyPath());
  84. }
  85. /**
  86. * @expectedException \OutOfBoundsException
  87. */
  88. public function testReplaceByIndexDoesNotAllowInvalidOffsets()
  89. {
  90. $this->builder->replaceByIndex(6, 'new1');
  91. }
  92. /**
  93. * @expectedException \OutOfBoundsException
  94. */
  95. public function testReplaceByIndexDoesNotAllowNegativeOffsets()
  96. {
  97. $this->builder->replaceByIndex(-1, 'new1');
  98. }
  99. public function testReplaceByProperty()
  100. {
  101. $this->builder->replaceByProperty(1, 'new1');
  102. $path = new PropertyPath('old1.new1.old3[old4][old5].old6');
  103. $this->assertEquals($path, $this->builder->getPropertyPath());
  104. }
  105. public function testReplaceByPropertyWithoutName()
  106. {
  107. $this->builder->replaceByProperty(1);
  108. $path = new PropertyPath('old1.old2.old3[old4][old5].old6');
  109. $this->assertEquals($path, $this->builder->getPropertyPath());
  110. }
  111. /**
  112. * @expectedException \OutOfBoundsException
  113. */
  114. public function testReplaceByPropertyDoesNotAllowInvalidOffsets()
  115. {
  116. $this->builder->replaceByProperty(6, 'new1');
  117. }
  118. /**
  119. * @expectedException \OutOfBoundsException
  120. */
  121. public function testReplaceByPropertyDoesNotAllowNegativeOffsets()
  122. {
  123. $this->builder->replaceByProperty(-1, 'new1');
  124. }
  125. public function testReplace()
  126. {
  127. $this->builder->replace(1, 1, new PropertyPath('new1[new2].new3'));
  128. $path = new PropertyPath('old1.new1[new2].new3.old3[old4][old5].old6');
  129. $this->assertEquals($path, $this->builder->getPropertyPath());
  130. }
  131. public function testReplaceUsingString()
  132. {
  133. $this->builder->replace(1, 1, 'new1[new2].new3');
  134. $path = new PropertyPath('old1.new1[new2].new3.old3[old4][old5].old6');
  135. $this->assertEquals($path, $this->builder->getPropertyPath());
  136. }
  137. public function testReplaceNegative()
  138. {
  139. $this->builder->replace(-1, 1, new PropertyPath('new1[new2].new3'));
  140. $path = new PropertyPath('old1[old2].old3[old4][old5].new1[new2].new3');
  141. $this->assertEquals($path, $this->builder->getPropertyPath());
  142. }
  143. /**
  144. * @dataProvider provideInvalidOffsets
  145. * @expectedException \OutOfBoundsException
  146. */
  147. public function testReplaceDoesNotAllowInvalidOffsets($offset)
  148. {
  149. $this->builder->replace($offset, 1, new PropertyPath('new1[new2].new3'));
  150. }
  151. public function provideInvalidOffsets()
  152. {
  153. return array(
  154. array(6),
  155. array(-7),
  156. );
  157. }
  158. public function testReplaceWithLengthGreaterOne()
  159. {
  160. $this->builder->replace(0, 2, new PropertyPath('new1[new2].new3'));
  161. $path = new PropertyPath('new1[new2].new3.old3[old4][old5].old6');
  162. $this->assertEquals($path, $this->builder->getPropertyPath());
  163. }
  164. public function testReplaceSubstring()
  165. {
  166. $this->builder->replace(1, 1, new PropertyPath('new1[new2].new3.new4[new5]'), 1, 3);
  167. $path = new PropertyPath('old1[new2].new3.new4.old3[old4][old5].old6');
  168. $this->assertEquals($path, $this->builder->getPropertyPath());
  169. }
  170. public function testReplaceSubstringWithLengthGreaterOne()
  171. {
  172. $this->builder->replace(1, 2, new PropertyPath('new1[new2].new3.new4[new5]'), 1, 3);
  173. $path = new PropertyPath('old1[new2].new3.new4[old4][old5].old6');
  174. $this->assertEquals($path, $this->builder->getPropertyPath());
  175. }
  176. // https://github.com/symfony/symfony/issues/5605
  177. public function testReplaceWithLongerPath()
  178. {
  179. // error occurs when path contains at least two more elements
  180. // than the builder
  181. $path = new PropertyPath('new1.new2.new3');
  182. $builder = new PropertyPathBuilder(new PropertyPath('old1'));
  183. $builder->replace(0, 1, $path);
  184. $this->assertEquals($path, $builder->getPropertyPath());
  185. }
  186. public function testReplaceWithLongerPathKeepsOrder()
  187. {
  188. $path = new PropertyPath('new1.new2.new3');
  189. $expected = new PropertyPath('new1.new2.new3.old2');
  190. $builder = new PropertyPathBuilder(new PropertyPath('old1.old2'));
  191. $builder->replace(0, 1, $path);
  192. $this->assertEquals($expected, $builder->getPropertyPath());
  193. }
  194. public function testRemove()
  195. {
  196. $this->builder->remove(3);
  197. $path = new PropertyPath('old1[old2].old3[old5].old6');
  198. $this->assertEquals($path, $this->builder->getPropertyPath());
  199. }
  200. /**
  201. * @expectedException \OutOfBoundsException
  202. */
  203. public function testRemoveDoesNotAllowInvalidOffsets()
  204. {
  205. $this->builder->remove(6);
  206. }
  207. /**
  208. * @expectedException \OutOfBoundsException
  209. */
  210. public function testRemoveDoesNotAllowNegativeOffsets()
  211. {
  212. $this->builder->remove(-1);
  213. }
  214. }