123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\PropertyAccess\Tests;
- use Symfony\Component\PropertyAccess\PropertyPath;
- class PropertyPathTest extends \PHPUnit_Framework_TestCase
- {
- public function testToString()
- {
- $path = new PropertyPath('reference.traversable[index].property');
- $this->assertEquals('reference.traversable[index].property', $path->__toString());
- }
- /**
- * @expectedException \Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException
- */
- public function testDotIsRequiredBeforeProperty()
- {
- new PropertyPath('[index]property');
- }
- /**
- * @expectedException \Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException
- */
- public function testDotCannotBePresentAtTheBeginning()
- {
- new PropertyPath('.property');
- }
- public function providePathsContainingUnexpectedCharacters()
- {
- return array(
- array('property.'),
- array('property.['),
- array('property..'),
- array('property['),
- array('property[['),
- array('property[.'),
- array('property[]'),
- );
- }
- /**
- * @dataProvider providePathsContainingUnexpectedCharacters
- * @expectedException \Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException
- */
- public function testUnexpectedCharacters($path)
- {
- new PropertyPath($path);
- }
- /**
- * @expectedException \Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException
- */
- public function testPathCannotBeEmpty()
- {
- new PropertyPath('');
- }
- /**
- * @expectedException \Symfony\Component\PropertyAccess\Exception\InvalidArgumentException
- */
- public function testPathCannotBeNull()
- {
- new PropertyPath(null);
- }
- /**
- * @expectedException \Symfony\Component\PropertyAccess\Exception\InvalidArgumentException
- */
- public function testPathCannotBeFalse()
- {
- new PropertyPath(false);
- }
- public function testZeroIsValidPropertyPath()
- {
- new PropertyPath('0');
- }
- public function testGetParentWithDot()
- {
- $propertyPath = new PropertyPath('grandpa.parent.child');
- $this->assertEquals(new PropertyPath('grandpa.parent'), $propertyPath->getParent());
- }
- public function testGetParentWithIndex()
- {
- $propertyPath = new PropertyPath('grandpa.parent[child]');
- $this->assertEquals(new PropertyPath('grandpa.parent'), $propertyPath->getParent());
- }
- public function testGetParentWhenThereIsNoParent()
- {
- $propertyPath = new PropertyPath('path');
- $this->assertNull($propertyPath->getParent());
- }
- public function testCopyConstructor()
- {
- $propertyPath = new PropertyPath('grandpa.parent[child]');
- $copy = new PropertyPath($propertyPath);
- $this->assertEquals($propertyPath, $copy);
- }
- public function testGetElement()
- {
- $propertyPath = new PropertyPath('grandpa.parent[child]');
- $this->assertEquals('child', $propertyPath->getElement(2));
- }
- /**
- * @expectedException \OutOfBoundsException
- */
- public function testGetElementDoesNotAcceptInvalidIndices()
- {
- $propertyPath = new PropertyPath('grandpa.parent[child]');
- $propertyPath->getElement(3);
- }
- /**
- * @expectedException \OutOfBoundsException
- */
- public function testGetElementDoesNotAcceptNegativeIndices()
- {
- $propertyPath = new PropertyPath('grandpa.parent[child]');
- $propertyPath->getElement(-1);
- }
- public function testIsProperty()
- {
- $propertyPath = new PropertyPath('grandpa.parent[child]');
- $this->assertTrue($propertyPath->isProperty(1));
- $this->assertFalse($propertyPath->isProperty(2));
- }
- /**
- * @expectedException \OutOfBoundsException
- */
- public function testIsPropertyDoesNotAcceptInvalidIndices()
- {
- $propertyPath = new PropertyPath('grandpa.parent[child]');
- $propertyPath->isProperty(3);
- }
- /**
- * @expectedException \OutOfBoundsException
- */
- public function testIsPropertyDoesNotAcceptNegativeIndices()
- {
- $propertyPath = new PropertyPath('grandpa.parent[child]');
- $propertyPath->isProperty(-1);
- }
- public function testIsIndex()
- {
- $propertyPath = new PropertyPath('grandpa.parent[child]');
- $this->assertFalse($propertyPath->isIndex(1));
- $this->assertTrue($propertyPath->isIndex(2));
- }
- /**
- * @expectedException \OutOfBoundsException
- */
- public function testIsIndexDoesNotAcceptInvalidIndices()
- {
- $propertyPath = new PropertyPath('grandpa.parent[child]');
- $propertyPath->isIndex(3);
- }
- /**
- * @expectedException \OutOfBoundsException
- */
- public function testIsIndexDoesNotAcceptNegativeIndices()
- {
- $propertyPath = new PropertyPath('grandpa.parent[child]');
- $propertyPath->isIndex(-1);
- }
- }
|