8000 [2.4][PropertyAccess] Fixed getValue() when accessing non-existing indices of ArrayAccess implementations by webmozart · Pull Request #10947 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[2.4][PropertyAccess] Fixed getValue() when accessing non-existing indices of ArrayAccess implementations #10947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 21, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next N 10000 ext commit
[PropertyAccess] Refactored PropertyAccessorCollectionTest
  • Loading branch information
webmozart committed May 20, 2014
commit 91ee82107b367aa553bf72629fca8c2dea8d984f
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,6 @@ public function getAxes()
}
}

class PropertyAccessorCollectionTest_CarCustomSingular
{
public function addFoo($axis) {}

public function removeFoo($axis) {}

public function getAxes() {}
}

class PropertyAccessorCollectionTest_Engine
{
}

class PropertyAccessorCollectionTest_CarOnlyAdder
{
public function addAxis($axis) {}
Expand All @@ -79,13 +66,6 @@ class PropertyAccessorCollectionTest_CarNoAdderAndRemover
public function getAxes() {}
}

class PropertyAccessorCollectionTest_CarNoAdderAndRemoverWithProperty
{
protected $axes = array();

public function getAxes() {}
}

class PropertyAccessorCollectionTest_CompositeCar
{
public function getStructure() {}
Expand Down Expand Up @@ -116,52 +96,34 @@ protected function setUp()

abstract protected function getCollection(array $array);

public function testGetValueReadsArrayAccess()
public function getValidPropertyPaths()
{
$object = $this->getCollection(array('firstName' => 'Bernhard'));

$this->assertEquals('Bernhard', $this->propertyAccessor->getValue($object, '[firstName]'));
}

public function testGetValueReadsNestedArrayAccess()
{
$object = $this->getCollection(array('person' => array('firstName' => 'Bernhard')));

$this->assertEquals('Bernhard', $this->propertyAccessor->getValue($object, '[person][firstName]'));
return array(
array(array('firstName' => 'Bernhard'), '[firstName]', 'Bernhard'),
array(array('person' => array('firstName' => 'Bernhard')), '[person][firstName]', 'Bernhard'),
);
}

/**
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException
* @dataProvider getValidPropertyPaths
*/
public function testGetValueThrowsExceptionIfArrayAccessExpected()
{
$this->propertyAccessor->getValue(new \stdClass(), '[firstName]');
}

public function testSetValueUpdatesArrayAccess()
{
< 10000 /td> $object = $this->getCollection(array());

$this->propertyAccessor->setValue($object, '[firstName]', 'Bernhard');

$this->assertEquals('Bernhard', $object['firstName']);
}

public function testSetValueUpdatesNestedArrayAccess()
public function testGetValue(array $array, $path, $value)
{
$object = $this->getCollection(array());
$collection = $this->getCollection($array);

$this->propertyAccessor->setValue($object, '[person][firstName]', 'Bernhard');

$this->assertEquals('Bernhard', $object['person']['firstName']);
$this->assertSame($value, $this->propertyAccessor->getValue($collection, $path));
}

/**
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException
* @dataProvider getValidPropertyPaths
*/
public function testSetValueThrowsExceptionIfArrayAccessExpected()
public function testSetValue(array $array, $path)
{
$this->propertyAccessor->setValue(new \stdClass(), '[firstName]', 'Bernhard');
$collection = $this->getCollection($array);

$this->propertyAccessor->setValue($collection, $path, 'Updated');

$this->assertSame('Updated', $this->propertyAccessor->getValue($collection, $path));
}

public function testSetValueCallsAdderAndRemoverForCollections()
Expand Down Expand Up @@ -210,32 +172,9 @@ public function testSetValueCallsAdderAndRemoverForNestedCollections()
$this->propertyAccessor->setValue($car, 'structure.axes', $axesAfter);
}

public function testSetValueCallsCustomAdderAndRemover()
{
$this->markTestSkipped('This feature is temporarily disabled as of 2.1');

$car = $this->getMock(__CLASS__.'_CarCustomSingular');
$axesBefore = $this->getCollection(array(1 => 'second', 3 => 'fourth'));
$axesAfter = $this->getCollection(array(0 => 'first', 1 => 'second', 2 => 'third'));

$car->expects($this->at(0))
->method('getAxes')
->will($this->returnValue($axesBefore));
$car->expects($this->at(1))
->method('removeFoo')
->with('fourth');
$car->expects($this->at(2))
->method('addFoo')
->with('first');
$car->expects($this->at(3))
->method('addFoo')
->with('third');

$this->propertyAccessor->setValue($car, 'axes|foo', $axesAfter);
}

