Skip to content

Commit

Permalink
Merge pull request grpc#3900 from stanley-cheung/php_unit_test_code_c…
Browse files Browse the repository at this point in the history
…overage

PHP: add more unit test for code coverage analysis
  • Loading branch information
murgatroid99 committed Oct 21, 2015
2 parents 849b2c7 + 75457ff commit 509088b
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 10 deletions.
11 changes: 7 additions & 4 deletions src/php/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/php/bin/run_gen_code_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/php/bin/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion src/php/lib/Grpc/BaseStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ private function _checkConnectivityState($new_state) {
return true;
}
if ($new_state == \Grpc\CHANNEL_FATAL_FAILURE) {
// @codeCoverageIgnoreStart
throw new \Exception('Failed to connect to server');
// @codeCoverageIgnoreEnd
}
return false;
}
Expand All @@ -132,7 +134,7 @@ public function close() {
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;
Expand Down
22 changes: 22 additions & 0 deletions src/php/phpunit.xml
Original file line number Diff line number Diff line change
@@ -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>
21 changes: 21 additions & 0 deletions src/php/tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -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');
84 changes: 84 additions & 0 deletions src/php/tests/generated_code/AbstractGeneratedCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -47,10 +48,24 @@ public function testWaitForReady() {
$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
*/
Expand All @@ -59,6 +74,36 @@ public function testInvalidMetadata() {
$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);
Expand All @@ -71,6 +116,36 @@ public function testWriteFlags() {
$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);
Expand Down Expand Up @@ -128,3 +203,12 @@ public function testBidiStreaming() {
$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);
}
}
2 changes: 1 addition & 1 deletion src/php/tests/generated_code/GeneratedCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'), []);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 509088b

Please sign in to comment.