PropertyPathTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. class PropertyPathTest extends TestCase
  14. {
  15. public function testToString()
  16. {
  17. $path = new PropertyPath('reference.traversable[index].property');
  18. $this->assertEquals('reference.traversable[index].property', $path->__toString());
  19. }
  20. /**
  21. * @expectedException \Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException
  22. */
  23. public function testDotIsRequiredBeforeProperty()
  24. {
  25. new PropertyPath('[index]property');
  26. }
  27. /**
  28. * @expectedException \Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException
  29. */
  30. public function testDotCannotBePresentAtTheBeginning()
  31. {
  32. new PropertyPath('.property');
  33. }
  34. public function providePathsContainingUnexpectedCharacters()
  35. {
  36. return array(
  37. array('property.'),
  38. array('property.['),
  39. array('property..'),
  40. array('property['),
  41. array('property[['),
  42. array('property[.'),
  43. array('property[]'),
  44. );
  45. }
  46. /**
  47. * @dataProvider providePathsContainingUnexpectedCharacters
  48. * @expectedException \Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException
  49. */
  50. public function testUnexpectedCharacters($path)
  51. {
  52. new PropertyPath($path);
  53. }
  54. /**
  55. * @expectedException \Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException
  56. */
  57. public function testPathCannotBeEmpty()
  58. {
  59. new PropertyPath('');
  60. }
  61. /**
  62. * @expectedException \Symfony\Component\PropertyAccess\Exception\InvalidArgumentException
  63. */
  64. public function testPathCannotBeNull()
  65. {
  66. new PropertyPath(null);
  67. }
  68. /**
  69. * @expectedException \Symfony\Component\PropertyAccess\Exception\InvalidArgumentException
  70. */
  71. public function testPathCannotBeFalse()
  72. {
  73. new PropertyPath(false);
  74. }
  75. public function testZeroIsValidPropertyPath()
  76. {
  77. $propertyPath = new PropertyPath('0');
  78. $this->assertSame('0', (string) $propertyPath);
  79. }
  80. public function testGetParentWithDot()
  81. {
  82. $propertyPath = new PropertyPath('grandpa.parent.child');
  83. $this->assertEquals(new PropertyPath('grandpa.parent'), $propertyPath->getParent());
  84. }
  85. public function testGetParentWithIndex()
  86. {
  87. $propertyPath = new PropertyPath('grandpa.parent[child]');
  88. $this->assertEquals(new PropertyPath('grandpa.parent'), $propertyPath->getParent());
  89. }
  90. public function testGetParentWhenThereIsNoParent()
  91. {
  92. $propertyPath = new PropertyPath('path');
  93. $this->assertNull($propertyPath->getParent());
  94. }
  95. public function testCopyConstructor()
  96. {
  97. $propertyPath = new PropertyPath('grandpa.parent[child]');
  98. $copy = new PropertyPath($propertyPath);
  99. $this->assertEquals($propertyPath, $copy);
  100. }
  101. public function testGetElement()
  102. {
  103. $propertyPath = new PropertyPath('grandpa.parent[child]');
  104. $this->assertEquals('child', $propertyPath->getElement(2));
  105. }
  106. /**
  107. * @expectedException \OutOfBoundsException
  108. */
  109. public function testGetElementDoesNotAcceptInvalidIndices()
  110. {
  111. $propertyPath = new PropertyPath('grandpa.parent[child]');
  112. $propertyPath->getElement(3);
  113. }
  114. /**
  115. * @expectedException \OutOfBoundsException
  116. */
  117. public function testGetElementDoesNotAcceptNegativeIndices()
  118. {
  119. $propertyPath = new PropertyPath('grandpa.parent[child]');
  120. $propertyPath->getElement(-1);
  121. }
  122. public function testIsProperty()
  123. {
  124. $propertyPath = new PropertyPath('grandpa.parent[child]');
  125. $this->assertTrue($propertyPath->isProperty(1));
  126. $this->assertFalse($propertyPath->isProperty(2));
  127. }
  128. /**
  129. * @expectedException \OutOfBoundsException
  130. */
  131. public function testIsPropertyDoesNotAcceptInvalidIndices()
  132. {
  133. $propertyPath = new PropertyPath('grandpa.parent[child]');
  134. $propertyPath->isProperty(3);
  135. }
  136. /**
  137. * @expectedException \OutOfBoundsException
  138. */
  139. public function testIsPropertyDoesNotAcceptNegativeIndices()
  140. {
  141. $propertyPath = new PropertyPath('grandpa.parent[child]');
  142. $propertyPath->isProperty(-1);
  143. }
  144. public function testIsIndex()
  145. {
  146. $propertyPath = new PropertyPath('grandpa.parent[child]');
  147. $this->assertFalse($propertyPath->isIndex(1));
  148. $this->assertTrue($propertyPath->isIndex(2));
  149. }
  150. /**
  151. * @expectedException \OutOfBoundsException
  152. */
  153. public function testIsIndexDoesNotAcceptInvalidIndices()
  154. {
  155. $propertyPath = new PropertyPath('grandpa.parent[child]');
  156. $propertyPath->isIndex(3);
  157. }
  158. /**
  159. * @expectedException \OutOfBoundsException
  160. */
  161. public function testIsIndexDoesNotAcceptNegativeIndices()
  162. {
  163. $propertyPath = new PropertyPath('grandpa.parent[child]');
  164. $propertyPath->isIndex(-1);
  165. }
  166. }