-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathBasicClientTest.php
More file actions
213 lines (202 loc) · 7.73 KB
/
BasicClientTest.php
File metadata and controls
213 lines (202 loc) · 7.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* GENERATED CODE WARNING
* This file was automatically generated - do not edit!
*/
namespace Testing\Basic\Tests\Unit\Client;
use Google\ApiCore\ApiException;
use Google\ApiCore\CredentialsWrapper;
use Google\ApiCore\Testing\GeneratedTest;
use Google\ApiCore\Testing\MockTransport;
use Google\Rpc\Code;
use Testing\Basic\Client\BasicClient;
use Testing\Basic\Request;
use Testing\Basic\RequestWithArgs;
use Testing\Basic\Response;
use stdClass;
/**
* @group basic
*
* @group gapic
*/
class BasicClientTest extends GeneratedTest
{
/** @return TransportInterface */
private function createTransport($deserialize = null)
{
return new MockTransport($deserialize);
}
/** @return CredentialsWrapper */
private function createCredentials()
{
return $this->getMockBuilder(CredentialsWrapper::class)->disableOriginalConstructor()->getMock();
}
/** @return BasicClient */
private function createClient(array $options = [])
{
$options += [
'credentials' => $this->createCredentials(),
];
return new BasicClient($options);
}
/** @test */
public function aMethodTest()
{
$transport = $this->createTransport();
$gapicClient = $this->createClient([
'transport' => $transport,
]);
$this->assertTrue($transport->isExhausted());
// Mock response
$expectedResponse = new Response();
$transport->addResponse($expectedResponse);
$request = new Request();
$response = $gapicClient->aMethod($request);
$this->assertEquals($expectedResponse, $response);
$actualRequests = $transport->popReceivedCalls();
$this->assertSame(1, count($actualRequests));
$actualFuncCall = $actualRequests[0]->getFuncCall();
$actualRequestObject = $actualRequests[0]->getRequestObject();
$this->assertSame('/testing.basic.Basic/AMethod', $actualFuncCall);
$this->assertTrue($transport->isExhausted());
}
/** @test */
public function aMethodExceptionTest()
{
$transport = $this->createTransport();
$gapicClient = $this->createClient([
'transport' => $transport,
]);
$this->assertTrue($transport->isExhausted());
$status = new stdClass();
$status->code = Code::DATA_LOSS;
$status->details = 'internal error';
$expectedExceptionMessage = json_encode([
'message' => 'internal error',
'code' => Code::DATA_LOSS,
'status' => 'DATA_LOSS',
'details' => [],
], JSON_PRETTY_PRINT);
$transport->addResponse(null, $status);
$request = new Request();
try {
$gapicClient->aMethod($request);
// If the $gapicClient 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($expectedExceptionMessage, $ex->getMessage());
}
// Call popReceivedCalls to ensure the stub is exhausted
$transport->popReceivedCalls();
$this->assertTrue($transport->isExhausted());
}
/** @test */
public function methodWithArgsTest()
{
$transport = $this->createTransport();
$gapicClient = $this->createClient([
'transport' => $transport,
]);
$this->assertTrue($transport->isExhausted());
// Mock response
$expectedResponse = new Response();
$transport->addResponse($expectedResponse);
// Mock request
$aString = 'aString-929604177';
$partOfRequestA = [];
$status = 892481550;
$request = (new RequestWithArgs())
->setAString($aString)
->setPartOfRequestA($partOfRequestA)
->setStatus($status);
$response = $gapicClient->methodWithArgs($request);
$this->assertEquals($expectedResponse, $response);
$actualRequests = $transport->popReceivedCalls();
$this->assertSame(1, count($actualRequests));
$actualFuncCall = $actualRequests[0]->getFuncCall();
$actualRequestObject = $actualRequests[0]->getRequestObject();
$this->assertSame('/testing.basic.Basic/MethodWithArgs', $actualFuncCall);
$actualValue = $actualRequestObject->getAString();
$this->assertProtobufEquals($aString, $actualValue);
$actualValue = $actualRequestObject->getPartOfRequestA();
$this->assertProtobufEquals($partOfRequestA, $actualValue);
$actualValue = $actualRequestObject->getStatus();
$this->assertProtobufEquals($status, $actualValue);
$this->assertTrue($transport->isExhausted());
}
/** @test */
public function methodWithArgsExceptionTest()
{
$transport = $this->createTransport();
$gapicClient = $this->createClient([
'transport' => $transport,
]);
$this->assertTrue($transport->isExhausted());
$status = new stdClass();
$status->code = Code::DATA_LOSS;
$status->details = 'internal error';
$expectedExceptionMessage = json_encode([
'message' => 'internal error',
'code' => Code::DATA_LOSS,
'status' => 'DATA_LOSS',
'details' => [],
], JSON_PRETTY_PRINT);
$transport->addResponse(null, $status);
// Mock request
$aString = 'aString-929604177';
$partOfRequestA = [];
$requestStatus = 892481550;
$request = (new RequestWithArgs())
->setAString($aString)
->setPartOfRequestA($partOfRequestA)
->setStatus($requestStatus);
try {
$gapicClient->methodWithArgs($request);
// If the $gapicClient 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($expectedExceptionMessage, $ex->getMessage());
}
// Call popReceivedCalls to ensure the stub is exhausted
$transport->popReceivedCalls();
$this->assertTrue($transport->isExhausted());
}
/** @test */
public function aMethodAsyncTest()
{
$transport = $this->createTransport();
$gapicClient = $this->createClient([
'transport' => $transport,
]);
$this->assertTrue($transport->isExhausted());
// Mock response
$expectedResponse = new Response();
$transport->addResponse($expectedResponse);
$request = new Request();
$response = $gapicClient->aMethodAsync($request)->wait();
$this->assertEquals($expectedResponse, $response);
$actualRequests = $transport->popReceivedCalls();
$this->assertSame(1, count($actualRequests));
$actualFuncCall = $actualRequests[0]->getFuncCall();
$actualRequestObject = $actualRequests[0]->getRequestObject();
$this->assertSame('/testing.basic.Basic/AMethod', $actualFuncCall);
$this->assertTrue($transport->isExhausted());
}
}