[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/ocramius/proxy-manager/tests/ProxyManagerTest/Functional/ -> RemoteObjectFunctionalTest.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\Factory\RemoteObject\Adapter\JsonRpc as JsonRpcAdapter;
  23  use ProxyManager\Factory\RemoteObject\Adapter\XmlRpc as XmlRpcAdapter;
  24  use ProxyManager\Generator\ClassGenerator;
  25  use ProxyManager\Generator\Util\UniqueIdentifierGenerator;
  26  use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
  27  use ProxyManager\ProxyGenerator\RemoteObjectGenerator;
  28  use ProxyManagerTestAsset\ClassWithSelfHint;
  29  use ProxyManagerTestAsset\RemoteProxy\Foo;
  30  use ReflectionClass;
  31  
  32  /**
  33   * Tests for {@see \ProxyManager\ProxyGenerator\RemoteObjectGenerator} produced objects
  34   *
  35   * @author Vincent Blanchon <blanchon.vincent@gmail.com>
  36   * @license MIT
  37   *
  38   * @group Functional
  39   * @coversNothing
  40   */
  41  class RemoteObjectFunctionalTest extends PHPUnit_Framework_TestCase
  42  {
  43      /**
  44       * @param mixed  $expectedValue
  45       * @param string $method
  46       * @param array  $params
  47       *
  48       * @return XmlRpcAdapter
  49       */
  50      protected function getXmlRpcAdapter($expectedValue, $method, array $params)
  51      {
  52          $client = $this
  53              ->getMockBuilder('Zend\Server\Client')
  54              ->setMethods(array('call'))
  55              ->getMock();
  56  
  57          $client
  58              ->expects($this->any())
  59              ->method('call')
  60              ->with($this->stringEndsWith($method), $params)
  61              ->will($this->returnValue($expectedValue));
  62  
  63          $adapter = new XmlRpcAdapter(
  64              $client,
  65              array(
  66                   'ProxyManagerTestAsset\RemoteProxy\Foo.foo'
  67                       => 'ProxyManagerTestAsset\RemoteProxy\FooServiceInterface.foo'
  68              )
  69          );
  70  
  71          return $adapter;
  72      }
  73  
  74      /**
  75       * @param mixed  $expectedValue
  76       * @param string $method
  77       * @param array  $params
  78       *
  79       * @return JsonRpcAdapter
  80       */
  81      protected function getJsonRpcAdapter($expectedValue, $method, array $params)
  82      {
  83          $client = $this
  84              ->getMockBuilder('Zend\Server\Client')
  85              ->setMethods(array('call'))
  86              ->getMock();
  87  
  88          $client
  89              ->expects($this->any())
  90              ->method('call')
  91              ->with($this->stringEndsWith($method), $params)
  92              ->will($this->returnValue($expectedValue));
  93  
  94          $adapter = new JsonRpcAdapter(
  95              $client,
  96              array(
  97                   'ProxyManagerTestAsset\RemoteProxy\Foo.foo'
  98                      => 'ProxyManagerTestAsset\RemoteProxy\FooServiceInterface.foo'
  99              )
 100          );
 101  
 102          return $adapter;
 103      }
 104  
 105      /**
 106       * @dataProvider getProxyMethods
 107       */
 108      public function testXmlRpcMethodCalls($instanceOrClassname, $method, $params, $expectedValue)
 109      {
 110          $proxyName = $this->generateProxy($instanceOrClassname);
 111  
 112          /* @var $proxy \ProxyManager\Proxy\RemoteObjectInterface */
 113          $proxy     = new $proxyName($this->getXmlRpcAdapter($expectedValue, $method, $params));
 114  
 115          $this->assertSame($expectedValue, call_user_func_array(array($proxy, $method), $params));
 116      }
 117  
 118      /**
 119       * @dataProvider getProxyMethods
 120       */
 121      public function testJsonRpcMethodCalls($instanceOrClassname, $method, $params, $expectedValue)
 122      {
 123          $proxyName = $this->generateProxy($instanceOrClassname);
 124  
 125          /* @var $proxy \ProxyManager\Proxy\RemoteObjectInterface */
 126          $proxy     = new $proxyName($this->getJsonRpcAdapter($expectedValue, $method, $params));
 127  
 128          $this->assertSame($expectedValue, call_user_func_array(array($proxy, $method), $params));
 129      }
 130  
 131      /**
 132       * @dataProvider getPropertyAccessProxies
 133       */
 134      public function testJsonRpcPropertyReadAccess($instanceOrClassname, $publicProperty, $propertyValue)
 135      {
 136          $proxyName = $this->generateProxy($instanceOrClassname);
 137  
 138          /* @var $proxy \ProxyManager\Proxy\RemoteObjectInterface */
 139          $proxy     = new $proxyName(
 140              $this->getJsonRpcAdapter($propertyValue, '__get', array($publicProperty))
 141          );
 142  
 143          /* @var $proxy \ProxyManager\Proxy\NullObjectInterface */
 144          $this->assertSame($propertyValue, $proxy->$publicProperty);
 145      }
 146  
 147      /**
 148       * Generates a proxy for the given class name, and retrieves its class name
 149       *
 150       * @param string $parentClassName
 151       *
 152       * @return string
 153       */
 154      private function generateProxy($parentClassName)
 155      {
 156          $generatedClassName = __NAMESPACE__ . '\\' . UniqueIdentifierGenerator::getIdentifier('Foo');
 157          $generator          = new RemoteObjectGenerator();
 158          $generatedClass     = new ClassGenerator($generatedClassName);
 159          $strategy           = new EvaluatingGeneratorStrategy();
 160  
 161          $generator->generate(new ReflectionClass($parentClassName), $generatedClass);
 162          $strategy->generate($generatedClass);
 163  
 164          return $generatedClassName;
 165      }
 166  
 167      /**
 168       * Generates a list of object | invoked method | parameters | expected result
 169       *
 170       * @return array
 171       */
 172      public function getProxyMethods()
 173      {
 174          $selfHintParam = new ClassWithSelfHint();
 175  
 176          $data = array(
 177              array(
 178                  'ProxyManagerTestAsset\RemoteProxy\FooServiceInterface',
 179                  'foo',
 180                  array(),
 181                  'bar remote'
 182              ),
 183              array(
 184                  'ProxyManagerTestAsset\RemoteProxy\Foo',
 185                  'foo',
 186                  array(),
 187                  'bar remote'
 188              ),
 189              array(
 190                  new Foo(),
 191                  'foo',
 192                  array(),
 193                  'bar remote'
 194              ),
 195              array(
 196                  'ProxyManagerTestAsset\RemoteProxy\BazServiceInterface',
 197                  'baz',
 198                  array('baz'),
 199                  'baz remote'
 200              ),
 201          );
 202  
 203          if (PHP_VERSION_ID >= 50401) {
 204              // PHP < 5.4.1 misbehaves, throwing strict standards, see https://bugs.php.net/bug.php?id=60573
 205              $data[] = array(
 206                  new ClassWithSelfHint(),
 207                  'selfHintMethod',
 208                  array($selfHintParam),
 209                  $selfHintParam
 210              );
 211          }
 212  
 213          return $data;
 214      }
 215  
 216      /**
 217       * Generates proxies and instances with a public property to feed to the property accessor methods
 218       *
 219       * @return array
 220       */
 221      public function getPropertyAccessProxies()
 222      {
 223          return array(
 224              array(
 225                  'ProxyManagerTestAsset\RemoteProxy\FooServiceInterface',
 226                  'publicProperty',
 227                  'publicProperty remote',
 228              ),
 229          );
 230      }
 231  }


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