Home | History | Annotate | Download | only in generated_code
      1 <?php
      2 /*
      3  *
      4  * Copyright 2015 gRPC authors.
      5  *
      6  * Licensed under the Apache License, Version 2.0 (the "License");
      7  * you may not use this file except in compliance with the License.
      8  * You may obtain a copy of the License at
      9  *
     10  *     http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  * Unless required by applicable law or agreed to in writing, software
     13  * distributed under the License is distributed on an "AS IS" BASIS,
     14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15  * See the License for the specific language governing permissions and
     16  * limitations under the License.
     17  *
     18  */
     19 require_once realpath(dirname(__FILE__).'/../../vendor/autoload.php');
     20 
     21 // The following includes are needed when using protobuf 3.1.0
     22 // and will suppress warnings when using protobuf 3.2.0+
     23 @include_once dirname(__FILE__).'/math.pb.php';
     24 @include_once dirname(__FILE__).'/math_grpc_pb.php';
     25 
     26 abstract class AbstractGeneratedCodeTest extends PHPUnit_Framework_TestCase
     27 {
     28     /**
     29      * These tests require that a server exporting the math service must be
     30      * running on $GRPC_TEST_HOST.
     31      */
     32     protected static $client;
     33 
     34     public function testWaitForNotReady()
     35     {
     36         $this->assertFalse(self::$client->waitForReady(1));
     37     }
     38 
     39     public function testWaitForReady()
     40     {
     41         $this->assertTrue(self::$client->waitForReady(250000));
     42     }
     43 
     44     public function testAlreadyReady()
     45     {
     46         $this->assertTrue(self::$client->waitForReady(250000));
     47         $this->assertTrue(self::$client->waitForReady(100));
     48     }
     49 
     50     public function testGetTarget()
     51     {
     52         $this->assertTrue(is_string(self::$client->getTarget()));
     53     }
     54 
     55     /**
     56      * @expectedException InvalidArgumentException
     57      */
     58     public function testClose()
     59     {
     60         self::$client->close();
     61         $div_arg = new Math\DivArgs();
     62         $call = self::$client->Div($div_arg);
     63     }
     64 
     65     /**
     66      * @expectedException InvalidArgumentException
     67      */
     68     public function testInvalidMetadata()
     69     {
     70         $div_arg = new Math\DivArgs();
     71         $call = self::$client->Div($div_arg, [' ' => 'abc123']);
     72     }
     73 
     74     public function testGetCallMetadata()
     75     {
     76         $div_arg = new Math\DivArgs();
     77         $call = self::$client->Div($div_arg);
     78         $this->assertTrue(is_array($call->getMetadata()));
     79     }
     80 
     81     public function testTimeout()
     82     {
     83         $div_arg = new Math\DivArgs();
     84         $call = self::$client->Div($div_arg, [], ['timeout' => 1]);
     85         list($response, $status) = $call->wait();
     86         $this->assertSame(\Grpc\STATUS_DEADLINE_EXCEEDED, $status->code);
     87     }
     88 
     89     public function testCancel()
     90     {
     91         $div_arg = new Math\DivArgs();
     92         $call = self::$client->Div($div_arg);
     93         $call->cancel();
     94         list($response, $status) = $call->wait();
     95         $this->assertSame(\Grpc\STATUS_CANCELLED, $status->code);
     96     }
     97 
     98     public function testCallCredentialsCallback()
     99     {
    100         $div_arg = new Math\DivArgs();
    101         $call = self::$client->Div($div_arg, array(), array(
    102             'call_credentials_callback' => function ($context) {
    103                 return array();
    104             },
    105         ));
    106         $call->cancel();
    107         list($response, $status) = $call->wait();
    108         $this->assertSame(\Grpc\STATUS_CANCELLED, $status->code);
    109     }
    110 
    111     public function testCallCredentialsCallback2()
    112     {
    113         $div_arg = new Math\DivArgs();
    114         $call = self::$client->Div($div_arg);
    115         $call_credentials = Grpc\CallCredentials::createFromPlugin(
    116             function ($context) {
    117                 return array();
    118             }
    119         );
    120         $call->setCallCredentials($call_credentials);
    121         $call->cancel();
    122         list($response, $status) = $call->wait();
    123         $this->assertSame(\Grpc\STATUS_CANCELLED, $status->code);
    124     }
    125 
    126     /**
    127      * @expectedException InvalidArgumentException
    128      */
    129     public function testInvalidMethodName()
    130     {
    131         $invalid_client = new DummyInvalidClient('host', [
    132             'credentials' => Grpc\ChannelCredentials::createInsecure(),
    133         ]);
    134         $div_arg = new Math\DivArgs();
    135         $invalid_client->InvalidUnaryCall($div_arg);
    136     }
    137 
    138     /**
    139      * @expectedException Exception
    140      */
    141     public function testMissingCredentials()
    142     {
    143         $invalid_client = new DummyInvalidClient('host', [
    144         ]);
    145     }
    146 
    147     public function testPrimaryUserAgentString()
    148     {
    149         $invalid_client = new DummyInvalidClient('host', [
    150             'credentials' => Grpc\ChannelCredentials::createInsecure(),
    151             'grpc.primary_user_agent' => 'testUserAgent',
    152         ]);
    153     }
    154 
    155     public function testWriteFlags()
    156     {
    157         $div_arg = new Math\DivArgs();
    158         $div_arg->setDividend(7);
    159         $div_arg->setDivisor(4);
    160         $call = self::$client->Div($div_arg, [],
    161                                    ['flags' => Grpc\WRITE_NO_COMPRESS]);
    162         $this->assertTrue(is_string($call->getPeer()));
    163         list($response, $status) = $call->wait();
    164         $this->assertSame(1, $response->getQuotient());
    165         $this->assertSame(3, $response->getRemainder());
    166         $this->assertSame(\Grpc\STATUS_OK, $status->code);
    167     }
    168 
    169     public function testWriteFlagsServerStreaming()
    170     {
    171         $fib_arg = new Math\FibArgs();
    172         $fib_arg->setLimit(7);
    173         $call = self::$client->Fib($fib_arg, [],
    174                                    ['flags' => Grpc\WRITE_NO_COMPRESS]);
    175         $result_array = iterator_to_array($call->responses());
    176         $status = $call->getStatus();
    177         $this->assertSame(\Grpc\STATUS_OK, $status->code);
    178     }
    179 
    180     public function testWriteFlagsClientStreaming()
    181     {
    182         $call = self::$client->Sum();
    183         $num = new Math\Num();
    184         $num->setNum(1);
    185         $call->write($num, ['flags' => Grpc\WRITE_NO_COMPRESS]);
    186         list($response, $status) = $call->wait();
    187         $this->assertSame(\Grpc\STATUS_OK, $status->code);
    188     }
    189 
    190     public function testWriteFlagsBidiStreaming()
    191     {
    192         $call = self::$client->DivMany();
    193         $div_arg = new Math\DivArgs();
    194         $div_arg->setDividend(7);
    195         $div_arg->setDivisor(4);
    196         $call->write($div_arg, ['flags' => Grpc\WRITE_NO_COMPRESS]);
    197         $response = $call->read();
    198         $call->writesDone();
    199         $status = $call->getStatus();
    200         $this->assertSame(\Grpc\STATUS_OK, $status->code);
    201     }
    202 
    203     public function testSimpleRequest()
    204     {
    205         $div_arg = new Math\DivArgs();
    206         $div_arg->setDividend(7);
    207         $div_arg->setDivisor(4);
    208         $call = self::$client->Div($div_arg);
    209         $this->assertTrue(is_string($call->getPeer()));
    210         list($response, $status) = $call->wait();
    211         $this->assertSame(1, $response->getQuotient());
    212         $this->assertSame(3, $response->getRemainder());
    213         $this->assertSame(\Grpc\STATUS_OK, $status->code);
    214     }
    215 
    216     public function testServerStreaming()
    217     {
    218         $fib_arg = new Math\FibArgs();
    219         $fib_arg->setLimit(7);
    220         $call = self::$client->Fib($fib_arg);
    221         $this->assertTrue(is_string($call->getPeer()));
    222         $result_array = iterator_to_array($call->responses());
    223         $extract_num = function ($num) {
    224                          return $num->getNum();
    225                        };
    226         $values = array_map($extract_num, $result_array);
    227         $this->assertSame([1, 1, 2, 3, 5, 8, 13], $values);
    228         $status = $call->getStatus();
    229         $this->assertSame(\Grpc\STATUS_OK, $status->code);
    230     }
    231 
    232     public function testClientStreaming()
    233     {
    234         $call = self::$client->Sum();
    235         $this->assertTrue(is_string($call->getPeer()));
    236         for ($i = 0; $i < 7; ++$i) {
    237             $num = new Math\Num();
    238             $num->setNum($i);
    239             $call->write($num);
    240         }
    241         list($response, $status) = $call->wait();
    242         $this->assertSame(21, $response->getNum());
    243         $this->assertSame(\Grpc\STATUS_OK, $status->code);
    244     }
    245 
    246     public function testBidiStreaming()
    247     {
    248         $call = self::$client->DivMany();
    249         $this->assertTrue(is_string($call->getPeer()));
    250         for ($i = 0; $i < 7; ++$i) {
    251             $div_arg = new Math\DivArgs();
    252             $div_arg->setDividend(2 * $i + 1);
    253             $div_arg->setDivisor(2);
    254             $call->write($div_arg);
    255             $response = $call->read();
    256             $this->assertSame($i, $response->getQuotient());
    257             $this->assertSame(1, $response->getRemainder());
    258         }
    259         $call->writesDone();
    260         $status = $call->getStatus();
    261         $this->assertSame(\Grpc\STATUS_OK, $status->code);
    262     }
    263 }
    264 
    265 class DummyInvalidClient extends \Grpc\BaseStub
    266 {
    267     public function InvalidUnaryCall(\Math\DivArgs $argument,
    268                                      $metadata = [],
    269                                      $options = [])
    270     {
    271         return $this->_simpleRequest('invalidMethodName',
    272                                      $argument,
    273                                      function () {},
    274                                      $metadata,
    275                                      $options);
    276     }
    277 }
    278