/**
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException
* @expectedExceptionMessage Found the public method "addAxis()", but did not find a public "removeAxis()" on class Mock_PropertyAccessorCollectionTest_CarOnlyAdder
*/
public function testSetValueFailsIfOnlyAdderFound()
{
Expand All @@ -252,6 +191,7 @@ public function testSetValueFailsIfOnlyAdderFound()

/**
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException
* @expectedExceptionMessage Found the public method "removeAxis()", but did not find a public "addAxis()" on class Mock_PropertyAccessorCollectionTest_CarOnlyRemover
*/
public function testSetValueFailsIfOnlyRemoverFound()
{
Expand All @@ -267,58 +207,14 @@ public function testSetValueFailsIfOnlyRemoverFound()
}

/**
* @dataProvider noAdderRemoverData
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException
* @expectedExceptionMessage Neither the property "axes" nor one of the methods "addAx()", "addAxe()", "addAxis()", "setAxes()", "__set()" or "__call()" exist and have public access in class "Mock_PropertyAccessorCollectionTest_CarNoAdderAndRemover
*/
public function testNoAdderAndRemoverThrowsSensibleError($car, $path, $message)
{
$axes = $this->getCollection(array(0 => 'first', 1 => 'second', 2 => 'third'));

try {
$this->propertyAccessor->setValue($car, $path, $axes);
$this->fail('An expected exception was not thrown!');
} catch (ExceptionInterface $e) {
$this->assertEquals($message, $e->getMessage());
}
}

public function noAdderRemoverData()
public function testSetValueFailsIfNoAdderAndNoRemoverFound()
{
$data = array();

$car = $this->getMock(__CLASS__.'_CarNoAdderAndRemover');
$propertyPath = 'axes';
$expectedMessage = sprintf(
'Neither the property "axes" nor one of the methods "addAx()", '.
'"addAxe()", "addAxis()", "setAxes()", "__set()" or "__call()" exist and have '.
'public access in class "%s".',
get_class($car)
);
$data[] = array($car, $propertyPath, $expectedMessage);

/*
Temporarily disabled in 2.1

$propertyPath = new PropertyPath('axes|boo');
$expectedMessage = sprintf(
'Neither element "axes" nor method "setAxes()" exists in class '
.'"%s", nor could adders and removers be found based on the '
.'passed singular: %s',
get_class($car),
'boo'
);
$data[] = array($car, $propertyPath, $expectedMessage);
*/

$car = $this->getMock(__CLASS__.'_CarNoAdderAndRemoverWithProperty');
$propertyPath = 'axes';
$expectedMessage = sprintf(
'Neither the property "axes" nor one of the methods "addAx()", '.
'"addAxe()", "addAxis()", "setAxes()", "__set()" or "__call()" exist and have '.
'public access in class "%s".',
get_class($car)
);
$data[] = array($car, $propertyPath, $expectedMessage);
$axes = $this->getCollection(array(0 => 'first', 1 => 'second', 2 => 'third'));

return $data;
$this->propertyAccessor->setValue($car, 'axes', $axes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ protected function setUp()

public function getValidPropertyPaths()
{

return array(
array(array('Bernhard', 'Schussek'), '[0]', 'Bernhard'),
array(array('Bernhard', 'Schussek'), '[1]', 'Schussek'),
Expand Down Expand Up @@ -64,7 +63,6 @@ public function getValidPropertyPaths()

public function getPathsWithMissingProperty()
{

return array(
array((object) array('firstName' => 'Bernhard'), 'lastName'),
array((object) array('property' => (object) array('firstName' => 'Bernhard')), 'property.lastName'),
Expand Down Expand Up @@ -129,6 +127,14 @@ public function testGetValueThrowsExceptionIfIndexNotFoundAndIndexExceptionsEnab
$this->propertyAccessor->getValue($objectOrArray, $path);
}

/**
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException
*/
public function testGetValueThrowsExceptionIfNotArrayAccess()
{
$this->propertyAccessor->getValue(new \stdClass(), '[index]');
}

public function testGetValueReadsMagicGet()
{
$this->assertSame('Bernhard', $this->propertyAccessor->getValue(new TestClassMagicGet('Bernhard'), 'magicProperty'));
Expand Down Expand Up @@ -227,6 +233,14 @@ public function testSetValueThrowsNoExceptionIfIndexNotFoundAndIndexExceptionsEn
$this->assertSame('Updated', $this->propertyAccessor->getValue($objectOrArray, $path));
}

/**
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException
*/
public function testSetValueThrowsExceptionIfNotArrayAccess()
{
$this->propertyAccessor->setValue(new \stdClass(), '[index]', 'Updated');
}

public function testSetValueUpdatesMagicSet()
{
$author = new TestClassMagicGet('Bernhard');
Expand Down
0