[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/ocramius/proxy-manager/tests/ProxyManagerTest/Functional/ -> AccessInterceptorValueHolderFunctionalTest.php (source)

   1  <?php
   2  /*
   3   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   4   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   5   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   6   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   7   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   8   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   9   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14   *
  15   * This software consists of voluntary contributions made by many individuals
  16   * and is licensed under the MIT license.
  17   */
  18  
  19  namespace ProxyManagerTest\Functional;
  20  
  21  use PHPUnit_Framework_TestCase;
  22  use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
  23  use ProxyManager\ProxyGenerator\AccessInterceptorValueHolderGenerator;
  24  use ProxyManagerTestAsset\BaseClass;
  25  use ProxyManagerTestAsset\ClassWithPublicArrayProperty;
  26  use ProxyManagerTestAsset\ClassWithPublicProperties;
  27  use ProxyManagerTestAsset\ClassWithSelfHint;
  28  use ReflectionClass;
  29  use ProxyManager\Generator\ClassGenerator;
  30  use ProxyManager\Generator\Util\UniqueIdentifierGenerator;
  31  
  32  /**
  33   * Tests for {@see \ProxyManager\ProxyGenerator\LazyLoadingValueHolderGenerator} produced objects
  34   *
  35   * @author Marco Pivetta <ocramius@gmail.com>
  36   * @license MIT
  37   *
  38   * @group Functional
  39   * @coversNothing
  40   */
  41  class AccessInterceptorValueHolderFunctionalTest extends PHPUnit_Framework_TestCase
  42  {
  43      /**
  44       * @dataProvider getProxyMethods
  45       */
  46      public function testMethodCalls($className, $instance, $method, $params, $expectedValue)
  47      {
  48          $proxyName = $this->generateProxy($className);
  49  
  50          /* @var $proxy \ProxyManager\Proxy\AccessInterceptorInterface|\ProxyManager\Proxy\ValueHolderInterface */
  51          $proxy     = new $proxyName($instance);
  52  
  53          $this->assertSame($instance, $proxy->getWrappedValueHolderValue());
  54          $this->assertSame($expectedValue, call_user_func_array(array($proxy, $method), $params));
  55  
  56          $listener  = $this->getMock('stdClass', array('__invoke'));
  57          $listener
  58              ->expects($this->once())
  59              ->method('__invoke')
  60              ->with($proxy, $instance, $method, $params, false);
  61  
  62          $proxy->setMethodPrefixInterceptor(
  63              $method,
  64              function ($proxy, $instance, $method, $params, & $returnEarly) use ($listener) {
  65                  $listener->__invoke($proxy, $instance, $method, $params, $returnEarly);
  66              }
  67          );
  68  
  69          $this->assertSame($expectedValue, call_user_func_array(array($proxy, $method), $params));
  70  
  71          $random = uniqid();
  72  
  73          $proxy->setMethodPrefixInterceptor(
  74              $method,
  75              function ($proxy, $instance, $method, $params, & $returnEarly) use ($random) {
  76                  $returnEarly = true;
  77  
  78                  return $random;
  79              }
  80          );
  81  
  82          $this->assertSame($random, call_user_func_array(array($proxy, $method), $params));
  83      }
  84  
  85      /**
  86       * @dataProvider getProxyMethods
  87       */
  88      public function testMethodCallsWithSuffixListener($className, $instance, $method, $params, $expectedValue)
  89      {
  90          $proxyName = $this->generateProxy($className);
  91  
  92          /* @var $proxy \ProxyManager\Proxy\AccessInterceptorInterface|\ProxyManager\Proxy\ValueHolderInterface */
  93          $proxy     = new $proxyName($instance);
  94          $listener  = $this->getMock('stdClass', array('__invoke'));
  95          $listener
  96              ->expects($this->once())
  97              ->method('__invoke')
  98              ->with($proxy, $instance, $method, $params, $expectedValue, false);
  99  
 100          $proxy->setMethodSuffixInterceptor(
 101              $method,
 102              function ($proxy, $instance, $method, $params, $returnValue, & $returnEarly) use ($listener) {
 103                  $listener->__invoke($proxy, $instance, $method, $params, $returnValue, $returnEarly);
 104              }
 105          );
 106  
 107          $this->assertSame($expectedValue, call_user_func_array(array($proxy, $method), $params));
 108  
 109          $random = uniqid();
 110  
 111          $proxy->setMethodSuffixInterceptor(
 112              $method,
 113              function ($proxy, $instance, $method, $params, $returnValue, & $returnEarly) use ($random) {
 114                  $returnEarly = true;
 115  
 116                  return $random;
 117              }
 118          );
 119  
 120          $this->assertSame($random, call_user_func_array(array($proxy, $method), $params));
 121      }
 122  
 123      /**
 124       * @dataProvider getProxyMethods
 125       */
 126      public function testMethodCallsAfterUnSerialization($className, $instance, $method, $params, $expectedValue)
 127      {
 128          $proxyName = $this->generateProxy($className);
 129          /* @var $proxy \ProxyManager\Proxy\AccessInterceptorInterface|\ProxyManager\Proxy\ValueHolderInterface */
 130          $proxy     = unserialize(serialize(new $proxyName($instance)));
 131  
 132          $this->assertSame($expectedValue, call_user_func_array(array($proxy, $method), $params));
 133          $this->assertEquals($instance, $proxy->getWrappedValueHolderValue());
 134      }
 135  
 136      /**
 137       * @dataProvider getProxyMethods
 138       */
 139      public function testMethodCallsAfterCloning($className, $instance, $method, $params, $expectedValue)
 140      {
 141          $proxyName = $this->generateProxy($className);
 142  
 143          /* @var $proxy \ProxyManager\Proxy\AccessInterceptorInterface|\ProxyManager\Proxy\ValueHolderInterface */
 144          $proxy     = new $proxyName($instance);
 145          $cloned    = clone $proxy;
 146  
 147          $this->assertNotSame($proxy->getWrappedValueHolderValue(), $cloned->getWrappedValueHolderValue());
 148          $this->assertSame($expectedValue, call_user_func_array(array($cloned, $method), $params));
 149          $this->assertEquals($instance, $cloned->getWrappedValueHolderValue());
 150      }
 151  
 152      /**
 153       * @dataProvider getPropertyAccessProxies
 154       */
 155      public function testPropertyReadAccess($instance, $proxy, $publicProperty, $propertyValue)
 156      {
 157          /* @var $proxy \ProxyManager\Proxy\AccessInterceptorInterface|\ProxyManager\Proxy\ValueHolderInterface */
 158          $this->assertSame($propertyValue, $proxy->$publicProperty);
 159          $this->assertEquals($instance, $proxy->getWrappedValueHolderValue());
 160      }
 161  
 162      /**
 163       * @dataProvider getPropertyAccessProxies
 164       */
 165      public function testPropertyWriteAccess($instance, $proxy, $publicProperty)
 166      {
 167          /* @var $proxy \ProxyManager\Proxy\AccessInterceptorInterface|\ProxyManager\Proxy\ValueHolderInterface */
 168          $newValue               = uniqid();
 169          $proxy->$publicProperty = $newValue;
 170  
 171          $this->assertSame($newValue, $proxy->$publicProperty);
 172          $this->assertSame($newValue, $proxy->getWrappedValueHolderValue()->$publicProperty);
 173      }
 174  
 175      /**
 176       * @dataProvider getPropertyAccessProxies
 177       */
 178      public function testPropertyExistence($instance, $proxy, $publicProperty)
 179      {
 180          /* @var $proxy \ProxyManager\Proxy\AccessInterceptorInterface|\ProxyManager\Proxy\ValueHolderInterface */
 181          $this->assertSame(isset($instance->$publicProperty), isset($proxy->$publicProperty));
 182          $this->assertEquals($instance, $proxy->getWrappedValueHolderValue());
 183  
 184          $proxy->getWrappedValueHolderValue()->$publicProperty = null;
 185          $this->assertFalse(isset($proxy->$publicProperty));
 186      }
 187  
 188      /**
 189       * @dataProvider getPropertyAccessProxies
 190       */
 191      public function testPropertyUnset($instance, $proxy, $publicProperty)
 192      {
 193          /* @var $proxy \ProxyManager\Proxy\AccessInterceptorInterface|\ProxyManager\Proxy\ValueHolderInterface */
 194          $instance = $proxy->getWrappedValueHolderValue() ? $proxy->getWrappedValueHolderValue() : $instance;
 195          unset($proxy->$publicProperty);
 196  
 197          $this->assertFalse(isset($instance->$publicProperty));
 198          $this->assertFalse(isset($proxy->$publicProperty));
 199      }
 200  
 201      /**
 202       * Verifies that accessing a public property containing an array behaves like in a normal context
 203       */
 204      public function testCanWriteToArrayKeysInPublicProperty()
 205      {
 206          $instance    = new ClassWithPublicArrayProperty();
 207          $className   = get_class($instance);
 208          $proxyName   = $this->generateProxy($className);
 209          /* @var $proxy ClassWithPublicArrayProperty */
 210          $proxy       = new $proxyName($instance);
 211  
 212          $proxy->arrayProperty['foo'] = 'bar';
 213  
 214          $this->assertSame('bar', $proxy->arrayProperty['foo']);
 215  
 216          $proxy->arrayProperty = array('tab' => 'taz');
 217  
 218          $this->assertSame(array('tab' => 'taz'), $proxy->arrayProperty);
 219      }
 220  
 221      /**
 222       * Verifies that public properties retrieved via `__get` don't get modified in the object state
 223       */
 224      public function testWillNotModifyRetrievedPublicProperties()
 225      {
 226          $instance    = new ClassWithPublicProperties();
 227          $className   = get_class($instance);
 228          $proxyName   = $this->generateProxy($className);
 229          /* @var $proxy ClassWithPublicProperties */
 230          $proxy       = new $proxyName($instance);
 231          $variable    = $proxy->property0;
 232  
 233          $this->assertSame('property0', $variable);
 234  
 235          $variable = 'foo';
 236  
 237          $this->assertSame('property0', $proxy->property0);
 238      }
 239  
 240      /**
 241       * Verifies that public properties references retrieved via `__get` modify in the object state
 242       */
 243      public function testWillModifyByRefRetrievedPublicProperties()
 244      {
 245          $instance    = new ClassWithPublicProperties();
 246          $className   = get_class($instance);
 247          $proxyName   = $this->generateProxy($className);
 248          /* @var $proxy ClassWithPublicProperties */
 249          $proxy       = new $proxyName($instance);
 250          $variable    = & $proxy->property0;
 251  
 252          $this->assertSame('property0', $variable);
 253  
 254          $variable = 'foo';
 255  
 256          $this->assertSame('foo', $proxy->property0);
 257      }
 258  
 259      /**
 260       * Generates a proxy for the given class name, and retrieves its class name
 261       *
 262       * @param string $parentClassName
 263       *
 264       * @return string
 265       */
 266      private function generateProxy($parentClassName)
 267      {
 268          $generatedClassName = __NAMESPACE__ . '\\' . UniqueIdentifierGenerator::getIdentifier('Foo');
 269          $generator          = new AccessInterceptorValueHolderGenerator();
 270          $generatedClass     = new ClassGenerator($generatedClassName);
 271          $strategy           = new EvaluatingGeneratorStrategy();
 272  
 273          $generator->generate(new ReflectionClass($parentClassName), $generatedClass);
 274          $strategy->generate($generatedClass);
 275  
 276          return $generatedClassName;
 277      }
 278  
 279      /**
 280       * Generates a list of object | invoked method | parameters | expected result
 281       *
 282       * @return array
 283       */
 284      public function getProxyMethods()
 285      {
 286          $selfHintParam = new ClassWithSelfHint();
 287  
 288          $data = array(
 289              array(
 290                  'ProxyManagerTestAsset\\BaseClass',
 291                  new BaseClass(),
 292                  'publicMethod',
 293                  array(),
 294                  'publicMethodDefault'
 295              ),
 296              array(
 297                  'ProxyManagerTestAsset\\BaseClass',
 298                  new BaseClass(),
 299                  'publicTypeHintedMethod',
 300                  array('param' => new \stdClass()),
 301                  'publicTypeHintedMethodDefault'
 302              ),
 303              array(
 304                  'ProxyManagerTestAsset\\BaseClass',
 305                  new BaseClass(),
 306                  'publicByReferenceMethod',
 307                  array(),
 308                  'publicByReferenceMethodDefault'
 309              ),
 310              array(
 311                  'ProxyManagerTestAsset\\BaseInterface',
 312                  new BaseClass(),
 313                  'publicMethod',
 314                  array(),
 315                  'publicMethodDefault'
 316              ),
 317          );
 318  
 319          if (PHP_VERSION_ID >= 50401) {
 320              // PHP < 5.4.1 misbehaves, throwing strict standards, see https://bugs.php.net/bug.php?id=60573
 321              $data[] = array(
 322                  'ProxyManagerTestAsset\\ClassWithSelfHint',
 323                  new ClassWithSelfHint(),
 324                  'selfHintMethod',
 325                  array('parameter' => $selfHintParam),
 326                  $selfHintParam
 327              );
 328          }
 329  
 330          return $data;
 331      }
 332  
 333      /**
 334       * Generates proxies and instances with a public property to feed to the property accessor methods
 335       *
 336       * @return array
 337       */
 338      public function getPropertyAccessProxies()
 339      {
 340          $instance1  = new BaseClass();
 341          $proxyName1 = $this->generateProxy(get_class($instance1));
 342          $instance2  = new BaseClass();
 343          $proxyName2 = $this->generateProxy(get_class($instance2));
 344  
 345          return array(
 346              array(
 347                  $instance1,
 348                  new $proxyName1($instance1),
 349                  'publicProperty',
 350                  'publicPropertyDefault',
 351              ),
 352              array(
 353                  $instance2,
 354                  unserialize(serialize(new $proxyName2($instance2))),
 355                  'publicProperty',
 356                  'publicPropertyDefault',
 357              ),
 358          );
 359      }
 360  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1