Skip to content

Commit

Permalink
Throw exception for ORDERPICKUP request response with additional message
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-novotny committed Oct 30, 2021
1 parent 2e895d8 commit 6b683ad
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Services/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use Inspirum\Balikobot\Contracts\RequesterInterface;
use Inspirum\Balikobot\Definitions\API;
use Inspirum\Balikobot\Definitions\Request;
use Inspirum\Balikobot\Exceptions\BadRequestException;
use function array_filter;
use function array_key_exists;
use function count;

class Client
Expand Down Expand Up @@ -306,14 +308,18 @@ public function orderPickup(
int $packageCount,
?string $message = null,
): void {
$this->requester->call(API::V2V1, $shipper, Request::ORDER_PICKUP, [
$response = $this->requester->call(API::V2V1, $shipper, Request::ORDER_PICKUP, [
'date' => $dateFrom->format('Y-m-d'),
'time_from' => $dateFrom->format('H:s'),
'time_to' => $dateTo->format('H:s'),
'weight' => $weight,
'package_count' => $packageCount,
'message' => $message,
]);

if (array_key_exists('message', $response)) {
throw new BadRequestException($response, 400, null, $response['message']);
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/Client/OrderPickupMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ public function testThrowsExceptionOnBadStatusCode(): void
$client->orderPickup('cp', new DateTime(), new DateTime('+4 HOURS'), 1, 2);
}

public function testThrowsExceptionOnMessage(): void
{
$this->expectException(BadRequestException::class);

$client = $this->newMockedClient(200, [
'status' => 200,
'message' => 'Tato metoda není u dopravce podporována.',
]);

$client->orderPickup('cp', new DateTime(), new DateTime('+4 HOURS'), 1, 2);
}

public function testMakeRequest(): void
{
$requester = $this->newRequesterWithMockedRequestMethod(200, [
Expand Down

0 comments on commit 6b683ad

Please sign in to comment.