Skip to content

Commit

Permalink
Merge d4e397f into 442bfd3
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocramius committed Jun 29, 2013
2 parents 442bfd3 + d4e397f commit e741665
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ public function __construct(
$inheritDoc = $originalClass->hasMethod('__get') ? "{@inheritDoc}\n" : '';

$this->setDocblock($inheritDoc . '@param string $name');
$this->setReturnsReference(true);
$this->setBody(
InterceptorGenerator::createInterceptedMethodBody(
'$returnValue = $this->' . $valueHolder->getName() . '->$name;',
'$returnValue = & $this->' . $valueHolder->getName() . '->$name;',
$this,
$valueHolder,
$prefixInterceptors,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function (ReflectionProperty $publicProperty) {
);

$this->setDocblock(($override ? "{@inheritDoc}\n" : '') . '@param string $name');
$this->setReturnsReference(true);

// @todo can be skipped when no public properties are available
$callParent = 'if (in_array($name, array(' . implode(', ', $publicProperties) . '))) {' . "\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function __construct(
$valueHolder = $valueHolderProperty->getName();

$this->setDocblock($inheritDoc . '@param string $name');
$this->setReturnsReference(true);
$this->setBody(
'$this->' . $initializer . ' && $this->' . $initializer
. '->__invoke($this->' . $valueHolder . ', $this, \'__get\', array(\'name\' => $name), $this->'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use ProxyManager\Proxy\ValueHolderInterface;
use ProxyManager\ProxyGenerator\AccessInterceptorValueHolderGenerator;
use ProxyManagerTestAsset\BaseClass;
use ProxyManagerTestAsset\ClassWithPublicArrayProperty;
use ReflectionClass;
use ProxyManager\Generator\ClassGenerator;
use ProxyManager\Generator\Util\UniqueIdentifierGenerator;
Expand Down Expand Up @@ -195,6 +196,26 @@ public function testPropertyUnset($instance, $proxy, $publicProperty)
$this->assertFalse(isset($proxy->$publicProperty));
}

/**
* Verifies that accessing a public property containing an array behaves like in a normal context
*/
public function testCanWriteToArrayKeysInPublicProperty()
{
$instance = new ClassWithPublicArrayProperty();
$className = get_class($instance);
$proxyName = $this->generateProxy($className);
/* @var $proxy ClassWithPublicArrayProperty */
$proxy = new $proxyName($instance);

$proxy->arrayProperty['foo'] = 'bar';

$this->assertSame('bar', $proxy->arrayProperty['foo']);

$proxy->arrayProperty = array('tab' => 'taz');

$this->assertSame(array('tab' => 'taz'), $proxy->arrayProperty);
}

/**
* Generates a proxy for the given class name, and retrieves its class name
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use ProxyManager\Proxy\GhostObjectInterface;
use ProxyManager\ProxyGenerator\LazyLoadingGhostGenerator;
use ProxyManagerTestAsset\BaseClass;
use ProxyManagerTestAsset\ClassWithPublicArrayProperty;
use ReflectionClass;

/**
Expand Down Expand Up @@ -141,6 +142,27 @@ public function testPropertyUnset($instance, $proxy, $publicProperty)
$this->assertFalse(isset($proxy->$publicProperty));
}

/**
* Verifies that accessing a public property containing an array behaves like in a normal context
*/
public function testCanWriteToArrayKeysInPublicProperty()
{
$instance = new ClassWithPublicArrayProperty();
$className = get_class($instance);
$initializer = $this->createInitializer($className, $instance);
$proxyName = $this->generateProxy($className);
/* @var $proxy ClassWithPublicArrayProperty */
$proxy = new $proxyName($initializer);

$proxy->arrayProperty['foo'] = 'bar';

$this->assertSame('bar', $proxy->arrayProperty['foo']);

$proxy->arrayProperty = array('tab' => 'taz');

$this->assertSame(array('tab' => 'taz'), $proxy->arrayProperty);
}

/**
* Generates a proxy for the given class name, and retrieves its class name
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use ProxyManager\Proxy\VirtualProxyInterface;
use ProxyManager\ProxyGenerator\LazyLoadingValueHolderGenerator;
use ProxyManagerTestAsset\BaseClass;
use ProxyManagerTestAsset\ClassWithPublicArrayProperty;
use ReflectionClass;

/**
Expand Down Expand Up @@ -150,6 +151,27 @@ public function testPropertyUnset($instance, $proxy, $publicProperty)
$this->assertFalse(isset($proxy->$publicProperty));
}

/**
* Verifies that accessing a public property containing an array behaves like in a normal context
*/
public function testCanWriteToArrayKeysInPublicProperty()
{
$instance = new ClassWithPublicArrayProperty();
$className = get_class($instance);
$initializer = $this->createInitializer($className, $instance);
$proxyName = $this->generateProxy($className);
/* @var $proxy ClassWithPublicArrayProperty */
$proxy = new $proxyName($initializer);

$proxy->arrayProperty['foo'] = 'bar';

$this->assertSame('bar', $proxy->arrayProperty['foo']);

$proxy->arrayProperty = array('tab' => 'taz');

$this->assertSame(array('tab' => 'taz'), $proxy->arrayProperty);
}

/**
* Generates a proxy for the given class name, and retrieves its class name
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ public function testBodyStructure()

$this->assertSame('__get', $magicGet->getName());
$this->assertCount(1, $magicGet->getParameters());
$this->assertGreaterThan(0, strpos($magicGet->getBody(), '$returnValue = $this->bar->$name;'));
$this->assertGreaterThan(0, strpos($magicGet->getBody(), '$returnValue = & $this->bar->$name;'));
}
}
31 changes: 31 additions & 0 deletions tests/ProxyManagerTestAsset/ClassWithPublicArrayProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace ProxyManagerTestAsset;

/**
* Base test class to verify that proxies actually modify the array keys of
* public properties that keep an array
*
* @author Marco Pivetta <ocramius@gmail.com>
* @license MIT
*/
class ClassWithPublicArrayProperty
{
public $arrayProperty = array();
}

0 comments on commit e741665

Please sign in to comment.