PropertyPathTest.php 5.2 KB

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