Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
Add PHP Exception tests (#951)
Browse files Browse the repository at this point in the history
* Add exception tests
* Remove unnecessary checks in exception tests
  • Loading branch information
michaelbausor authored May 26, 2017
1 parent 08344b7 commit 9316dc5
Show file tree
Hide file tree
Showing 4 changed files with 1,033 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ private List<TestCaseView> createTestCaseViews(GapicInterfaceContext context) {
}

private void addUnitTestImports(ModelTypeTable typeTable) {
typeTable.saveNicknameFor("\\Google\\GAX\\ApiException");
typeTable.saveNicknameFor("\\Google\\GAX\\GrpcCredentialsHelper");
typeTable.saveNicknameFor("\\Google\\GAX\\LongRunning\\OperationsClient");
typeTable.saveNicknameFor("\\Google\\GAX\\Testing\\MockStubTrait");
Expand All @@ -254,5 +255,7 @@ private void addUnitTestImports(ModelTypeTable typeTable) {
typeTable.saveNicknameFor("\\google\\protobuf\\Any");
typeTable.saveNicknameFor("\\google\\protobuf\\EmptyC");
typeTable.saveNicknameFor("\\google\\longrunning\\GetOperationRequest");
typeTable.saveNicknameFor("\\Grpc");
typeTable.saveNicknameFor("\\stdClass");
}
}
314 changes: 213 additions & 101 deletions src/main/resources/com/google/api/codegen/php/test.snip
Original file line number Diff line number Diff line change
Expand Up @@ -67,128 +67,220 @@
@end

@private nonStreamingOptionalArrayTestCase(test)
@switch test.clientMethodType
@case "OptionalArrayMethod"
{@simpleTestCase(test)}
{@simpleExceptionTestCase(test)}
@case "PagedOptionalArrayMethod"
{@pageStreamingTestCase(test)}
{@simpleExceptionTestCase(test)}
@case "OperationOptionalArrayMethod"
{@lroTestCase(test)}
{@lroExceptionTestCase(test)}
@default
$unhandled case: {@test.clientMethodType}$
@end
@end

@private simpleTestCase(test)
/**
* @@test
*/
public function {@test.name}()
{
@switch test.clientMethodType
@case "OptionalArrayMethod"
{@simpleTestCase(test)}
@case "PagedOptionalArrayMethod"
{@pageStreamingTestCase(test)}
@case "OperationOptionalArrayMethod"
{@lroTestCase(test)}
@default
$unhandled case: {@test.clientMethodType}$
{@singleCallTestSetup(test)}
@if test.hasReturnValue
$response = $client->{@test.clientMethodName}({@sampleMethodCallArgList(test.initCode.fieldSettings)});
$this->assertEquals($expectedResponse, $response);
@else
$client->{@test.clientMethodName}({@sampleMethodCallArgList(test.initCode.fieldSettings)});
@end
{@singleCallSuccessAsserts(test)}

$this->assertTrue($grpcStub->isExhausted());
}
@end

@private simpleTestCase(test)
{@singleCallTestSetup(test)}
@if test.hasReturnValue
@private pageStreamingTestCase(test)
/**
* @@test
*/
public function {@test.name}()
{
{@singleCallTestSetup(test)}
$response = $client->{@test.clientMethodName}({@sampleMethodCallArgList(test.initCode.fieldSettings)});
$this->assertEquals($expectedResponse, $response);
@else
$client->{@test.clientMethodName}({@sampleMethodCallArgList(test.initCode.fieldSettings)});
@end
{@singleCallSuccessAsserts(test)}
$this->assertEquals($expectedResponse, $response->getPage()->getResponseObject());
@join pageStreamingResponseView : test.pageStreamingResponseViews
${@pageStreamingResponseView.resourcesVarName} = iterator_to_array($response->{@pageStreamingResponseView.resourcesIterateMethod}());
$this->assertSame(1, count(${@pageStreamingResponseView.resourcesVarName}));
$this->assertEquals($expectedResponse->{@pageStreamingResponseView.resourcesFieldGetterName}()[0], ${@pageStreamingResponseView.resourcesVarName}[0]);
@end

$this->assertTrue($grpcStub->isExhausted());
{@singleCallSuccessAsserts(test)}
$this->assertTrue($grpcStub->isExhausted());
}
@end

@private pageStreamingTestCase(test)
{@singleCallTestSetup(test)}
$response = $client->{@test.clientMethodName}({@sampleMethodCallArgList(test.initCode.fieldSettings)});
$this->assertEquals($expectedResponse, $response->getPage()->getResponseObject());
@join pageStreamingResponseView : test.pageStreamingResponseViews
${@pageStreamingResponseView.resourcesVarName} = iterator_to_array($response->{@pageStreamingResponseView.resourcesIterateMethod}());
$this->assertSame(1, count(${@pageStreamingResponseView.resourcesVarName}));
$this->assertEquals($expectedResponse->{@pageStreamingResponseView.resourcesFieldGetterName}()[0], ${@pageStreamingResponseView.resourcesVarName}[0]);
@end
@private simpleExceptionTestCase(test)

{@singleCallSuccessAsserts(test)}
$this->assertTrue($grpcStub->isExhausted());
@end
/**
* @@test
*/
public function {@test.nameWithException}()
{
$grpcStub = $this->createStub([$this, 'create{@test.mockGrpcStubTypeName}']);
$client = $this->createClient('{@test.createStubFunctionName}', $grpcStub);

@private lroTestCase(test)
$operationsStub = $this->createStub([$this, 'createMockOperationsStub']);
$operationsClient = new OperationsClient([
'serviceAddress' => '',
'scopes' => [],
'createOperationsStubFunction' => function ($hostname, $opts) use ($operationsStub) {
return $operationsStub;
}
]);
$grpcStub = $this->createStub([$this, 'create{@test.mockGrpcStubTypeName}']);
$client = $this->createClient('{@test.createStubFunctionName}', $grpcStub, [
'operationsClient' => $operationsClient
]);
$this->assertTrue($grpcStub->isExhausted());

$this->assertTrue($grpcStub->isExhausted());
$this->assertTrue($operationsStub->isExhausted());
$status = new stdClass();
$status->code = Grpc\STATUS_INTERNAL;
$status->details = 'internal error';
$grpcStub->addResponse(null, $status);

// Mock response
$incompleteOperation = new Operation();
$incompleteOperation->setName('operations/{@test.name}')->setDone(false);
$grpcStub->addResponse($incompleteOperation);
@if test.hasReturnValue
{@initCode(test.mockResponse.initCode)}
@else
$expectedResponse = new EmptyC();
@end
$anyResponse = new Any();
$anyResponse->setValue($expectedResponse->serialize());
$completeOperation = new Operation();
$completeOperation->setName('operations/{@test.name}')->setDone(true)->setResponse($anyResponse);
$operationsStub->addResponse($completeOperation);
@if test.hasRequestParameters
// Mock request
{@initCode(test.initCode)}

@if test.hasRequestParameters
// Mock request
{@initCode(test.initCode)}
@end
try {
$client->{@test.clientMethodName}({@sampleMethodCallArgList(test.initCode.fieldSettings)});
// If the $client method call did not throw, fail the test
$this->fail("Expected an ApiException, but no exception was thrown.");
} catch (ApiException $ex) {
$this->assertEquals($status->code, $ex->getCode());
$this->assertEquals($status->details, $ex->getMessage());
}

@end
$response = $client->{@test.clientMethodName}({@sampleMethodCallArgList(test.initCode.fieldSettings)});
$this->assertFalse($response->isDone());
$this->assertNull($response->getResult());
$apiRequests = $grpcStub->getReceivedCalls();
$this->assertSame(1, count($apiRequests));
$operationsRequestsEmpty = $operationsStub->getReceivedCalls();
$this->assertSame(0, count($operationsRequestsEmpty));
$this->assertTrue($grpcStub->isExhausted());
}
@end

$actualApiFuncCall = $apiRequests[0]->getFuncCall();
$actualApiRequestObject = $apiRequests[0]->getRequestObject();
$this->assertSame('{@test.grpcStubCallString}', $actualApiFuncCall);
@join assert : test.asserts
@if assert.hasExpectedValueTransformFunction
$this->assertEquals({@assert.expectedValueTransformFunction}(\
${@assert.expectedValueIdentifier}), \
$actualApiRequestObject->{@assert.actualValueGetter}());
@private lroTestCase(test)
/**
* @@test
*/
public function {@test.name}()
{
$operationsStub = $this->createStub([$this, 'createMockOperationsStub']);
$operationsClient = new OperationsClient([
'serviceAddress' => '',
'scopes' => [],
'createOperationsStubFunction' => function ($hostname, $opts) use ($operationsStub) {
return $operationsStub;
}
]);
$grpcStub = $this->createStub([$this, 'create{@test.mockGrpcStubTypeName}']);
$client = $this->createClient('{@test.createStubFunctionName}', $grpcStub, [
'operationsClient' => $operationsClient
]);

$this->assertTrue($grpcStub->isExhausted());
$this->assertTrue($operationsStub->isExhausted());

// Mock response
$incompleteOperation = new Operation();
$incompleteOperation->setName('operations/{@test.name}')->setDone(false);
$grpcStub->addResponse($incompleteOperation);
@if test.hasReturnValue
{@initCode(test.mockResponse.initCode)}
@else
$this->assertEquals(${@assert.expectedValueIdentifier}, \
$actualApiRequestObject->{@assert.actualValueGetter}());
$expectedResponse = new EmptyC();
@end
@end
$anyResponse = new Any();
$anyResponse->setValue($expectedResponse->serialize());
$completeOperation = new Operation();
$completeOperation->setName('operations/{@test.name}')->setDone(true)->setResponse($anyResponse);
$operationsStub->addResponse($completeOperation);

$expectedOperationsRequestObject = new GetOperationRequest();
$expectedOperationsRequestObject->setName('operations/{@test.name}');
@if test.hasRequestParameters
// Mock request
{@initCode(test.initCode)}

$response->pollUntilComplete();
$this->assertTrue($response->isDone());
$this->assertEquals($expectedResponse, $response->getResult());
$apiRequestsEmpty = $grpcStub->getReceivedCalls();
$this->assertSame(0, count($apiRequestsEmpty));
$operationsRequests = $operationsStub->getReceivedCalls();
$this->assertSame(1, count($operationsRequests));
@end
$response = $client->{@test.clientMethodName}({@sampleMethodCallArgList(test.initCode.fieldSettings)});
$this->assertFalse($response->isDone());
$this->assertNull($response->getResult());
{@lroCallSuccessAsserts(test)}

$actualOperationsFuncCall = $operationsRequests[0]->getFuncCall();
$actualOperationsRequestObject = $operationsRequests[0]->getRequestObject();
$this->assertSame('/google.longrunning.Operations/GetOperation', $actualOperationsFuncCall);
$this->assertEquals($expectedOperationsRequestObject, $actualOperationsRequestObject);
$expectedOperationsRequestObject = new GetOperationRequest();
$expectedOperationsRequestObject->setName('operations/{@test.name}');

$this->assertTrue($grpcStub->isExhausted());
$this->assertTrue($operationsStub->isExhausted());
$response->pollUntilComplete();
$this->assertTrue($response->isDone());
$this->assertEquals($expectedResponse, $response->getResult());
$apiRequestsEmpty = $grpcStub->getReceivedCalls();
$this->assertSame(0, count($apiRequestsEmpty));
$operationsRequests = $operationsStub->getReceivedCalls();
$this->assertSame(1, count($operationsRequests));

$actualOperationsFuncCall = $operationsRequests[0]->getFuncCall();
$actualOperationsRequestObject = $operationsRequests[0]->getRequestObject();
$this->assertSame('/google.longrunning.Operations/GetOperation', $actualOperationsFuncCall);
$this->assertEquals($expectedOperationsRequestObject, $actualOperationsRequestObject);

$this->assertTrue($grpcStub->isExhausted());
$this->assertTrue($operationsStub->isExhausted());
}
@end

@private lroExceptionTestCase(test)

/**
* @@test
*/
public function {@test.nameWithException}()
{
$operationsStub = $this->createStub([$this, 'createMockOperationsStub']);
$operationsClient = new OperationsClient([
'serviceAddress' => '',
'scopes' => [],
'createOperationsStubFunction' => function ($hostname, $opts) use ($operationsStub) {
return $operationsStub;
}
]);
$grpcStub = $this->createStub([$this, 'create{@test.mockGrpcStubTypeName}']);
$client = $this->createClient('{@test.createStubFunctionName}', $grpcStub, [
'operationsClient' => $operationsClient
]);

$this->assertTrue($grpcStub->isExhausted());
$this->assertTrue($operationsStub->isExhausted());

// Mock response
$incompleteOperation = new Operation();
$incompleteOperation->setName('operations/{@test.name}')->setDone(false);
$grpcStub->addResponse($incompleteOperation);

$status = new stdClass();
$status->code = Grpc\STATUS_INTERNAL;
$status->details = 'internal error';
$operationsStub->addResponse(null, $status);

@if test.hasRequestParameters
// Mock request
{@initCode(test.initCode)}

@end
$response = $client->{@test.clientMethodName}({@sampleMethodCallArgList(test.initCode.fieldSettings)});
$this->assertFalse($response->isDone());
$this->assertNull($response->getResult());

$expectedOperationsRequestObject = new GetOperationRequest();
$expectedOperationsRequestObject->setName('operations/{@test.name}');

try {
$response->pollUntilComplete();
// If the pollUntilComplete() method call did not throw, fail the test
$this->fail("Expected an ApiException, but no exception was thrown.");
} catch (ApiException $ex) {
$this->assertEquals($status->code, $ex->getCode());
$this->assertEquals($status->details, $ex->getMessage());
}

$this->assertTrue($grpcStub->isExhausted());
$this->assertTrue($operationsStub->isExhausted());
}
@end

@private singleCallTestSetup(test)
Expand All @@ -197,15 +289,14 @@

$this->assertTrue($grpcStub->isExhausted());

// Mock response
@if test.hasReturnValue
// Mock response
{@initCode(test.mockResponse.initCode)}
$grpcStub->addResponse($expectedResponse);

@else
// Add empty response to the grpc stub
$grpcStub->addResponse(new EmptyC());
$expectedResponse = new EmptyC();
@end
$grpcStub->addResponse($expectedResponse);

@if test.hasRequestParameters
// Mock request
{@initCode(test.initCode)}
Expand All @@ -231,3 +322,24 @@
@end
@end
@end

@private lroCallSuccessAsserts(test)
$apiRequests = $grpcStub->getReceivedCalls();
$this->assertSame(1, count($apiRequests));
$operationsRequestsEmpty = $operationsStub->getReceivedCalls();
$this->assertSame(0, count($operationsRequestsEmpty));

$actualApiFuncCall = $apiRequests[0]->getFuncCall();
$actualApiRequestObject = $apiRequests[0]->getRequestObject();
$this->assertSame('{@test.grpcStubCallString}', $actualApiFuncCall);
@join assert : test.asserts
@if assert.hasExpectedValueTransformFunction
$this->assertEquals({@assert.expectedValueTransformFunction}(\
${@assert.expectedValueIdentifier}), \
$actualApiRequestObject->{@assert.actualValueGetter}());
@else
$this->assertEquals(${@assert.expectedValueIdentifier}, \
$actualApiRequestObject->{@assert.actualValueGetter}());
@end
@end
@end
Loading

0 comments on commit 9316dc5

Please sign in to comment.