PropertyPathTest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace Symfony\Tests\Component\Form;
  3. use Symfony\Component\Form\PropertyPath;
  4. class PropertyPathTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function testValidPropertyPath()
  7. {
  8. $path = new PropertyPath('reference.traversable[index].property');
  9. $this->assertEquals('reference', $path->getCurrent());
  10. $this->assertTrue($path->hasNext());
  11. $this->assertTrue($path->isProperty());
  12. $this->assertFalse($path->isIndex());
  13. $path->next();
  14. $this->assertEquals('traversable', $path->getCurrent());
  15. $this->assertTrue($path->hasNext());
  16. $this->assertTrue($path->isProperty());
  17. $this->assertFalse($path->isIndex());
  18. $path->next();
  19. $this->assertEquals('index', $path->getCurrent());
  20. $this->assertTrue($path->hasNext());
  21. $this->assertFalse($path->isProperty());
  22. $this->assertTrue($path->isIndex());
  23. $path->next();
  24. $this->assertEquals('property', $path->getCurrent());
  25. $this->assertFalse($path->hasNext());
  26. $this->assertTrue($path->isProperty());
  27. $this->assertFalse($path->isIndex());
  28. }
  29. public function testValidPropertyPath_zero()
  30. {
  31. $path = new PropertyPath('0');
  32. $this->assertEquals('0', $path->getCurrent());
  33. }
  34. public function testToString()
  35. {
  36. $path = new PropertyPath('reference.traversable[index].property');
  37. $this->assertEquals('reference.traversable[index].property', $path->__toString());
  38. }
  39. public function testInvalidPropertyPath_noDotBeforeProperty()
  40. {
  41. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  42. new PropertyPath('[index]property');
  43. }
  44. public function testInvalidPropertyPath_dotAtTheBeginning()
  45. {
  46. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  47. new PropertyPath('.property');
  48. }
  49. public function testInvalidPropertyPath_unexpectedCharacters()
  50. {
  51. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  52. new PropertyPath('property.$field');
  53. }
  54. public function testInvalidPropertyPath_empty()
  55. {
  56. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  57. new PropertyPath('');
  58. }
  59. public function testInvalidPropertyPath_null()
  60. {
  61. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
  62. new PropertyPath(null);
  63. }
  64. public function testNextThrowsExceptionIfNoNextElement()
  65. {
  66. $path = new PropertyPath('property');
  67. $this->setExpectedException('OutOfBoundsException');
  68. $path->next();
  69. }
  70. }