Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/errors_all_options.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
$errorSpecApi->setHumanTitle($genericTitle='Too much options');
$errorSpecApi->setHumanDetails($specificDetails='Please, choose a bit less. Consult your ...');
$errorSpecApi->setAboutLink($specificAboutLink='https://www.example.com/explanation.html', ['foo'=>'bar']);
$errorSpecApi->appendTypeLink($genericTypeLink='https://www.example.com/documentation.html', ['foo'=>'bar']);
$errorSpecApi->setTypeLink($genericTypeLink='https://www.example.com/documentation.html', ['foo'=>'bar']);

/**
* prepare multiple error objects for the errors response
Expand Down
6 changes: 6 additions & 0 deletions src/helpers/LinksManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function addLink($key, $href, array $meta=[]) {
/**
* append a link to a key with an array of links
*
* @deprecated array links are not supported anymore {@see ->addLink()}
*
* @param string $key
* @param string $href
* @param array $meta optional, if given a LinkObject is added, otherwise a link string is added
Expand Down Expand Up @@ -56,6 +58,8 @@ public function addLinkObject($key, LinkObject $linkObject) {
/**
* set a key containing a LinksArray
*
* @deprecated array links are not supported anymore {@see ->addLinkObject()}
*
* @param string $key
* @param LinksArray $linksArray
*/
Expand All @@ -67,6 +71,8 @@ public function addLinksArray($key, LinksArray $linksArray) {
/**
* append a LinkObject to a key with a LinksArray
*
* @deprecated array links are not supported anymore {@see ->addLinkObject()}
*
* @param string $key
* @param LinkObject $linkObject
*/
Expand Down
14 changes: 13 additions & 1 deletion src/objects/ErrorObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function setHumanExplanation($genericTitle, $specificDetails=null, $speci
$this->setAboutLink($specificAboutLink);
}
if ($genericTypeLink !== null) {
$this->appendTypeLink($genericTypeLink);
$this->setTypeLink($genericTypeLink);
}
}

Expand All @@ -152,9 +152,21 @@ public function setAboutLink($href, array $meta=[]) {
$this->addLink('about', $href, $meta);
}

/**
* set the link of the generic type of this error, explained in a human-friendly way
*
* @param string $href
* @param array $meta optional, if given a LinkObject is added, otherwise a link string is added
*/
public function setTypeLink($href, array $meta=[]) {
$this->addLink('type', $href, $meta);
}

