Skip to content

Commit

Permalink
[PropertyAccess] Refactored PropertyAccessorTest
Browse files Browse the repository at this point in the history
  • Loading branch information
webmozart committed May 20, 2014
1 parent 04fb113 commit 5614303
Show file tree
Hide file tree
Showing 8 changed files with 335 additions and 391 deletions.
11 changes: 6 additions & 5 deletions src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Expand Up @@ -116,7 +116,7 @@ public function setValue(&$objectOrArray, $propertyPath, $value)
*
* @throws UnexpectedTypeException If a value within the path is neither object nor array.
*/
private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $propertyPath, $lastIndex, $throwExceptionOnNonexistantIndex = false)
private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $propertyPath, $lastIndex, $throwExceptionOnInvalidIndex = false)
{
$propertyValues = array();

Expand All @@ -131,9 +131,10 @@ private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $pr

// Create missing nested arrays on demand
if ($isIndex && $isArrayAccess && !isset($objectOrArray[$property])) {
if ($throwExceptionOnNonexistantIndex) {
if ($throwExceptionOnInvalidIndex) {
throw new NoSuchIndexException(sprintf('Cannot read property "%s". Available properties are "%s"', $property, print_r(array_keys($objectOrArray), true)));
}

$objectOrArray[$property] = $i + 1 < $propertyPath->getLength() ? array() : null;
}

Expand Down Expand Up @@ -412,8 +413,8 @@ private function findAdderAndRemover(\ReflectionClass $reflClass, array $singula
$addMethod = 'add'.$singular;
$removeMethod = 'remove'.$singular;

$addMethodFound = $this->isAccessible($reflClass, $addMethod, 1);
$removeMethodFound = $this->isAccessible($reflClass, $removeMethod, 1);
$addMethodFound = $this->isMethodAccessible($reflClass, $addMethod, 1);
$removeMethodFound = $this->isMethodAccessible($reflClass, $removeMethod, 1);

if ($addMethodFound && $removeMethodFound) {
return array($addMethod, $removeMethod);
Expand All @@ -440,7 +441,7 @@ private function findAdderAndRemover(\ReflectionClass $reflClass, array $singula
* @return bool Whether the method is public and has $parameters
* required parameters
*/
private function isAccessible(\ReflectionClass $class, $methodName, $parameters)
private function isMethodAccessible(\ReflectionClass $class, $methodName, $parameters)
{
if ($class->hasMethod($methodName)) {
$method = $class->getMethod($methodName);
Expand Down
71 changes: 0 additions & 71 deletions src/Symfony/Component/PropertyAccess/Tests/Fixtures/Author.php

This file was deleted.

27 changes: 0 additions & 27 deletions src/Symfony/Component/PropertyAccess/Tests/Fixtures/Magician.php

This file was deleted.

This file was deleted.

115 changes: 115 additions & 0 deletions src/Symfony/Component/PropertyAccess/Tests/Fixtures/TestClass.php
@@ -0,0 +1,115 @@
<?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\Fixtures;

class TestClass
{
public $publicProperty;
protected $protectedProperty;
private $privateProperty;

private $publicAccessor;
private $publicIsAccessor;
private $publicHasAccessor;

public function __construct($value)
{
$this->publicProperty = $value;
$this->publicAccessor = $value;
$this->publicIsAccessor = $value;
$this->publicHasAccessor = $value;
}

public function setPublicAccessor($value)
{
$this->publicAccessor = $value;
}

public function getPublicAccessor()
{
return $this->publicAccessor;
}

public function setPublicIsAccessor($value)
{
$this->publicIsAccessor = $value;
}

public function isPublicIsAccessor()
{
return $this->publicIsAccessor;
}

public function setPublicHasAccessor($value)
{
$this->publicHasAccessor = $value;
}

public function hasPublicHasAccessor()
{
return $this->publicHasAccessor;
}

protected function setProtectedAccessor($value)
{
}

protected function getProtectedAccessor()
{
return 'foobar';
}

protected function setProtectedIsAccessor($value)
{
}

protected function isProtectedIsAccessor()
{
return 'foobar';
}

protected function setProtectedHasAccessor($value)
{
}

protected function hasProtectedHasAccessor()
{
return 'foobar';
}

private function setPrivateAccessor($value)
{
}

private function getPrivateAccessor()
{
return 'foobar';
}

private function setPrivateIsAccessor($value)
{
}

private function isPrivateIsAccessor()
{
return 'foobar';
}

private function setPrivateHasAccessor($value)
{
}

private function hasPrivateHasAccessor()
{
return 'foobar';
}
}
@@ -0,0 +1,39 @@
<?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\Fixtures;

class TestClassMagicCall
{
private $magicCallProperty;

public function __construct($value)
{
$this->magicCallProperty = $value;
}

public function __call($method, array $args)
{
if ('getMagicCallProperty' === $method) {
return $this->magicCallProperty;
}

if ('getConstantMagicCallProperty' === $method) {
return 'constant value';
}

if ('setMagicCallProperty' === $method) {
$this->magicCallProperty = reset($args);
}

return null;
}
}
@@ -0,0 +1,42 @@
<?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\Fixtures;

class TestClassMagicGet
{
private $magicProperty;

public function __construct($value)
{
$this->magicProperty = $value;
}

public function __set($property, $value)
{
if ('magicProperty' === $property) {
$this->magicProperty = $value;
}
}

public function __get($property)
{
if ('magicProperty' === $property) {
return $this->magicProperty;
}

if ('constantMagicProperty' === $property) {
return 'constant value';
}

return null;
}
}

0 comments on commit 5614303

Please sign in to comment.