Skip to content

Commit

Permalink
fixes a bug, where wrong ApiResponse was returned
Browse files Browse the repository at this point in the history
  • Loading branch information
asadali214 committed Sep 30, 2022
1 parent 4f4ad24 commit 46f0d8f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/Response/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public function getResponse(): ResponseInterface
return $this->response;
}

/**
* Is successful response.
*/
public function isSuccess(): bool
{
$statusCode = $this->response->getStatusCode();
return $statusCode == min(max($statusCode, 200), 208); // [200,208] = HTTP OK
}

/**
* Returns JsonHelper object.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Response/ResponseError.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function throw(Context $context)
return;
}
$statusCode = $context->getResponse()->getStatusCode();
if ($statusCode == min(max($statusCode, 200), 208)) { // [200,208] = HTTP OK
if ($context->isSuccess()) {
return;
}
if (isset($this->errors[strval($statusCode)])) {
Expand Down
3 changes: 3 additions & 0 deletions src/Response/ResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ public function getResult(Context $context)
return null;
}
$this->responseError->throw($context);
if (!$context->isSuccess()) {
return $context->toApiResponse($this->getBody($context));
}
$result = $this->deserializableType->getFrom($context);
$result = $result ?? $this->responseType->getFrom($context);
$result = $result ?? $this->responseMultiType->getFrom($context);
Expand Down
10 changes: 10 additions & 0 deletions tests/ApiCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,16 @@ public function testNullOn404()
$this->assertNull($result);
}

public function testApiResponseWith400()
{
$response = new MockResponse();
$response->setStatusCode(400);
$context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient());
$result = MockHelper::responseHandler()->type(MockClass::class)->returnApiResponse()->getResult($context);
$this->assertInstanceOf(MockApiResponse::class, $result);
$this->assertEquals(['res' => 'This is raw body'], $result->getResult());
}

public function testGlobalMockException()
{
$this->expectException(MockException::class);
Expand Down

0 comments on commit 46f0d8f

Please sign in to comment.