123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- <?php
- namespace Symfony\Tests\Component\Form;
- require_once __DIR__ . '/Fixtures/Author.php';
- use Symfony\Component\Form\PropertyPath;
- use Symfony\Tests\Component\Form\Fixtures\Author;
- class PropertyPathTest extends \PHPUnit_Framework_TestCase
- {
- public function testGetValueReadsArray()
- {
- $array = array('firstName' => 'Bernhard');
- $path = new PropertyPath('firstName');
- $this->assertEquals('Bernhard', $path->getValue($array));
- }
- public function testGetValueReadsZeroIndex()
- {
- $array = array('Bernhard');
- $path = new PropertyPath('0');
- $this->assertEquals('Bernhard', $path->getValue($array));
- }
- public function testGetValueReadsArrayWithCustomPropertyPath()
- {
- $array = array('child' => array('index' => array('firstName' => 'Bernhard')));
- $path = new PropertyPath('child[index].firstName');
- $this->assertEquals('Bernhard', $path->getValue($array));
- }
- public function testGetValueReadsProperty()
- {
- $object = new Author();
- $object->firstName = 'Bernhard';
- $path = new PropertyPath('firstName');
- $this->assertEquals('Bernhard', $path->getValue($object));
- }
- public function testGetValueReadsPropertyWithCustomPropertyPath()
- {
- $object = new Author();
- $object->child = array();
- $object->child['index'] = new Author();
- $object->child['index']->firstName = 'Bernhard';
- $path = new PropertyPath('child[index].firstName');
- $this->assertEquals('Bernhard', $path->getValue($object));
- }
- public function testGetValueReadsArrayAccess()
- {
- $object = new \ArrayObject();
- $object['firstName'] = 'Bernhard';
- $path = new PropertyPath('[firstName]');
- $this->assertEquals('Bernhard', $path->getValue($object));
- }
- public function testGetValueThrowsExceptionIfArrayAccessExpected()
- {
- $path = new PropertyPath('[firstName]');
- $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyException');
- $path->getValue(new Author());
- }
- public function testGetValueThrowsExceptionIfPropertyIsNotPublic()
- {
- $path = new PropertyPath('privateProperty');
- $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
- $path->getValue(new Author());
- }
- public function testGetValueReadsGetters()
- {
- $path = new PropertyPath('lastName');
- $object = new Author();
- $object->setLastName('Schussek');
- $this->assertEquals('Schussek', $path->getValue($object));
- }
- public function testGetValueCamelizesGetterNames()
- {
- $path = new PropertyPath('last_name');
- $object = new Author();
- $object->setLastName('Schussek');
- $this->assertEquals('Schussek', $path->getValue($object));
- }
- public function testGetValueThrowsExceptionIfGetterIsNotPublic()
- {
- $path = new PropertyPath('privateGetter');
- $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
- $path->getValue(new Author());
- }
- public function testGetValueReadsIssers()
- {
- $path = new PropertyPath('australian');
- $object = new Author();
- $object->setAustralian(false);
- $this->assertSame(false, $path->getValue($object));
- }
- public function testGetValueThrowsExceptionIfIsserIsNotPublic()
- {
- $path = new PropertyPath('privateIsser');
- $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
- $path->getValue(new Author());
- }
- public function testGetValueThrowsExceptionIfPropertyDoesNotExist()
- {
- $path = new PropertyPath('foobar');
- $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyException');
- $path->getValue(new Author());
- }
- public function testSetValueUpdatesArrays()
- {
- $array = array();
- $path = new PropertyPath('firstName');
- $path->setValue($array, 'Bernhard');
- $this->assertEquals(array('firstName' => 'Bernhard'), $array);
- }
- public function testSetValueUpdatesArraysWithCustomPropertyPath()
- {
- $array = array();
- $path = new PropertyPath('child[index].firstName');
- $path->setValue($array, 'Bernhard');
- $this->assertEquals(array('child' => array('index' => array('firstName' => 'Bernhard'))), $array);
- }
- public function testSetValueUpdatesProperties()
- {
- $object = new Author();
- $path = new PropertyPath('firstName');
- $path->setValue($object, 'Bernhard');
- $this->assertEquals('Bernhard', $object->firstName);
- }
- public function testSetValueUpdatesPropertiesWithCustomPropertyPath()
- {
- $object = new Author();
- $object->child = array();
- $object->child['index'] = new Author();
- $path = new PropertyPath('child[index].firstName');
- $path->setValue($object, 'Bernhard');
- $this->assertEquals('Bernhard', $object->child['index']->firstName);
- }
- public function testSetValueUpdatesArrayAccess()
- {
- $object = new \ArrayObject();
- $path = new PropertyPath('[firstName]');
- $path->setValue($object, 'Bernhard');
- $this->assertEquals('Bernhard', $object['firstName']);
- }
- public function testSetValueThrowsExceptionIfArrayAccessExpected()
- {
- $path = new PropertyPath('[firstName]');
- $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyException');
- $path->setValue(new Author(), 'Bernhard');
- }
- public function testSetValueUpdatesSetters()
- {
- $object = new Author();
- $path = new PropertyPath('lastName');
- $path->setValue($object, 'Schussek');
- $this->assertEquals('Schussek', $object->getLastName());
- }
- public function testSetValueCamelizesSetterNames()
- {
- $object = new Author();
- $path = new PropertyPath('last_name');
- $path->setValue($object, 'Schussek');
- $this->assertEquals('Schussek', $object->getLastName());
- }
- public function testSetValueThrowsExceptionIfGetterIsNotPublic()
- {
- $path = new PropertyPath('privateSetter');
- $this->setExpectedException('Symfony\Component\Form\Exception\PropertyAccessDeniedException');
- $path->setValue(new Author(), 'foobar');
- }
- public function testToString()
- {
- $path = new PropertyPath('reference.traversable[index].property');
- $this->assertEquals('reference.traversable[index].property', $path->__toString());
- }
- public function testInvalidPropertyPath_noDotBeforeProperty()
- {
- $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
- new PropertyPath('[index]property');
- }
- public function testInvalidPropertyPath_dotAtTheBeginning()
- {
- $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
- new PropertyPath('.property');
- }
- public function testInvalidPropertyPath_unexpectedCharacters()
- {
- $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
- new PropertyPath('property.$field');
- }
- public function testInvalidPropertyPath_empty()
- {
- $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
- new PropertyPath('');
- }
- public function testInvalidPropertyPath_null()
- {
- $this->setExpectedException('Symfony\Component\Form\Exception\InvalidPropertyPathException');
- new PropertyPath(null);
- }
- }
|