This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/tests/Symfony/Tests/Component/Form/PropertyPathTest.php
Jeremy Mikola df9ef79953 [Form] readPropertyPath should return null instead of empty array
When reading the last bit of a property path mapped to a missing array index, the method would initialize the value to an empty array.  This makes sense for cases where readPropertyPath would again be called recursively, but not when the value would be immediately returned (null would be preferable in that case).

For example, we have an object with a property called "options" that's an array of arbitrary key/value pairs.  That "options" property (and getOptions()) maps directly to a FieldGroup within the Form for this object.  That FieldGroup contains multiple TextFields for a few expected keys in the array.  As-is, if those keys were not defined, the default data set for those TextFields could end up being "Array" (string representation of an empty array).  If readPropertyPath instead returns null for this case, the default data would be transformed into an empty string.
2010-10-13 08:17:05 +02:00

287 lines
7.9 KiB
PHP

<?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 testGetValueReadsArrayWithMissingIndexForCustomPropertyPath()
{
$array = array('child' => array('index' => array()));
$path = new PropertyPath('child[index].firstName');
$this->assertNull($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);
}
}