/**
* append a link of the generic type of this error, explained in a human-friendly way
*
* @deprecated array links are not supported anymore {@see ->setTypeLink()}
*
* @param string $href
* @param array $meta optional, if given a LinkObject is added, otherwise a link string is added
*/
Expand Down
6 changes: 6 additions & 0 deletions src/objects/LinksObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public function add($key, $href, array $meta=[]) {
*
* @see LinksArray for use cases
*
* @deprecated array links are not supported anymore {@see ->add()}
*
* @param string $key
* @param string $href
* @param array $meta optional, if given a LinkObject is added, otherwise a link string is added
Expand Down Expand Up @@ -120,6 +122,8 @@ public function addLinkObject($key, LinkObject $linkObject) {
}

/**
* @deprecated array links are not supported anymore {@see ->addLinkObject()}
*
* @param string $key
* @param LinksArray $linksArray
*
Expand All @@ -136,6 +140,8 @@ public function addLinksArray($key, LinksArray $linksArray) {
}

/**
* @deprecated array links are not supported anymore {@see ->addLinkObject()}
*
* @param string $key
* @param LinkObject $linkObject
*
Expand Down
8 changes: 4 additions & 4 deletions src/profiles/CursorPaginationProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public function setPaginationMeta(PaginableInterface $paginable, $exactTotal=nul
*/
public function getUnsupportedSortErrorObject($genericTitle=null, $specificDetails=null) {
$errorObject = new ErrorObject('Unsupported sort');
$errorObject->appendTypeLink('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/unsupported-sort');
$errorObject->setTypeLink('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/unsupported-sort');
$errorObject->blameQueryParameter('sort');
$errorObject->setHttpStatusCode(400);

Expand Down Expand Up @@ -278,7 +278,7 @@ public function getUnsupportedSortErrorObject($genericTitle=null, $specificDetai
*/
public function getMaxPageSizeExceededErrorObject($maxSize, $genericTitle=null, $specificDetails=null) {
$errorObject = new ErrorObject('Max page size exceeded');
$errorObject->appendTypeLink('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/max-size-exceeded');
$errorObject->setTypeLink('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/max-size-exceeded');
$errorObject->blameQueryParameter('page[size]');
$errorObject->setHttpStatusCode(400);
$errorObject->addMeta('page', $value=['maxSize' => $maxSize]);
Expand Down Expand Up @@ -313,7 +313,7 @@ public function getInvalidParameterValueErrorObject($queryParameter, $typeLink=n
$errorObject->setHttpStatusCode(400);

if ($typeLink !== null) {
$errorObject->appendTypeLink($typeLink);
$errorObject->setTypeLink($typeLink);
}

if ($genericTitle !== null) {
Expand All @@ -337,7 +337,7 @@ public function getInvalidParameterValueErrorObject($queryParameter, $typeLink=n
*/
public function getRangePaginationNotSupportedErrorObject($genericTitle=null, $specificDetails=null) {
$errorObject = new ErrorObject('Range pagination not supported');
$errorObject->appendTypeLink('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/range-pagination-not-supported');
$errorObject->setTypeLink('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/range-pagination-not-supported');
$errorObject->setHttpStatusCode(400);

if ($genericTitle !== null) {
Expand Down
16 changes: 6 additions & 10 deletions tests/example_output/errors_all_options/errors_all_options.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
"detail": "Please, choose a bit less. Consult your ...",
"links": {
"about": "https://www.example.com/explanation.html",
"type": [
"https://www.example.com/documentation.html"
]
"type": "https://www.example.com/documentation.html"
}
},
{
Expand All @@ -35,14 +33,12 @@
"foo": "bar"
}
},
"type": [
{
"href": "https://www.example.com/documentation.html",
"meta": {
"foo": "bar"
}
"type": {
"href": "https://www.example.com/documentation.html",
"meta": {
"foo": "bar"
}
]
}
},
"source": {
"pointer": "/data/attributes/title",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static function createJsonapiDocument() {
$errorSpecApi->setHumanTitle($genericTitle='Too much options');
$errorSpecApi->setHumanDetails($specificDetails='Please, choose a bit less. Consult your ...');
$errorSpecApi->setAboutLink($specificAboutLink='https://www.example.com/explanation.html', ['foo'=>'bar']);
$errorSpecApi->appendTypeLink($genericTypeLink='https://www.example.com/documentation.html', ['foo'=>'bar']);
$errorSpecApi->setTypeLink($genericTypeLink='https://www.example.com/documentation.html', ['foo'=>'bar']);

$metaObject = new \stdClass();
$metaObject->property = 'value';
Expand Down
6 changes: 6 additions & 0 deletions tests/helpers/LinksManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public function testAddLinkObject_HappyPath() {
$this->assertSame('https://jsonapi.org', $array['foo']['href']);
}

/**
* @deprecated array links are not supported anymore
*/
public function testAddLinksArray_HappyPath() {
$linksArray = new LinksArray();
$linksArray->add('https://jsonapi.org');
Expand All @@ -98,6 +101,9 @@ public function testAddLinksArray_HappyPath() {
$this->assertSame('https://jsonapi.org', $array['foo'][0]);
}

/**
* @deprecated array links are not supported anymore
*/
public function testAppendLinkObject_HappyPath() {
$linksManager = new LinksManager();
$linksManager->addLinksArray('foo', LinksArray::fromArray(['https://jsonapi.org/1']));
Expand Down
18 changes: 18 additions & 0 deletions tests/objects/ErrorObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,24 @@ public function testFromException_BlocksNonException() {
ErrorObject::fromException(new \stdClass());
}

/**
* @deprecated array links are not supported anymore
*/
public function testAppendTypeLink_HappyPath() {
$errorObject = new ErrorObject();
$this->assertTrue($errorObject->isEmpty());

$errorObject->appendTypeLink('https://jsonapi.org');

$this->assertFalse($errorObject->isEmpty());

$array = $errorObject->toArray();

$this->assertArrayHasKey('links', $array);
$this->assertArrayHasKey('type', $array['links']);
$this->assertSame(['https://jsonapi.org'], $array['links']['type']);
}

public function testIsEmpty_All() {
$errorObject = new ErrorObject();
$this->assertTrue($errorObject->isEmpty());
Expand Down
3 changes: 3 additions & 0 deletions tests/objects/LinksArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use alsvanzelf\jsonapi\objects\LinksArray;
use PHPUnit\Framework\TestCase;

/**
* @deprecated array links are not supported anymore
*/
class LinksArrayTest extends TestCase {
public function testFromObject_HappyPath() {
$object = new \stdClass();
Expand Down
6 changes: 6 additions & 0 deletions tests/objects/LinksObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ public function testAddLinkObject_ExistingKey() {
$linksObject->addLinkObject($key='foo', $linkObject);
}

/**
* @deprecated array links are not supported anymore
*/
public function testAddLinksArray_HappyPath() {
$linksObject = new LinksObject();
$linksObject->addLinksArray('foo', LinksArray::fromArray(['https://jsonapi.org']));
Expand All @@ -122,6 +125,9 @@ public function testAddLinksArray_HappyPath() {
$this->assertSame('https://jsonapi.org', $array['foo'][0]);
}

/**
* @deprecated array links are not supported anymore
*/
public function testAddLinksArray_BlocksReusingNonArray() {
$linksObject = new LinksObject();
$linksObject->add('foo', 'https://jsonapi.org');
Expand Down
12 changes: 4 additions & 8 deletions tests/profiles/CursorPaginationProfileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,11 @@ public function testGetUnsupportedSortErrorObject_HappyPath() {
$this->assertArrayHasKey('type', $array['links']);
$this->assertArrayHasKey('source', $array);
$this->assertArrayHasKey('parameter', $array['source']);
$this->assertCount(1, $array['links']['type']);
$this->assertSame('400', $array['status']);
$this->assertSame('Unsupported sort', $array['code']);
$this->assertSame($genericTitle, $array['title']);
$this->assertSame($specificDetails, $array['detail']);
$this->assertSame('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/unsupported-sort', $array['links']['type'][0]);
$this->assertSame('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/unsupported-sort', $array['links']['type']);
$this->assertSame('sort', $array['source']['parameter']);
}

Expand All @@ -259,13 +258,12 @@ public function testGetMaxPageSizeExceededErrorObject_HappyPath() {
$this->assertArrayHasKey('meta', $array);
$this->assertArrayHasKey('page', $array['meta']);
$this->assertArrayHasKey('maxSize', $array['meta']['page']);
$this->assertCount(1, $array['links']['type']);
$this->assertSame('400', $array['status']);
$this->assertSame('Max page size exceeded', $array['code']);
$this->assertSame($genericTitle, $array['title']);
$this->assertSame($specificDetails, $array['detail']);
$this->assertSame('page[size]', $array['source']['parameter']);
$this->assertSame('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/max-size-exceeded', $array['links']['type'][0]);
$this->assertSame('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/max-size-exceeded', $array['links']['type']);
$this->assertSame(42, $array['meta']['page']['maxSize']);
}

Expand All @@ -288,13 +286,12 @@ public function testGetInvalidParameterValueErrorObject_HappyPath() {
$this->assertArrayHasKey('type', $array['links']);
$this->assertArrayHasKey('source', $array);
$this->assertArrayHasKey('parameter', $array['source']);
$this->assertCount(1, $array['links']['type']);
$this->assertSame('400', $array['status']);
$this->assertSame('Invalid parameter value', $array['code']);
$this->assertSame($genericTitle, $array['title']);
$this->assertSame($specificDetails, $array['detail']);
$this->assertSame('page[size]', $array['source']['parameter']);
$this->assertSame('https://jsonapi.org', $array['links']['type'][0]);
$this->assertSame('https://jsonapi.org', $array['links']['type']);
}

public function testGetRangePaginationNotSupportedErrorObject_HappyPath() {
Expand All @@ -312,12 +309,11 @@ public function testGetRangePaginationNotSupportedErrorObject_HappyPath() {
$this->assertArrayHasKey('detail', $array);
$this->assertArrayHasKey('links', $array);
$this->assertArrayHasKey('type', $array['links']);
$this->assertCount(1, $array['links']['type']);
$this->assertSame('400', $array['status']);
$this->assertSame('Range pagination not supported', $array['code']);
$this->assertSame($genericTitle, $array['title']);
$this->assertSame($specificDetails, $array['detail']);
$this->assertSame('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/range-pagination-not-supported', $array['links']['type'][0]);
$this->assertSame('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/range-pagination-not-supported', $array['links']['type']);
}

public function testSetQueryParameter_HappyPath() {
Expand Down