diff options
-rw-r--r-- | src/php/README.md | 11 | ||||
-rwxr-xr-x | src/php/bin/run_gen_code_test.sh | 4 | ||||
-rwxr-xr-x | src/php/bin/run_tests.sh | 2 | ||||
-rwxr-xr-x | src/php/lib/Grpc/BaseStub.php | 4 | ||||
-rw-r--r-- | src/php/phpunit.xml | 22 | ||||
-rw-r--r-- | src/php/tests/bootstrap.php | 21 | ||||
-rw-r--r-- | src/php/tests/generated_code/AbstractGeneratedCodeTest.php | 84 | ||||
-rwxr-xr-x | src/php/tests/generated_code/GeneratedCodeTest.php | 2 | ||||
-rw-r--r-- | src/php/tests/generated_code/GeneratedCodeWithCallbackTest.php | 2 |
9 files changed, 142 insertions, 10 deletions
diff --git a/src/php/README.md b/src/php/README.md index a054258782..b1823b9226 100644 --- a/src/php/README.md +++ b/src/php/README.md @@ -14,18 +14,21 @@ Prerequisite: PHP 5.5 or later, `phpunit`, `pecl` **Linux:** ```sh -$ sudo apt-get install php5 php5-dev phpunit php-pear +$ sudo apt-get install php5 php5-dev php-pear ``` **Mac OS X:** ```sh +$ curl -O http://pear.php.net/go-pear.phar +$ sudo php -d detect_unicode=0 go-pear.phar +``` + +**PHPUnit: (Both Linux and Mac OS X)** +```sh $ curl https://phar.phpunit.de/phpunit.phar -o phpunit.phar $ chmod +x phpunit.phar $ sudo mv phpunit.phar /usr/local/bin/phpunit - -$ curl -O http://pear.php.net/go-pear.phar -$ sudo php -d detect_unicode=0 go-pear.phar ``` ## Quick Install diff --git a/src/php/bin/run_gen_code_test.sh b/src/php/bin/run_gen_code_test.sh index 5dfebb42b6..1c18756ed2 100755 --- a/src/php/bin/run_gen_code_test.sh +++ b/src/php/bin/run_gen_code_test.sh @@ -32,7 +32,7 @@ set -e cd $(dirname $0) source ./determine_extension_dir.sh export GRPC_TEST_HOST=localhost:50051 -php $extension_dir -d max_execution_time=300 $(which phpunit) -v --debug --strict \ +php $extension_dir -d max_execution_time=300 $(which phpunit) -v --debug \ ../tests/generated_code/GeneratedCodeTest.php -php $extension_dir -d max_execution_time=300 $(which phpunit) -v --debug --strict \ +php $extension_dir -d max_execution_time=300 $(which phpunit) -v --debug \ ../tests/generated_code/GeneratedCodeWithCallbackTest.php diff --git a/src/php/bin/run_tests.sh b/src/php/bin/run_tests.sh index 5adc77879a..2a7335e20a 100755 --- a/src/php/bin/run_tests.sh +++ b/src/php/bin/run_tests.sh @@ -37,5 +37,5 @@ cd src/php/bin source ./determine_extension_dir.sh # in some jenkins macos machine, somehow the PHP build script can't find libgrpc.dylib export DYLD_LIBRARY_PATH=$root/libs/$config -php $extension_dir -d max_execution_time=300 $(which phpunit) -v --debug --strict \ +php $extension_dir -d max_execution_time=300 $(which phpunit) -v --debug \ ../tests/unit_tests diff --git a/src/php/lib/Grpc/BaseStub.php b/src/php/lib/Grpc/BaseStub.php index d4fb3ba17e..5614314598 100755 --- a/src/php/lib/Grpc/BaseStub.php +++ b/src/php/lib/Grpc/BaseStub.php @@ -114,7 +114,9 @@ class BaseStub { return true; } if ($new_state == \Grpc\CHANNEL_FATAL_FAILURE) { + // @codeCoverageIgnoreStart throw new \Exception('Failed to connect to server'); + // @codeCoverageIgnoreEnd } return false; } @@ -132,7 +134,7 @@ class BaseStub { private function _get_jwt_aud_uri($method) { $last_slash_idx = strrpos($method, '/'); if ($last_slash_idx === false) { - return false; + throw new \InvalidArgumentException('service name must have a slash'); } $service_name = substr($method, 0, $last_slash_idx); return "https://" . $this->hostname . $service_name; diff --git a/src/php/phpunit.xml b/src/php/phpunit.xml new file mode 100644 index 0000000000..52ea6e8f0e --- /dev/null +++ b/src/php/phpunit.xml @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="UTF-8"?> +<phpunit bootstrap="tests/bootstrap.php" colors="true"> + <testsuites> + <testsuite name="grpc-unit-tests"> + <directory suffix="Test.php">tests/unit_tests</directory> + </testsuite> + <testsuite name="grpc-genereated-code-tests"> + <file>tests/generated_code/GeneratedCodeTest.php</file> + <file>tests/generated_code/GeneratedCodeWithCallbackTest.php</file> + </testsuite> + </testsuites> + <filter> + <whitelist> + <directory suffix=".php">lib/Grpc</directory> + </whitelist> + </filter> + <logging> + <log type="coverage-html" target="./log/codeCoverage" charset="UTF-8" + yui="true" highlight="true" + lowUpperBound="75" highLowerBound="95"/> + </logging> +</phpunit> diff --git a/src/php/tests/bootstrap.php b/src/php/tests/bootstrap.php new file mode 100644 index 0000000000..b61f2c40a5 --- /dev/null +++ b/src/php/tests/bootstrap.php @@ -0,0 +1,21 @@ +<?php +/* + * Copyright 2014 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +error_reporting(E_ALL | E_STRICT); +require dirname(__DIR__) . '/vendor/autoload.php'; +date_default_timezone_set('UTC'); diff --git a/src/php/tests/generated_code/AbstractGeneratedCodeTest.php b/src/php/tests/generated_code/AbstractGeneratedCodeTest.php index 940635389b..1819e401f6 100644 --- a/src/php/tests/generated_code/AbstractGeneratedCodeTest.php +++ b/src/php/tests/generated_code/AbstractGeneratedCodeTest.php @@ -33,6 +33,7 @@ */ require_once realpath(dirname(__FILE__) . '/../../vendor/autoload.php'); require_once dirname(__FILE__) . '/math.php'; + abstract class AbstractGeneratedCodeTest extends PHPUnit_Framework_TestCase { /* These tests require that a server exporting the math service must be * running on $GRPC_TEST_HOST */ @@ -47,6 +48,11 @@ abstract class AbstractGeneratedCodeTest extends PHPUnit_Framework_TestCase { $this->assertTrue(self::$client->waitForReady(250000)); } + public function testAlreadyReady() { + $this->assertTrue(self::$client->waitForReady(250000)); + $this->assertTrue(self::$client->waitForReady(100)); + } + public function testGetTarget() { $this->assertTrue(is_string(self::$client->getTarget())); } @@ -54,11 +60,50 @@ abstract class AbstractGeneratedCodeTest extends PHPUnit_Framework_TestCase { /** * @expectedException InvalidArgumentException */ + public function testClose() { + self::$client->close(); + $div_arg = new math\DivArgs(); + $call = self::$client->Div($div_arg); + } + + /** + * @expectedException InvalidArgumentException + */ public function testInvalidMetadata() { $div_arg = new math\DivArgs(); $call = self::$client->Div($div_arg, array(' ' => 'abc123')); } + public function testGetCallMetadata() { + $div_arg = new math\DivArgs(); + $call = self::$client->Div($div_arg); + $this->assertTrue(is_array($call->getMetadata())); + } + + public function testTimeout() { + $div_arg = new math\DivArgs(); + $call = self::$client->Div($div_arg, array('timeout' => 100)); + list($response, $status) = $call->wait(); + $this->assertSame(\Grpc\STATUS_DEADLINE_EXCEEDED, $status->code); + } + + public function testCancel() { + $div_arg = new math\DivArgs(); + $call = self::$client->Div($div_arg); + $call->cancel(); + list($response, $status) = $call->wait(); + $this->assertSame(\Grpc\STATUS_CANCELLED, $status->code); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testInvalidMethodName() { + $invalid_client = new DummyInvalidClient('host', array()); + $div_arg = new math\DivArgs(); + $invalid_client->InvalidUnaryCall($div_arg); + } + public function testWriteFlags() { $div_arg = new math\DivArgs(); $div_arg->setDividend(7); @@ -71,6 +116,36 @@ abstract class AbstractGeneratedCodeTest extends PHPUnit_Framework_TestCase { $this->assertSame(\Grpc\STATUS_OK, $status->code); } + public function testWriteFlagsServerStreaming() { + $fib_arg = new math\FibArgs(); + $fib_arg->setLimit(7); + $call = self::$client->Fib($fib_arg, array(), array('flags' => Grpc\WRITE_NO_COMPRESS)); + $result_array = iterator_to_array($call->responses()); + $status = $call->getStatus(); + $this->assertSame(\Grpc\STATUS_OK, $status->code); + } + + public function testWriteFlagsClientStreaming() { + $call = self::$client->Sum(); + $num = new math\Num(); + $num->setNum(1); + $call->write($num, array('flags' => Grpc\WRITE_NO_COMPRESS)); + list($response, $status) = $call->wait(); + $this->assertSame(\Grpc\STATUS_OK, $status->code); + } + + public function testWriteFlagsBidiStreaming() { + $call = self::$client->DivMany(); + $div_arg = new math\DivArgs(); + $div_arg->setDividend(7); + $div_arg->setDivisor(4); + $call->write($div_arg, array('flags' => Grpc\WRITE_NO_COMPRESS)); + $response = $call->read(); + $call->writesDone(); + $status = $call->getStatus(); + $this->assertSame(\Grpc\STATUS_OK, $status->code); + } + public function testSimpleRequest() { $div_arg = new math\DivArgs(); $div_arg->setDividend(7); @@ -128,3 +203,12 @@ abstract class AbstractGeneratedCodeTest extends PHPUnit_Framework_TestCase { $this->assertSame(\Grpc\STATUS_OK, $status->code); } } + +class DummyInvalidClient extends \Grpc\BaseStub { + public function InvalidUnaryCall(\math\DivArgs $argument, + $metadata = array(), + $options = array()) { + return $this->_simpleRequest('invalidMethodName', $argument, + function() {}, $metadata, $options); + } +} diff --git a/src/php/tests/generated_code/GeneratedCodeTest.php b/src/php/tests/generated_code/GeneratedCodeTest.php index e5600dc6a5..64bcf45b4a 100755 --- a/src/php/tests/generated_code/GeneratedCodeTest.php +++ b/src/php/tests/generated_code/GeneratedCodeTest.php @@ -34,7 +34,7 @@ require_once dirname(__FILE__) . '/AbstractGeneratedCodeTest.php'; class GeneratedCodeTest extends AbstractGeneratedCodeTest { - public static function setUpBeforeClass() { + public function setUp() { self::$client = new math\MathClient( getenv('GRPC_TEST_HOST'), []); } diff --git a/src/php/tests/generated_code/GeneratedCodeWithCallbackTest.php b/src/php/tests/generated_code/GeneratedCodeWithCallbackTest.php index 902eeb41e6..09c09cf353 100644 --- a/src/php/tests/generated_code/GeneratedCodeWithCallbackTest.php +++ b/src/php/tests/generated_code/GeneratedCodeWithCallbackTest.php @@ -34,7 +34,7 @@ require_once dirname(__FILE__) . '/AbstractGeneratedCodeTest.php'; class GeneratedCodeWithCallbackTest extends AbstractGeneratedCodeTest { - public static function setUpBeforeClass() { + public function setUp() { self::$client = new math\MathClient( getenv('GRPC_TEST_HOST'), ['update_metadata' => function($a_hash, |