-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[2.5][PropertyAccess] Fixed getValue() when accessing non-existing indices of ArrayAccess implementations #10946
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/Symfony/Component/PropertyAccess/Tests/Fixtures/NonTraversableArrayObject.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\PropertyAccess\Tests\Fixtures; | ||
|
||
/** | ||
* This class is a hand written simplified version of PHP native `ArrayObject` | ||
* class, to show that it behaves differently than the PHP native implementation. | ||
*/ | ||
class NonTraversableArrayObject implements \ArrayAccess, \Countable, \Serializable | ||
{ | ||
private $array; | ||
|
||
public function __construct(array $array = null) | ||
{ | ||
$this->array = $array ?: array(); | ||
} | ||
|
||
public function offsetExists($offset) | ||
{ | ||
return array_key_exists($offset, $this->array); | ||
} | ||
|
||
public function offsetGet($offset) | ||
{ | ||
return $this->array[$offset]; | ||
} | ||
|
||
public function offsetSet($offset, $value) | ||
{ | ||
if (null === $offset) { | ||
$this->array[] = $value; | ||
} else { | ||
$this->array[$offset] = $value; | ||
} | ||
} | ||
|
||
public function offsetUnset($offset) | ||
{ | ||
unset($this->array[$offset]); | ||
} | ||
|
||
public function count() | ||
{ | ||
return count($this->array); | ||
} | ||
|
||
public function serialize() | ||
{ | ||
return serialize($this->array); | ||
} | ||
|
||
public function unserialize($serialized) | ||
{ | ||
$this->array = (array) unserialize((string) $serialized); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorArrayAccessTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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\PropertyAccess; | ||
use Symfony\Component\PropertyAccess\PropertyAccessor; | ||
|
||
abstract class PropertyAccessorArrayAccessTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var PropertyAccessor | ||
*/ | ||
protected $propertyAccessor; | ||
|
||
protected function setUp() | ||
{ | ||
$this->propertyAccessor = new PropertyAccessor(); | ||
} | ||
|
||
abstract protected function getContainer(array $array); | ||
|
||
public function getValidPropertyPaths() | ||
{ | ||
return array( | ||
array($this->getContainer(array('firstName' => 'Bernhard')), '[firstName]', 'Bernhard'), | ||
array($this->getContainer(array('person' => $this->getContainer(array('firstName' => 'Bernhard')))), '[person][firstName]', 'Bernhard'), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider getValidPropertyPaths | ||
*/ | ||
public function testGetValue($collection, $path, $value) | ||
{ | ||
$this->assertSame($value, $this->propertyAccessor->getValue($collection, $path)); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchIndexException | ||
*/ | ||
public function testGetValueFailsIfNoSuchIndex() | ||
{ | ||
$this->propertyAccessor = PropertyAccess::createPropertyAccessorBuilder() | ||
->enableExceptionOnInvalidIndex() | ||
->getPropertyAccessor(); | ||
|
||
$object = $this->getContainer(array('firstName' => 'Bernhard')); | ||
|
||
$this->propertyAccessor->getValue($object, '[lastName]'); | ||
} | ||
|
||
/** | ||
* @dataProvider getValidPropertyPaths | ||
*/ | ||
public function testSetValue($collection, $path) | ||
{ | ||
$this->propertyAccessor->setValue($collection, $path, 'Updated'); | ||
|
||
$this->assertSame('Updated', $this->propertyAccessor->getValue($collection, $path)); | ||
} | ||
|
||
/** | ||
* @dataProvider getValidPropertyPaths | ||
*/ | ||
public function testIsReadable($collection, $path) | ||
{ | ||
$this->assertTrue($this->propertyAccessor->isReadable($collection, $path)); | ||
} | ||
|
||
/** | ||
* @dataProvider getValidPropertyPaths | ||
*/ | ||
public function testIsWritable($collection, $path) | ||
{ | ||
$this->assertTrue($this->propertyAccessor->isWritable($collection, $path, 'Updated')); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorNonTraversableArrayObjectTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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\Tests\Fixtures\NonTraversableArrayObject; | ||
|
||
class PropertyAccessorNonTraversableArrayObjectTest extends PropertyAccessorArrayAccessTest | ||
{ | ||
protected function getContainer(array $array) | ||
{ | ||
return new NonTraversableArrayObject($array); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you sould keep it on 1 line, like previously
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@webmozart why use print_r? It prints way more than the available keys.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Tobion Good question, actually. I kept the previous code, which was like this.
@stof I prefer to be able to actually read the line. Overall, we use both.