From 75457ff7e15263fd659049d7b09a382dde706aea Mon Sep 17 00:00:00 2001 From: Stanley Cheung Date: Tue, 20 Oct 2015 16:27:55 -0700 Subject: [PATCH] php: add more unit test for code coverage analysis --- src/php/README.md | 11 ++- src/php/bin/run_gen_code_test.sh | 4 +- src/php/bin/run_tests.sh | 2 +- src/php/lib/Grpc/BaseStub.php | 4 +- src/php/phpunit.xml | 22 +++++ src/php/tests/bootstrap.php | 21 +++++ .../AbstractGeneratedCodeTest.php | 84 +++++++++++++++++++ .../generated_code/GeneratedCodeTest.php | 2 +- .../GeneratedCodeWithCallbackTest.php | 2 +- 9 files changed, 142 insertions(+), 10 deletions(-) create mode 100644 src/php/phpunit.xml create mode 100644 src/php/tests/bootstrap.php diff --git a/src/php/README.md b/src/php/README.md index a054258782d..b1823b92261 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 5dfebb42b63..1c18756ed2d 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 5adc77879a2..2a7335e20a9 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 d4fb3ba17e5..56143145980 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 00000000000..52ea6e8f0ea --- /dev/null +++ b/src/php/phpunit.xml @@ -0,0 +1,22 @@ + + + + + tests/unit_tests + + + tests/generated_code/GeneratedCodeTest.php + tests/generated_code/GeneratedCodeWithCallbackTest.php + + + + + lib/Grpc + + + + + + diff --git a/src/php/tests/bootstrap.php b/src/php/tests/bootstrap.php new file mode 100644 index 00000000000..b61f2c40a5d --- /dev/null +++ b/src/php/tests/bootstrap.php @@ -0,0 +1,21 @@ +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())); } + /** + * @expectedException InvalidArgumentException + */ + public function testClose() { + self::$client->close(); + $div_arg = new math\DivArgs(); + $call = self::$client->Div($div_arg); + } + /** * @expectedException InvalidArgumentException */ @@ -59,6 +74,36 @@ abstract class AbstractGeneratedCodeTest extends PHPUnit_Framework_TestCase { $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 e5600dc6a51..64bcf45b4a0 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 902eeb41e6c..09c09cf3535 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,