PropertyPathTest.php 2.5 KB

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