From c01a21ea7f4a2d9de43bf1426f4b024f6d973a76 Mon Sep 17 00:00:00 2001 From: Lode Claassen Date: Sun, 16 May 2021 14:54:59 +0200 Subject: [PATCH 1/5] mark all methods using link arrays as deprecated --- src/helpers/LinksManager.php | 6 ++++++ src/objects/ErrorObject.php | 12 ++++++++++++ src/objects/LinksObject.php | 6 ++++++ tests/helpers/LinksManagerTest.php | 6 ++++++ tests/objects/LinksArrayTest.php | 3 +++ tests/objects/LinksObjectTest.php | 6 ++++++ 6 files changed, 39 insertions(+) diff --git a/src/helpers/LinksManager.php b/src/helpers/LinksManager.php index 97c1c7f..37bd14c 100644 --- a/src/helpers/LinksManager.php +++ b/src/helpers/LinksManager.php @@ -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 @@ -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 */ @@ -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 */ diff --git a/src/objects/ErrorObject.php b/src/objects/ErrorObject.php index 9f0eade..25a5c0c 100644 --- a/src/objects/ErrorObject.php +++ b/src/objects/ErrorObject.php @@ -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 */ diff --git a/src/objects/LinksObject.php b/src/objects/LinksObject.php index 67e9640..0d9c659 100644 --- a/src/objects/LinksObject.php +++ b/src/objects/LinksObject.php @@ -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 @@ -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 * @@ -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 * diff --git a/tests/helpers/LinksManagerTest.php b/tests/helpers/LinksManagerTest.php index 980f253..14bf66b 100644 --- a/tests/helpers/LinksManagerTest.php +++ b/tests/helpers/LinksManagerTest.php @@ -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'); @@ -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'])); diff --git a/tests/objects/LinksArrayTest.php b/tests/objects/LinksArrayTest.php index 5b9945c..f0d0c7d 100644 --- a/tests/objects/LinksArrayTest.php +++ b/tests/objects/LinksArrayTest.php @@ -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(); diff --git a/tests/objects/LinksObjectTest.php b/tests/objects/LinksObjectTest.php index ac7b166..ac2995f 100644 --- a/tests/objects/LinksObjectTest.php +++ b/tests/objects/LinksObjectTest.php @@ -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'])); @@ -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'); From c168e95aaa1f8c8efcd3568bf49fae1119ec5882 Mon Sep 17 00:00:00 2001 From: Lode Claassen Date: Sun, 16 May 2021 14:55:32 +0200 Subject: [PATCH 2/5] adjust tests --- examples/errors_all_options.php | 2 +- src/objects/ErrorObject.php | 2 +- src/profiles/CursorPaginationProfile.php | 8 ++++---- .../errors_all_options/errors_all_options.json | 16 ++++++---------- .../errors_all_options/errors_all_options.php | 2 +- tests/profiles/CursorPaginationProfileTest.php | 12 ++++-------- 6 files changed, 17 insertions(+), 25 deletions(-) diff --git a/examples/errors_all_options.php b/examples/errors_all_options.php index 5be9ab7..4f628cc 100644 --- a/examples/errors_all_options.php +++ b/examples/errors_all_options.php @@ -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 diff --git a/src/objects/ErrorObject.php b/src/objects/ErrorObject.php index 25a5c0c..e0c6646 100644 --- a/src/objects/ErrorObject.php +++ b/src/objects/ErrorObject.php @@ -138,7 +138,7 @@ public function setHumanExplanation($genericTitle, $specificDetails=null, $speci $this->setAboutLink($specificAboutLink); } if ($genericTypeLink !== null) { - $this->appendTypeLink($genericTypeLink); + $this->setTypeLink($genericTypeLink); } } diff --git a/src/profiles/CursorPaginationProfile.php b/src/profiles/CursorPaginationProfile.php index 77d9fc2..e95f523 100644 --- a/src/profiles/CursorPaginationProfile.php +++ b/src/profiles/CursorPaginationProfile.php @@ -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); @@ -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]); @@ -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) { @@ -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) { diff --git a/tests/example_output/errors_all_options/errors_all_options.json b/tests/example_output/errors_all_options/errors_all_options.json index b0cd4a3..11c8e33 100644 --- a/tests/example_output/errors_all_options/errors_all_options.json +++ b/tests/example_output/errors_all_options/errors_all_options.json @@ -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" } }, { @@ -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", diff --git a/tests/example_output/errors_all_options/errors_all_options.php b/tests/example_output/errors_all_options/errors_all_options.php index 7ca6e96..f0a4076 100644 --- a/tests/example_output/errors_all_options/errors_all_options.php +++ b/tests/example_output/errors_all_options/errors_all_options.php @@ -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'; diff --git a/tests/profiles/CursorPaginationProfileTest.php b/tests/profiles/CursorPaginationProfileTest.php index 4fb3db8..97704d3 100644 --- a/tests/profiles/CursorPaginationProfileTest.php +++ b/tests/profiles/CursorPaginationProfileTest.php @@ -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']); } @@ -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']); } @@ -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() { @@ -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() { From 96471988370c3c37378058b28e53efba5dbd18e4 Mon Sep 17 00:00:00 2001 From: Lode Claassen Date: Sun, 16 May 2021 14:55:40 +0200 Subject: [PATCH 3/5] keep code coverage for the deprecated method --- tests/objects/ErrorObjectTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/objects/ErrorObjectTest.php b/tests/objects/ErrorObjectTest.php index 7046e31..98a3125 100644 --- a/tests/objects/ErrorObjectTest.php +++ b/tests/objects/ErrorObjectTest.php @@ -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()); From 11a2fc74934983932cc695fefb1a01d4fa21a4b2 Mon Sep 17 00:00:00 2001 From: Lode Claassen Date: Sun, 16 May 2021 15:49:30 +0200 Subject: [PATCH 4/5] CS --- src/helpers/LinksManager.php | 6 +++--- src/objects/ErrorObject.php | 2 +- src/objects/LinksObject.php | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/helpers/LinksManager.php b/src/helpers/LinksManager.php index 37bd14c..ab234a4 100644 --- a/src/helpers/LinksManager.php +++ b/src/helpers/LinksManager.php @@ -29,7 +29,7 @@ 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()} + * @deprecated array links are not supported anymore {@see ->addLink()} * * @param string $key * @param string $href @@ -58,7 +58,7 @@ public function addLinkObject($key, LinkObject $linkObject) { /** * set a key containing a LinksArray * - * @deprecated array links are not supported anymore {@see addLinkObject()} + * @deprecated array links are not supported anymore {@see ->addLinkObject()} * * @param string $key * @param LinksArray $linksArray @@ -71,7 +71,7 @@ public function addLinksArray($key, LinksArray $linksArray) { /** * append a LinkObject to a key with a LinksArray * - * @deprecated array links are not supported anymore {@see addLinkObject()} + * @deprecated array links are not supported anymore {@see ->addLinkObject()} * * @param string $key * @param LinkObject $linkObject diff --git a/src/objects/ErrorObject.php b/src/objects/ErrorObject.php index e0c6646..3d40d78 100644 --- a/src/objects/ErrorObject.php +++ b/src/objects/ErrorObject.php @@ -165,7 +165,7 @@ public function setTypeLink($href, array $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()} + * @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 diff --git a/src/objects/LinksObject.php b/src/objects/LinksObject.php index 0d9c659..1e759ee 100644 --- a/src/objects/LinksObject.php +++ b/src/objects/LinksObject.php @@ -64,7 +64,7 @@ public function add($key, $href, array $meta=[]) { * * @see LinksArray for use cases * - * @deprecated array links are not supported anymore {@see add()} + * @deprecated array links are not supported anymore {@see ->add()} * * @param string $key * @param string $href @@ -122,7 +122,7 @@ public function addLinkObject($key, LinkObject $linkObject) { } /** - * @deprecated array links are not supported anymore {@see addLinkObject()} + * @deprecated array links are not supported anymore {@see ->addLinkObject()} * * @param string $key * @param LinksArray $linksArray @@ -140,7 +140,7 @@ public function addLinksArray($key, LinksArray $linksArray) { } /** - * @deprecated array links are not supported anymore {@see addLinkObject()} + * @deprecated array links are not supported anymore {@see ->addLinkObject()} * * @param string $key * @param LinkObject $linkObject From 9c64d6c3d23e56e6fd4a15fd1ebc1b555febcb01 Mon Sep 17 00:00:00 2001 From: Lode Claassen Date: Sun, 16 May 2021 15:53:43 +0200 Subject: [PATCH 5/5] test