diff options
Diffstat (limited to 'src/php/tests')
-rw-r--r-- | src/php/tests/generated_code/AbstractGeneratedCodeTest.php | 384 | ||||
-rwxr-xr-x | src/php/tests/generated_code/GeneratedCodeTest.php | 19 | ||||
-rw-r--r-- | src/php/tests/generated_code/GeneratedCodeWithCallbackTest.php | 34 | ||||
-rw-r--r-- | src/php/tests/generated_code/math_client.php | 55 | ||||
-rwxr-xr-x | src/php/tests/interop/interop_client.php | 599 | ||||
-rwxr-xr-x | src/php/tests/interop/message_set.php | 26 | ||||
-rwxr-xr-x | src/php/tests/unit_tests/CallTest.php | 98 | ||||
-rwxr-xr-x | src/php/tests/unit_tests/EndToEndTest.php | 415 | ||||
-rwxr-xr-x | src/php/tests/unit_tests/SecureEndToEndTest.php | 365 | ||||
-rwxr-xr-x | src/php/tests/unit_tests/TimevalTest.php | 100 |
10 files changed, 1085 insertions, 1010 deletions
diff --git a/src/php/tests/generated_code/AbstractGeneratedCodeTest.php b/src/php/tests/generated_code/AbstractGeneratedCodeTest.php index 1819e401f6..4a0bd6a1f1 100644 --- a/src/php/tests/generated_code/AbstractGeneratedCodeTest.php +++ b/src/php/tests/generated_code/AbstractGeneratedCodeTest.php @@ -31,184 +31,212 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ -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 */ - protected static $client; - protected static $timeout; - - public function testWaitForNotReady() { - $this->assertFalse(self::$client->waitForReady(1)); - } - - 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 - */ - 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); - $div_arg->setDivisor(4); - $call = self::$client->Div($div_arg, array(), array('flags' => Grpc\WRITE_NO_COMPRESS)); - $this->assertTrue(is_string($call->getPeer())); - list($response, $status) = $call->wait(); - $this->assertSame(1, $response->getQuotient()); - $this->assertSame(3, $response->getRemainder()); - $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); - $div_arg->setDivisor(4); - $call = self::$client->Div($div_arg); - $this->assertTrue(is_string($call->getPeer())); - list($response, $status) = $call->wait(); - $this->assertSame(1, $response->getQuotient()); - $this->assertSame(3, $response->getRemainder()); - $this->assertSame(\Grpc\STATUS_OK, $status->code); - } - - public function testServerStreaming() { - $fib_arg = new math\FibArgs(); - $fib_arg->setLimit(7); - $call = self::$client->Fib($fib_arg); - $this->assertTrue(is_string($call->getPeer())); - $result_array = iterator_to_array($call->responses()); - $extract_num = function($num){ - return $num->getNum(); - }; - $values = array_map($extract_num, $result_array); - $this->assertSame([1, 1, 2, 3, 5, 8, 13], $values); - $status = $call->getStatus(); - $this->assertSame(\Grpc\STATUS_OK, $status->code); - } - - public function testClientStreaming() { - $call = self::$client->Sum(); - $this->assertTrue(is_string($call->getPeer())); - for ($i = 0; $i < 7; $i++) { - $num = new math\Num(); - $num->setNum($i); - $call->write($num); - } - list($response, $status) = $call->wait(); - $this->assertSame(21, $response->getNum()); - $this->assertSame(\Grpc\STATUS_OK, $status->code); - } - - public function testBidiStreaming() { - $call = self::$client->DivMany(); - $this->assertTrue(is_string($call->getPeer())); - for ($i = 0; $i < 7; $i++) { - $div_arg = new math\DivArgs(); - $div_arg->setDividend(2 * $i + 1); - $div_arg->setDivisor(2); - $call->write($div_arg); - $response = $call->read(); - $this->assertSame($i, $response->getQuotient()); - $this->assertSame(1, $response->getRemainder()); - } - $call->writesDone(); - $status = $call->getStatus(); - $this->assertSame(\Grpc\STATUS_OK, $status->code); - } +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. + */ + protected static $client; + protected static $timeout; + + public function testWaitForNotReady() + { + $this->assertFalse(self::$client->waitForReady(1)); + } + + 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 + */ + public function testInvalidMetadata() + { + $div_arg = new math\DivArgs(); + $call = self::$client->Div($div_arg, [' ' => '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, ['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', []); + $div_arg = new math\DivArgs(); + $invalid_client->InvalidUnaryCall($div_arg); + } + + public function testWriteFlags() + { + $div_arg = new math\DivArgs(); + $div_arg->setDividend(7); + $div_arg->setDivisor(4); + $call = self::$client->Div($div_arg, [], + ['flags' => Grpc\WRITE_NO_COMPRESS]); + $this->assertTrue(is_string($call->getPeer())); + list($response, $status) = $call->wait(); + $this->assertSame(1, $response->getQuotient()); + $this->assertSame(3, $response->getRemainder()); + $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, [], + ['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, ['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, ['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); + $div_arg->setDivisor(4); + $call = self::$client->Div($div_arg); + $this->assertTrue(is_string($call->getPeer())); + list($response, $status) = $call->wait(); + $this->assertSame(1, $response->getQuotient()); + $this->assertSame(3, $response->getRemainder()); + $this->assertSame(\Grpc\STATUS_OK, $status->code); + } + + public function testServerStreaming() + { + $fib_arg = new math\FibArgs(); + $fib_arg->setLimit(7); + $call = self::$client->Fib($fib_arg); + $this->assertTrue(is_string($call->getPeer())); + $result_array = iterator_to_array($call->responses()); + $extract_num = function ($num) { + return $num->getNum(); + }; + $values = array_map($extract_num, $result_array); + $this->assertSame([1, 1, 2, 3, 5, 8, 13], $values); + $status = $call->getStatus(); + $this->assertSame(\Grpc\STATUS_OK, $status->code); + } + + public function testClientStreaming() + { + $call = self::$client->Sum(); + $this->assertTrue(is_string($call->getPeer())); + for ($i = 0; $i < 7; ++$i) { + $num = new math\Num(); + $num->setNum($i); + $call->write($num); + } + list($response, $status) = $call->wait(); + $this->assertSame(21, $response->getNum()); + $this->assertSame(\Grpc\STATUS_OK, $status->code); + } + + public function testBidiStreaming() + { + $call = self::$client->DivMany(); + $this->assertTrue(is_string($call->getPeer())); + for ($i = 0; $i < 7; ++$i) { + $div_arg = new math\DivArgs(); + $div_arg->setDividend(2 * $i + 1); + $div_arg->setDivisor(2); + $call->write($div_arg); + $response = $call->read(); + $this->assertSame($i, $response->getQuotient()); + $this->assertSame(1, $response->getRemainder()); + } + $call->writesDone(); + $status = $call->getStatus(); + $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); - } +class DummyInvalidClient extends \Grpc\BaseStub +{ + public function InvalidUnaryCall(\math\DivArgs $argument, + $metadata = [], + $options = []) + { + 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 64bcf45b4a..7043e8e1d1 100755 --- a/src/php/tests/generated_code/GeneratedCodeTest.php +++ b/src/php/tests/generated_code/GeneratedCodeTest.php @@ -31,15 +31,18 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ -require_once dirname(__FILE__) . '/AbstractGeneratedCodeTest.php'; +require_once dirname(__FILE__).'/AbstractGeneratedCodeTest.php'; -class GeneratedCodeTest extends AbstractGeneratedCodeTest { - public function setUp() { - self::$client = new math\MathClient( +class GeneratedCodeTest extends AbstractGeneratedCodeTest +{ + public function setUp() + { + self::$client = new math\MathClient( getenv('GRPC_TEST_HOST'), []); - } + } - public static function tearDownAfterClass() { - self::$client->close(); - } + public static function tearDownAfterClass() + { + self::$client->close(); + } } diff --git a/src/php/tests/generated_code/GeneratedCodeWithCallbackTest.php b/src/php/tests/generated_code/GeneratedCodeWithCallbackTest.php index 09c09cf353..5a20e684c7 100644 --- a/src/php/tests/generated_code/GeneratedCodeWithCallbackTest.php +++ b/src/php/tests/generated_code/GeneratedCodeWithCallbackTest.php @@ -31,21 +31,25 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ -require_once dirname(__FILE__) . '/AbstractGeneratedCodeTest.php'; +require_once dirname(__FILE__).'/AbstractGeneratedCodeTest.php'; -class GeneratedCodeWithCallbackTest extends AbstractGeneratedCodeTest { - public function setUp() { - self::$client = new math\MathClient( - getenv('GRPC_TEST_HOST'), ['update_metadata' => - function($a_hash, - $client = array()) { - $a_copy = $a_hash; - $a_copy['foo'] = ['bar']; - return $a_copy; - }]); - } +class GeneratedCodeWithCallbackTest extends AbstractGeneratedCodeTest +{ + public function setUp() + { + self::$client = new math\MathClient( + getenv('GRPC_TEST_HOST'), + ['update_metadata' => function ($a_hash, + $client = []) { + $a_copy = $a_hash; + $a_copy['foo'] = ['bar']; - public static function tearDownAfterClass() { - self::$client->close(); - } + return $a_copy; + }]); + } + + public static function tearDownAfterClass() + { + self::$client->close(); + } } diff --git a/src/php/tests/generated_code/math_client.php b/src/php/tests/generated_code/math_client.php index 7bc78287be..76ccabc068 100644 --- a/src/php/tests/generated_code/math_client.php +++ b/src/php/tests/generated_code/math_client.php @@ -36,31 +36,32 @@ include 'vendor/autoload.php'; include 'tests/generated_code/math.php'; -function p($line) { - print("$line<br/>\n"); +function p($line) +{ + print("$line<br/>\n"); } -$host = "localhost:50051"; +$host = 'localhost:50051'; p("Connecting to host: $host"); $client = new math\MathClient($host, []); -p("Client class: ".get_class($client)); +p('Client class: '.get_class($client)); p(''); -p("Running unary call test:"); +p('Running unary call test:'); $dividend = 7; $divisor = 4; $div_arg = new math\DivArgs(); $div_arg->setDividend($dividend); $div_arg->setDivisor($divisor); $call = $client->Div($div_arg); -p("Call peer: ".$call->getPeer()); +p('Call peer: '.$call->getPeer()); p("Dividing $dividend by $divisor"); list($response, $status) = $call->wait(); -p("quotient = ".$response->getQuotient()); -p("remainder = ".$response->getRemainder()); +p('quotient = '.$response->getQuotient()); +p('remainder = '.$response->getRemainder()); p(''); -p("Running server streaming test:"); +p('Running server streaming test:'); $limit = 7; $fib_arg = new math\FibArgs(); $fib_arg->setLimit($limit); @@ -68,35 +69,35 @@ $call = $client->Fib($fib_arg); $result_array = iterator_to_array($call->responses()); $result = ''; foreach ($result_array as $num) { - $result .= ' '.$num->getNum(); + $result .= ' '.$num->getNum(); } p("The first $limit Fibonacci numbers are:".$result); p(''); -p("Running client streaming test:"); +p('Running client streaming test:'); $call = $client->Sum(); -for ($i = 0; $i <= $limit; $i++) { - $num = new math\Num(); - $num->setNum($i); - $call->write($num); +for ($i = 0; $i <= $limit; ++$i) { + $num = new math\Num(); + $num->setNum($i); + $call->write($num); } list($response, $status) = $call->wait(); -p(sprintf("The first %d positive integers sum to: %d", +p(sprintf('The first %d positive integers sum to: %d', $limit, $response->getNum())); p(''); -p("Running bidi-streaming test:"); +p('Running bidi-streaming test:'); $call = $client->DivMany(); -for ($i = 0; $i < 7; $i++) { - $div_arg = new math\DivArgs(); - $dividend = 2 * $i + 1; - $divisor = 3; - $div_arg->setDividend($dividend); - $div_arg->setDivisor($divisor); - $call->write($div_arg); - p("client writing: $dividend / $divisor"); - $response = $call->read(); - p(sprintf("server writing: quotient = %d, remainder = %d", +for ($i = 0; $i < 7; ++$i) { + $div_arg = new math\DivArgs(); + $dividend = 2 * $i + 1; + $divisor = 3; + $div_arg->setDividend($dividend); + $div_arg->setDivisor($divisor); + $call->write($div_arg); + p("client writing: $dividend / $divisor"); + $response = $call->read(); + p(sprintf('server writing: quotient = %d, remainder = %d', $response->getQuotient(), $response->getRemainder())); } $call->writesDone(); diff --git a/src/php/tests/interop/interop_client.php b/src/php/tests/interop/interop_client.php index 6670ef3ab9..3019866561 100755 --- a/src/php/tests/interop/interop_client.php +++ b/src/php/tests/interop/interop_client.php @@ -31,9 +31,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ -require_once realpath(dirname(__FILE__) . '/../../vendor/autoload.php'); +require_once realpath(dirname(__FILE__).'/../../vendor/autoload.php'); require 'empty.php'; -require 'message_set.php'; require 'messages.php'; require 'test.php'; use Google\Auth\CredentialsLoader; @@ -42,395 +41,427 @@ use GuzzleHttp\ClientInterface; /** * Assertion function that always exits with an error code if the assertion is - * falsy + * falsy. + * * @param $value Assertion value. Should be true. * @param $error_message Message to display if the assertion is false */ -function hardAssert($value, $error_message) { - if (!$value) { - echo $error_message . "\n"; - exit(1); - } +function hardAssert($value, $error_message) +{ + if (!$value) { + echo $error_message."\n"; + exit(1); + } } /** * Run the empty_unary test. + * * @param $stub Stub object that has service methods */ -function emptyUnary($stub) { - list($result, $status) = $stub->EmptyCall(new grpc\testing\EmptyMessage())->wait(); - hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully'); - hardAssert($result !== null, 'Call completed with a null response'); +function emptyUnary($stub) +{ + list($result, $status) = $stub->EmptyCall(new grpc\testing\EmptyMessage())->wait(); + hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully'); + hardAssert($result !== null, 'Call completed with a null response'); } /** * Run the large_unary test. + * * @param $stub Stub object that has service methods */ -function largeUnary($stub) { - performLargeUnary($stub); +function largeUnary($stub) +{ + performLargeUnary($stub); } /** - * Shared code between large unary test and auth test + * Shared code between large unary test and auth test. + * * @param $stub Stub object that has service methods * @param $fillUsername boolean whether to fill result with username * @param $fillOauthScope boolean whether to fill result with oauth scope */ function performLargeUnary($stub, $fillUsername = false, $fillOauthScope = false, - $metadata = array()) { - $request_len = 271828; - $response_len = 314159; - - $request = new grpc\testing\SimpleRequest(); - $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE); - $request->setResponseSize($response_len); - $payload = new grpc\testing\Payload(); - $payload->setType(grpc\testing\PayloadType::COMPRESSABLE); - $payload->setBody(str_repeat("\0", $request_len)); - $request->setPayload($payload); - $request->setFillUsername($fillUsername); - $request->setFillOauthScope($fillOauthScope); - - list($result, $status) = $stub->UnaryCall($request, $metadata)->wait(); - hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully'); - hardAssert($result !== null, 'Call returned a null response'); - $payload = $result->getPayload(); - hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE, + $metadata = []) +{ + $request_len = 271828; + $response_len = 314159; + + $request = new grpc\testing\SimpleRequest(); + $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE); + $request->setResponseSize($response_len); + $payload = new grpc\testing\Payload(); + $payload->setType(grpc\testing\PayloadType::COMPRESSABLE); + $payload->setBody(str_repeat("\0", $request_len)); + $request->setPayload($payload); + $request->setFillUsername($fillUsername); + $request->setFillOauthScope($fillOauthScope); + + list($result, $status) = $stub->UnaryCall($request, $metadata)->wait(); + hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully'); + hardAssert($result !== null, 'Call returned a null response'); + $payload = $result->getPayload(); + hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE, 'Payload had the wrong type'); - hardAssert(strlen($payload->getBody()) === $response_len, + hardAssert(strlen($payload->getBody()) === $response_len, 'Payload had the wrong length'); - hardAssert($payload->getBody() === str_repeat("\0", $response_len), + hardAssert($payload->getBody() === str_repeat("\0", $response_len), 'Payload had the wrong content'); - return $result; + + return $result; } /** * Run the service account credentials auth test. + * * @param $stub Stub object that has service methods * @param $args array command line args */ -function serviceAccountCreds($stub, $args) { - if (!array_key_exists('oauth_scope', $args)) { - throw new Exception('Missing oauth scope'); - } - $jsonKey = json_decode( - file_get_contents(getenv(CredentialsLoader::ENV_VAR)), - true); - $result = performLargeUnary($stub, $fillUsername=true, $fillOauthScope=true); - hardAssert($result->getUsername() == $jsonKey['client_email'], +function serviceAccountCreds($stub, $args) +{ + if (!array_key_exists('oauth_scope', $args)) { + throw new Exception('Missing oauth scope'); + } + $jsonKey = json_decode( + file_get_contents(getenv(CredentialsLoader::ENV_VAR)), + true); + $result = performLargeUnary($stub, $fillUsername = true, $fillOauthScope = true); + hardAssert($result->getUsername() == $jsonKey['client_email'], 'invalid email returned'); - hardAssert(strpos($args['oauth_scope'], $result->getOauthScope()) !== false, + hardAssert(strpos($args['oauth_scope'], $result->getOauthScope()) !== false, 'invalid oauth scope returned'); } /** * Run the compute engine credentials auth test. - * Has not been run from gcloud as of 2015-05-05 + * Has not been run from gcloud as of 2015-05-05. + * * @param $stub Stub object that has service methods * @param $args array command line args */ -function computeEngineCreds($stub, $args) { - if (!array_key_exists('oauth_scope', $args)) { - throw new Exception('Missing oauth scope'); - } - if (!array_key_exists('default_service_account', $args)) { - throw new Exception('Missing default_service_account'); - } - $result = performLargeUnary($stub, $fillUsername=true, $fillOauthScope=true); - hardAssert($args['default_service_account'] == $result->getUsername(), +function computeEngineCreds($stub, $args) +{ + if (!array_key_exists('oauth_scope', $args)) { + throw new Exception('Missing oauth scope'); + } + if (!array_key_exists('default_service_account', $args)) { + throw new Exception('Missing default_service_account'); + } + $result = performLargeUnary($stub, $fillUsername = true, $fillOauthScope = true); + hardAssert($args['default_service_account'] == $result->getUsername(), 'invalid email returned'); } /** * Run the jwt token credentials auth test. + * * @param $stub Stub object that has service methods * @param $args array command line args */ -function jwtTokenCreds($stub, $args) { - $jsonKey = json_decode( - file_get_contents(getenv(CredentialsLoader::ENV_VAR)), - true); - $result = performLargeUnary($stub, $fillUsername=true, $fillOauthScope=true); - hardAssert($result->getUsername() == $jsonKey['client_email'], +function jwtTokenCreds($stub, $args) +{ + $jsonKey = json_decode( + file_get_contents(getenv(CredentialsLoader::ENV_VAR)), + true); + $result = performLargeUnary($stub, $fillUsername = true, $fillOauthScope = true); + hardAssert($result->getUsername() == $jsonKey['client_email'], 'invalid email returned'); } /** * Run the oauth2_auth_token auth test. + * * @param $stub Stub object that has service methods * @param $args array command line args */ -function oauth2AuthToken($stub, $args) { - $jsonKey = json_decode( - file_get_contents(getenv(CredentialsLoader::ENV_VAR)), - true); - $result = performLargeUnary($stub, $fillUsername=true, $fillOauthScope=true); - hardAssert($result->getUsername() == $jsonKey['client_email'], +function oauth2AuthToken($stub, $args) +{ + $jsonKey = json_decode( + file_get_contents(getenv(CredentialsLoader::ENV_VAR)), + true); + $result = performLargeUnary($stub, $fillUsername = true, $fillOauthScope = true); + hardAssert($result->getUsername() == $jsonKey['client_email'], 'invalid email returned'); } /** * Run the per_rpc_creds auth test. + * * @param $stub Stub object that has service methods * @param $args array command line args */ -function perRpcCreds($stub, $args) { - $jsonKey = json_decode( - file_get_contents(getenv(CredentialsLoader::ENV_VAR)), - true); - $auth_credentials = ApplicationDefaultCredentials::getCredentials( - $args['oauth_scope'] - ); - $token = $auth_credentials->fetchAuthToken(); - $metadata = array(CredentialsLoader::AUTH_METADATA_KEY => - array(sprintf("%s %s", - $token['token_type'], - $token['access_token']))); - $result = performLargeUnary($stub, $fillUsername=true, $fillOauthScope=true, +function perRpcCreds($stub, $args) +{ + $jsonKey = json_decode( + file_get_contents(getenv(CredentialsLoader::ENV_VAR)), + true); + $auth_credentials = ApplicationDefaultCredentials::getCredentials( + $args['oauth_scope'] + ); + $token = $auth_credentials->fetchAuthToken(); + $metadata = [CredentialsLoader::AUTH_METADATA_KEY => [sprintf('%s %s', + $token['token_type'], + $token['access_token'])]]; + $result = performLargeUnary($stub, $fillUsername = true, $fillOauthScope = true, $metadata); - hardAssert($result->getUsername() == $jsonKey['client_email'], + hardAssert($result->getUsername() == $jsonKey['client_email'], 'invalid email returned'); } /** * Run the client_streaming test. + * * @param $stub Stub object that has service methods */ -function clientStreaming($stub) { - $request_lengths = array(27182, 8, 1828, 45904); - - $requests = array_map( - function($length) { - $request = new grpc\testing\StreamingInputCallRequest(); - $payload = new grpc\testing\Payload(); - $payload->setBody(str_repeat("\0", $length)); - $request->setPayload($payload); - return $request; - }, $request_lengths); - - $call = $stub->StreamingInputCall(); - foreach ($requests as $request) { - $call->write($request); - } - list($result, $status) = $call->wait(); - hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully'); - hardAssert($result->getAggregatedPayloadSize() === 74922, +function clientStreaming($stub) +{ + $request_lengths = [27182, 8, 1828, 45904]; + + $requests = array_map( + function ($length) { + $request = new grpc\testing\StreamingInputCallRequest(); + $payload = new grpc\testing\Payload(); + $payload->setBody(str_repeat("\0", $length)); + $request->setPayload($payload); + + return $request; + }, $request_lengths); + + $call = $stub->StreamingInputCall(); + foreach ($requests as $request) { + $call->write($request); + } + list($result, $status) = $call->wait(); + hardAssert($status->code === Grpc\STATUS_OK, 'Call did not complete successfully'); + hardAssert($result->getAggregatedPayloadSize() === 74922, 'aggregated_payload_size was incorrect'); } /** * Run the server_streaming test. + * * @param $stub Stub object that has service methods. */ -function serverStreaming($stub) { - $sizes = array(31415, 9, 2653, 58979); - - $request = new grpc\testing\StreamingOutputCallRequest(); - $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE); - foreach($sizes as $size) { - $response_parameters = new grpc\testing\ResponseParameters(); - $response_parameters->setSize($size); - $request->addResponseParameters($response_parameters); - } +function serverStreaming($stub) +{ + $sizes = [31415, 9, 2653, 58979]; - $call = $stub->StreamingOutputCall($request); - $i = 0; - foreach($call->responses() as $value) { - hardAssert($i < 4, 'Too many responses'); - $payload = $value->getPayload(); - hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE, - 'Payload ' . $i . ' had the wrong type'); - hardAssert(strlen($payload->getBody()) === $sizes[$i], - 'Response ' . $i . ' had the wrong length'); - $i += 1; - } - hardAssert($call->getStatus()->code === Grpc\STATUS_OK, + $request = new grpc\testing\StreamingOutputCallRequest(); + $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE); + foreach ($sizes as $size) { + $response_parameters = new grpc\testing\ResponseParameters(); + $response_parameters->setSize($size); + $request->addResponseParameters($response_parameters); + } + + $call = $stub->StreamingOutputCall($request); + $i = 0; + foreach ($call->responses() as $value) { + hardAssert($i < 4, 'Too many responses'); + $payload = $value->getPayload(); + hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE, + 'Payload '.$i.' had the wrong type'); + hardAssert(strlen($payload->getBody()) === $sizes[$i], + 'Response '.$i.' had the wrong length'); + $i += 1; + } + hardAssert($call->getStatus()->code === Grpc\STATUS_OK, 'Call did not complete successfully'); } /** * Run the ping_pong test. + * * @param $stub Stub object that has service methods. */ -function pingPong($stub) { - $request_lengths = array(27182, 8, 1828, 45904); - $response_lengths = array(31415, 9, 2653, 58979); - - $call = $stub->FullDuplexCall(); - for($i = 0; $i < 4; $i++) { - $request = new grpc\testing\StreamingOutputCallRequest(); - $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE); - $response_parameters = new grpc\testing\ResponseParameters(); - $response_parameters->setSize($response_lengths[$i]); - $request->addResponseParameters($response_parameters); - $payload = new grpc\testing\Payload(); - $payload->setBody(str_repeat("\0", $request_lengths[$i])); - $request->setPayload($payload); - - $call->write($request); - $response = $call->read(); +function pingPong($stub) +{ + $request_lengths = [27182, 8, 1828, 45904]; + $response_lengths = [31415, 9, 2653, 58979]; + + $call = $stub->FullDuplexCall(); + for ($i = 0; $i < 4; ++$i) { + $request = new grpc\testing\StreamingOutputCallRequest(); + $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE); + $response_parameters = new grpc\testing\ResponseParameters(); + $response_parameters->setSize($response_lengths[$i]); + $request->addResponseParameters($response_parameters); + $payload = new grpc\testing\Payload(); + $payload->setBody(str_repeat("\0", $request_lengths[$i])); + $request->setPayload($payload); - hardAssert($response !== null, 'Server returned too few responses'); - $payload = $response->getPayload(); - hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE, - 'Payload ' . $i . ' had the wrong type'); - hardAssert(strlen($payload->getBody()) === $response_lengths[$i], - 'Payload ' . $i . ' had the wrong length'); - } - $call->writesDone(); - hardAssert($call->read() === null, 'Server returned too many responses'); - hardAssert($call->getStatus()->code === Grpc\STATUS_OK, + $call->write($request); + $response = $call->read(); + + hardAssert($response !== null, 'Server returned too few responses'); + $payload = $response->getPayload(); + hardAssert($payload->getType() === grpc\testing\PayloadType::COMPRESSABLE, + 'Payload '.$i.' had the wrong type'); + hardAssert(strlen($payload->getBody()) === $response_lengths[$i], + 'Payload '.$i.' had the wrong length'); + } + $call->writesDone(); + hardAssert($call->read() === null, 'Server returned too many responses'); + hardAssert($call->getStatus()->code === Grpc\STATUS_OK, 'Call did not complete successfully'); } /** * Run the empty_stream test. + * * @param $stub Stub object that has service methods. */ -function emptyStream($stub) { - $call = $stub->FullDuplexCall(); - $call->writesDone(); - hardAssert($call->read() === null, 'Server returned too many responses'); - hardAssert($call->getStatus()->code === Grpc\STATUS_OK, +function emptyStream($stub) +{ + $call = $stub->FullDuplexCall(); + $call->writesDone(); + hardAssert($call->read() === null, 'Server returned too many responses'); + hardAssert($call->getStatus()->code === Grpc\STATUS_OK, 'Call did not complete successfully'); } /** * Run the cancel_after_begin test. + * * @param $stub Stub object that has service methods. */ -function cancelAfterBegin($stub) { - $call = $stub->StreamingInputCall(); - $call->cancel(); - list($result, $status) = $call->wait(); - hardAssert($status->code === Grpc\STATUS_CANCELLED, +function cancelAfterBegin($stub) +{ + $call = $stub->StreamingInputCall(); + $call->cancel(); + list($result, $status) = $call->wait(); + hardAssert($status->code === Grpc\STATUS_CANCELLED, 'Call status was not CANCELLED'); } /** * Run the cancel_after_first_response test. + * * @param $stub Stub object that has service methods. */ -function cancelAfterFirstResponse($stub) { - $call = $stub->FullDuplexCall(); - $request = new grpc\testing\StreamingOutputCallRequest(); - $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE); - $response_parameters = new grpc\testing\ResponseParameters(); - $response_parameters->setSize(31415); - $request->addResponseParameters($response_parameters); - $payload = new grpc\testing\Payload(); - $payload->setBody(str_repeat("\0", 27182)); - $request->setPayload($payload); - - $call->write($request); - $response = $call->read(); - - $call->cancel(); - hardAssert($call->getStatus()->code === Grpc\STATUS_CANCELLED, +function cancelAfterFirstResponse($stub) +{ + $call = $stub->FullDuplexCall(); + $request = new grpc\testing\StreamingOutputCallRequest(); + $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE); + $response_parameters = new grpc\testing\ResponseParameters(); + $response_parameters->setSize(31415); + $request->addResponseParameters($response_parameters); + $payload = new grpc\testing\Payload(); + $payload->setBody(str_repeat("\0", 27182)); + $request->setPayload($payload); + + $call->write($request); + $response = $call->read(); + + $call->cancel(); + hardAssert($call->getStatus()->code === Grpc\STATUS_CANCELLED, 'Call status was not CANCELLED'); } -function timeoutOnSleepingServer($stub) { - $call = $stub->FullDuplexCall(array('timeout' => 1000)); - $request = new grpc\testing\StreamingOutputCallRequest(); - $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE); - $response_parameters = new grpc\testing\ResponseParameters(); - $response_parameters->setSize(8); - $request->addResponseParameters($response_parameters); - $payload = new grpc\testing\Payload(); - $payload->setBody(str_repeat("\0", 9)); - $request->setPayload($payload); - - $call->write($request); - $response = $call->read(); - - hardAssert($call->getStatus()->code === Grpc\STATUS_DEADLINE_EXCEEDED, +function timeoutOnSleepingServer($stub) +{ + $call = $stub->FullDuplexCall(['timeout' => 1000]); + $request = new grpc\testing\StreamingOutputCallRequest(); + $request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE); + $response_parameters = new grpc\testing\ResponseParameters(); + $response_parameters->setSize(8); + $request->addResponseParameters($response_parameters); + $payload = new grpc\testing\Payload(); + $payload->setBody(str_repeat("\0", 9)); + $request->setPayload($payload); + + $call->write($request); + $response = $call->read(); + + hardAssert($call->getStatus()->code === Grpc\STATUS_DEADLINE_EXCEEDED, 'Call status was not DEADLINE_EXCEEDED'); } -$args = getopt('', array('server_host:', 'server_port:', 'test_case:', - 'use_tls::', 'use_test_ca::', - 'server_host_override:', 'oauth_scope:', - 'default_service_account:')); +$args = getopt('', ['server_host:', 'server_port:', 'test_case:', + 'use_tls::', 'use_test_ca::', + 'server_host_override:', 'oauth_scope:', + 'default_service_account:', ]); if (!array_key_exists('server_host', $args)) { - throw new Exception('Missing argument: --server_host is required'); + throw new Exception('Missing argument: --server_host is required'); } if (!array_key_exists('server_port', $args)) { - throw new Exception('Missing argument: --server_port is required'); + throw new Exception('Missing argument: --server_port is required'); } if (!array_key_exists('test_case', $args)) { - throw new Exception('Missing argument: --test_case is required'); + throw new Exception('Missing argument: --test_case is required'); } if ($args['server_port'] == 443) { - $server_address = $args['server_host']; + $server_address = $args['server_host']; } else { - $server_address = $args['server_host'] . ':' . $args['server_port']; + $server_address = $args['server_host'].':'.$args['server_port']; } $test_case = $args['test_case']; $host_override = 'foo.test.google.fr'; if (array_key_exists('server_host_override', $args)) { - $host_override = $args['server_host_override']; + $host_override = $args['server_host_override']; } $use_tls = false; if (array_key_exists('use_tls', $args) && $args['use_tls'] != 'false') { - $use_tls = true; + $use_tls = true; } $use_test_ca = false; if (array_key_exists('use_test_ca', $args) && $args['use_test_ca'] != 'false') { - $use_test_ca = true; + $use_test_ca = true; } $opts = []; if ($use_tls) { - if ($use_test_ca) { - $ssl_cert_file = dirname(__FILE__) . '/../data/ca.pem'; - } else { - $ssl_cert_file = getenv('SSL_CERT_FILE'); - } - $ssl_credentials = Grpc\Credentials::createSsl( - file_get_contents($ssl_cert_file)); - $opts['credentials'] = $ssl_credentials; - $opts['grpc.ssl_target_name_override'] = $host_override; + if ($use_test_ca) { + $ssl_credentials = Grpc\Credentials::createSsl( + file_get_contents(dirname(__FILE__).'/../data/ca.pem')); + } else { + $ssl_credentials = Grpc\Credentials::createSsl(); + } + $opts['credentials'] = $ssl_credentials; + $opts['grpc.ssl_target_name_override'] = $host_override; } -if (in_array($test_case, array('service_account_creds', - 'compute_engine_creds', 'jwt_token_creds'))) { - if ($test_case == 'jwt_token_creds') { - $auth_credentials = ApplicationDefaultCredentials::getCredentials(); - } else { - $auth_credentials = ApplicationDefaultCredentials::getCredentials( - $args['oauth_scope'] - ); - } - $opts['update_metadata'] = $auth_credentials->getUpdateMetadataFunc(); +if (in_array($test_case, ['service_account_creds', + 'compute_engine_creds', 'jwt_token_creds', ])) { + if ($test_case == 'jwt_token_creds') { + $auth_credentials = ApplicationDefaultCredentials::getCredentials(); + } else { + $auth_credentials = ApplicationDefaultCredentials::getCredentials( + $args['oauth_scope'] + ); + } + $opts['update_metadata'] = $auth_credentials->getUpdateMetadataFunc(); } if ($test_case == 'oauth2_auth_token') { - $auth_credentials = ApplicationDefaultCredentials::getCredentials( - $args['oauth_scope'] - ); - $token = $auth_credentials->fetchAuthToken(); - $update_metadata = - function($metadata, - $authUri = null, - ClientInterface $client = null) use ($token) { - $metadata_copy = $metadata; - $metadata_copy[CredentialsLoader::AUTH_METADATA_KEY] = - array(sprintf("%s %s", - $token['token_type'], - $token['access_token'])); - return $metadata_copy; - }; - $opts['update_metadata'] = $update_metadata; + $auth_credentials = ApplicationDefaultCredentials::getCredentials( + $args['oauth_scope'] + ); + $token = $auth_credentials->fetchAuthToken(); + $update_metadata = + function ($metadata, + $authUri = null, + ClientInterface $client = null) use ($token) { + $metadata_copy = $metadata; + $metadata_copy[CredentialsLoader::AUTH_METADATA_KEY] = + [sprintf('%s %s', + $token['token_type'], + $token['access_token'])]; + + return $metadata_copy; + }; + $opts['update_metadata'] = $update_metadata; } $stub = new grpc\testing\TestServiceClient($server_address, $opts); @@ -439,49 +470,49 @@ echo "Connecting to $server_address\n"; echo "Running test case $test_case\n"; switch ($test_case) { - case 'empty_unary': - emptyUnary($stub); - break; - case 'large_unary': - largeUnary($stub); - break; - case 'client_streaming': - clientStreaming($stub); - break; - case 'server_streaming': - serverStreaming($stub); - break; - case 'ping_pong': - pingPong($stub); - break; - case 'empty_stream': - emptyStream($stub); - break; - case 'cancel_after_begin': - cancelAfterBegin($stub); - break; - case 'cancel_after_first_response': - cancelAfterFirstResponse($stub); - break; - case 'timeout_on_sleeping_server': - timeoutOnSleepingServer($stub); - break; - case 'service_account_creds': - serviceAccountCreds($stub, $args); - break; - case 'compute_engine_creds': - computeEngineCreds($stub, $args); - break; - case 'jwt_token_creds': - jwtTokenCreds($stub, $args); - break; - case 'oauth2_auth_token': - oauth2AuthToken($stub, $args); - break; - case 'per_rpc_creds': - perRpcCreds($stub, $args); - break; - default: - echo "Unsupported test case $test_case\n"; - exit(1); + case 'empty_unary': + emptyUnary($stub); + break; + case 'large_unary': + largeUnary($stub); + break; + case 'client_streaming': + clientStreaming($stub); + break; + case 'server_streaming': + serverStreaming($stub); + break; + case 'ping_pong': + pingPong($stub); + break; + case 'empty_stream': + emptyStream($stub); + break; + case 'cancel_after_begin': + cancelAfterBegin($stub); + break; + case 'cancel_after_first_response': + cancelAfterFirstResponse($stub); + break; + case 'timeout_on_sleeping_server': + timeoutOnSleepingServer($stub); + break; + case 'service_account_creds': + serviceAccountCreds($stub, $args); + break; + case 'compute_engine_creds': + computeEngineCreds($stub, $args); + break; + case 'jwt_token_creds': + jwtTokenCreds($stub, $args); + break; + case 'oauth2_auth_token': + oauth2AuthToken($stub, $args); + break; + case 'per_rpc_creds': + perRpcCreds($stub, $args); + break; + default: + echo "Unsupported test case $test_case\n"; + exit(1); } diff --git a/src/php/tests/interop/message_set.php b/src/php/tests/interop/message_set.php deleted file mode 100755 index c35c6d74b2..0000000000 --- a/src/php/tests/interop/message_set.php +++ /dev/null @@ -1,26 +0,0 @@ -<?php -// DO NOT EDIT! Generated by Protobuf-PHP protoc plugin 1.0 -// Source: net/proto2/bridge/proto/message_set.proto -// Date: 2014-12-03 22:02:20 - -namespace proto2\bridge { - - class MessageSet extends \DrSlump\Protobuf\Message { - - - /** @var \Closure[] */ - protected static $__extensions = array(); - - public static function descriptor() - { - $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'proto2.bridge.MessageSet'); - - foreach (self::$__extensions as $cb) { - $descriptor->addField($cb(), true); - } - - return $descriptor; - } - } -} - diff --git a/src/php/tests/unit_tests/CallTest.php b/src/php/tests/unit_tests/CallTest.php index caff15ee11..3b697b50c3 100755 --- a/src/php/tests/unit_tests/CallTest.php +++ b/src/php/tests/unit_tests/CallTest.php @@ -31,56 +31,64 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ -class CallTest extends PHPUnit_Framework_TestCase{ - static $server; - static $port; +class CallTest extends PHPUnit_Framework_TestCase +{ + public static $server; + public static $port; - public static function setUpBeforeClass() { - self::$server = new Grpc\Server([]); - self::$port = self::$server->addHttp2Port('0.0.0.0:0'); - } + public static function setUpBeforeClass() + { + self::$server = new Grpc\Server([]); + self::$port = self::$server->addHttp2Port('0.0.0.0:0'); + } - public function setUp() { - $this->channel = new Grpc\Channel('localhost:' . self::$port, []); - $this->call = new Grpc\Call($this->channel, - '/foo', - Grpc\Timeval::infFuture()); - } + public function setUp() + { + $this->channel = new Grpc\Channel('localhost:'.self::$port, []); + $this->call = new Grpc\Call($this->channel, + '/foo', + Grpc\Timeval::infFuture()); + } - public function testAddEmptyMetadata() { - $batch = [ - Grpc\OP_SEND_INITIAL_METADATA => [] - ]; - $result = $this->call->startBatch($batch); - $this->assertTrue($result->send_metadata); - } + public function testAddEmptyMetadata() + { + $batch = [ + Grpc\OP_SEND_INITIAL_METADATA => [], + ]; + $result = $this->call->startBatch($batch); + $this->assertTrue($result->send_metadata); + } - public function testAddSingleMetadata() { - $batch = [ - Grpc\OP_SEND_INITIAL_METADATA => ['key' => ['value']] - ]; - $result = $this->call->startBatch($batch); - $this->assertTrue($result->send_metadata); - } + public function testAddSingleMetadata() + { + $batch = [ + Grpc\OP_SEND_INITIAL_METADATA => ['key' => ['value']], + ]; + $result = $this->call->startBatch($batch); + $this->assertTrue($result->send_metadata); + } - public function testAddMultiValueMetadata() { - $batch = [ - Grpc\OP_SEND_INITIAL_METADATA => ['key' => ['value1', 'value2']] - ]; - $result = $this->call->startBatch($batch); - $this->assertTrue($result->send_metadata); - } + public function testAddMultiValueMetadata() + { + $batch = [ + Grpc\OP_SEND_INITIAL_METADATA => ['key' => ['value1', 'value2']], + ]; + $result = $this->call->startBatch($batch); + $this->assertTrue($result->send_metadata); + } - public function testAddSingleAndMultiValueMetadata() { - $batch = [ - Grpc\OP_SEND_INITIAL_METADATA => ['key1' => ['value1'], - 'key2' => ['value2', 'value3']] - ]; - $result = $this->call->startBatch($batch); - $this->assertTrue($result->send_metadata); - } + public function testAddSingleAndMultiValueMetadata() + { + $batch = [ + Grpc\OP_SEND_INITIAL_METADATA => ['key1' => ['value1'], + 'key2' => ['value2', 'value3'], ], + ]; + $result = $this->call->startBatch($batch); + $this->assertTrue($result->send_metadata); + } - public function testGetPeer() { - $this->assertTrue(is_string($this->call->getPeer())); - } + public function testGetPeer() + { + $this->assertTrue(is_string($this->call->getPeer())); + } } diff --git a/src/php/tests/unit_tests/EndToEndTest.php b/src/php/tests/unit_tests/EndToEndTest.php index b65366233a..5a38262451 100755 --- a/src/php/tests/unit_tests/EndToEndTest.php +++ b/src/php/tests/unit_tests/EndToEndTest.php @@ -31,217 +31,228 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ -class EndToEndTest extends PHPUnit_Framework_TestCase{ - public function setUp() { - $this->server = new Grpc\Server([]); - $this->port = $this->server->addHttp2Port('0.0.0.0:0'); - $this->channel = new Grpc\Channel('localhost:' . $this->port, []); - $this->server->start(); - } - - public function tearDown() { - unset($this->channel); - unset($this->server); - } - - public function testSimpleRequestBody() { - $deadline = Grpc\Timeval::infFuture(); - $status_text = 'xyz'; - $call = new Grpc\Call($this->channel, - 'dummy_method', - $deadline); - - $event = $call->startBatch([ - Grpc\OP_SEND_INITIAL_METADATA => [], - Grpc\OP_SEND_CLOSE_FROM_CLIENT => true - ]); - - $this->assertTrue($event->send_metadata); - $this->assertTrue($event->send_close); - - $event = $this->server->requestCall(); - $this->assertSame('dummy_method', $event->method); - $server_call = $event->call; - - $event = $server_call->startBatch([ - Grpc\OP_SEND_INITIAL_METADATA => [], - Grpc\OP_SEND_STATUS_FROM_SERVER => [ - 'metadata' => [], - 'code' => Grpc\STATUS_OK, - 'details' => $status_text - ], - Grpc\OP_RECV_CLOSE_ON_SERVER => true - ]); - - $this->assertTrue($event->send_metadata); - $this->assertTrue($event->send_status); - $this->assertFalse($event->cancelled); - - $event = $call->startBatch([ - Grpc\OP_RECV_INITIAL_METADATA => true, - Grpc\OP_RECV_STATUS_ON_CLIENT => true - ]); - - $status = $event->status; - $this->assertSame([], $status->metadata); - $this->assertSame(Grpc\STATUS_OK, $status->code); - $this->assertSame($status_text, $status->details); - - unset($call); - unset($server_call); - } - - public function testMessageWriteFlags() { - $deadline = Grpc\Timeval::infFuture(); - $req_text = 'message_write_flags_test'; - $status_text = 'xyz'; - $call = new Grpc\Call($this->channel, - 'dummy_method', - $deadline); - - $event = $call->startBatch([ - Grpc\OP_SEND_INITIAL_METADATA => [], - Grpc\OP_SEND_MESSAGE => ['message' => $req_text, - 'flags' => Grpc\WRITE_NO_COMPRESS], - Grpc\OP_SEND_CLOSE_FROM_CLIENT => true - ]); - - $this->assertTrue($event->send_metadata); - $this->assertTrue($event->send_close); - - $event = $this->server->requestCall(); - $this->assertSame('dummy_method', $event->method); - $server_call = $event->call; - - $event = $server_call->startBatch([ - Grpc\OP_SEND_INITIAL_METADATA => [], - Grpc\OP_SEND_STATUS_FROM_SERVER => [ - 'metadata' => [], - 'code' => Grpc\STATUS_OK, - 'details' => $status_text - ], - ]); - - $event = $call->startBatch([ - Grpc\OP_RECV_INITIAL_METADATA => true, - Grpc\OP_RECV_STATUS_ON_CLIENT => true - ]); - - $status = $event->status; - $this->assertSame([], $status->metadata); - $this->assertSame(Grpc\STATUS_OK, $status->code); - $this->assertSame($status_text, $status->details); - - unset($call); - unset($server_call); - } - - public function testClientServerFullRequestResponse() { - $deadline = Grpc\Timeval::infFuture(); - $req_text = 'client_server_full_request_response'; - $reply_text = 'reply:client_server_full_request_response'; - $status_text = 'status:client_server_full_response_text'; - - $call = new Grpc\Call($this->channel, - 'dummy_method', - $deadline); - - $event = $call->startBatch([ - Grpc\OP_SEND_INITIAL_METADATA => [], - Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, - Grpc\OP_SEND_MESSAGE => ['message' => $req_text], - ]); - - $this->assertTrue($event->send_metadata); - $this->assertTrue($event->send_close); - $this->assertTrue($event->send_message); - - $event = $this->server->requestCall(); - $this->assertSame('dummy_method', $event->method); - $server_call = $event->call; - - $event = $server_call->startBatch([ - Grpc\OP_SEND_INITIAL_METADATA => [], - Grpc\OP_SEND_MESSAGE => ['message' => $reply_text], - Grpc\OP_SEND_STATUS_FROM_SERVER => [ - 'metadata' => [], - 'code' => Grpc\STATUS_OK, - 'details' => $status_text - ], - Grpc\OP_RECV_MESSAGE => true, - Grpc\OP_RECV_CLOSE_ON_SERVER => true, - ]); - - $this->assertTrue($event->send_metadata); - $this->assertTrue($event->send_status); - $this->assertTrue($event->send_message); - $this->assertFalse($event->cancelled); - $this->assertSame($req_text, $event->message); - - $event = $call->startBatch([ - Grpc\OP_RECV_INITIAL_METADATA => true, - Grpc\OP_RECV_MESSAGE => true, - Grpc\OP_RECV_STATUS_ON_CLIENT => true, - ]); - - $this->assertSame([], $event->metadata); - $this->assertSame($reply_text, $event->message); - $status = $event->status; - $this->assertSame([], $status->metadata); - $this->assertSame(Grpc\STATUS_OK, $status->code); - $this->assertSame($status_text, $status->details); - - unset($call); - unset($server_call); - } - - public function testGetTarget() { - $this->assertTrue(is_string($this->channel->getTarget())); - } - - public function testGetConnectivityState() { - $this->assertTrue($this->channel->getConnectivityState() == Grpc\CHANNEL_IDLE); - } - - public function testWatchConnectivityStateFailed() { - $idle_state = $this->channel->getConnectivityState(); - $this->assertTrue($idle_state == Grpc\CHANNEL_IDLE); - - $now = Grpc\Timeval::now(); - $delta = new Grpc\Timeval(500000); // should timeout - $deadline = $now->add($delta); - - $this->assertFalse($this->channel->watchConnectivityState( +class EndToEndTest extends PHPUnit_Framework_TestCase +{ + public function setUp() + { + $this->server = new Grpc\Server([]); + $this->port = $this->server->addHttp2Port('0.0.0.0:0'); + $this->channel = new Grpc\Channel('localhost:'.$this->port, []); + $this->server->start(); + } + + public function tearDown() + { + unset($this->channel); + unset($this->server); + } + + public function testSimpleRequestBody() + { + $deadline = Grpc\Timeval::infFuture(); + $status_text = 'xyz'; + $call = new Grpc\Call($this->channel, + 'dummy_method', + $deadline); + + $event = $call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, + ]); + + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_close); + + $event = $this->server->requestCall(); + $this->assertSame('dummy_method', $event->method); + $server_call = $event->call; + + $event = $server_call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_STATUS_FROM_SERVER => [ + 'metadata' => [], + 'code' => Grpc\STATUS_OK, + 'details' => $status_text, + ], + Grpc\OP_RECV_CLOSE_ON_SERVER => true, + ]); + + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_status); + $this->assertFalse($event->cancelled); + + $event = $call->startBatch([ + Grpc\OP_RECV_INITIAL_METADATA => true, + Grpc\OP_RECV_STATUS_ON_CLIENT => true, + ]); + + $status = $event->status; + $this->assertSame([], $status->metadata); + $this->assertSame(Grpc\STATUS_OK, $status->code); + $this->assertSame($status_text, $status->details); + + unset($call); + unset($server_call); + } + + public function testMessageWriteFlags() + { + $deadline = Grpc\Timeval::infFuture(); + $req_text = 'message_write_flags_test'; + $status_text = 'xyz'; + $call = new Grpc\Call($this->channel, + 'dummy_method', + $deadline); + + $event = $call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_MESSAGE => ['message' => $req_text, + 'flags' => Grpc\WRITE_NO_COMPRESS, ], + Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, + ]); + + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_close); + + $event = $this->server->requestCall(); + $this->assertSame('dummy_method', $event->method); + $server_call = $event->call; + + $event = $server_call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_STATUS_FROM_SERVER => [ + 'metadata' => [], + 'code' => Grpc\STATUS_OK, + 'details' => $status_text, + ], + ]); + + $event = $call->startBatch([ + Grpc\OP_RECV_INITIAL_METADATA => true, + Grpc\OP_RECV_STATUS_ON_CLIENT => true, + ]); + + $status = $event->status; + $this->assertSame([], $status->metadata); + $this->assertSame(Grpc\STATUS_OK, $status->code); + $this->assertSame($status_text, $status->details); + + unset($call); + unset($server_call); + } + + public function testClientServerFullRequestResponse() + { + $deadline = Grpc\Timeval::infFuture(); + $req_text = 'client_server_full_request_response'; + $reply_text = 'reply:client_server_full_request_response'; + $status_text = 'status:client_server_full_response_text'; + + $call = new Grpc\Call($this->channel, + 'dummy_method', + $deadline); + + $event = $call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, + Grpc\OP_SEND_MESSAGE => ['message' => $req_text], + ]); + + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_close); + $this->assertTrue($event->send_message); + + $event = $this->server->requestCall(); + $this->assertSame('dummy_method', $event->method); + $server_call = $event->call; + + $event = $server_call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_MESSAGE => ['message' => $reply_text], + Grpc\OP_SEND_STATUS_FROM_SERVER => [ + 'metadata' => [], + 'code' => Grpc\STATUS_OK, + 'details' => $status_text, + ], + Grpc\OP_RECV_MESSAGE => true, + Grpc\OP_RECV_CLOSE_ON_SERVER => true, + ]); + + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_status); + $this->assertTrue($event->send_message); + $this->assertFalse($event->cancelled); + $this->assertSame($req_text, $event->message); + + $event = $call->startBatch([ + Grpc\OP_RECV_INITIAL_METADATA => true, + Grpc\OP_RECV_MESSAGE => true, + Grpc\OP_RECV_STATUS_ON_CLIENT => true, + ]); + + $this->assertSame([], $event->metadata); + $this->assertSame($reply_text, $event->message); + $status = $event->status; + $this->assertSame([], $status->metadata); + $this->assertSame(Grpc\STATUS_OK, $status->code); + $this->assertSame($status_text, $status->details); + + unset($call); + unset($server_call); + } + + public function testGetTarget() + { + $this->assertTrue(is_string($this->channel->getTarget())); + } + + public function testGetConnectivityState() + { + $this->assertTrue($this->channel->getConnectivityState() == Grpc\CHANNEL_IDLE); + } + + public function testWatchConnectivityStateFailed() + { + $idle_state = $this->channel->getConnectivityState(); + $this->assertTrue($idle_state == Grpc\CHANNEL_IDLE); + + $now = Grpc\Timeval::now(); + $delta = new Grpc\Timeval(500000); // should timeout + $deadline = $now->add($delta); + + $this->assertFalse($this->channel->watchConnectivityState( $idle_state, $deadline)); - } + } - public function testWatchConnectivityStateSuccess() { - $idle_state = $this->channel->getConnectivityState(true); - $this->assertTrue($idle_state == Grpc\CHANNEL_IDLE); + public function testWatchConnectivityStateSuccess() + { + $idle_state = $this->channel->getConnectivityState(true); + $this->assertTrue($idle_state == Grpc\CHANNEL_IDLE); - $now = Grpc\Timeval::now(); - $delta = new Grpc\Timeval(3000000); // should finish well before - $deadline = $now->add($delta); + $now = Grpc\Timeval::now(); + $delta = new Grpc\Timeval(3000000); // should finish well before + $deadline = $now->add($delta); - $this->assertTrue($this->channel->watchConnectivityState( + $this->assertTrue($this->channel->watchConnectivityState( $idle_state, $deadline)); - $new_state = $this->channel->getConnectivityState(); - $this->assertTrue($idle_state != $new_state); - } + $new_state = $this->channel->getConnectivityState(); + $this->assertTrue($idle_state != $new_state); + } - public function testWatchConnectivityStateDoNothing() { - $idle_state = $this->channel->getConnectivityState(); - $this->assertTrue($idle_state == Grpc\CHANNEL_IDLE); + public function testWatchConnectivityStateDoNothing() + { + $idle_state = $this->channel->getConnectivityState(); + $this->assertTrue($idle_state == Grpc\CHANNEL_IDLE); - $now = Grpc\Timeval::now(); - $delta = new Grpc\Timeval(100000); - $deadline = $now->add($delta); + $now = Grpc\Timeval::now(); + $delta = new Grpc\Timeval(100000); + $deadline = $now->add($delta); - $this->assertFalse($this->channel->watchConnectivityState( + $this->assertFalse($this->channel->watchConnectivityState( $idle_state, $deadline)); - $new_state = $this->channel->getConnectivityState(); - $this->assertTrue($new_state == Grpc\CHANNEL_IDLE); - } + $new_state = $this->channel->getConnectivityState(); + $this->assertTrue($new_state == Grpc\CHANNEL_IDLE); + } } diff --git a/src/php/tests/unit_tests/SecureEndToEndTest.php b/src/php/tests/unit_tests/SecureEndToEndTest.php index d7fca14a0d..e66bde376c 100755 --- a/src/php/tests/unit_tests/SecureEndToEndTest.php +++ b/src/php/tests/unit_tests/SecureEndToEndTest.php @@ -31,186 +31,193 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ -class SecureEndToEndTest extends PHPUnit_Framework_TestCase{ - public function setUp() { - $credentials = Grpc\Credentials::createSsl( - file_get_contents(dirname(__FILE__) . '/../data/ca.pem')); - $server_credentials = Grpc\ServerCredentials::createSsl( - null, - file_get_contents(dirname(__FILE__) . '/../data/server1.key'), - file_get_contents(dirname(__FILE__) . '/../data/server1.pem')); - $this->server = new Grpc\Server(); - $this->port = $this->server->addSecureHttp2Port('0.0.0.0:0', +class SecureEndToEndTest extends PHPUnit_Framework_TestCase +{ + public function setUp() + { + $credentials = Grpc\Credentials::createSsl( + file_get_contents(dirname(__FILE__).'/../data/ca.pem')); + $server_credentials = Grpc\ServerCredentials::createSsl( + null, + file_get_contents(dirname(__FILE__).'/../data/server1.key'), + file_get_contents(dirname(__FILE__).'/../data/server1.pem')); + $this->server = new Grpc\Server(); + $this->port = $this->server->addSecureHttp2Port('0.0.0.0:0', $server_credentials); - $this->server->start(); - $this->host_override = 'foo.test.google.fr'; - $this->channel = new Grpc\Channel( - 'localhost:' . $this->port, - [ + $this->server->start(); + $this->host_override = 'foo.test.google.fr'; + $this->channel = new Grpc\Channel( + 'localhost:'.$this->port, + [ 'grpc.ssl_target_name_override' => $this->host_override, 'grpc.default_authority' => $this->host_override, - 'credentials' => $credentials - ]); - } - - public function tearDown() { - unset($this->channel); - unset($this->server); - } - - public function testSimpleRequestBody() { - $deadline = Grpc\Timeval::infFuture(); - $status_text = 'xyz'; - $call = new Grpc\Call($this->channel, - 'dummy_method', - $deadline, - $this->host_override); - - $event = $call->startBatch([ - Grpc\OP_SEND_INITIAL_METADATA => [], - Grpc\OP_SEND_CLOSE_FROM_CLIENT => true - ]); - - $this->assertTrue($event->send_metadata); - $this->assertTrue($event->send_close); - - $event = $this->server->requestCall(); - $this->assertSame('dummy_method', $event->method); - $server_call = $event->call; - - $event = $server_call->startBatch([ - Grpc\OP_SEND_INITIAL_METADATA => [], - Grpc\OP_SEND_STATUS_FROM_SERVER => [ - 'metadata' => [], - 'code' => Grpc\STATUS_OK, - 'details' => $status_text - ], - Grpc\OP_RECV_CLOSE_ON_SERVER => true - ]); - - $this->assertTrue($event->send_metadata); - $this->assertTrue($event->send_status); - $this->assertFalse($event->cancelled); - - $event = $call->startBatch([ - Grpc\OP_RECV_INITIAL_METADATA => true, - Grpc\OP_RECV_STATUS_ON_CLIENT => true - ]); - - $this->assertSame([], $event->metadata); - $status = $event->status; - $this->assertSame([], $status->metadata); - $this->assertSame(Grpc\STATUS_OK, $status->code); - $this->assertSame($status_text, $status->details); - - unset($call); - unset($server_call); - } - - public function testMessageWriteFlags() { - $deadline = Grpc\Timeval::infFuture(); - $req_text = 'message_write_flags_test'; - $status_text = 'xyz'; - $call = new Grpc\Call($this->channel, - 'dummy_method', - $deadline, - $this->host_override); - - $event = $call->startBatch([ - Grpc\OP_SEND_INITIAL_METADATA => [], - Grpc\OP_SEND_MESSAGE => ['message' => $req_text, - 'flags' => Grpc\WRITE_NO_COMPRESS], - Grpc\OP_SEND_CLOSE_FROM_CLIENT => true - ]); - - $this->assertTrue($event->send_metadata); - $this->assertTrue($event->send_close); - - $event = $this->server->requestCall(); - $this->assertSame('dummy_method', $event->method); - $server_call = $event->call; - - $event = $server_call->startBatch([ - Grpc\OP_SEND_INITIAL_METADATA => [], - Grpc\OP_SEND_STATUS_FROM_SERVER => [ - 'metadata' => [], - 'code' => Grpc\STATUS_OK, - 'details' => $status_text - ], - ]); - - $event = $call->startBatch([ - Grpc\OP_RECV_INITIAL_METADATA => true, - Grpc\OP_RECV_STATUS_ON_CLIENT => true - ]); - - $this->assertSame([], $event->metadata); - $status = $event->status; - $this->assertSame([], $status->metadata); - $this->assertSame(Grpc\STATUS_OK, $status->code); - $this->assertSame($status_text, $status->details); - - unset($call); - unset($server_call); - } - - public function testClientServerFullRequestResponse() { - $deadline = Grpc\Timeval::infFuture(); - $req_text = 'client_server_full_request_response'; - $reply_text = 'reply:client_server_full_request_response'; - $status_text = 'status:client_server_full_response_text'; - - $call = new Grpc\Call($this->channel, - 'dummy_method', - $deadline, - $this->host_override); - - $event = $call->startBatch([ - Grpc\OP_SEND_INITIAL_METADATA => [], - Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, - Grpc\OP_SEND_MESSAGE => ['message' => $req_text] - ]); - - $this->assertTrue($event->send_metadata); - $this->assertTrue($event->send_close); - $this->assertTrue($event->send_message); - - $event = $this->server->requestCall(); - $this->assertSame('dummy_method', $event->method); - $server_call = $event->call; - - $event = $server_call->startBatch([ - Grpc\OP_SEND_INITIAL_METADATA => [], - Grpc\OP_SEND_MESSAGE => ['message' => $reply_text], - Grpc\OP_SEND_STATUS_FROM_SERVER => [ - 'metadata' => [], - 'code' => Grpc\STATUS_OK, - 'details' => $status_text - ], - Grpc\OP_RECV_MESSAGE => true, - Grpc\OP_RECV_CLOSE_ON_SERVER => true, - ]); - - $this->assertTrue($event->send_metadata); - $this->assertTrue($event->send_status); - $this->assertTrue($event->send_message); - $this->assertFalse($event->cancelled); - $this->assertSame($req_text, $event->message); - - $event = $call->startBatch([ - Grpc\OP_RECV_INITIAL_METADATA => true, - Grpc\OP_RECV_MESSAGE => true, - Grpc\OP_RECV_STATUS_ON_CLIENT => true, - ]); - - $this->assertSame([], $event->metadata); - $this->assertSame($reply_text, $event->message); - $status = $event->status; - $this->assertSame([], $status->metadata); - $this->assertSame(Grpc\STATUS_OK, $status->code); - $this->assertSame($status_text, $status->details); - - unset($call); - unset($server_call); - } + 'credentials' => $credentials, + ] + ); + } + + public function tearDown() + { + unset($this->channel); + unset($this->server); + } + + public function testSimpleRequestBody() + { + $deadline = Grpc\Timeval::infFuture(); + $status_text = 'xyz'; + $call = new Grpc\Call($this->channel, + 'dummy_method', + $deadline, + $this->host_override); + + $event = $call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, + ]); + + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_close); + + $event = $this->server->requestCall(); + $this->assertSame('dummy_method', $event->method); + $server_call = $event->call; + + $event = $server_call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_STATUS_FROM_SERVER => [ + 'metadata' => [], + 'code' => Grpc\STATUS_OK, + 'details' => $status_text, + ], + Grpc\OP_RECV_CLOSE_ON_SERVER => true, + ]); + + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_status); + $this->assertFalse($event->cancelled); + + $event = $call->startBatch([ + Grpc\OP_RECV_INITIAL_METADATA => true, + Grpc\OP_RECV_STATUS_ON_CLIENT => true, + ]); + + $this->assertSame([], $event->metadata); + $status = $event->status; + $this->assertSame([], $status->metadata); + $this->assertSame(Grpc\STATUS_OK, $status->code); + $this->assertSame($status_text, $status->details); + + unset($call); + unset($server_call); + } + + public function testMessageWriteFlags() + { + $deadline = Grpc\Timeval::infFuture(); + $req_text = 'message_write_flags_test'; + $status_text = 'xyz'; + $call = new Grpc\Call($this->channel, + 'dummy_method', + $deadline, + $this->host_override); + + $event = $call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_MESSAGE => ['message' => $req_text, + 'flags' => Grpc\WRITE_NO_COMPRESS, ], + Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, + ]); + + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_close); + + $event = $this->server->requestCall(); + $this->assertSame('dummy_method', $event->method); + $server_call = $event->call; + + $event = $server_call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_STATUS_FROM_SERVER => [ + 'metadata' => [], + 'code' => Grpc\STATUS_OK, + 'details' => $status_text, + ], + ]); + + $event = $call->startBatch([ + Grpc\OP_RECV_INITIAL_METADATA => true, + Grpc\OP_RECV_STATUS_ON_CLIENT => true, + ]); + + $this->assertSame([], $event->metadata); + $status = $event->status; + $this->assertSame([], $status->metadata); + $this->assertSame(Grpc\STATUS_OK, $status->code); + $this->assertSame($status_text, $status->details); + + unset($call); + unset($server_call); + } + + public function testClientServerFullRequestResponse() + { + $deadline = Grpc\Timeval::infFuture(); + $req_text = 'client_server_full_request_response'; + $reply_text = 'reply:client_server_full_request_response'; + $status_text = 'status:client_server_full_response_text'; + + $call = new Grpc\Call($this->channel, + 'dummy_method', + $deadline, + $this->host_override); + + $event = $call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, + Grpc\OP_SEND_MESSAGE => ['message' => $req_text], + ]); + + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_close); + $this->assertTrue($event->send_message); + + $event = $this->server->requestCall(); + $this->assertSame('dummy_method', $event->method); + $server_call = $event->call; + + $event = $server_call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_MESSAGE => ['message' => $reply_text], + Grpc\OP_SEND_STATUS_FROM_SERVER => [ + 'metadata' => [], + 'code' => Grpc\STATUS_OK, + 'details' => $status_text, + ], + Grpc\OP_RECV_MESSAGE => true, + Grpc\OP_RECV_CLOSE_ON_SERVER => true, + ]); + + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_status); + $this->assertTrue($event->send_message); + $this->assertFalse($event->cancelled); + $this->assertSame($req_text, $event->message); + + $event = $call->startBatch([ + Grpc\OP_RECV_INITIAL_METADATA => true, + Grpc\OP_RECV_MESSAGE => true, + Grpc\OP_RECV_STATUS_ON_CLIENT => true, + ]); + + $this->assertSame([], $event->metadata); + $this->assertSame($reply_text, $event->message); + $status = $event->status; + $this->assertSame([], $status->metadata); + $this->assertSame(Grpc\STATUS_OK, $status->code); + $this->assertSame($status_text, $status->details); + + unset($call); + unset($server_call); + } } diff --git a/src/php/tests/unit_tests/TimevalTest.php b/src/php/tests/unit_tests/TimevalTest.php index 7b4925cad6..1d2a8d303e 100755 --- a/src/php/tests/unit_tests/TimevalTest.php +++ b/src/php/tests/unit_tests/TimevalTest.php @@ -31,56 +31,64 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ -class TimevalTest extends PHPUnit_Framework_TestCase{ - public function testCompareSame() { - $zero = Grpc\Timeval::zero(); - $this->assertSame(0, Grpc\Timeval::compare($zero, $zero)); - } +class TimevalTest extends PHPUnit_Framework_TestCase +{ + public function testCompareSame() + { + $zero = Grpc\Timeval::zero(); + $this->assertSame(0, Grpc\Timeval::compare($zero, $zero)); + } - public function testPastIsLessThanZero() { - $zero = Grpc\Timeval::zero(); - $past = Grpc\Timeval::infPast(); - $this->assertLessThan(0, Grpc\Timeval::compare($past, $zero)); - $this->assertGreaterThan(0, Grpc\Timeval::compare($zero, $past)); - } + public function testPastIsLessThanZero() + { + $zero = Grpc\Timeval::zero(); + $past = Grpc\Timeval::infPast(); + $this->assertLessThan(0, Grpc\Timeval::compare($past, $zero)); + $this->assertGreaterThan(0, Grpc\Timeval::compare($zero, $past)); + } - public function testFutureIsGreaterThanZero() { - $zero = Grpc\Timeval::zero(); - $future = Grpc\Timeval::infFuture(); - $this->assertLessThan(0, Grpc\Timeval::compare($zero, $future)); - $this->assertGreaterThan(0, Grpc\Timeval::compare($future, $zero)); - } + public function testFutureIsGreaterThanZero() + { + $zero = Grpc\Timeval::zero(); + $future = Grpc\Timeval::infFuture(); + $this->assertLessThan(0, Grpc\Timeval::compare($zero, $future)); + $this->assertGreaterThan(0, Grpc\Timeval::compare($future, $zero)); + } - /** - * @depends testFutureIsGreaterThanZero - */ - public function testNowIsBetweenZeroAndFuture() { - $zero = Grpc\Timeval::zero(); - $future = Grpc\Timeval::infFuture(); - $now = Grpc\Timeval::now(); - $this->assertLessThan(0, Grpc\Timeval::compare($zero, $now)); - $this->assertLessThan(0, Grpc\Timeval::compare($now, $future)); - } + /** + * @depends testFutureIsGreaterThanZero + */ + public function testNowIsBetweenZeroAndFuture() + { + $zero = Grpc\Timeval::zero(); + $future = Grpc\Timeval::infFuture(); + $now = Grpc\Timeval::now(); + $this->assertLessThan(0, Grpc\Timeval::compare($zero, $now)); + $this->assertLessThan(0, Grpc\Timeval::compare($now, $future)); + } - public function testNowAndAdd() { - $now = Grpc\Timeval::now(); - $delta = new Grpc\Timeval(1000); - $deadline = $now->add($delta); - $this->assertGreaterThan(0, Grpc\Timeval::compare($deadline, $now)); - } + public function testNowAndAdd() + { + $now = Grpc\Timeval::now(); + $delta = new Grpc\Timeval(1000); + $deadline = $now->add($delta); + $this->assertGreaterThan(0, Grpc\Timeval::compare($deadline, $now)); + } - public function testNowAndSubtract() { - $now = Grpc\Timeval::now(); - $delta = new Grpc\Timeval(1000); - $deadline = $now->subtract($delta); - $this->assertLessThan(0, Grpc\Timeval::compare($deadline, $now)); - } + public function testNowAndSubtract() + { + $now = Grpc\Timeval::now(); + $delta = new Grpc\Timeval(1000); + $deadline = $now->subtract($delta); + $this->assertLessThan(0, Grpc\Timeval::compare($deadline, $now)); + } - public function testAddAndSubtract() { - $now = Grpc\Timeval::now(); - $delta = new Grpc\Timeval(1000); - $deadline = $now->add($delta); - $back_to_now = $deadline->subtract($delta); - $this->assertSame(0, Grpc\Timeval::compare($back_to_now, $now)); - } + public function testAddAndSubtract() + { + $now = Grpc\Timeval::now(); + $delta = new Grpc\Timeval(1000); + $deadline = $now->add($delta); + $back_to_now = $deadline->subtract($delta); + $this->assertSame(0, Grpc\Timeval::compare($back_to_now, $now)); + } } |