Skip to content

Commit

Permalink
Adding isDataResourceResponse() and isErrorResponse()
Browse files Browse the repository at this point in the history
  • Loading branch information
elb98rm committed May 3, 2022
1 parent 26f0118 commit 3eb7dcb
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Models/JsonApiFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -905,4 +905,32 @@ public function validateDataResourceArray(array $data_resource_array): bool
return true;
}

/**
* Checks to see if the loaded contents are for a DataResource response
*
* @return bool
*/
public function isDataResourceResponse(): bool
{
if($this->getData() && !$this->getErrors()) {
return true;
}

return false;
}

/**
* Checks to see if the loaded contents are for an Error response
*
* @return bool
*/
public function isErrorResponse(): bool
{
if(!$this->getData() && $this->getErrors()) {
return true;
}

return false;
}

}
48 changes: 48 additions & 0 deletions tests/Unit/JsonApiFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,54 @@ public function testValidateDataResourceArrayExceptionBadAttributes()
$json_api_formatter->quickValidatorDataResourceArray($bad_array);
}

/**
* @throws JsonApiFormatterException
*/
public function testIsDataResourceResponse()
{
$json_api_formatter = new JsonApiFormatter();

$json_api_formatter->setData(new DataResource('1', 'test', ['hello' => 'world']));

$this->assertTrue(
$json_api_formatter->isDataResourceResponse(),
'isDataResourceResponse did not find a data response'
);

$json_api_formatter->reset();

$json_api_formatter->setErrors([new Error('0')]);

$this->assertFalse(
$json_api_formatter->isDataResourceResponse(),
'isDataResourceResponse found a data response when it should not have'
);
}

/**
* @throws JsonApiFormatterException
*/
public function testIsErrorResponse()
{
$json_api_formatter = new JsonApiFormatter();

$json_api_formatter->setData(new DataResource('1', 'test', ['hello' => 'world']));

$this->assertFalse(
$json_api_formatter->isErrorResponse(),
'isDataResourceResponse found an error response when it should not have'
);

$json_api_formatter->reset();

$json_api_formatter->setErrors([new Error('0')]);

$this->assertTrue(
$json_api_formatter->isErrorResponse(),
'isDataResourceResponse did not find an error response'
);
}

// Non testing functions

/**
Expand Down

0 comments on commit 3eb7dcb

Please sign in to comment.