Skip to content

Commit 75457ff

Browse files
php: add more unit test for code coverage analysis
1 parent 63544f7 commit 75457ff

9 files changed

+142
-10
lines changed

src/php/README.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,21 @@ Prerequisite: PHP 5.5 or later, `phpunit`, `pecl`
1414
**Linux:**
1515

1616
```sh
17-
$ sudo apt-get install php5 php5-dev phpunit php-pear
17+
$ sudo apt-get install php5 php5-dev php-pear
1818
```
1919

2020
**Mac OS X:**
2121

22+
```sh
23+
$ curl -O http://pear.php.net/go-pear.phar
24+
$ sudo php -d detect_unicode=0 go-pear.phar
25+
```
26+
27+
**PHPUnit: (Both Linux and Mac OS X)**
2228
```sh
2329
$ curl https://phar.phpunit.de/phpunit.phar -o phpunit.phar
2430
$ chmod +x phpunit.phar
2531
$ sudo mv phpunit.phar /usr/local/bin/phpunit
26-
27-
$ curl -O http://pear.php.net/go-pear.phar
28-
$ sudo php -d detect_unicode=0 go-pear.phar
2932
```
3033

3134
## Quick Install

src/php/bin/run_gen_code_test.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ set -e
3232
cd $(dirname $0)
3333
source ./determine_extension_dir.sh
3434
export GRPC_TEST_HOST=localhost:50051
35-
php $extension_dir -d max_execution_time=300 $(which phpunit) -v --debug --strict \
35+
php $extension_dir -d max_execution_time=300 $(which phpunit) -v --debug \
3636
../tests/generated_code/GeneratedCodeTest.php
37-
php $extension_dir -d max_execution_time=300 $(which phpunit) -v --debug --strict \
37+
php $extension_dir -d max_execution_time=300 $(which phpunit) -v --debug \
3838
../tests/generated_code/GeneratedCodeWithCallbackTest.php

src/php/bin/run_tests.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ cd src/php/bin
3737
source ./determine_extension_dir.sh
3838
# in some jenkins macos machine, somehow the PHP build script can't find libgrpc.dylib
3939
export DYLD_LIBRARY_PATH=$root/libs/$config
40-
php $extension_dir -d max_execution_time=300 $(which phpunit) -v --debug --strict \
40+
php $extension_dir -d max_execution_time=300 $(which phpunit) -v --debug \
4141
../tests/unit_tests

src/php/lib/Grpc/BaseStub.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ private function _checkConnectivityState($new_state) {
114114
return true;
115115
}
116116
if ($new_state == \Grpc\CHANNEL_FATAL_FAILURE) {
117+
// @codeCoverageIgnoreStart
117118
throw new \Exception('Failed to connect to server');
119+
// @codeCoverageIgnoreEnd
118120
}
119121
return false;
120122
}
@@ -132,7 +134,7 @@ public function close() {
132134
private function _get_jwt_aud_uri($method) {
133135
$last_slash_idx = strrpos($method, '/');
134136
if ($last_slash_idx === false) {
135-
return false;
137+
throw new \InvalidArgumentException('service name must have a slash');
136138
}
137139
$service_name = substr($method, 0, $last_slash_idx);
138140
return "https://" . $this->hostname . $service_name;

src/php/phpunit.xml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="tests/bootstrap.php" colors="true">
3+
<testsuites>
4+
<testsuite name="grpc-unit-tests">
5+
<directory suffix="Test.php">tests/unit_tests</directory>
6+
</testsuite>
7+
<testsuite name="grpc-genereated-code-tests">
8+
<file>tests/generated_code/GeneratedCodeTest.php</file>
9+
<file>tests/generated_code/GeneratedCodeWithCallbackTest.php</file>
10+
</testsuite>
11+
</testsuites>
12+
<filter>
13+
<whitelist>
14+
<directory suffix=".php">lib/Grpc</directory>
15+
</whitelist>
16+
</filter>
17+
<logging>
18+
<log type="coverage-html" target="./log/codeCoverage" charset="UTF-8"
19+
yui="true" highlight="true"
20+
lowUpperBound="75" highLowerBound="95"/>
21+
</logging>
22+
</phpunit>

src/php/tests/bootstrap.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/*
3+
* Copyright 2014 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14+
* implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
error_reporting(E_ALL | E_STRICT);
20+
require dirname(__DIR__) . '/vendor/autoload.php';
21+
date_default_timezone_set('UTC');

src/php/tests/generated_code/AbstractGeneratedCodeTest.php

+84
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
*/
3434
require_once realpath(dirname(__FILE__) . '/../../vendor/autoload.php');
3535
require_once dirname(__FILE__) . '/math.php';
36+
3637
abstract class AbstractGeneratedCodeTest extends PHPUnit_Framework_TestCase {
3738
/* These tests require that a server exporting the math service must be
3839
* running on $GRPC_TEST_HOST */
@@ -47,10 +48,24 @@ public function testWaitForReady() {
4748
$this->assertTrue(self::$client->waitForReady(250000));
4849
}
4950

51+
public function testAlreadyReady() {
52+
$this->assertTrue(self::$client->waitForReady(250000));
53+
$this->assertTrue(self::$client->waitForReady(100));
54+
}
55+
5056
public function testGetTarget() {
5157
$this->assertTrue(is_string(self::$client->getTarget()));
5258
}
5359

60+
/**
61+
* @expectedException InvalidArgumentException
62+
*/
63+
public function testClose() {
64+
self::$client->close();
65+
$div_arg = new math\DivArgs();
66+
$call = self::$client->Div($div_arg);
67+
}
68+
5469
/**
5570
* @expectedException InvalidArgumentException
5671
*/
@@ -59,6 +74,36 @@ public function testInvalidMetadata() {
5974
$call = self::$client->Div($div_arg, array(' ' => 'abc123'));
6075
}
6176

77+
public function testGetCallMetadata() {
78+
$div_arg = new math\DivArgs();
79+
$call = self::$client->Div($div_arg);
80+
$this->assertTrue(is_array($call->getMetadata()));
81+
}
82+
83+
public function testTimeout() {
84+
$div_arg = new math\DivArgs();
85+
$call = self::$client->Div($div_arg, array('timeout' => 100));
86+
list($response, $status) = $call->wait();
87+
$this->assertSame(\Grpc\STATUS_DEADLINE_EXCEEDED, $status->code);
88+
}
89+
90+
public function testCancel() {
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+
/**
99+
* @expectedException InvalidArgumentException
100+
*/
101+
public function testInvalidMethodName() {
102+
$invalid_client = new DummyInvalidClient('host', array());
103+
$div_arg = new math\DivArgs();
104+
$invalid_client->InvalidUnaryCall($div_arg);
105+
}
106+
62107
public function testWriteFlags() {
63108
$div_arg = new math\DivArgs();
64109
$div_arg->setDividend(7);
@@ -71,6 +116,36 @@ public function testWriteFlags() {
71116
$this->assertSame(\Grpc\STATUS_OK, $status->code);
72117
}
73118

119+
public function testWriteFlagsServerStreaming() {
120+
$fib_arg = new math\FibArgs();
121+
$fib_arg->setLimit(7);
122+
$call = self::$client->Fib($fib_arg, array(), array('flags' => Grpc\WRITE_NO_COMPRESS));
123+
$result_array = iterator_to_array($call->responses());
124+
$status = $call->getStatus();
125+
$this->assertSame(\Grpc\STATUS_OK, $status->code);
126+
}
127+
128+
public function testWriteFlagsClientStreaming() {
129+
$call = self::$client->Sum();
130+
$num = new math\Num();
131+
$num->setNum(1);
132+
$call->write($num, array('flags' => Grpc\WRITE_NO_COMPRESS));
133+
list($response, $status) = $call->wait();
134+
$this->assertSame(\Grpc\STATUS_OK, $status->code);
135+
}
136+
137+
public function testWriteFlagsBidiStreaming() {
138+
$call = self::$client->DivMany();
139+
$div_arg = new math\DivArgs();
140+
$div_arg->setDividend(7);
141+
$div_arg->setDivisor(4);
142+
$call->write($div_arg, array('flags' => Grpc\WRITE_NO_COMPRESS));
143+
$response = $call->read();
144+
$call->writesDone();
145+
$status = $call->getStatus();
146+
$this->assertSame(\Grpc\STATUS_OK, $status->code);
147+
}
148+
74149
public function testSimpleRequest() {
75150
$div_arg = new math\DivArgs();
76151
$div_arg->setDividend(7);
@@ -128,3 +203,12 @@ public function testBidiStreaming() {
128203
$this->assertSame(\Grpc\STATUS_OK, $status->code);
129204
}
130205
}
206+
207+
class DummyInvalidClient extends \Grpc\BaseStub {
208+
public function InvalidUnaryCall(\math\DivArgs $argument,
209+
$metadata = array(),
210+
$options = array()) {
211+
return $this->_simpleRequest('invalidMethodName', $argument,
212+
function() {}, $metadata, $options);
213+
}
214+
}

src/php/tests/generated_code/GeneratedCodeTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
require_once dirname(__FILE__) . '/AbstractGeneratedCodeTest.php';
3535

3636
class GeneratedCodeTest extends AbstractGeneratedCodeTest {
37-
public static function setUpBeforeClass() {
37+
public function setUp() {
3838
self::$client = new math\MathClient(
3939
getenv('GRPC_TEST_HOST'), []);
4040
}

src/php/tests/generated_code/GeneratedCodeWithCallbackTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
require_once dirname(__FILE__) . '/AbstractGeneratedCodeTest.php';
3535

3636
class GeneratedCodeWithCallbackTest extends AbstractGeneratedCodeTest {
37-
public static function setUpBeforeClass() {
37+
public function setUp() {
3838
self::$client = new math\MathClient(
3939
getenv('GRPC_TEST_HOST'), ['update_metadata' =>
4040
function($a_hash,

0 commit comments

Comments
 (0)