Skip to content

Instantly share code, notes, and snippets.

@trq
Last active December 1, 2016 12:07
Show Gist options
  • Save trq/ba2836061cf40d9486554d8b1507680d to your computer and use it in GitHub Desktop.
Save trq/ba2836061cf40d9486554d8b1507680d to your computer and use it in GitHub Desktop.
<?php
// ...
/**
* Does the response contain all required attributes of a JSONApi Response Resource.
*
* @param array|null $resource
*/
protected function assertJsonAPIIsValidResource(?array $resource = null)
{
if ($resource == null) {
$resource = $this->decodeResponseJson();
} else {
// Wrap the passed in resource in "data" so we
// can re-use this function more easily
$resource = ['data' => $resource];
}
PHPUnit::assertTrue(
isset($resource['data']) &&
isset($resource['data']['type']) &&
isset($resource['data']['id'])
, 'The response does not appear to be a valid JSONApi Resource Object');
}
/**
* Does the Resource have the specific id
*
* @param string $id
* @param array|null $resource
*/
protected function assertJsonAPIResourceId($id, ?array $resource = null)
{
if ($resource == null) {
$resource = $this->decodeResponseJson();
} else {
// Wrap the passed in resource in "data" so we
// can re-use this function more easily
$resource = ['data' => $resource];
}
if (isset($resource['data']['id'])) {
PHPUnit::assertSame(
$id,
$resource['data']['id'],
"Expected id {$id}, got {$resource['data']['id']}"
);
return;
}
PHPUnit::assertTrue(false, "Expected id {$id}, but id does not exist");
}
/**
* Does the resource have the specified type
*
* @param string $type
* @param array|null $resource
*/
protected function assertJsonAPIResourceType($type, ?array $resource = null)
{
if ($resource == null) {
$resource = $this->decodeResponseJson();
} else {
// Wrap the passed in resource in "data" so we
// can re-use this function more easily
$resource = ['data' => $resource];
}
if (isset($resource['data']['type'])) {
PHPUnit::assertSame(
$type,
$resource['data']['type'],
"Expected type {$type}, got {$resource['data']['type']}"
);
return;
}
PHPUnit::assertTrue(false, "Expected type {$type}, but type does not exist");
}
/**
* Does the resource contain the specified attributes.
*
* @param string $attribute
* @param array|null $resource
*/
protected function assertJsonAPIResourceHasAttribute(string $attribute, ?array $resource = null)
{
if ($resource == null) {
$resource = $this->decodeResponseJson();
} else {
// Wrap the passed in resource in "data" so we
// can re-use this function more easily
$resource = ['data' => $resource];
}
PHPUnit::assertTrue(
array_key_exists(
$attribute,
$resource['data']['attributes']
),
"Expected attribute {$attribute}, but attribute does not exist"
);
}
/**
* Does the resource attribute contain the specific value.
*
* @param string $attribute
* @param $value
* @param array|null $resource
*/
protected function assertJsonAPIResourceAttributeHasValue(string $attribute, $value, ?array $resource = null)
{
if ($resource == null) {
$resource = $this->decodeResponseJson();
} else {
// Wrap the passed in resource in "data" so we
// can re-use this function more easily
$resource = ['data' => $resource];
}
if (isset($resource['data']['attributes'][$attribute])) {
PHPUnit::assertSame(
$value,
$resource['data']['attributes'][$attribute],
"Expected attribute {$attribute} with value {$value}, got {$resource['data']['attributes'][$attribute]}"
);
return;
}
PHPUnit::assertTrue(
false,
"Expected attribute {$attribute} to have a value of {$value}, but attribute does not exist"
);
}
/**
* Does the resource contain the specified relationship?
*
* @param string $relation
* @param array|null $resource
*/
protected function assertJsonAPIResourceHasRelationship(string $relation, ?array $resource = null)
{
if ($resource == null) {
$resource = $this->decodeResponseJson();
} else {
// Wrap the passed in resource in "data" so we
// can re-use this function more easily
$resource = ['data' => $resource];
}
PHPUnit::assertTrue(
isset($resource['data']['relationships'][$relation]),
"Expected relation {$relation}, but relation does not exist"
);
}
/**
* Get the resource relationship
*
* @param string $relation
*
* @param array|null $resource
*
* @return array|null
*/
protected function getJsonAPIResourceRelationship(string $relation, ?array $resource = null): ?array
{
if ($resource == null) {
$resource = $this->decodeResponseJson();
} else {
// Wrap the passed in resource in "data" so we
// can re-use this function more easily
$resource = ['data' => $resource];
}
if (isset($resource['data']['relationships'][$relation])) {
return $resource['data']['relationships'][$relation]['data'];
}
return null;
}
/**
* @param string $type
* @param string $id
*/
protected function assertJsonAPIIncludes(string $type, string $id)
{
$resource = $this->decodeResponseJson();
if (isset($resource['included'])) {
PHPUnit::assertTrue(
collect($resource['included'])->search(function($include) use ($type, $id) {
return $include['type'] === $type && $include['id'] === $id;
}) !== false,
"Expected to find include of type {$type} and id {$id}, include not found"
);
return;
}
PHPUnit::assertTrue(false, "Expected to find include of type {$type} and id {$id}, no includes found");
}
/**
* @param string $type
* @param string $id
*
* @return array
*/
protected function getJsonAPIInclude(string $type, string $id)
{
$resource = $this->decodeResponseJson();
if (isset($resource['included'])) {
return collect($resource['included'])->filter(function($include) use ($type, $id) {
return $include['type'] == $type && $include['id'] == $id;
})->first();
}
return [];
}
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment