PropertyPathBuilderTest.php 7.7 KB

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