From cb5fabd14d61ca091d6fd9a3e675d3c69b5706bc Mon Sep 17 00:00:00 2001 From: Lode Claassen Date: Tue, 13 Aug 2019 22:56:07 +0200 Subject: [PATCH 1/2] aliases are removed from the 1.1 spec --- examples/bootstrap_examples.php | 15 ++-- examples/example_profile.php | 4 +- src/Document.php | 10 +-- src/helpers/Converter.php | 4 +- src/helpers/ProfileAliasManager.php | 80 ----------------- src/interfaces/ProfileInterface.php | 52 ----------- src/objects/ProfileLinkObject.php | 47 ---------- src/profiles/CursorPaginationProfile.php | 30 ++++--- tests/ConverterTest.php | 14 +-- tests/DocumentTest.php | 30 +------ tests/SeparateProcessTest.php | 5 +- .../example_output/ExampleVersionProfile.php | 17 +--- .../example_profile/example_profile.json | 9 +- .../example_profile/example_profile.php | 2 +- tests/helpers/ProfileAliasManagerTest.php | 87 ------------------- ...TestableNonAbstractProfileAliasManager.php | 39 --------- .../profiles/CursorPaginationProfileTest.php | 84 +++++++++--------- tests/profiles/TestProfile.php | 14 ++- 18 files changed, 85 insertions(+), 458 deletions(-) delete mode 100644 src/helpers/ProfileAliasManager.php delete mode 100644 src/objects/ProfileLinkObject.php delete mode 100644 tests/helpers/ProfileAliasManagerTest.php delete mode 100644 tests/helpers/TestableNonAbstractProfileAliasManager.php diff --git a/examples/bootstrap_examples.php b/examples/bootstrap_examples.php index 28329701..40bc6cd8 100644 --- a/examples/bootstrap_examples.php +++ b/examples/bootstrap_examples.php @@ -2,7 +2,6 @@ use alsvanzelf\jsonapi\Document; use alsvanzelf\jsonapi\ResourceDocument; -use alsvanzelf\jsonapi\helpers\ProfileAliasManager; use alsvanzelf\jsonapi\interfaces\ProfileInterface; use alsvanzelf\jsonapi\interfaces\ResourceInterface; @@ -102,17 +101,13 @@ function getCurrentLocation() { } } -class ExampleVersionProfile extends ProfileAliasManager implements ProfileInterface { +class ExampleVersionProfile implements ProfileInterface { /** - * the required methods (next to extending ProfileAliasManager) + * the required method */ public function getOfficialLink() { - return 'https://jsonapi.org/format/1.1/#profile-keywords-and-aliases'; - } - - public function getOfficialKeywords() { - return ['version']; + return 'https://jsonapi.org/format/1.1/#profile-keywords'; } /** @@ -121,10 +116,10 @@ public function getOfficialKeywords() { public function setVersion(ResourceInterface $resource, $version) { if ($resource instanceof ResourceDocument) { - $resource->addMeta($this->getKeyword('version'), $version, $level=Document::LEVEL_RESOURCE); + $resource->addMeta('version', $version, $level=Document::LEVEL_RESOURCE); } else { - $resource->addMeta($this->getKeyword('version'), $version); + $resource->addMeta('version', $version); } } } diff --git a/examples/example_profile.php b/examples/example_profile.php index 66cef955..ac12aa1b 100644 --- a/examples/example_profile.php +++ b/examples/example_profile.php @@ -6,11 +6,9 @@ /** * use a profile as extension to the document - * - * allowing to define aliases for the keywords to solve conflicts between different profiles */ -$profile = new ExampleVersionProfile(['version' => 'ref']); +$profile = new ExampleVersionProfile(); $document = new ResourceDocument('user', 42); $document->applyProfile($profile); diff --git a/src/Document.php b/src/Document.php index 2967cd2c..836990f1 100644 --- a/src/Document.php +++ b/src/Document.php @@ -12,10 +12,8 @@ use alsvanzelf\jsonapi\interfaces\DocumentInterface; use alsvanzelf\jsonapi\interfaces\ProfileInterface; use alsvanzelf\jsonapi\objects\JsonapiObject; -use alsvanzelf\jsonapi\objects\LinkObject; use alsvanzelf\jsonapi\objects\LinksObject; use alsvanzelf\jsonapi\objects\MetaObject; -use alsvanzelf\jsonapi\objects\ProfileLinkObject; /** * @see ResourceDocument, CollectionDocument, ErrorsDocument or MetaDocument @@ -196,13 +194,7 @@ public function applyProfile(ProfileInterface $profile) { $this->setLinksObject(new LinksObject()); } - $link = $profile->getAliasedLink(); - if ($link instanceof LinkObject) { - $this->links->appendLinkObject('profile', $link); - } - else { - $this->links->append('profile', $link); - } + $this->links->append('profile', $profile->getOfficialLink()); } /** diff --git a/src/helpers/Converter.php b/src/helpers/Converter.php index aa66da95..0d16caf1 100644 --- a/src/helpers/Converter.php +++ b/src/helpers/Converter.php @@ -4,7 +4,6 @@ use alsvanzelf\jsonapi\interfaces\ObjectInterface; use alsvanzelf\jsonapi\interfaces\ProfileInterface; -use alsvanzelf\jsonapi\objects\LinkObject; /** * @internal @@ -48,8 +47,7 @@ public static function mergeProfilesInContentType($contentType, array $profiles) $profileLinks = []; foreach ($profiles as $profile) { - $link = $profile->getAliasedLink(); - $profileLinks[] = ($link instanceof LinkObject) ? $link->toArray()['href'] : $link; + $profileLinks[] = $profile->getOfficialLink(); } $profileLinks = implode(' ', $profileLinks); diff --git a/src/helpers/ProfileAliasManager.php b/src/helpers/ProfileAliasManager.php deleted file mode 100644 index 1ef00759..00000000 --- a/src/helpers/ProfileAliasManager.php +++ /dev/null @@ -1,80 +0,0 @@ -getOfficialKeywords(); - if ($officialKeywords === []) { - return; - } - - $this->keywordMapping = array_combine($officialKeywords, $officialKeywords); - if ($aliases === []) { - return; - } - - foreach ($aliases as $keyword => $alias) { - if ($alias === $keyword) { - throw new InputException('an alias should be different from its keyword'); - } - if (in_array($keyword, $officialKeywords, $strict=true) === false) { - throw new InputException('unknown keyword "'.$keyword.'" to alias'); - } - Validator::checkMemberName($alias); - - $this->keywordMapping[$keyword] = $alias; - } - - $this->aliasMapping = $aliases; - } - - /** - * @inheritDoc - */ - public function getKeyword($keyword) { - if (isset($this->keywordMapping[$keyword]) === false) { - throw new InputException('unknown keyword "'.$keyword.'"'); - } - - return $this->keywordMapping[$keyword]; - } - - /** - * @inheritDoc - */ - abstract public function getOfficialKeywords(); - - /** - * @inheritDoc - */ - abstract public function getOfficialLink(); - - /** - * @inheritDoc - */ - public function getAliasedLink() { - if ($this->aliasMapping === []) { - return $this->getOfficialLink(); - } - - return new ProfileLinkObject($this->getOfficialLink(), $this->aliasMapping); - } -} diff --git a/src/interfaces/ProfileInterface.php b/src/interfaces/ProfileInterface.php index 3e6df08b..b4a6f2f5 100644 --- a/src/interfaces/ProfileInterface.php +++ b/src/interfaces/ProfileInterface.php @@ -2,48 +2,7 @@ namespace alsvanzelf\jsonapi\interfaces; -use alsvanzelf\jsonapi\exceptions\InputException; -use alsvanzelf\jsonapi\objects\LinkObject; - -/** - * @see ProfileAliasManager which implement most of the methods - */ interface ProfileInterface { - /** - * get a profile with its aliases to keywords of the profile - * - * having this in the constructor makes sure the aliases are used from the start - * - * @param array $aliases optional mapping keywords to aliases - * - * @throws InputException if the alias is not different from the keyword - * @throws InputException if the keyword is not known to the profile - * @throws InputException if the alias is not a valid member name - */ - public function __construct(array $aliases=[]); - - /** - * get the keyword or current alias based on the official keyword from the profile - * - * e.g. for a profile defining an official keyword 'version', this would return 'version' - * or if ->alias('version', 'v') was called before, this would return 'v' - * - * @param string $keyword - * @return string - * - * @throws InputException if the keyword is not known to the profile - */ - public function getKeyword($keyword); - - /** - * returns an array of official keywords this profile defines - * - * @internal - * - * @return string[] - */ - public function getOfficialKeywords(); - /** * the unique link identifying and describing the profile * @@ -52,15 +11,4 @@ public function getOfficialKeywords(); * @return string */ public function getOfficialLink(); - - /** - * get the official link, or a LinkObject with the link and its aliases - * - * optionally also contains the aliases applied - * - * @internal - * - * @return LinkObject|string - */ - public function getAliasedLink(); } diff --git a/src/objects/ProfileLinkObject.php b/src/objects/ProfileLinkObject.php deleted file mode 100644 index 4652a533..00000000 --- a/src/objects/ProfileLinkObject.php +++ /dev/null @@ -1,47 +0,0 @@ -aliases = $aliases; - } - - /** - * human api - */ - - /** - * spec api - */ - - /** - * ObjectInterface - */ - - /** - * @inheritDoc - */ - public function toArray() { - $array = parent::toArray(); - - if ($this->aliases !== []) { - $array['aliases'] = $this->aliases; - } - - return $array; - } -} diff --git a/src/profiles/CursorPaginationProfile.php b/src/profiles/CursorPaginationProfile.php index 1700f927..77d9fc29 100644 --- a/src/profiles/CursorPaginationProfile.php +++ b/src/profiles/CursorPaginationProfile.php @@ -4,7 +4,6 @@ use alsvanzelf\jsonapi\Document; use alsvanzelf\jsonapi\ResourceDocument; -use alsvanzelf\jsonapi\helpers\ProfileAliasManager; use alsvanzelf\jsonapi\interfaces\PaginableInterface; use alsvanzelf\jsonapi\interfaces\ProfileInterface; use alsvanzelf\jsonapi\interfaces\ResourceInterface; @@ -44,7 +43,7 @@ * - {@see get*ErrorObject} to generate ErrorObjects for specific error cases * - {@see generatePreviousLink} {@see generateNextLink} to apply the links manually */ -class CursorPaginationProfile extends ProfileAliasManager implements ProfileInterface { +class CursorPaginationProfile implements ProfileInterface { /** * human api */ @@ -115,7 +114,7 @@ public function setCount(PaginableInterface $paginable, $exactTotal=null, $bestG * @return string */ public function generatePreviousLink($baseOrCurrentUrl, $beforeCursor) { - return $this->setQueryParameter($baseOrCurrentUrl, $this->getKeyword('page').'[before]', $beforeCursor); + return $this->setQueryParameter($baseOrCurrentUrl, 'page[before]', $beforeCursor); } /** @@ -126,7 +125,7 @@ public function generatePreviousLink($baseOrCurrentUrl, $beforeCursor) { * @return string */ public function generateNextLink($baseOrCurrentUrl, $afterCursor) { - return $this->setQueryParameter($baseOrCurrentUrl, $this->getKeyword('page').'[after]', $afterCursor); + return $this->setQueryParameter($baseOrCurrentUrl, 'page[after]', $afterCursor); } /** @@ -192,10 +191,10 @@ public function setItemMeta(ResourceInterface $resource, $cursor) { ]; if ($resource instanceof ResourceDocument) { - $resource->addMeta($this->getKeyword('page'), $metadata, $level=Document::LEVEL_RESOURCE); + $resource->addMeta('page', $metadata, $level=Document::LEVEL_RESOURCE); } else { - $resource->addMeta($this->getKeyword('page'), $metadata); + $resource->addMeta('page', $metadata); } } @@ -229,7 +228,7 @@ public function setPaginationMeta(PaginableInterface $paginable, $exactTotal=nul $metadata['rangeTruncated'] = $rangeIsTruncated; } - $paginable->addMeta($this->getKeyword('page'), $metadata); + $paginable->addMeta('page', $metadata); } /** @@ -280,9 +279,9 @@ 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->blameQueryParameter($this->getKeyword('page').'[size]'); + $errorObject->blameQueryParameter('page[size]'); $errorObject->setHttpStatusCode(400); - $errorObject->addMeta($this->getKeyword('page'), $value=['maxSize' => $maxSize]); + $errorObject->addMeta('page', $value=['maxSize' => $maxSize]); if ($genericTitle !== null) { $errorObject->setHumanExplanation($genericTitle, $specificDetails); @@ -302,7 +301,7 @@ public function getMaxPageSizeExceededErrorObject($maxSize, $genericTitle=null, * - /errors/0/title optional * - /errors/0/detail optional * - * @param int $queryParameter e.g. 'sort' or 'page[size]', aliasing should already be done using {@see getKeyword} + * @param int $queryParameter e.g. 'sort' or 'page[size]' * @param string $typeLink optional * @param string $genericTitle optional, e.g. 'Invalid Parameter.' * @param string $specificDetails optional, e.g. 'page[size] must be a positive integer; got 0' @@ -394,9 +393,14 @@ public function getOfficialLink() { } /** - * @inheritDoc + * returns the keyword without aliasing + * + * @deprecated since aliasing was removed from the profiles spec + * + * @param string $keyword + * @return string */ - public function getOfficialKeywords() { - return ['page']; + public function getKeyword($keyword) { + return $keyword; } } diff --git a/tests/ConverterTest.php b/tests/ConverterTest.php index ad334782..85839c9f 100644 --- a/tests/ConverterTest.php +++ b/tests/ConverterTest.php @@ -4,7 +4,6 @@ use alsvanzelf\jsonapi\helpers\Converter; use alsvanzelf\jsonapi\objects\AttributesObject; -use alsvanzelf\jsonapi\objects\LinkObject; use alsvanzelf\jsonapiTests\profiles\TestProfile; use PHPUnit\Framework\TestCase; @@ -70,24 +69,17 @@ public function testMergeProfilesInContentType_HappyPath() { public function testMergeProfilesInContentType_WithProfileStringLink() { $profile = new TestProfile(); - $profile->setAliasedLink('bar'); - - $this->assertSame('foo;profile="bar", foo', Converter::mergeProfilesInContentType('foo', [$profile])); - } - - public function testMergeProfilesInContentType_WithProfileObjectLink() { - $profile = new TestProfile(); - $profile->setAliasedLink(new LinkObject('bar')); + $profile->setOfficialLink('bar'); $this->assertSame('foo;profile="bar", foo', Converter::mergeProfilesInContentType('foo', [$profile])); } public function testMergeProfilesInContentType_WithMultipleProfiles() { $profile1 = new TestProfile(); - $profile1->setAliasedLink('bar'); + $profile1->setOfficialLink('bar'); $profile2 = new TestProfile(); - $profile2->setAliasedLink(new LinkObject('baz')); + $profile2->setOfficialLink('baz'); $this->assertSame('foo;profile="bar baz", foo', Converter::mergeProfilesInContentType('foo', [$profile1, $profile2])); } diff --git a/tests/DocumentTest.php b/tests/DocumentTest.php index 2cd0916c..61e99368 100644 --- a/tests/DocumentTest.php +++ b/tests/DocumentTest.php @@ -5,7 +5,6 @@ use alsvanzelf\jsonapi\exceptions\Exception; use alsvanzelf\jsonapi\exceptions\InputException; use alsvanzelf\jsonapi\objects\LinkObject; -use alsvanzelf\jsonapi\objects\ProfileLinkObject; use alsvanzelf\jsonapiTests\TestableNonAbstractDocument as Document; use alsvanzelf\jsonapiTests\profiles\TestProfile; use PHPUnit\Framework\TestCase; @@ -180,7 +179,7 @@ public function testAddLinkObject_HappyPath() { public function testApplyProfile_HappyPath() { $profile = new TestProfile(); - $profile->setAliasedLink('https://jsonapi.org'); + $profile->setOfficialLink('https://jsonapi.org'); $document = new Document(); $document->applyProfile($profile); @@ -195,33 +194,6 @@ public function testApplyProfile_HappyPath() { $this->assertSame('https://jsonapi.org', $array['links']['profile'][0]); } - public function testApplyProfile_WithLinkObject() { - $profile = new TestProfile(); - $profile->setAliasedLink(new ProfileLinkObject('https://jsonapi.org', $aliases=['foo' => 'bar'], $meta=['baz' => 'baf'])); - - $document = new Document(); - $document->applyProfile($profile); - - $array = $document->toArray(); - - $this->assertArrayHasKey('links', $array); - $this->assertCount(1, $array['links']); - $this->assertArrayHasKey('profile', $array['links']); - $this->assertCount(1, $array['links']['profile']); - $this->assertArrayHasKey(0, $array['links']['profile']); - $this->assertCount(3, $array['links']['profile'][0]); - $this->assertArrayHasKey('href', $array['links']['profile'][0]); - $this->assertArrayHasKey('aliases', $array['links']['profile'][0]); - $this->assertArrayHasKey('meta', $array['links']['profile'][0]); - $this->assertSame('https://jsonapi.org', $array['links']['profile'][0]['href']); - $this->assertCount(1, $array['links']['profile'][0]['aliases']); - $this->assertArrayHasKey('foo', $array['links']['profile'][0]['aliases']); - $this->assertSame('bar', $array['links']['profile'][0]['aliases']['foo']); - $this->assertCount(1, $array['links']['profile'][0]['meta']); - $this->assertArrayHasKey('baz', $array['links']['profile'][0]['meta']); - $this->assertSame('baf', $array['links']['profile'][0]['meta']['baz']); - } - public function testToJson_HappyPath() { $document = new Document(); diff --git a/tests/SeparateProcessTest.php b/tests/SeparateProcessTest.php index dc8c9e1d..4c7938f0 100644 --- a/tests/SeparateProcessTest.php +++ b/tests/SeparateProcessTest.php @@ -2,7 +2,6 @@ namespace alsvanzelf\jsonapiTests; -use alsvanzelf\jsonapi\objects\ProfileLinkObject; use alsvanzelf\jsonapiTests\TestableNonAbstractDocument as Document; use alsvanzelf\jsonapiTests\profiles\TestProfile; use PHPUnit\Framework\TestCase; @@ -74,7 +73,7 @@ public function testSendResponse_ContentTypeHeader() { */ public function testSendResponse_ContentTypeHeaderWithProfiles() { $profile = new TestProfile(); - $profile->setAliasedLink('https://jsonapi.org'); + $profile->setOfficialLink('https://jsonapi.org'); $document = new Document(); $document->applyProfile($profile); @@ -85,7 +84,7 @@ public function testSendResponse_ContentTypeHeaderWithProfiles() { $this->assertSame(['Content-Type: '.Document::CONTENT_TYPE_OFFICIAL.';profile="https://jsonapi.org", '.Document::CONTENT_TYPE_OFFICIAL], xdebug_get_headers()); $profile = new TestProfile(); - $profile->setAliasedLink('https://jsonapi.org/2'); + $profile->setOfficialLink('https://jsonapi.org/2'); $document->applyProfile($profile); ob_start(); diff --git a/tests/example_output/ExampleVersionProfile.php b/tests/example_output/ExampleVersionProfile.php index 824a2e62..266be3f2 100644 --- a/tests/example_output/ExampleVersionProfile.php +++ b/tests/example_output/ExampleVersionProfile.php @@ -4,21 +4,12 @@ use alsvanzelf\jsonapi\Document; use alsvanzelf\jsonapi\ResourceDocument; -use alsvanzelf\jsonapi\helpers\ProfileAliasManager; use alsvanzelf\jsonapi\interfaces\ProfileInterface; use alsvanzelf\jsonapi\interfaces\ResourceInterface; -class ExampleVersionProfile extends ProfileAliasManager implements ProfileInterface { - /** - * the required methods (next to extending ProfileAliasManager) - */ - +class ExampleVersionProfile implements ProfileInterface { public function getOfficialLink() { - return 'https://jsonapi.org/format/1.1/#profile-keywords-and-aliases'; - } - - public function getOfficialKeywords() { - return ['version']; + return 'https://jsonapi.org/format/1.1/#profile-keywords'; } /** @@ -27,10 +18,10 @@ public function getOfficialKeywords() { public function setVersion(ResourceInterface $resource, $version) { if ($resource instanceof ResourceDocument) { - $resource->addMeta($this->getKeyword('version'), $version, $level=Document::LEVEL_RESOURCE); + $resource->addMeta('version', $version, $level=Document::LEVEL_RESOURCE); } else { - $resource->addMeta($this->getKeyword('version'), $version); + $resource->addMeta('version', $version); } } } diff --git a/tests/example_output/example_profile/example_profile.json b/tests/example_output/example_profile/example_profile.json index e7ee9ffd..53daa9f0 100644 --- a/tests/example_output/example_profile/example_profile.json +++ b/tests/example_output/example_profile/example_profile.json @@ -4,19 +4,14 @@ }, "links": { "profile": [ - { - "href": "https://jsonapi.org/format/1.1/#profile-keywords-and-aliases", - "aliases": { - "version": "ref" - } - } + "https://jsonapi.org/format/1.1/#profile-keywords" ] }, "data": { "type": "user", "id": "42", "meta": { - "ref": "2019" + "version": "2019" } } } diff --git a/tests/example_output/example_profile/example_profile.php b/tests/example_output/example_profile/example_profile.php index c881f259..65b3bb42 100644 --- a/tests/example_output/example_profile/example_profile.php +++ b/tests/example_output/example_profile/example_profile.php @@ -7,7 +7,7 @@ class example_profile { public static function createJsonapiDocument() { - $profile = new ExampleVersionProfile(['version' => 'ref']); + $profile = new ExampleVersionProfile(); $document = new ResourceDocument('user', 42); $document->applyProfile($profile); diff --git a/tests/helpers/ProfileAliasManagerTest.php b/tests/helpers/ProfileAliasManagerTest.php deleted file mode 100644 index c1a1fed3..00000000 --- a/tests/helpers/ProfileAliasManagerTest.php +++ /dev/null @@ -1,87 +0,0 @@ -assertSame([], $profileAliasManager->getAliasMapping()); - $this->assertSame(['foo' => 'foo', 'bar' => 'bar'], $profileAliasManager->getKeywordMapping()); - } - - public function testConstructor_WithAliases() { - $profileAliasManager = new ProfileAliasManager(['bar' => 'baz']); - - $this->assertSame(['bar' => 'baz'], $profileAliasManager->getAliasMapping()); - $this->assertSame(['foo' => 'foo', 'bar' => 'baz'], $profileAliasManager->getKeywordMapping()); - } - - public function testConstructor_WithoutOfficialKeywords() { - $profileAliasManager = new ProfileAliasManager_WithoutKeywords(); - - $this->assertSame([], $profileAliasManager->getAliasMapping()); - $this->assertSame([], $profileAliasManager->getKeywordMapping()); - } - - public function testConstructor_NonAdjustedAliases() { - $this->expectException(InputException::class); - - new ProfileAliasManager(['foo' => 'foo']); - } - - public function testConstructor_NonExistingKeyword() { - $this->expectException(InputException::class); - - new ProfileAliasManager(['baz' => 'bar']); - } - - public function testGetKeyword_HappyPath() { - $profileAliasManager = new ProfileAliasManager(['bar' => 'baz']); - - $this->assertSame('foo', $profileAliasManager->getKeyword('foo')); - $this->assertSame('baz', $profileAliasManager->getKeyword('bar')); - } - - public function testGetKeyword_NonExistingKeyword() { - $profileAliasManager = new ProfileAliasManager(); - - $this->expectException(InputException::class); - - $profileAliasManager->getKeyword('baz'); - } - - public function testGetAliasedLink_HappyPath() { - $profileAliasManager = new ProfileAliasManager(); - - if (method_exists($this, 'assertIsString')) { - $this->assertIsString($profileAliasManager->getAliasedLink()); - } - else { - $this->assertInternalType('string', $profileAliasManager->getAliasedLink()); - } - $this->assertSame('https://jsonapi.org', $profileAliasManager->getAliasedLink()); - } - - public function testGetAliasedLink_ObjectWithAliases() { - $profileAliasManager = new ProfileAliasManager(['bar' => 'baz']); - - $this->assertInstanceOf(ProfileLinkObject::class, $profileAliasManager->getAliasedLink()); - - $array = $profileAliasManager->getAliasedLink()->toArray(); - - $this->assertArrayHasKey('href', $array); - $this->assertSame('https://jsonapi.org', $array['href']); - - $this->assertArrayHasKey('aliases', $array); - $this->assertCount(1, $array['aliases']); - $this->assertArrayHasKey('bar', $array['aliases']); - $this->assertSame('baz', $array['aliases']['bar']); - } -} diff --git a/tests/helpers/TestableNonAbstractProfileAliasManager.php b/tests/helpers/TestableNonAbstractProfileAliasManager.php deleted file mode 100644 index e73d7ddb..00000000 --- a/tests/helpers/TestableNonAbstractProfileAliasManager.php +++ /dev/null @@ -1,39 +0,0 @@ -setAccessible(true); - - return $aliasMapping->getValue($this); - } - - public function getKeywordMapping() { - $keywordMapping = new \ReflectionProperty(ProfileAliasManager::class, 'keywordMapping'); - $keywordMapping->setAccessible(true); - - return $keywordMapping->getValue($this); - } - - public function getOfficialKeywords() { - return ['foo', 'bar']; - } - - public function getOfficialLink() { - return 'https://jsonapi.org'; - } -} - -class TestableNonAbstractProfileAliasManager_WithoutKeywords extends TestableNonAbstractProfileAliasManager { - public function getOfficialKeywords() { - return []; - } -} diff --git a/tests/profiles/CursorPaginationProfileTest.php b/tests/profiles/CursorPaginationProfileTest.php index 183df747..533f43a2 100644 --- a/tests/profiles/CursorPaginationProfileTest.php +++ b/tests/profiles/CursorPaginationProfileTest.php @@ -14,9 +14,9 @@ */ class CursorPaginationProfileTest extends TestCase { public function testSetLinks_HappyPath() { - $profile = new CursorPaginationProfile(['page' => 'pagination']); + $profile = new CursorPaginationProfile(); $collection = new CollectionDocument(); - $baseOrCurrentUrl = '/people?'.$profile->getKeyword('page').'[size]=10'; + $baseOrCurrentUrl = '/people?page[size]=10'; $firstCursor = 'bar'; $lastCursor = 'foo'; @@ -30,12 +30,12 @@ public function testSetLinks_HappyPath() { $this->assertArrayHasKey('next', $array['links']); $this->assertArrayHasKey('href', $array['links']['prev']); $this->assertArrayHasKey('href', $array['links']['next']); - $this->assertSame('/people?'.$profile->getKeyword('page').'[size]=10&'.$profile->getKeyword('page').'[before]='.$firstCursor, $array['links']['prev']['href']); - $this->assertSame('/people?'.$profile->getKeyword('page').'[size]=10&'.$profile->getKeyword('page').'[after]='.$lastCursor, $array['links']['next']['href']); + $this->assertSame('/people?page[size]=10&page[before]='.$firstCursor, $array['links']['prev']['href']); + $this->assertSame('/people?page[size]=10&page[after]='.$lastCursor, $array['links']['next']['href']); } public function test_WithRelationship() { - $profile = new CursorPaginationProfile(['page' => 'pagination']); + $profile = new CursorPaginationProfile(); $document = new ResourceDocument('test', 1); $person1 = new ResourceObject('person', 1); @@ -45,7 +45,7 @@ public function test_WithRelationship() { $profile->setCursor($person2, 'arthur'); $profile->setCursor($person42, 'zaphod'); - $baseOrCurrentUrl = '/people?'.$profile->getKeyword('page').'[size]=10'; + $baseOrCurrentUrl = '/people?page[size]=10'; $firstCursor = 'ford'; $lastCursor = 'zaphod'; $exactTotal = 3; @@ -67,22 +67,22 @@ public function test_WithRelationship() { $this->assertArrayHasKey('meta', $array['data']['relationships']['people']); $this->assertArrayHasKey('prev', $array['data']['relationships']['people']['links']); $this->assertArrayHasKey('next', $array['data']['relationships']['people']['links']); - $this->assertArrayHasKey('pagination', $array['data']['relationships']['people']['meta']); + $this->assertArrayHasKey('page', $array['data']['relationships']['people']['meta']); $this->assertArrayHasKey('href', $array['data']['relationships']['people']['links']['prev']); $this->assertArrayHasKey('href', $array['data']['relationships']['people']['links']['next']); - $this->assertArrayHasKey('total', $array['data']['relationships']['people']['meta']['pagination']); - $this->assertArrayHasKey('estimatedTotal', $array['data']['relationships']['people']['meta']['pagination']); - $this->assertArrayHasKey('bestGuess', $array['data']['relationships']['people']['meta']['pagination']['estimatedTotal']); + $this->assertArrayHasKey('total', $array['data']['relationships']['people']['meta']['page']); + $this->assertArrayHasKey('estimatedTotal', $array['data']['relationships']['people']['meta']['page']); + $this->assertArrayHasKey('bestGuess', $array['data']['relationships']['people']['meta']['page']['estimatedTotal']); $this->assertCount(3, $array['data']['relationships']['people']['data']); $this->assertArrayHasKey('meta', $array['data']['relationships']['people']['data'][0]); - $this->assertArrayHasKey('pagination', $array['data']['relationships']['people']['data'][0]['meta']); - $this->assertArrayHasKey('cursor', $array['data']['relationships']['people']['data'][0]['meta']['pagination']); + $this->assertArrayHasKey('page', $array['data']['relationships']['people']['data'][0]['meta']); + $this->assertArrayHasKey('cursor', $array['data']['relationships']['people']['data'][0]['meta']['page']); } public function testSetLinksFirstPage_HappyPath() { - $profile = new CursorPaginationProfile(['page' => 'pagination']); + $profile = new CursorPaginationProfile(); $collection = new CollectionDocument(); - $baseOrCurrentUrl = '/people?'.$profile->getKeyword('page').'[size]=10'; + $baseOrCurrentUrl = '/people?page[size]=10'; $lastCursor = 'foo'; $profile->setLinksFirstPage($collection, $baseOrCurrentUrl, $lastCursor); @@ -95,13 +95,13 @@ public function testSetLinksFirstPage_HappyPath() { $this->assertArrayHasKey('next', $array['links']); $this->assertNull($array['links']['prev']); $this->assertArrayHasKey('href', $array['links']['next']); - $this->assertSame('/people?'.$profile->getKeyword('page').'[size]=10&'.$profile->getKeyword('page').'[after]='.$lastCursor, $array['links']['next']['href']); + $this->assertSame('/people?page[size]=10&page[after]='.$lastCursor, $array['links']['next']['href']); } public function testSetLinksLastPage_HappyPath() { - $profile = new CursorPaginationProfile(['page' => 'pagination']); + $profile = new CursorPaginationProfile(); $collection = new CollectionDocument(); - $baseOrCurrentUrl = '/people?'.$profile->getKeyword('page').'[size]=10'; + $baseOrCurrentUrl = '/people?page[size]=10'; $firstCursor = 'bar'; $profile->setLinksLastPage($collection, $baseOrCurrentUrl, $firstCursor); @@ -114,11 +114,11 @@ public function testSetLinksLastPage_HappyPath() { $this->assertArrayHasKey('next', $array['links']); $this->assertArrayHasKey('href', $array['links']['prev']); $this->assertNull($array['links']['next']); - $this->assertSame('/people?'.$profile->getKeyword('page').'[size]=10&'.$profile->getKeyword('page').'[before]='.$firstCursor, $array['links']['prev']['href']); + $this->assertSame('/people?page[size]=10&page[before]='.$firstCursor, $array['links']['prev']['href']); } public function testSetCursor() { - $profile = new CursorPaginationProfile(['page' => 'pagination']); + $profile = new CursorPaginationProfile(); $resourceDocument = new ResourceDocument('user', 42); $profile->setCursor($resourceDocument, 'foo'); @@ -127,13 +127,13 @@ public function testSetCursor() { $this->assertArrayHasKey('data', $array); $this->assertArrayHasKey('meta', $array['data']); - $this->assertArrayHasKey('pagination', $array['data']['meta']); - $this->assertArrayHasKey('cursor', $array['data']['meta']['pagination']); - $this->assertSame('foo', $array['data']['meta']['pagination']['cursor']); + $this->assertArrayHasKey('page', $array['data']['meta']); + $this->assertArrayHasKey('cursor', $array['data']['meta']['page']); + $this->assertSame('foo', $array['data']['meta']['page']['cursor']); } public function testSetPaginationLinkObjectsExplicitlyEmpty_HapptPath() { - $profile = new CursorPaginationProfile(['page' => 'pagination']); + $profile = new CursorPaginationProfile(); $collection = new CollectionDocument(); $profile->setPaginationLinkObjectsExplicitlyEmpty($collection); @@ -149,7 +149,7 @@ public function testSetPaginationLinkObjectsExplicitlyEmpty_HapptPath() { } public function testSetPaginationMeta() { - $profile = new CursorPaginationProfile(['page' => 'pagination']); + $profile = new CursorPaginationProfile(); $collection = new CollectionDocument(); $exactTotal = 42; $bestGuessTotal = 100; @@ -160,18 +160,18 @@ public function testSetPaginationMeta() { $array = $collection->toArray(); $this->assertArrayHasKey('meta', $array); - $this->assertArrayHasKey('pagination', $array['meta']); - $this->assertArrayHasKey('total', $array['meta']['pagination']); - $this->assertArrayHasKey('estimatedTotal', $array['meta']['pagination']); - $this->assertArrayHasKey('bestGuess', $array['meta']['pagination']['estimatedTotal']); - $this->assertArrayHasKey('rangeTruncated', $array['meta']['pagination']); - $this->assertSame(42, $array['meta']['pagination']['total']); - $this->assertSame(100, $array['meta']['pagination']['estimatedTotal']['bestGuess']); - $this->assertSame(true, $array['meta']['pagination']['rangeTruncated']); + $this->assertArrayHasKey('page', $array['meta']); + $this->assertArrayHasKey('total', $array['meta']['page']); + $this->assertArrayHasKey('estimatedTotal', $array['meta']['page']); + $this->assertArrayHasKey('bestGuess', $array['meta']['page']['estimatedTotal']); + $this->assertArrayHasKey('rangeTruncated', $array['meta']['page']); + $this->assertSame(42, $array['meta']['page']['total']); + $this->assertSame(100, $array['meta']['page']['estimatedTotal']['bestGuess']); + $this->assertSame(true, $array['meta']['page']['rangeTruncated']); } public function testGetUnsupportedSortErrorObject_HappyPath() { - $profile = new CursorPaginationProfile(['page' => 'pagination']); + $profile = new CursorPaginationProfile(); $genericTitle = 'foo'; $specificDetails = 'bar'; @@ -197,7 +197,7 @@ public function testGetUnsupportedSortErrorObject_HappyPath() { } public function testGetMaxPageSizeExceededErrorObject_HappyPath() { - $profile = new CursorPaginationProfile(['page' => 'pagination']); + $profile = new CursorPaginationProfile(); $maxSize = 42; $genericTitle = 'foo'; $specificDetails = 'bar'; @@ -215,21 +215,21 @@ public function testGetMaxPageSizeExceededErrorObject_HappyPath() { $this->assertArrayHasKey('source', $array); $this->assertArrayHasKey('parameter', $array['source']); $this->assertArrayHasKey('meta', $array); - $this->assertArrayHasKey('pagination', $array['meta']); - $this->assertArrayHasKey('maxSize', $array['meta']['pagination']); + $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('pagination[size]', $array['source']['parameter']); + $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(42, $array['meta']['pagination']['maxSize']); + $this->assertSame(42, $array['meta']['page']['maxSize']); } public function testGetInvalidParameterValueErrorObject_HappyPath() { - $profile = new CursorPaginationProfile(['page' => 'pagination']); - $queryParameter = 'pagination[size]'; + $profile = new CursorPaginationProfile(); + $queryParameter = 'page[size]'; $typeLink = 'https://jsonapi.org'; $genericTitle = 'foo'; $specificDetails = 'bar'; @@ -251,12 +251,12 @@ public function testGetInvalidParameterValueErrorObject_HappyPath() { $this->assertSame('Invalid parameter value', $array['code']); $this->assertSame($genericTitle, $array['title']); $this->assertSame($specificDetails, $array['detail']); - $this->assertSame('pagination[size]', $array['source']['parameter']); + $this->assertSame('page[size]', $array['source']['parameter']); $this->assertSame('https://jsonapi.org', $array['links']['type'][0]); } public function testGetRangePaginationNotSupportedErrorObject_HappyPath() { - $profile = new CursorPaginationProfile(['page' => 'pagination']); + $profile = new CursorPaginationProfile(); $genericTitle = 'foo'; $specificDetails = 'bar'; diff --git a/tests/profiles/TestProfile.php b/tests/profiles/TestProfile.php index 91fe1469..331e1a83 100644 --- a/tests/profiles/TestProfile.php +++ b/tests/profiles/TestProfile.php @@ -5,17 +5,13 @@ use alsvanzelf\jsonapi\interfaces\ProfileInterface; class TestProfile implements ProfileInterface { - private $aliasedLink; + private $officialLink; - public function setAliasedLink($aliasedLink) { - $this->aliasedLink = $aliasedLink; + public function setOfficialLink($officialLink) { + $this->officialLink = $officialLink; } - public function __construct(array $aliases=[]) {} - public function getKeyword($keyword) {} - public function getOfficialKeywords() {} - public function getOfficialLink() {} - public function getAliasedLink() { - return $this->aliasedLink; + public function getOfficialLink() { + return $this->officialLink; } } From e7fc8ff4b5288b0785c09ccd89d8a1ff7748e9e9 Mon Sep 17 00:00:00 2001 From: Lode Claassen Date: Mon, 5 Apr 2021 23:22:54 +0200 Subject: [PATCH 2/2] improve test coverage --- tests/profiles/CursorPaginationProfileTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/profiles/CursorPaginationProfileTest.php b/tests/profiles/CursorPaginationProfileTest.php index 533f43a2..cd40ec4f 100644 --- a/tests/profiles/CursorPaginationProfileTest.php +++ b/tests/profiles/CursorPaginationProfileTest.php @@ -305,4 +305,15 @@ public function testSetQueryParameter_EncodedUrl() { $this->assertSame('/people?sort=x&page%5Bsize%5D=10&page%5Bafter%5D=bar', $newUrl); } + + /** + * test method while it is part of the interface + */ + public function testGetKeyword_HappyPath() { + $profile = new CursorPaginationProfile(); + + $keyword = $profile->getKeyword('page'); + + $this->assertSame('page', $keyword); + } }