Skip to content

Commit 84f862d

Browse files
authored
Merge pull request #11 from exonet/validate-patch
Validate response for PATCH and DELETE requests
2 parents b9cf609 + b917e26 commit 84f862d

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ All notable changes to `exonet-api-php` will be documented in this file.
55
Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.
66

77
## Unreleased
8-
[Compare v2.3.0 - Unreleased](https://github.com/exonet/exonet-api-php/compare/v2.3.0...master)
8+
[Compare v2.4.0 - Unreleased](https://github.com/exonet/exonet-api-php/compare/v2.4.0...master)
9+
10+
## [v2.4.0](https://github.com/exonet/exonet-api-php/releases/tag/v2.4.0) - 2021-01-20
11+
[Compare v2.3.0 - v2.4.0](https://github.com/exonet/exonet-api-php/compare/v2.3.0...v2.4.0)
12+
### Changed
13+
- On `PATCH` and `DELETE` requests the response will be parsed to trigger a possible `ValidationException`.
914

1015
## [v2.3.0](https://github.com/exonet/exonet-api-php/releases/tag/v2.3.0) - 2020-08-07
1116
[Compare v2.2.0 - v2.3.0](https://github.com/exonet/exonet-api-php/compare/v2.2.0...v2.3.0)

src/Connector.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ public function patch(string $urlPath, array $data): bool
126126
json_encode($data)
127127
);
128128

129-
self::httpClient()->send($request);
129+
$response = self::httpClient()->send($request);
130+
131+
$this->parseResponse($response);
130132

131133
return true;
132134
}
@@ -151,7 +153,9 @@ public function delete(string $urlPath, array $data = []): bool
151153
json_encode($data)
152154
);
153155

154-
self::httpClient()->send($request);
156+
$response = self::httpClient()->send($request);
157+
158+
$this->parseResponse($response);
155159

156160
return true;
157161
}

tests/ConnectorTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exonet\Api\Auth\PersonalAccessToken;
66
use Exonet\Api\Exceptions\AuthenticationException;
7+
use Exonet\Api\Exceptions\ValidationException;
78
use Exonet\Api\Structures\ApiResource;
89
use Exonet\Api\Structures\ApiResourceSet;
910
use GuzzleHttp\Handler\MockHandler;
@@ -210,4 +211,64 @@ public function testDelete()
210211
$this->assertSame('exonet-api-php/'.Client::CLIENT_VERSION, $request->getHeader('User-Agent')[0]);
211212
$this->assertSame('application/json', $request->getHeader('Content-Type')[0]);
212213
}
214+
215+
public function testInvalidPatch()
216+
{
217+
$apiCalls = [];
218+
$mock = new MockHandler([
219+
new Response(
220+
422,
221+
[],
222+
'{"errors":[{"status":422,"code":"102.10001","title":"validation.generic","detail":"Validation did not pass.","variables":[]}]}'
223+
),
224+
]);
225+
226+
$history = Middleware::history($apiCalls);
227+
$handler = HandlerStack::create($mock);
228+
$handler->push($history);
229+
230+
new Client(new PersonalAccessToken('test-token'));
231+
$connectorClass = new Connector($handler);
232+
233+
$payload = ['test' => 'demo'];
234+
235+
try {
236+
$connectorClass->patch('url', $payload);
237+
} catch (ValidationException $exception) {
238+
$validationTested = true;
239+
$this->assertSame($exception->getMessage(), 'There is 1 validation error.');
240+
$this->assertCount(1, $exception->getFailedValidations());
241+
$this->assertSame('Validation did not pass.', $exception->getFailedValidations()['generic'][0]);
242+
}
243+
}
244+
245+
public function testInvalidDelete()
246+
{
247+
$apiCalls = [];
248+
$mock = new MockHandler([
249+
new Response(
250+
422,
251+
[],
252+
'{"errors":[{"status":422,"code":"102.10001","title":"validation.generic","detail":"Validation did not pass.","variables":[]}]}'
253+
),
254+
]);
255+
256+
$history = Middleware::history($apiCalls);
257+
$handler = HandlerStack::create($mock);
258+
$handler->push($history);
259+
260+
new Client(new PersonalAccessToken('test-token'));
261+
$connectorClass = new Connector($handler);
262+
263+
$payload = ['test' => 'demo'];
264+
265+
try {
266+
$connectorClass->patch('url', $payload);
267+
} catch (ValidationException $exception) {
268+
$validationTested = true;
269+
$this->assertSame($exception->getMessage(), 'There is 1 validation error.');
270+
$this->assertCount(1, $exception->getFailedValidations());
271+
$this->assertSame('Validation did not pass.', $exception->getFailedValidations()['generic'][0]);
272+
}
273+
}
213274
}

0 commit comments

Comments
 (0)