Merge pull request #3900 from stanley-cheung/php_unit_test_code_coverage

PHP: add more unit test for code coverage analysis
pull/3871/head
Michael Lumish 9 years ago
commit 509088bd8b
  1. 11
      src/php/README.md
  2. 4
      src/php/bin/run_gen_code_test.sh
  3. 2
      src/php/bin/run_tests.sh
  4. 4
      src/php/lib/Grpc/BaseStub.php
  5. 22
      src/php/phpunit.xml
  6. 21
      src/php/tests/bootstrap.php
  7. 84
      src/php/tests/generated_code/AbstractGeneratedCodeTest.php
  8. 2
      src/php/tests/generated_code/GeneratedCodeTest.php
  9. 2
      src/php/tests/generated_code/GeneratedCodeWithCallbackTest.php

@ -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

@ -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

@ -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

@ -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;

@ -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>

@ -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');

@ -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,10 +48,24 @@ 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()));
}
/**
* @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);
}
}

@ -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'), []);
}

@ -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,

Loading…
Cancel
Save