Skip to content

Commit

Permalink
Merge pull request #11 from exonet/validate-patch
Browse files Browse the repository at this point in the history
Validate response for PATCH and DELETE requests
  • Loading branch information
rsplithof authored Jan 20, 2021
2 parents b9cf609 + b917e26 commit 84f862d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ All notable changes to `exonet-api-php` will be documented in this file.
Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## Unreleased
[Compare v2.3.0 - Unreleased](https://github.com/exonet/exonet-api-php/compare/v2.3.0...master)
[Compare v2.4.0 - Unreleased](https://github.com/exonet/exonet-api-php/compare/v2.4.0...master)

## [v2.4.0](https://github.com/exonet/exonet-api-php/releases/tag/v2.4.0) - 2021-01-20
[Compare v2.3.0 - v2.4.0](https://github.com/exonet/exonet-api-php/compare/v2.3.0...v2.4.0)
### Changed
- On `PATCH` and `DELETE` requests the response will be parsed to trigger a possible `ValidationException`.

## [v2.3.0](https://github.com/exonet/exonet-api-php/releases/tag/v2.3.0) - 2020-08-07
[Compare v2.2.0 - v2.3.0](https://github.com/exonet/exonet-api-php/compare/v2.2.0...v2.3.0)
Expand Down
8 changes: 6 additions & 2 deletions src/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ public function patch(string $urlPath, array $data): bool
json_encode($data)
);

self::httpClient()->send($request);
$response = self::httpClient()->send($request);

$this->parseResponse($response);

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

self::httpClient()->send($request);
$response = self::httpClient()->send($request);

$this->parseResponse($response);

return true;
}
Expand Down
61 changes: 61 additions & 0 deletions tests/ConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exonet\Api\Auth\PersonalAccessToken;
use Exonet\Api\Exceptions\AuthenticationException;
use Exonet\Api\Exceptions\ValidationException;
use Exonet\Api\Structures\ApiResource;
use Exonet\Api\Structures\ApiResourceSet;
use GuzzleHttp\Handler\MockHandler;
Expand Down Expand Up @@ -210,4 +211,64 @@ public function testDelete()
$this->assertSame('exonet-api-php/'.Client::CLIENT_VERSION, $request->getHeader('User-Agent')[0]);
$this->assertSame('application/json', $request->getHeader('Content-Type')[0]);
}

public function testInvalidPatch()
{
$apiCalls = [];
$mock = new MockHandler([
new Response(
422,
[],
'{"errors":[{"status":422,"code":"102.10001","title":"validation.generic","detail":"Validation did not pass.","variables":[]}]}'
),
]);

$history = Middleware::history($apiCalls);
$handler = HandlerStack::create($mock);
$handler->push($history);

new Client(new PersonalAccessToken('test-token'));
$connectorClass = new Connector($handler);

$payload = ['test' => 'demo'];

try {
$connectorClass->patch('url', $payload);
} catch (ValidationException $exception) {
$validationTested = true;
$this->assertSame($exception->getMessage(), 'There is 1 validation error.');
$this->assertCount(1, $exception->getFailedValidations());
$this->assertSame('Validation did not pass.', $exception->getFailedValidations()['generic'][0]);
}
}

public function testInvalidDelete()
{
$apiCalls = [];
$mock = new MockHandler([
new Response(
422,
[],
'{"errors":[{"status":422,"code":"102.10001","title":"validation.generic","detail":"Validation did not pass.","variables":[]}]}'
),
]);

$history = Middleware::history($apiCalls);
$handler = HandlerStack::create($mock);
$handler->push($history);

new Client(new PersonalAccessToken('test-token'));
$connectorClass = new Connector($handler);

$payload = ['test' => 'demo'];

try {
$connectorClass->patch('url', $payload);
} catch (ValidationException $exception) {
$validationTested = true;
$this->assertSame($exception->getMessage(), 'There is 1 validation error.');
$this->assertCount(1, $exception->getFailedValidations());
$this->assertSame('Validation did not pass.', $exception->getFailedValidations()['generic'][0]);
}
}
}

0 comments on commit 84f862d

Please sign in to comment.