Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

AccessInterceptorValueHolderFunctionalTest.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 12.89 KiB


001  <?php
002  /*
003   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
004   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
005   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
006   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
007   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
008   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
009   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
010   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
011   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
012   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
013   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
014   *
015   * This software consists of voluntary contributions made by many individuals
016   * and is licensed under the MIT license.
017   */
018   
019  namespace ProxyManagerTest\Functional;
020   
021  use PHPUnit_Framework_TestCase;
022  use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
023  use ProxyManager\ProxyGenerator\AccessInterceptorValueHolderGenerator;
024  use ProxyManagerTestAsset\BaseClass;
025  use ProxyManagerTestAsset\ClassWithPublicArrayProperty;
026  use ProxyManagerTestAsset\ClassWithPublicProperties;
027  use ProxyManagerTestAsset\ClassWithSelfHint;
028  use ReflectionClass;
029  use ProxyManager\Generator\ClassGenerator;
030  use ProxyManager\Generator\Util\UniqueIdentifierGenerator;
031   
032  /**
033   * Tests for {@see \ProxyManager\ProxyGenerator\LazyLoadingValueHolderGenerator} produced objects
034   *
035   * @author Marco Pivetta <ocramius@gmail.com>
036   * @license MIT
037   *
038   * @group Functional
039   * @coversNothing
040   */
041  class AccessInterceptorValueHolderFunctionalTest extends PHPUnit_Framework_TestCase
042  {
043      /**
044       * @dataProvider getProxyMethods
045       */
046      public function testMethodCalls($className, $instance, $method, $params, $expectedValue)
047      {
048          $proxyName = $this->generateProxy($className);
049   
050          /* @var $proxy \ProxyManager\Proxy\AccessInterceptorInterface|\ProxyManager\Proxy\ValueHolderInterface */
051          $proxy     = new $proxyName($instance);
052   
053          $this->assertSame($instance, $proxy->getWrappedValueHolderValue());
054          $this->assertSame($expectedValue, call_user_func_array(array($proxy, $method), $params));
055   
056          $listener  = $this->getMock('stdClass', array('__invoke'));
057          $listener
058              ->expects($this->once())
059              ->method('__invoke')
060              ->with($proxy, $instance, $method, $params, false);
061   
062          $proxy->setMethodPrefixInterceptor(
063              $method,
064              function ($proxy, $instance, $method, $params, & $returnEarly) use ($listener) {
065                  $listener->__invoke($proxy, $instance, $method, $params, $returnEarly);
066              }
067          );
068   
069          $this->assertSame($expectedValue, call_user_func_array(array($proxy, $method), $params));
070   
071          $random = uniqid();
072   
073          $proxy->setMethodPrefixInterceptor(
074              $method,
075              function ($proxy, $instance, $method, $params, & $returnEarly) use ($random) {
076                  $returnEarly = true;
077   
078                  return $random;
079              }
080          );
081   
082          $this->assertSame($random, call_user_func_array(array($proxy, $method), $params));
083      }
084   
085      /**
086       * @dataProvider getProxyMethods
087       */
088      public function testMethodCallsWithSuffixListener($className, $instance, $method, $params, $expectedValue)
089      {
090          $proxyName = $this->generateProxy($className);
091   
092          /* @var $proxy \ProxyManager\Proxy\AccessInterceptorInterface|\ProxyManager\Proxy\ValueHolderInterface */
093          $proxy     = new $proxyName($instance);
094          $listener  = $this->getMock('stdClass', array('__invoke'));
095          $listener
096              ->expects($this->once())
097              ->method('__invoke')
098              ->with($proxy, $instance, $method, $params, $expectedValue, false);
099   
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  }
361