PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.4
Booking for Appointments and Events Calendar – Amelia v2.4.4
2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / vendor / apimatic / core / tests / ClientTest.php
ameliabooking / vendor / apimatic / core / tests Last commit date
Mocking 1 year ago ApiCallTest.php 1 year ago AuthenticationTest.php 1 year ago ClientTest.php 1 year ago CoreTestCaseTest.php 1 year ago EndToEndTest.php 1 year ago LoggerTest.php 1 year ago TypesTest.php 1 year ago UtilsTest.php 1 year ago bootstrap.php 1 year ago
ClientTest.php
271 lines
1 <?php
2
3 namespace Core\Tests;
4
5 use apimatic\jsonmapper\JsonMapperException;
6 use Core\Client;
7 use Core\ClientBuilder;
8 use Core\Request\Parameters\BodyParam;
9 use Core\Request\Parameters\FormParam;
10 use Core\Request\Parameters\HeaderParam;
11 use Core\Request\Parameters\QueryParam;
12 use Core\Request\Parameters\TemplateParam;
13 use Core\Request\Request;
14 use Core\Response\Context;
15 use Core\Response\ResponseHandler;
16 use Core\Tests\Mocking\MockConverter;
17 use Core\Tests\Mocking\MockHelper;
18 use Core\Tests\Mocking\MockHttpClient;
19 use CoreInterfaces\Core\Response\ResponseInterface;
20 use CoreInterfaces\Http\HttpClientInterface;
21 use Exception;
22 use InvalidArgumentException;
23 use PHPUnit\Framework\TestCase;
24
25 class ClientTest extends TestCase
26 {
27 public function testHttpClient()
28 {
29 $httpClient = MockHelper::getClient()->getHttpClient();
30 $this->assertInstanceOf(HttpClientInterface::class, $httpClient);
31
32 $request = new Request('https://some/path');
33 $response = $httpClient->execute($request);
34
35 $this->assertInstanceOf(ResponseInterface::class, $response);
36 $this->assertEquals(200, $response->getStatusCode());
37 $this->assertEquals(['content-type' => 'application/json'], $response->getHeaders());
38 $this->assertIsObject($response->getBody());
39 $this->assertEquals(
40 '{"body":{"httpMethod":"Get","queryUrl":"https:\/\/some\/path","headers":[],' .
41 '"parameters":[],"parametersEncoded":[],"parametersMultipart":[],"body":null,' .
42 '"retryOption":"useGlobalSettings"},"additionalProperties":[]}',
43 $response->getRawBody()
44 );
45 }
46
47 public function testClientInstanceWithDifferentConfig()
48 {
49 $client = ClientBuilder::init(new MockHttpClient())
50 ->converter(new MockConverter())
51 ->serverUrls([
52 'Server1' => 'http://my/path:3000/{one}',
53 'Server2' => 'https://my/path/{two}'
54 ], 'Server1')
55 ->jsonHelper(MockHelper::getJsonHelper())
56 ->apiCallback("my call back")
57 ->build();
58
59 $this->assertInstanceOf(Client::class, $client);
60 $request = $client->getGlobalRequest();
61 $this->assertInstanceOf(Request::class, $request);
62 $client->beforeRequest($request);
63 $client->afterResponse(new Context($request, MockHelper::getResponse(), $client));
64 $responseHandler = $client->getGlobalResponseHandler();
65 $this->assertInstanceOf(ResponseHandler::class, $responseHandler);
66 }
67
68 public function testApplyingParamsWithoutValidation()
69 {
70 $request = MockHelper::getClient()->getGlobalRequest();
71 $request->appendPath('/{newKey}');
72 $queryUrl = $request->getQueryUrl();
73 $headers = $request->getHeaders();
74 $parameters = $request->getParameters();
75 $body = $request->getBody();
76
77 QueryParam::init('newKey', 'newVal')->apply($request);
78 $this->assertEquals($request->getQueryUrl(), $queryUrl);
79
80 TemplateParam::init('newKey', 'newVal')->apply($request);
81 $this->assertEquals($request->getQueryUrl(), $queryUrl);
82
83 HeaderParam::init('newKey', 'newVal')->apply($request);
84 $this->assertEquals($request->getHeaders(), $headers);
85
86 FormParam::init('newKey', 'newVal')->apply($request);
87 $this->assertEquals($request->getParameters(), $parameters);
88
89 BodyParam::init('newVal')->apply($request);
90 $this->assertEquals($request->getBody(), $body);
91 }
92
93 /**
94 * @throws Exception
95 */
96 public function fakeSerializeBy($argument)
97 {
98 throw new Exception('Invalid argument found');
99 }
100
101 public function testRequiredQueryParamValidation()
102 {
103 $this->expectException(InvalidArgumentException::class);
104 $this->expectExceptionMessage("Missing required query field: newKey");
105
106 QueryParam::init('newKey', null)->required()->validate(Client::getJsonHelper(MockHelper::getClient()));
107 }
108
109 public function testSerializeByQueryParamValidation()
110 {
111 $this->expectException(InvalidArgumentException::class);
112 $this->expectExceptionMessage("Unable to serialize field: newKey, Due to:\nInvalid argument found");
113
114 QueryParam::init('newKey', 'someVal')->serializeBy([$this, 'fakeSerializeBy'])->validate(
115 Client::getJsonHelper(MockHelper::getClient())
116 );
117 }
118
119 public function testStrictTypeQueryParamValidation()
120 {
121 $this->expectException(JsonMapperException::class);
122 $this->expectExceptionMessage("Unable to map Type: string on: oneof(int,bool)");
123
124 QueryParam::init('newKey', 'someVal')->strictType('oneof(int,bool)')->validate(
125 Client::getJsonHelper(MockHelper::getClient())
126 );
127 }
128
129 public function testRequiredTemplateParamValidation()
130 {
131 $this->expectException(InvalidArgumentException::class);
132 $this->expectExceptionMessage("Missing required template field: newKey");
133
134 TemplateParam::init('newKey', null)->required()->validate(
135 Client::getJsonHelper(MockHelper::getClient())
136 );
137 }
138
139 public function testSerializeByTemplateParamValidation()
140 {
141 $this->expectException(InvalidArgumentException::class);
142 $this->expectExceptionMessage("Unable to serialize field: newKey, Due to:\nInvalid argument found");
143
144 TemplateParam::init('newKey', 'someVal')->serializeBy([$this, 'fakeSerializeBy'])->validate(
145 Client::getJsonHelper(MockHelper::getClient())
146 );
147 }
148
149 public function testStrictTypeTemplateParamValidation()
150 {
151 $this->expectException(JsonMapperException::class);
152 $this->expectExceptionMessage("Unable to map Type: string on: oneof(int,bool)");
153
154 TemplateParam::init('newKey', 'someVal')->strictType('oneof(int,bool)')->validate(
155 Client::getJsonHelper(MockHelper::getClient())
156 );
157 }
158
159 public function testRequiredFormParamValidation()
160 {
161 $this->expectException(InvalidArgumentException::class);
162 $this->expectExceptionMessage("Missing required form field: newKey");
163
164 FormParam::init('newKey', null)->required()->validate(Client::getJsonHelper(MockHelper::getClient()));
165 }
166
167 public function testSerializeByFormParamValidation()
168 {
169 $this->expectException(InvalidArgumentException::class);
170 $this->expectExceptionMessage("Unable to serialize field: newKey, Due to:\nInvalid argument found");
171
172 FormParam::init('newKey', 'someVal')->serializeBy([$this, 'fakeSerializeBy'])->validate(
173 Client::getJsonHelper(MockHelper::getClient())
174 );
175 }
176
177 public function testStrictTypeFormParamValidation()
178 {
179 $this->expectException(JsonMapperException::class);
180 $this->expectExceptionMessage("Unable to map Type: string on: oneof(int,bool)");
181
182 FormParam::init('newKey', 'someVal')->strictType('oneof(int,bool)')->validate(
183 Client::getJsonHelper(MockHelper::getClient())
184 );
185 }
186
187 public function testRequiredHeaderParamValidation()
188 {
189 $this->expectException(InvalidArgumentException::class);
190 $this->expectExceptionMessage("Missing required header field: newKey");
191
192 HeaderParam::init('newKey', null)->required()->validate(
193 Client::getJsonHelper(MockHelper::getClient())
194 );
195 }
196
197 public function testSerializeByHeaderParamValidation()
198 {
199 $this->expectException(InvalidArgumentException::class);
200 $this->expectExceptionMessage("Unable to serialize field: newKey, Due to:\nInvalid argument found");
201
202 HeaderParam::init('newKey', 'someVal')->serializeBy([$this, 'fakeSerializeBy'])->validate(
203 Client::getJsonHelper(MockHelper::getClient())
204 );
205 }
206
207 public function testStrictTypeHeaderParamValidation()
208 {
209 $this->expectException(JsonMapperException::class);
210 $this->expectExceptionMessage("Unable to map Type: string on: oneof(int,bool)");
211
212 HeaderParam::init('newKey', 'someVal')->strictType('oneof(int,bool)')->validate(
213 Client::getJsonHelper(MockHelper::getClient())
214 );
215 }
216
217 public function testRequiredBodyParamValidation()
218 {
219 $this->expectException(InvalidArgumentException::class);
220 $this->expectExceptionMessage("Missing required body field: body");
221
222 BodyParam::init(null)->required()->validate(Client::getJsonHelper(MockHelper::getClient()));
223 }
224
225 public function testSerializeByBodyParamValidation()
226 {
227 $this->expectException(InvalidArgumentException::class);
228 $this->expectExceptionMessage("Unable to serialize field: body, Due to:\nInvalid argument found");
229
230 BodyParam::init('someVal')->serializeBy([$this, 'fakeSerializeBy'])->validate(
231 Client::getJsonHelper(MockHelper::getClient())
232 );
233 }
234
235 public function testStrictTypeBodyParamValidation()
236 {
237 $this->expectException(JsonMapperException::class);
238 $this->expectExceptionMessage("Unable to map Type: string on: oneof(int,bool)");
239
240 BodyParam::init('someVal')->strictType('oneof(int,bool)')->validate(
241 Client::getJsonHelper(MockHelper::getClient())
242 );
243 }
244
245 public function testRequestInitializationWithCustomBaseUrl()
246 {
247 $customUrl = 'https://my/path/';
248 $customUrlWithoutSlash = 'https://my/path';
249
250 $client = ClientBuilder::init(new MockHttpClient())
251 ->converter(new MockConverter())
252 ->apiCallback(MockHelper::getCallbackCatcher())
253 ->jsonHelper(MockHelper::getJsonHelper())
254 ->serverUrls([
255 'ServerA' => '{custom-url-a}',
256 'ServerB' => '{custom-url-b}',
257 ], 'ServerA')
258 ->globalConfig([
259 TemplateParam::init('custom-url-a', $customUrl)->dontEncode(),
260 TemplateParam::init('custom-url-b', $customUrlWithoutSlash)->dontEncode()
261 ])
262 ->build();
263
264 $requestA = $client->getGlobalRequest('ServerA');
265 $this->assertEquals($customUrlWithoutSlash, $requestA->getQueryUrl());
266
267 $requestB = $client->getGlobalRequest('ServerB');
268 $this->assertEquals($customUrlWithoutSlash, $requestB->getQueryUrl());
269 }
270 }
271