diff --git a/UPGRADE_2_TO_3.md b/UPGRADE_2_TO_3.md new file mode 100644 index 0000000..0f60442 --- /dev/null +++ b/UPGRADE_2_TO_3.md @@ -0,0 +1,31 @@ +# Upgrade from library v2 to v3 + +## Enums + +Content types: +- `Document::CONTENT_TYPE_OFFICIAL` to `ContentTypeEnum::Official` +- `Document::CONTENT_TYPE_DEBUG` to `ContentTypeEnum::Debug` +- `Document::CONTENT_TYPE_JSONP` to `ContentTypeEnum::Jsonp` + +Jsonapi versions: +- `Document::JSONAPI_VERSION_1_0` to `JsonapiVersionEnum::V_1_0` +- `Document::JSONAPI_VERSION_1_1` to `JsonapiVersionEnum::V_1_1` +- `Document::JSONAPI_VERSION_LATEST` to `JsonapiVersionEnum::Latest` + +Document levels: +- `Document::LEVEL_ROOT` to `DocumentLevelEnum::Root` +- `Document::LEVEL_JSONAPI` to `DocumentLevelEnum::Jsonapi` +- `Document::LEVEL_Resource` to `DocumentLevelEnum::Resource` + +Sorting orders: +- `RequestParser->getSortFields()` returns a `SortOrderEnum` instead of `string` for the `order` field + +Relationship types: +- `RelationshipObject::TO_ONE` to `RelationshipTypeEnum::ToOne` +- `RelationshipObject::TO_MANY` to `RelationshipTypeEnum::ToMany` + +## Internal + +Enums: +- `RequestParser::SORT_*` to `SortOrderEnum::*` +- `Validator::OBJECT_CONTAINER_*` to `ObjectContainerEnum::*` diff --git a/examples/atomic_operations_extension.php b/examples/atomic_operations_extension.php index 4e97c6d..caa6aaa 100644 --- a/examples/atomic_operations_extension.php +++ b/examples/atomic_operations_extension.php @@ -1,5 +1,7 @@ Content-Type: '.$contentType.''.PHP_EOL; $options = [ diff --git a/examples/meta_only.php b/examples/meta_only.php index a41df02..60562d7 100644 --- a/examples/meta_only.php +++ b/examples/meta_only.php @@ -1,5 +1,7 @@ addRelationship('bar', null); -$document->addRelationshipObject('baz', new RelationshipObject(RelationshipObject::TO_ONE)); -$document->addRelationshipObject('baf', new RelationshipObject(RelationshipObject::TO_MANY)); +$document->addRelationshipObject('baz', new RelationshipObject(RelationshipTypeEnum::ToOne)); +$document->addRelationshipObject('baf', new RelationshipObject(RelationshipTypeEnum::ToMany)); /** * sending the response diff --git a/examples/output.php b/examples/output.php index 0844c33..f6726e8 100644 --- a/examples/output.php +++ b/examples/output.php @@ -1,7 +1,9 @@ '; $document->sendResponse($options); echo ''; -echo '

Also sends http status code ('.$document->getHttpStatusCode().') and headers: [Content-Type: '.Document::CONTENT_TYPE_OFFICIAL.']

'; +echo '

Also sends http status code ('.$document->getHttpStatusCode().') and headers: [Content-Type: '.ContentTypeEnum::Official->value.']

'; diff --git a/examples/profile.php b/examples/profile.php index 8b070b6..8245855 100644 --- a/examples/profile.php +++ b/examples/profile.php @@ -1,7 +1,9 @@ Content-Type: '.$contentType.''.PHP_EOL; $options = [ diff --git a/examples/relationship_to_many_document.php b/examples/relationship_to_many_document.php index 1f02263..f2a2af2 100644 --- a/examples/relationship_to_many_document.php +++ b/examples/relationship_to_many_document.php @@ -1,5 +1,7 @@ setSelfLink('/articles/1/relationship/author', $meta=[], $level=Document::LEVEL_ROOT); +$relationshipDocument->setSelfLink('/articles/1/relationship/author', $meta=[], $level=DocumentLevelEnum::Root); $relationshipDocument->addLink('related', '/articles/1/author'); /** diff --git a/examples/relationships.php b/examples/relationships.php index bc25dfe..6755f34 100644 --- a/examples/relationships.php +++ b/examples/relationships.php @@ -1,7 +1,10 @@ addResource($friend1Resource); $relationshipObject->addResource($friend2Resource); @@ -65,7 +68,7 @@ * to-many relationship, different types */ -$relationshipObject = new RelationshipObject($type=RelationshipObject::TO_MANY); +$relationshipObject = new RelationshipObject($type=RelationshipTypeEnum::ToMany); $relationshipObject->addResource($ship1Resource); $relationshipObject->addResource($dockResource); diff --git a/examples/request_superglobals.php b/examples/request_superglobals.php index fff5992..cb93d06 100644 --- a/examples/request_superglobals.php +++ b/examples/request_superglobals.php @@ -1,6 +1,8 @@ value; /** * parsing the request diff --git a/examples/resource_human_api.php b/examples/resource_human_api.php index 67ed657..b9ae514 100644 --- a/examples/resource_human_api.php +++ b/examples/resource_human_api.php @@ -1,5 +1,7 @@ id); -$selfResourceMeta = ['level' => Document::LEVEL_RESOURCE]; -$partnerMeta = ['level' => Document::LEVEL_RESOURCE]; -$redirectMeta = ['level' => Document::LEVEL_ROOT]; +$selfResourceMeta = ['level' => DocumentLevelEnum::Resource->name]; +$partnerMeta = ['level' => DocumentLevelEnum::Resource->name]; +$redirectMeta = ['level' => DocumentLevelEnum::Root->name]; $document->setSelfLink('/user/42', $selfResourceMeta); -$document->addLink('partner', '/user/1', $partnerMeta, $level=Document::LEVEL_RESOURCE); -$document->addLink('redirect', '/login', $redirectMeta, $level=Document::LEVEL_ROOT); +$document->addLink('partner', '/user/1', $partnerMeta, $level=DocumentLevelEnum::Resource); +$document->addLink('redirect', '/login', $redirectMeta, $level=DocumentLevelEnum::Root); /** * sending the response diff --git a/examples/resource_nested_relations.php b/examples/resource_nested_relations.php index b0418f3..e182a48 100644 --- a/examples/resource_nested_relations.php +++ b/examples/resource_nested_relations.php @@ -1,5 +1,7 @@ setType('user'); $resource->setAttributesObject($attributes42); -$relationship = new RelationshipObject(RelationshipObject::TO_ONE); +$relationship = new RelationshipObject(RelationshipTypeEnum::ToOne); $relationship->setResource($resource); $relationships = new RelationshipsObject(); $relationships->addRelationshipObject('friend', $relationship); diff --git a/examples/status_only.php b/examples/status_only.php index abcced8..94439c0 100644 --- a/examples/status_only.php +++ b/examples/status_only.php @@ -1,5 +1,7 @@ withRules([ + DeclareStrictTypesRector::class, + ]) + // tab-based indenting ->withIndent(indentChar: "\t", indentSize: 1) diff --git a/src/CollectionDocument.php b/src/CollectionDocument.php index ba7c458..55cae30 100644 --- a/src/CollectionDocument.php +++ b/src/CollectionDocument.php @@ -1,5 +1,7 @@ Document::CONTENT_TYPE_OFFICIAL, + 'contentType' => ContentTypeEnum::Official, /** * overwrite the array to encode to json @@ -97,23 +89,15 @@ public function __construct() { * @param string $key * @param string $href * @param array $meta optional, if given a LinkObject is added, otherwise a link string is added - * @param string $level one of the Document::LEVEL_* constants, optional, defaults to Document::LEVEL_ROOT * - * @throws InputException if the $level is Document::LEVEL_JSONAPI, Document::LEVEL_RESOURCE, or unknown + * @throws InputException if the $level is not DocumentLevelEnum::Root */ - public function addLink($key, $href, array $meta=[], $level=Document::LEVEL_ROOT) { - if ($level === Document::LEVEL_ROOT) { - $this->linkManagerAddLink($key, $href, $meta); - } - elseif ($level === Document::LEVEL_JSONAPI) { - throw new InputException('level "jsonapi" can not be used for links'); - } - elseif ($level === Document::LEVEL_RESOURCE) { - throw new InputException('level "resource" can only be set on a ResourceDocument'); - } - else { - throw new InputException('unknown level "'.$level.'"'); - } + public function addLink($key, $href, array $meta=[], DocumentLevelEnum $level=DocumentLevelEnum::Root) { + match ($level) { + DocumentLevelEnum::Root => $this->linkManagerAddLink($key, $href, $meta), + DocumentLevelEnum::Jsonapi => throw new InputException('level "jsonapi" can not be used for links'), + DocumentLevelEnum::Resource => throw new InputException('level "resource" can only be set on a ResourceDocument'), + }; } /** @@ -123,11 +107,10 @@ public function addLink($key, $href, array $meta=[], $level=Document::LEVEL_ROOT * * @param string $href * @param array $meta optional, if given a LinkObject is added, otherwise a link string is added - * @param string $level one of the Document::LEVEL_* constants, optional, defaults to Document::LEVEL_ROOT */ - public function setSelfLink($href, array $meta=[], $level=Document::LEVEL_ROOT) { - if ($level === Document::LEVEL_ROOT && ($this->extensions !== [] || $this->profiles !== [])) { - $contentType = Converter::prepareContentType(Document::CONTENT_TYPE_OFFICIAL, $this->extensions, $this->profiles); + public function setSelfLink($href, array $meta=[], DocumentLevelEnum $level=DocumentLevelEnum::Root) { + if ($level === DocumentLevelEnum::Root && ($this->extensions !== [] || $this->profiles !== [])) { + $contentType = Converter::prepareContentType(ContentTypeEnum::Official, $this->extensions, $this->profiles); $linkObject = new LinkObject($href, $meta); $linkObject->setMediaType($contentType); @@ -144,43 +127,42 @@ public function setSelfLink($href, array $meta=[], $level=Document::LEVEL_ROOT) * * for example this could link to an OpenAPI or JSON Schema document * - * @note according to the spec, this can only be set to Document::LEVEL_ROOT + * @note according to the spec, this can only be set to DocumentLevelEnum::Root * * @param string $href * @param array $meta optional, if given a LinkObject is added, otherwise a link string is added */ public function setDescribedByLink($href, array $meta=[]) { - $this->addLink('describedby', $href, $meta, $level=Document::LEVEL_ROOT); + $this->addLink('describedby', $href, $meta, $level=DocumentLevelEnum::Root); } /** * @param string $key * @param mixed $value - * @param string $level one of the Document::LEVEL_* constants, optional, defaults to Document::LEVEL_ROOT * * @throws InputException if the $level is unknown - * @throws InputException if the $level is Document::LEVEL_RESOURCE + * @throws InputException if the $level is DocumentLevelEnum::Resource */ - public function addMeta($key, $value, $level=Document::LEVEL_ROOT) { - if ($level === Document::LEVEL_ROOT) { + public function addMeta($key, $value, DocumentLevelEnum $level=DocumentLevelEnum::Root) { + if ($level === DocumentLevelEnum::Root) { if ($this->meta === null) { $this->setMetaObject(new MetaObject()); } $this->meta->add($key, $value); } - elseif ($level === Document::LEVEL_JSONAPI) { + elseif ($level === DocumentLevelEnum::Jsonapi) { if ($this->jsonapi === null) { $this->setJsonapiObject(new JsonapiObject()); } $this->jsonapi->addMeta($key, $value); } - elseif ($level === Document::LEVEL_RESOURCE) { + elseif ($level === DocumentLevelEnum::Resource) { throw new InputException('level "resource" can only be set on a ResourceDocument'); } else { - throw new InputException('unknown level "'.$level.'"'); + throw new InputException('unknown level "'.$level->value.'"'); } } diff --git a/src/ErrorsDocument.php b/src/ErrorsDocument.php index 69163db..48ef29e 100644 --- a/src/ErrorsDocument.php +++ b/src/ErrorsDocument.php @@ -1,9 +1,10 @@ resource instanceof ResourceObject === false) { throw new Exception('the resource is an identifier-only object'); } - if ($level === Document::LEVEL_RESOURCE) { + if ($level === DocumentLevelEnum::Resource) { $this->resource->addLink($key, $href, $meta); } else { @@ -143,12 +145,12 @@ public function addLink($key, $href, array $meta=[], $level=Document::LEVEL_ROOT * @param string $href * @param array $meta optional */ - public function setSelfLink($href, array $meta=[], $level=Document::LEVEL_RESOURCE) { + public function setSelfLink($href, array $meta=[], DocumentLevelEnum $level=DocumentLevelEnum::Resource) { if ($this->resource instanceof ResourceObject === false) { throw new Exception('the resource is an identifier-only object'); } - if ($level === Document::LEVEL_RESOURCE) { + if ($level === DocumentLevelEnum::Resource) { $this->resource->setSelfLink($href, $meta); } else { @@ -159,10 +161,9 @@ public function setSelfLink($href, array $meta=[], $level=Document::LEVEL_RESOUR /** * @param string $key * @param mixed $value - * @param string $level one of the Document::LEVEL_* constants, optional, defaults to Document::LEVEL_ROOT */ - public function addMeta($key, $value, $level=Document::LEVEL_ROOT) { - if ($level === Document::LEVEL_RESOURCE) { + public function addMeta($key, $value, DocumentLevelEnum $level=DocumentLevelEnum::Root) { + if ($level === DocumentLevelEnum::Resource) { $this->resource->addMeta($key, $value); } else { diff --git a/src/enums/ContentTypeEnum.php b/src/enums/ContentTypeEnum.php new file mode 100644 index 0000000..a3d23d1 --- /dev/null +++ b/src/enums/ContentTypeEnum.php @@ -0,0 +1,11 @@ +value; +} diff --git a/src/enums/ObjectContainerEnum.php b/src/enums/ObjectContainerEnum.php new file mode 100644 index 0000000..d2258c0 --- /dev/null +++ b/src/enums/ObjectContainerEnum.php @@ -0,0 +1,16 @@ +value; + if ($extensions !== []) { $extensionLinks = []; foreach ($extensions as $extension) { diff --git a/src/helpers/ExtensionMemberManager.php b/src/helpers/ExtensionMemberManager.php index 1bfd257..f971fc8 100644 --- a/src/helpers/ExtensionMemberManager.php +++ b/src/helpers/ExtensionMemberManager.php @@ -1,5 +1,7 @@ value)); + $documentIsJson = (str_contains((string) $_SERVER['CONTENT_TYPE'], ContentTypeEnum::Debug->value)); if ($documentIsJsonapi || $documentIsJson) { $document = json_decode(file_get_contents('php://input'), true); @@ -186,7 +186,7 @@ public function hasSortFields() { * @param array $options optional {@see RequestParser::$defaults} * @return string[]|array */ public function getSortFields(array $options=[]) { @@ -203,11 +203,11 @@ public function getSortFields(array $options=[]) { $sort = []; foreach ($fields as $field) { - $order = RequestParser::SORT_ASCENDING; + $order = SortOrderEnum::Ascending; if (str_starts_with($field, '-')) { $field = substr($field, 1); - $order = RequestParser::SORT_DESCENDING; + $order = SortOrderEnum::Descending; } $sort[] = [ diff --git a/src/helpers/Validator.php b/src/helpers/Validator.php index 2c9e573..a31d1d5 100644 --- a/src/helpers/Validator.php +++ b/src/helpers/Validator.php @@ -1,7 +1,10 @@ usedFields[$fieldName] === Validator::OBJECT_CONTAINER_TYPE && $options['enforceTypeFieldNamespace'] === false) { + if ($this->usedFields[$fieldName] === ObjectContainerEnum::Type && $options['enforceTypeFieldNamespace'] === false) { continue; } - throw new DuplicateException('field name "'.$fieldName.'" already in use at "data.'.$this->usedFields[$fieldName].'"'); + throw new DuplicateException('field name "'.$fieldName.'" already in use at "data.'.$this->usedFields[$fieldName]->value.'"'); } } - /** - * @param string $objectContainerToClear one of the Validator::OBJECT_CONTAINER_* constants - */ - public function clearUsedFields($objectContainerToClear) { + public function clearUsedFields(ObjectContainerEnum $objectContainerToClear) { foreach ($this->usedFields as $fieldName => $containerFound) { if ($containerFound !== $objectContainerToClear) { continue; diff --git a/src/interfaces/DocumentInterface.php b/src/interfaces/DocumentInterface.php index 6d3e348..b776845 100644 --- a/src/interfaces/DocumentInterface.php +++ b/src/interfaces/DocumentInterface.php @@ -1,5 +1,7 @@ setVersion($version); } @@ -48,10 +48,7 @@ public function addMeta($key, $value) { * spec api */ - /** - * @param string $version - */ - public function setVersion($version) { + public function setVersion(JsonapiVersionEnum $version) { $this->version = $version; } @@ -113,7 +110,7 @@ public function toArray() { $array = array_merge($array, $this->getExtensionMembers()); } if ($this->version !== null) { - $array['version'] = $this->version; + $array['version'] = $this->version->value; } if ($this->extensions !== []) { $array['ext'] = []; diff --git a/src/objects/LinkObject.php b/src/objects/LinkObject.php index e7a06e0..9e6286e 100644 --- a/src/objects/LinkObject.php +++ b/src/objects/LinkObject.php @@ -1,5 +1,7 @@ type = $type; - } + public function __construct( + protected RelationshipTypeEnum $type, + ) {} /** * human api @@ -69,7 +58,7 @@ public static function fromAnything($relation, array $links=[], array $meta=[]) $relationshipObject = self::fromCollectionDocument($relation, $links, $meta); } elseif ($relation === null) { - $relationshipObject = new RelationshipObject(RelationshipObject::TO_ONE); + $relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToOne); } else { throw new InputException('unknown format of relation "'.gettype($relation).'"'); @@ -82,18 +71,15 @@ public static function fromAnything($relation, array $links=[], array $meta=[]) * @param ResourceInterface $resource * @param array $links optional * @param array $meta optional - * @param string $type optional, one of the RelationshipObject::TO_* constants, defaults to RelationshipObject::TO_ONE * @return RelationshipObject */ - public static function fromResource(ResourceInterface $resource, array $links=[], array $meta=[], $type=RelationshipObject::TO_ONE) { + public static function fromResource(ResourceInterface $resource, array $links=[], array $meta=[], RelationshipTypeEnum $type=RelationshipTypeEnum::ToOne) { $relationshipObject = new self($type); - if ($type === RelationshipObject::TO_ONE) { - $relationshipObject->setResource($resource); - } - elseif ($type === RelationshipObject::TO_MANY) { - $relationshipObject->addResource($resource); - } + match ($type) { + RelationshipTypeEnum::ToOne => $relationshipObject->setResource($resource), + RelationshipTypeEnum::ToMany => $relationshipObject->addResource($resource), + }; if ($links !== []) { $relationshipObject->setLinksObject(LinksObject::fromArray($links)); @@ -112,7 +98,7 @@ public static function fromResource(ResourceInterface $resource, array $links=[] * @return RelationshipObject */ public static function fromCollectionDocument(CollectionDocument $collectionDocument, array $links=[], array $meta=[]) { - $relationshipObject = new self(RelationshipObject::TO_MANY); + $relationshipObject = new self(RelationshipTypeEnum::ToMany); foreach ($collectionDocument->getContainedResources() as $resource) { $relationshipObject->addResource($resource); @@ -148,7 +134,7 @@ public function setRelatedLink($href, array $meta=[]) { * @throws InputException if used on a to-one relationship */ public function setPaginationLinks($previousHref=null, $nextHref=null, $firstHref=null, $lastHref=null) { - if ($this->type === RelationshipObject::TO_ONE) { + if ($this->type === RelationshipTypeEnum::ToOne) { throw new InputException('can not add pagination links to a to-one relationship'); } @@ -190,7 +176,7 @@ public function addMeta($key, $value) { * @throws InputException if used on a to-many relationship, use {@see ->addResource()} instead */ public function setResource(ResourceInterface $resource) { - if ($this->type === RelationshipObject::TO_MANY) { + if ($this->type === RelationshipTypeEnum::ToMany) { throw new InputException('can not set a resource on a to-many relationship, use ->addResource()'); } @@ -205,7 +191,7 @@ public function setResource(ResourceInterface $resource) { * @throws InputException if used on a to-one relationship, use {@see ->setResource()} instead */ public function addResource(ResourceInterface $resource) { - if ($this->type === RelationshipObject::TO_ONE) { + if ($this->type === RelationshipTypeEnum::ToOne) { throw new InputException('can not add a resource to a to-one relationship, use ->setResource()'); } @@ -235,18 +221,20 @@ public function hasResource(ResourceInterface $otherResource) { if ($this->isEmpty()) { return false; } - if ($this->type === RelationshipObject::TO_ONE) { - return $this->resource->getResource()->equals($otherResource->getResource()); - } - if ($this->type === RelationshipObject::TO_MANY) { - foreach ($this->resources as $ownResource) { - if ($ownResource->getResource()->equals($otherResource->getResource())) { - return true; + + switch ($this->type) { + case RelationshipTypeEnum::ToOne: + return $this->resource->getResource()->equals($otherResource->getResource()); + + case RelationshipTypeEnum::ToMany: + foreach ($this->resources as $ownResource) { + if ($ownResource->getResource()->equals($otherResource->getResource())) { + return true; + } } - } + + return false; } - - return false; } /** @@ -254,10 +242,10 @@ public function hasResource(ResourceInterface $otherResource) { */ public function isEmpty() { - if ($this->type === RelationshipObject::TO_ONE && $this->resource !== null) { + if ($this->type === RelationshipTypeEnum::ToOne && $this->resource !== null) { return false; } - if ($this->type === RelationshipObject::TO_MANY && $this->resources !== []) { + if ($this->type === RelationshipTypeEnum::ToMany && $this->resources !== []) { return false; } if ($this->links !== null && $this->links->isEmpty() === false) { @@ -289,13 +277,13 @@ public function toArray() { if ($this->links !== null && $this->links->isEmpty() === false) { $array['links'] = $this->links->toArray(); } - if ($this->type === RelationshipObject::TO_ONE) { + if ($this->type === RelationshipTypeEnum::ToOne) { $array['data'] = null; if ($this->resource !== null) { $array['data'] = $this->resource->getResource($identifierOnly=true)->toArray(); } } - if ($this->type === RelationshipObject::TO_MANY) { + if ($this->type === RelationshipTypeEnum::ToMany) { $array['data'] = []; foreach ($this->resources as $resource) { $array['data'][] = $resource->getResource($identifierOnly=true)->toArray(); @@ -317,7 +305,7 @@ public function getNestedContainedResourceObjects() { return []; } - $resources = ($this->type === RelationshipObject::TO_ONE) ? [$this->resource] : $this->resources; + $resources = ($this->type === RelationshipTypeEnum::ToOne) ? [$this->resource] : $this->resources; $resourceObjects = []; foreach ($resources as $resource) { diff --git a/src/objects/RelationshipsObject.php b/src/objects/RelationshipsObject.php index 8c8d0be..a9a684e 100644 --- a/src/objects/RelationshipsObject.php +++ b/src/objects/RelationshipsObject.php @@ -1,5 +1,7 @@ validator->claimUsedFields($fieldNames=['type'], Validator::OBJECT_CONTAINER_TYPE); - $this->validator->claimUsedFields($fieldNames=['id'], Validator::OBJECT_CONTAINER_ID); - $this->validator->claimUsedFields($fieldNames=['lid'], Validator::OBJECT_CONTAINER_LID); + $this->validator->claimUsedFields($fieldNames=['type'], ObjectContainerEnum::Type); + $this->validator->claimUsedFields($fieldNames=['id'], ObjectContainerEnum::Id); + $this->validator->claimUsedFields($fieldNames=['lid'], ObjectContainerEnum::Lid); } /** diff --git a/src/objects/ResourceObject.php b/src/objects/ResourceObject.php index e0d59d3..eff9d30 100644 --- a/src/objects/ResourceObject.php +++ b/src/objects/ResourceObject.php @@ -1,12 +1,14 @@ attributes = new AttributesObject(); } - $this->validator->claimUsedFields([$key], Validator::OBJECT_CONTAINER_ATTRIBUTES, $options); + $this->validator->claimUsedFields([$key], ObjectContainerEnum::Attributes, $options); $this->attributes->add($key, $value); } @@ -129,8 +131,8 @@ public function setSelfLink($href, array $meta=[]) { */ public function setAttributesObject(AttributesObject $attributesObject, array $options=[]) { $newKeys = $attributesObject->getKeys(); - $this->validator->clearUsedFields(Validator::OBJECT_CONTAINER_ATTRIBUTES); - $this->validator->claimUsedFields($newKeys, Validator::OBJECT_CONTAINER_ATTRIBUTES, $options); + $this->validator->clearUsedFields(ObjectContainerEnum::Attributes); + $this->validator->claimUsedFields($newKeys, ObjectContainerEnum::Attributes, $options); $this->attributes = $attributesObject; } @@ -151,7 +153,7 @@ public function addRelationshipObject($key, RelationshipObject $relationshipObje $this->setRelationshipsObject(new RelationshipsObject()); } - $this->validator->claimUsedFields([$key], Validator::OBJECT_CONTAINER_RELATIONSHIPS, $options); + $this->validator->claimUsedFields([$key], ObjectContainerEnum::Relationships, $options); $this->relationships->addRelationshipObject($key, $relationshipObject); } @@ -161,8 +163,8 @@ public function addRelationshipObject($key, RelationshipObject $relationshipObje */ public function setRelationshipsObject(RelationshipsObject $relationshipsObject) { $newKeys = $relationshipsObject->getKeys(); - $this->validator->clearUsedFields(Validator::OBJECT_CONTAINER_RELATIONSHIPS); - $this->validator->claimUsedFields($newKeys, Validator::OBJECT_CONTAINER_RELATIONSHIPS); + $this->validator->clearUsedFields(ObjectContainerEnum::Relationships); + $this->validator->claimUsedFields($newKeys, ObjectContainerEnum::Relationships); $this->relationships = $relationshipsObject; } diff --git a/src/profiles/CursorPaginationProfile.php b/src/profiles/CursorPaginationProfile.php index 8ab4a9f..5140cb7 100644 --- a/src/profiles/CursorPaginationProfile.php +++ b/src/profiles/CursorPaginationProfile.php @@ -1,9 +1,11 @@ addMeta('page', $metadata, $level=Document::LEVEL_RESOURCE); + $resource->addMeta('page', $metadata, $level=DocumentLevelEnum::Resource); } else { $resource->addMeta('page', $metadata); diff --git a/tests/CollectionDocumentTest.php b/tests/CollectionDocumentTest.php index fd255f0..97ce3e8 100644 --- a/tests/CollectionDocumentTest.php +++ b/tests/CollectionDocumentTest.php @@ -1,5 +1,7 @@ assertSame('foo', Converter::prepareContentType('foo', [], [])); + $this->assertSame(ContentTypeEnum::Official->value, Converter::prepareContentType(ContentTypeEnum::Official, [], [])); } /** @@ -78,7 +81,7 @@ public function testPrepareContentType_WithExtensionStringLink() { $extension = new TestExtension(); $extension->setOfficialLink('bar'); - $this->assertSame('foo; ext="bar"', Converter::prepareContentType('foo', [$extension], [])); + $this->assertSame(ContentTypeEnum::Official->value.'; ext="bar"', Converter::prepareContentType(ContentTypeEnum::Official, [$extension], [])); } /** @@ -88,7 +91,7 @@ public function testPrepareContentType_WithProfileStringLink() { $profile = new TestProfile(); $profile->setOfficialLink('bar'); - $this->assertSame('foo; profile="bar"', Converter::prepareContentType('foo', [], [$profile])); + $this->assertSame(ContentTypeEnum::Official->value.'; profile="bar"', Converter::prepareContentType(ContentTypeEnum::Official, [], [$profile])); } /** @@ -108,7 +111,7 @@ public function testPrepareContentType_WithMultipleExtensionsAndProfiles() { $profile2 = new TestProfile(); $profile2->setOfficialLink('baz'); - $this->assertSame('foo; ext="bar baz"; profile="bar baz"', Converter::prepareContentType('foo', [$extension1, $extension2], [$profile1, $profile2])); + $this->assertSame(ContentTypeEnum::Official->value.'; ext="bar baz"; profile="bar baz"', Converter::prepareContentType(ContentTypeEnum::Official, [$extension1, $extension2], [$profile1, $profile2])); } } diff --git a/tests/DocumentTest.php b/tests/DocumentTest.php index 6e8e51c..024f989 100644 --- a/tests/DocumentTest.php +++ b/tests/DocumentTest.php @@ -1,7 +1,10 @@ expectException(InputException::class); $this->expectExceptionMessage('level "jsonapi" can not be used for links'); - $document->addLink('foo', 'https://jsonapi.org', $meta=[], $level=Document::LEVEL_JSONAPI); + $document->addLink('foo', 'https://jsonapi.org', $meta=[], $level=DocumentLevelEnum::Jsonapi); } public function testAddLink_BlocksResourceLevel() { @@ -77,16 +80,7 @@ public function testAddLink_BlocksResourceLevel() { $this->expectException(InputException::class); $this->expectExceptionMessage('level "resource" can only be set on a ResourceDocument'); - $document->addLink('foo', 'https://jsonapi.org', $meta=[], $level=Document::LEVEL_RESOURCE); - } - - public function testAddLink_BlocksUnknownLevel() { - $document = new Document(); - - $this->expectException(InputException::class); - $this->expectExceptionMessage('unknown level "foo"'); - - $document->addLink('foo', 'https://jsonapi.org', $meta=[], $level='foo'); + $document->addLink('foo', 'https://jsonapi.org', $meta=[], $level=DocumentLevelEnum::Resource); } public function testSetSelfLink_HappyPath() { @@ -160,7 +154,7 @@ public function testAddMeta_AtJsonapiLevel() { $this->assertArrayHasKey('jsonapi', $array); $this->assertArrayNotHasKey('meta', $array['jsonapi']); - $document->addMeta('foo', 'bar', $level=Document::LEVEL_JSONAPI); + $document->addMeta('foo', 'bar', $level=DocumentLevelEnum::Jsonapi); $array = $document->toArray(); @@ -178,16 +172,7 @@ public function testAddMeta_BlocksResourceLevel() { $this->expectException(InputException::class); $this->expectExceptionMessage('level "resource" can only be set on a ResourceDocument'); - $document->addMeta('foo', 'bar', $level=Document::LEVEL_RESOURCE); - } - - public function testAddMeta_BlocksUnknownLevel() { - $document = new Document(); - - $this->expectException(InputException::class); - $this->expectExceptionMessage('unknown level "foo"'); - - $document->addMeta('foo', 'bar', $level='foo'); + $document->addMeta('foo', 'bar', $level=DocumentLevelEnum::Resource); } public function testAddLinkObject_HappyPath() { diff --git a/tests/ErrorsDocumentTest.php b/tests/ErrorsDocumentTest.php index 151f44e..10e694f 100644 --- a/tests/ErrorsDocumentTest.php +++ b/tests/ErrorsDocumentTest.php @@ -1,5 +1,7 @@ addMeta('foo', 'root', $level=Document::LEVEL_ROOT); - $document->addMeta('bar', 'resource', $level=Document::LEVEL_RESOURCE); - $document->addMeta('baz', 'jsonapi', $level=Document::LEVEL_JSONAPI); + $document->addMeta('foo', 'root', $level=DocumentLevelEnum::Root); + $document->addMeta('bar', 'resource', $level=DocumentLevelEnum::Resource); + $document->addMeta('baz', 'jsonapi', $level=DocumentLevelEnum::Jsonapi); $array = $document->toArray(); @@ -122,7 +124,7 @@ public function testAddMeta_RecreateJsonapiObject() { $this->assertArrayNotHasKey('jsonapi', $array); - $document->addMeta('baz', 'jsonapi', $level=Document::LEVEL_JSONAPI); + $document->addMeta('baz', 'jsonapi', $level=DocumentLevelEnum::Jsonapi); $array = $document->toArray(); diff --git a/tests/SeparateProcessTest.php b/tests/SeparateProcessTest.php index ec00ccd..95f8290 100644 --- a/tests/SeparateProcessTest.php +++ b/tests/SeparateProcessTest.php @@ -1,8 +1,11 @@ sendResponse(); @@ -28,7 +31,7 @@ public function testSendResponse_HappyPath() { * @runInSeparateProcess */ public function testSendResponse_NoContent() { - $document = new Document(); + $document = new TestableNonAbstractDocument(); $document->setHttpStatusCode(204); ob_start(); @@ -47,30 +50,30 @@ public function testSendResponse_ContentTypeHeader() { $this->markTestSkipped('can not run without xdebug'); } - $document = new Document(); + $document = new TestableNonAbstractDocument(); ob_start(); $document->sendResponse(); ob_end_clean(); - $this->assertSame(['Content-Type: '.Document::CONTENT_TYPE_OFFICIAL], xdebug_get_headers()); + $this->assertSame(['Content-Type: '.ContentTypeEnum::Official->value], xdebug_get_headers()); - $options = ['contentType' => Document::CONTENT_TYPE_OFFICIAL]; + $options = ['contentType' => ContentTypeEnum::Official]; ob_start(); $document->sendResponse($options); ob_end_clean(); - $this->assertSame(['Content-Type: '.Document::CONTENT_TYPE_OFFICIAL], xdebug_get_headers()); + $this->assertSame(['Content-Type: '.ContentTypeEnum::Official->value], xdebug_get_headers()); - $options = ['contentType' => Document::CONTENT_TYPE_DEBUG]; + $options = ['contentType' => ContentTypeEnum::Debug]; ob_start(); $document->sendResponse($options); ob_end_clean(); - $this->assertSame(['Content-Type: '.Document::CONTENT_TYPE_DEBUG], xdebug_get_headers()); + $this->assertSame(['Content-Type: '.ContentTypeEnum::Debug->value], xdebug_get_headers()); - $options = ['contentType' => Document::CONTENT_TYPE_JSONP]; + $options = ['contentType' => ContentTypeEnum::Jsonp]; ob_start(); $document->sendResponse($options); ob_end_clean(); - $this->assertSame(['Content-Type: '.Document::CONTENT_TYPE_JSONP], xdebug_get_headers()); + $this->assertSame(['Content-Type: '.ContentTypeEnum::Jsonp->value], xdebug_get_headers()); } /** @@ -86,13 +89,13 @@ public function testSendResponse_ContentTypeHeaderWithExtensions() { $extension->setNamespace('one'); $extension->setOfficialLink('https://jsonapi.org'); - $document = new Document(); + $document = new TestableNonAbstractDocument(); $document->applyExtension($extension); ob_start(); $document->sendResponse(); ob_end_clean(); - $this->assertSame(['Content-Type: '.Document::CONTENT_TYPE_OFFICIAL.'; ext="https://jsonapi.org"'], xdebug_get_headers()); + $this->assertSame(['Content-Type: '.ContentTypeEnum::Official->value.'; ext="https://jsonapi.org"'], xdebug_get_headers()); $extension = new TestExtension(); $extension->setNamespace('two'); @@ -102,7 +105,7 @@ public function testSendResponse_ContentTypeHeaderWithExtensions() { ob_start(); $document->sendResponse(); ob_end_clean(); - $this->assertSame(['Content-Type: '.Document::CONTENT_TYPE_OFFICIAL.'; ext="https://jsonapi.org https://jsonapi.org/2"'], xdebug_get_headers()); + $this->assertSame(['Content-Type: '.ContentTypeEnum::Official->value.'; ext="https://jsonapi.org https://jsonapi.org/2"'], xdebug_get_headers()); } /** @@ -117,13 +120,13 @@ public function testSendResponse_ContentTypeHeaderWithProfiles() { $profile = new TestProfile(); $profile->setOfficialLink('https://jsonapi.org'); - $document = new Document(); + $document = new TestableNonAbstractDocument(); $document->applyProfile($profile); ob_start(); $document->sendResponse(); ob_end_clean(); - $this->assertSame(['Content-Type: '.Document::CONTENT_TYPE_OFFICIAL.'; profile="https://jsonapi.org"'], xdebug_get_headers()); + $this->assertSame(['Content-Type: '.ContentTypeEnum::Official->value.'; profile="https://jsonapi.org"'], xdebug_get_headers()); $profile = new TestProfile(); $profile->setOfficialLink('https://jsonapi.org/2'); @@ -132,14 +135,14 @@ public function testSendResponse_ContentTypeHeaderWithProfiles() { ob_start(); $document->sendResponse(); ob_end_clean(); - $this->assertSame(['Content-Type: '.Document::CONTENT_TYPE_OFFICIAL.'; profile="https://jsonapi.org https://jsonapi.org/2"'], xdebug_get_headers()); + $this->assertSame(['Content-Type: '.ContentTypeEnum::Official->value.'; profile="https://jsonapi.org https://jsonapi.org/2"'], xdebug_get_headers()); } /** * @runInSeparateProcess */ public function testSendResponse_StatusCodeHeader() { - $document = new Document(); + $document = new TestableNonAbstractDocument(); ob_start(); $document->sendResponse(); @@ -169,7 +172,7 @@ public function testSendResponse_StatusCodeHeader() { * @runInSeparateProcess */ public function testSendResponse_CustomJson() { - $document = new Document(); + $document = new TestableNonAbstractDocument(); $options = ['json' => '{"foo":42}']; ob_start(); diff --git a/tests/TestableNonAbstractDocument.php b/tests/TestableNonAbstractDocument.php index 4f1b018..57f5516 100644 --- a/tests/TestableNonAbstractDocument.php +++ b/tests/TestableNonAbstractDocument.php @@ -1,5 +1,7 @@ claimUsedFields($fieldNames, $objectContainer); $fieldNames = ['bar']; - $objectContainer = Validator::OBJECT_CONTAINER_ATTRIBUTES; + $objectContainer = ObjectContainerEnum::Attributes; $validator->claimUsedFields($fieldNames, $objectContainer); } @@ -28,12 +31,12 @@ public function testClaimUsedFields_EnforceNamespace() { $validator = new Validator(); $fieldNames = ['foo']; - $objectContainer = Validator::OBJECT_CONTAINER_ATTRIBUTES; + $objectContainer = ObjectContainerEnum::Attributes; $validator->claimUsedFields($fieldNames, $objectContainer); $this->expectException(DuplicateException::class); - $objectContainer = Validator::OBJECT_CONTAINER_RELATIONSHIPS; + $objectContainer = ObjectContainerEnum::Relationships; $validator->claimUsedFields($fieldNames, $objectContainer); } @@ -42,10 +45,10 @@ public function testClaimUsedFields_AllowSameContainer() { $validator = new Validator(); $fieldNames = ['foo']; - $objectContainer = Validator::OBJECT_CONTAINER_ATTRIBUTES; + $objectContainer = ObjectContainerEnum::Attributes; $validator->claimUsedFields($fieldNames, $objectContainer); - $objectContainer = Validator::OBJECT_CONTAINER_ATTRIBUTES; + $objectContainer = ObjectContainerEnum::Attributes; $validator->claimUsedFields($fieldNames, $objectContainer); } @@ -54,10 +57,10 @@ public function testClaimUsedFields_OptionForReusingTypeField() { $validator = new Validator(); $fieldNames = ['type']; - $objectContainer = Validator::OBJECT_CONTAINER_TYPE; + $objectContainer = ObjectContainerEnum::Type; $validator->claimUsedFields($fieldNames, $objectContainer); - $objectContainer = Validator::OBJECT_CONTAINER_ATTRIBUTES; + $objectContainer = ObjectContainerEnum::Attributes; $options = ['enforceTypeFieldNamespace' => false]; $validator->claimUsedFields($fieldNames, $objectContainer, $options); } @@ -67,7 +70,7 @@ public function testClearUsedFields_HappyPath() { $validator = new Validator(); $fieldNames = ['foo']; - $objectContainer = Validator::OBJECT_CONTAINER_ATTRIBUTES; + $objectContainer = ObjectContainerEnum::Attributes; $validator->claimUsedFields($fieldNames, $objectContainer); $validator->clearUsedFields($objectContainer); @@ -77,13 +80,13 @@ public function testClearUsedFields_FreesForAnotherNamespace() { $validator = new Validator(); $fieldNames = ['foo', 'bar']; - $objectContainer = Validator::OBJECT_CONTAINER_ATTRIBUTES; + $objectContainer = ObjectContainerEnum::Attributes; $validator->claimUsedFields($fieldNames, $objectContainer); $thrown = false; try { $fieldNames = ['bar']; - $objectContainer = Validator::OBJECT_CONTAINER_RELATIONSHIPS; + $objectContainer = ObjectContainerEnum::Relationships; $validator->claimUsedFields($fieldNames, $objectContainer); } catch (DuplicateException) { @@ -91,21 +94,21 @@ public function testClearUsedFields_FreesForAnotherNamespace() { } $this->assertTrue($thrown); - $objectContainer = Validator::OBJECT_CONTAINER_ATTRIBUTES; + $objectContainer = ObjectContainerEnum::Attributes; $validator->clearUsedFields($objectContainer); $fieldNames = ['foo']; - $objectContainer = Validator::OBJECT_CONTAINER_ATTRIBUTES; + $objectContainer = ObjectContainerEnum::Attributes; $validator->claimUsedFields($fieldNames, $objectContainer); $fieldNames = ['bar']; - $objectContainer = Validator::OBJECT_CONTAINER_RELATIONSHIPS; + $objectContainer = ObjectContainerEnum::Relationships; $validator->claimUsedFields($fieldNames, $objectContainer); $this->expectException(DuplicateException::class); $fieldNames = ['foo']; - $objectContainer = Validator::OBJECT_CONTAINER_RELATIONSHIPS; + $objectContainer = ObjectContainerEnum::Relationships; $validator->claimUsedFields($fieldNames, $objectContainer); } diff --git a/tests/bootstrap_tests.php b/tests/bootstrap_tests.php index f695b44..80a03b3 100644 --- a/tests/bootstrap_tests.php +++ b/tests/bootstrap_tests.php @@ -1,5 +1,7 @@ addAtMember('context', '/data/relationships/foo/meta/@context'); - $relationshipObject = new RelationshipObject(RelationshipObject::TO_ONE); + $relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToOne); $relationshipObject->addAtMember('context', '/data/relationships/foo/@context'); $relationshipObject->setResource($resourceObject); $relationshipObject->setLinksObject($linksObject); @@ -102,7 +105,7 @@ public static function createJsonapiDocument() { $resourceIdentifierObject->addAtMember('context', '/data/relationships/bar/data/@context'); $resourceIdentifierObject->setMetaObject($metaObject); - $relationshipObject = new RelationshipObject(RelationshipObject::TO_ONE); + $relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToOne); $relationshipObject->addAtMember('context', '/data/relationships/bar/@context'); $relationshipObject->setResource($resourceIdentifierObject); diff --git a/tests/example_output/at_members_in_errors/at_members_in_errors.php b/tests/example_output/at_members_in_errors/at_members_in_errors.php index 8d97513..e937eb8 100644 --- a/tests/example_output/at_members_in_errors/at_members_in_errors.php +++ b/tests/example_output/at_members_in_errors/at_members_in_errors.php @@ -1,5 +1,7 @@ addExtensionMember($extension, 'key', '/data/relationships/foo/meta/key'); - $relationshipObject = new RelationshipObject(RelationshipObject::TO_ONE); + $relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToOne); $relationshipObject->addExtensionMember($extension, 'key', '/data/relationships/foo/key'); $relationshipObject->setResource($resourceObject); $relationshipObject->setLinksObject($linksObject); @@ -107,7 +110,7 @@ public static function createJsonapiDocument() { $resourceIdentifierObject->addExtensionMember($extension, 'key', '/data/relationships/bar/data/key'); $resourceIdentifierObject->setMetaObject($metaObject); - $relationshipObject = new RelationshipObject(RelationshipObject::TO_ONE); + $relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToOne); $relationshipObject->addExtensionMember($extension, 'key', '/data/relationships/bar/key'); $relationshipObject->setResource($resourceIdentifierObject); diff --git a/tests/example_output/meta_only/meta_only.php b/tests/example_output/meta_only/meta_only.php index 2bf1ee6..2bf360d 100644 --- a/tests/example_output/meta_only/meta_only.php +++ b/tests/example_output/meta_only/meta_only.php @@ -1,5 +1,7 @@ addLinkObject('bar', new LinkObject()); $document->addRelationship('bar', null); - $document->addRelationshipObject('baz', new RelationshipObject(RelationshipObject::TO_ONE)); - $document->addRelationshipObject('baf', new RelationshipObject(RelationshipObject::TO_MANY)); + $document->addRelationshipObject('baz', new RelationshipObject(RelationshipTypeEnum::ToOne)); + $document->addRelationshipObject('baf', new RelationshipObject(RelationshipTypeEnum::ToMany)); return $document; } diff --git a/tests/example_output/profile/profile.php b/tests/example_output/profile/profile.php index 16deba8..56bdde4 100644 --- a/tests/example_output/profile/profile.php +++ b/tests/example_output/profile/profile.php @@ -1,5 +1,7 @@ setSelfLink('/articles/1/relationship/author', $meta=[], $level=Document::LEVEL_ROOT); + $document->setSelfLink('/articles/1/relationship/author', $meta=[], $level=DocumentLevelEnum::Root); $document->addLink('related', '/articles/1/author'); return $document; diff --git a/tests/example_output/relationships/relationships.php b/tests/example_output/relationships/relationships.php index 8426cdd..49f833f 100644 --- a/tests/example_output/relationships/relationships.php +++ b/tests/example_output/relationships/relationships.php @@ -1,9 +1,12 @@ addResource($friend1Resource); $relationshipObject->addResource($friend2Resource); @@ -63,7 +66,7 @@ public static function createJsonapiDocument() { * to-many relationship, different types */ - $relationshipObject = new RelationshipObject($type=RelationshipObject::TO_MANY); + $relationshipObject = new RelationshipObject($type=RelationshipTypeEnum::ToMany); $relationshipObject->addResource($ship1Resource); $relationshipObject->addResource($dockResource); diff --git a/tests/example_output/resource_document_identifier_only/resource_document_identifier_only.php b/tests/example_output/resource_document_identifier_only/resource_document_identifier_only.php index 493f7cc..3c96058 100644 --- a/tests/example_output/resource_document_identifier_only/resource_document_identifier_only.php +++ b/tests/example_output/resource_document_identifier_only/resource_document_identifier_only.php @@ -1,5 +1,7 @@ id); - $selfResourceMeta = ['level' => Document::LEVEL_RESOURCE]; - $partnerMeta = ['level' => Document::LEVEL_RESOURCE]; - $redirectMeta = ['level' => Document::LEVEL_ROOT]; + $selfResourceMeta = ['level' => DocumentLevelEnum::Resource->name]; + $partnerMeta = ['level' => DocumentLevelEnum::Resource->name]; + $redirectMeta = ['level' => DocumentLevelEnum::Root->name]; $document->setSelfLink('/user/42', $selfResourceMeta); - $document->addLink('partner', '/user/1', $partnerMeta, $level=Document::LEVEL_RESOURCE); - $document->addLink('redirect', '/login', $redirectMeta, $level=Document::LEVEL_ROOT); + $document->addLink('partner', '/user/1', $partnerMeta, $level=DocumentLevelEnum::Resource); + $document->addLink('redirect', '/login', $redirectMeta, $level=DocumentLevelEnum::Root); return $document; } diff --git a/tests/example_output/resource_nested_relations/resource_nested_relations.php b/tests/example_output/resource_nested_relations/resource_nested_relations.php index 4095c84..5db1d54 100644 --- a/tests/example_output/resource_nested_relations/resource_nested_relations.php +++ b/tests/example_output/resource_nested_relations/resource_nested_relations.php @@ -1,5 +1,7 @@ setType('user'); $resource->setAttributesObject($attributes42); - $relationship = new RelationshipObject(RelationshipObject::TO_ONE); + $relationship = new RelationshipObject(RelationshipTypeEnum::ToOne); $relationship->setResource($resource); $relationships = new RelationshipsObject(); $relationships->addRelationshipObject('friend', $relationship); diff --git a/tests/example_output/status_only/status_only.php b/tests/example_output/status_only/status_only.php index 3546205..209e9cb 100644 --- a/tests/example_output/status_only/status_only.php +++ b/tests/example_output/status_only/status_only.php @@ -1,5 +1,7 @@ value; $_POST = [ 'data' => [ @@ -61,7 +64,7 @@ public function testFromSuperglobals_HappyPath() { $this->assertSame(['ship' => ['wing' => []]], $requestParser->getIncludePaths()); $this->assertSame(['name', 'location'], $requestParser->getSparseFieldset('user')); - $this->assertSame([['field' => 'name', 'order' => RequestParser::SORT_ASCENDING], ['field' => 'location', 'order' => RequestParser::SORT_DESCENDING]], $requestParser->getSortFields()); + $this->assertSame([['field' => 'name', 'order' => SortOrderEnum::Ascending], ['field' => 'location', 'order' => SortOrderEnum::Descending]], $requestParser->getSortFields()); $this->assertSame(['number' => '2', 'size' => '10'], $requestParser->getPagination()); $this->assertSame('42', $requestParser->getFilter()); @@ -80,7 +83,7 @@ public function testFromSuperglobals_WithPhpInputStream() { $_SERVER['REQUEST_SCHEME'] = 'https'; $_SERVER['HTTP_HOST'] = 'example.org'; $_SERVER['REQUEST_URI'] = '/'; - $_SERVER['CONTENT_TYPE'] = Document::CONTENT_TYPE_OFFICIAL; + $_SERVER['CONTENT_TYPE'] = ContentTypeEnum::Official->value; $_GET = []; $_POST = []; @@ -152,7 +155,7 @@ public function testFromPsrRequest_WithRequestInterface() { $this->assertSame(['ship' => ['wing' => []]], $requestParser->getIncludePaths()); $this->assertSame(['name', 'location'], $requestParser->getSparseFieldset('user')); - $this->assertSame([['field' => 'name', 'order' => RequestParser::SORT_ASCENDING], ['field' => 'location', 'order' => RequestParser::SORT_DESCENDING]], $requestParser->getSortFields()); + $this->assertSame([['field' => 'name', 'order' => SortOrderEnum::Ascending], ['field' => 'location', 'order' => SortOrderEnum::Descending]], $requestParser->getSortFields()); $this->assertSame(['number' => '2', 'size' => '10'], $requestParser->getPagination()); $this->assertSame('42', $requestParser->getFilter()); @@ -190,7 +193,7 @@ public function testFromPsrRequest_WithServerRequestInterface() { $this->assertSame('https://example.org/user/42?'.http_build_query($queryParameters), $requestParser->getSelfLink()); $this->assertTrue($requestParser->hasSortFields()); - $this->assertSame([['field' => 'name', 'order' => RequestParser::SORT_ASCENDING], ['field' => 'location', 'order' => RequestParser::SORT_DESCENDING]], $requestParser->getSortFields()); + $this->assertSame([['field' => 'name', 'order' => SortOrderEnum::Ascending], ['field' => 'location', 'order' => SortOrderEnum::Descending]], $requestParser->getSortFields()); } public function testGetSelfLink() { @@ -292,15 +295,15 @@ public function testHasSortFields() { public function testGetSortFields_Reformatted() { $queryParameters = ['sort' => 'foo']; $requestParser = new RequestParser($selfLink='', $queryParameters); - $this->assertSame([['field' => 'foo', 'order' => RequestParser::SORT_ASCENDING]], $requestParser->getSortFields()); + $this->assertSame([['field' => 'foo', 'order' => SortOrderEnum::Ascending]], $requestParser->getSortFields()); $queryParameters = ['sort' => '-bar']; $requestParser = new RequestParser($selfLink='', $queryParameters); - $this->assertSame([['field' => 'bar', 'order' => RequestParser::SORT_DESCENDING]], $requestParser->getSortFields()); + $this->assertSame([['field' => 'bar', 'order' => SortOrderEnum::Descending]], $requestParser->getSortFields()); $queryParameters = ['sort' => 'foo,-bar']; $requestParser = new RequestParser($selfLink='', $queryParameters); - $this->assertSame([['field' => 'foo', 'order' => RequestParser::SORT_ASCENDING], ['field' => 'bar', 'order' => RequestParser::SORT_DESCENDING]], $requestParser->getSortFields()); + $this->assertSame([['field' => 'foo', 'order' => SortOrderEnum::Ascending], ['field' => 'bar', 'order' => SortOrderEnum::Descending]], $requestParser->getSortFields()); } public function testGetSortFields_Raw() { diff --git a/tests/helpers/TestableNonInterfaceRequestInterface.php b/tests/helpers/TestableNonInterfaceRequestInterface.php index e2d26da..1918c52 100644 --- a/tests/helpers/TestableNonInterfaceRequestInterface.php +++ b/tests/helpers/TestableNonInterfaceRequestInterface.php @@ -1,5 +1,7 @@ setResource(new ResourceObject('user', 42)); $this->validateToOneRelationshipArray($relationshipObject->toArray()); } public function testConstructor_ToMany() { - $relationshipObject = new RelationshipObject(RelationshipObject::TO_MANY); + $relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToMany); $relationshipObject->addResource(new ResourceObject('user', 42)); $this->validateToManyRelationshipArray($relationshipObject->toArray()); } - public function testConstructor_UnknownType() { - $this->expectException(InputException::class); - - $relationshipObject = new RelationshipObject('foo'); - } - public function testFromAnything_WithResourceObject() { $resourceObject = new ResourceObject('user', 42); $resourceObject->addMeta('foo', 'bar'); @@ -80,7 +77,7 @@ public function testFromAnything_WithUnknownType() { public function testFromResource_ToMany() { $resourceObject = new ResourceObject('user', 42); - $type = RelationshipObject::TO_MANY; + $type = RelationshipTypeEnum::ToMany; $relationshipObject = RelationshipObject::fromResource($resourceObject, $links=[], $meta=[], $type); @@ -137,7 +134,7 @@ public function testFromCollectionDocument_WithMeta() { } public function testSetSelfLink_HappyPath() { - $relationshipObject = new RelationshipObject(RelationshipObject::TO_ONE); + $relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToOne); $relationshipObject->setSelfLink('https://jsonapi.org'); $array = $relationshipObject->toArray(); @@ -148,7 +145,7 @@ public function testSetSelfLink_HappyPath() { } public function testSetRelatedLink_HappyPath() { - $relationshipObject = new RelationshipObject(RelationshipObject::TO_ONE); + $relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToOne); $relationshipObject->setRelatedLink('https://jsonapi.org'); $array = $relationshipObject->toArray(); @@ -159,7 +156,7 @@ public function testSetRelatedLink_HappyPath() { } public function testSetPaginationLinks_HappyPath() { - $relationshipObject = new RelationshipObject(RelationshipObject::TO_MANY); + $relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToMany); $baseUrl = 'https://jsonapi.org/?page='; $relationshipObject->setPaginationLinks($baseUrl.'prev', $baseUrl.'next', $baseUrl.'first', $baseUrl.'last'); @@ -179,7 +176,7 @@ public function testSetPaginationLinks_HappyPath() { } public function testSetPaginationLinks_BlockedOnToOne() { - $relationshipObject = new RelationshipObject(RelationshipObject::TO_ONE); + $relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToOne); $this->expectException(InputException::class); @@ -187,7 +184,7 @@ public function testSetPaginationLinks_BlockedOnToOne() { } public function testAddMeta_HappyPath() { - $relationshipObject = new RelationshipObject(RelationshipObject::TO_ONE); + $relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToOne); $this->assertTrue($relationshipObject->isEmpty()); @@ -205,7 +202,7 @@ public function testAddMeta_HappyPath() { public function testHasResource_ToMany() { $resourceObject = new ResourceObject('user', 42); - $relationshipObject = new RelationshipObject(RelationshipObject::TO_MANY); + $relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToMany); $relationshipObject->addResource($resourceObject); $this->assertTrue($relationshipObject->hasResource($resourceObject)); @@ -214,7 +211,7 @@ public function testHasResource_ToMany() { } public function testGetContainedResources_SkipsResourceIdentifierObjects() { - $relationshipObject = new RelationshipObject(RelationshipObject::TO_MANY); + $relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToMany); $resourceIdentifierObject = new ResourceIdentifierObject('user', 24); $resourceObjectIdentifierOnly = new ResourceObject('user', 42); $resourceObjectWithAttributes = new ResourceObject('user', 42); @@ -236,14 +233,14 @@ public function testGetContainedResources_SkipsResourceIdentifierObjects() { } public function testSetResource_HappyPath() { - $relationshipObject = new RelationshipObject(RelationshipObject::TO_ONE); + $relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToOne); $relationshipObject->setResource(new ResourceObject('user', 42)); $this->validateToOneRelationshipArray($relationshipObject->toArray()); } public function testSetResource_RequiresToOneType() { - $relationshipObject = new RelationshipObject(RelationshipObject::TO_MANY); + $relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToMany); $this->expectException(InputException::class); @@ -251,14 +248,14 @@ public function testSetResource_RequiresToOneType() { } public function testAddResource_HappyPath() { - $relationshipObject = new RelationshipObject(RelationshipObject::TO_MANY); + $relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToMany); $relationshipObject->addResource(new ResourceObject('user', 42)); $this->validateToManyRelationshipArray($relationshipObject->toArray()); } public function testAddResource_RequiresToOneType() { - $relationshipObject = new RelationshipObject(RelationshipObject::TO_ONE); + $relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToOne); $this->expectException(InputException::class); @@ -266,7 +263,7 @@ public function testAddResource_RequiresToOneType() { } public function testAddLinkObject_HappyPath() { - $relationshipObject = new RelationshipObject(RelationshipObject::TO_ONE); + $relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToOne); $this->assertTrue($relationshipObject->isEmpty()); @@ -284,7 +281,7 @@ public function testAddLinkObject_HappyPath() { } public function testToArray_EmptyResource() { - $relationshipObject = new RelationshipObject(RelationshipObject::TO_ONE); + $relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToOne); $array = $relationshipObject->toArray(); @@ -293,7 +290,7 @@ public function testToArray_EmptyResource() { } public function testToArray_EmptyResources() { - $relationshipObject = new RelationshipObject(RelationshipObject::TO_MANY); + $relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToMany); $array = $relationshipObject->toArray(); @@ -302,7 +299,7 @@ public function testToArray_EmptyResources() { } public function testIsEmpty_WithAtMembers() { - $relationshipObject = new RelationshipObject(RelationshipObject::TO_ONE); + $relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToOne); $this->assertTrue($relationshipObject->isEmpty()); @@ -315,7 +312,7 @@ public function testIsEmpty_WithAtMembers() { * @group Extensions */ public function testIsEmpty_WithExtensionMembers() { - $relationshipObject = new RelationshipObject(RelationshipObject::TO_ONE); + $relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToOne); $this->assertTrue($relationshipObject->isEmpty()); diff --git a/tests/objects/RelationshipsObjectTest.php b/tests/objects/RelationshipsObjectTest.php index a1eb510..9c98b61 100644 --- a/tests/objects/RelationshipsObjectTest.php +++ b/tests/objects/RelationshipsObjectTest.php @@ -1,7 +1,10 @@ addRelationshipObject($key='foo', $relationshipObject); diff --git a/tests/objects/ResourceIdentifierObjectTest.php b/tests/objects/ResourceIdentifierObjectTest.php index b7eb1ea..5724491 100644 --- a/tests/objects/ResourceIdentifierObjectTest.php +++ b/tests/objects/ResourceIdentifierObjectTest.php @@ -1,5 +1,7 @@ setResource(new ResourceObject('user', 42)); $resourceObject = new ResourceObject('user', 24); @@ -151,7 +154,7 @@ public function testAddRelationshipObject_HappyPath() { } public function testAddRelationshipObject_BlockDrosteEffect() { - $relationshipObject = new RelationshipObject(RelationshipObject::TO_ONE); + $relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToOne); $relationshipObject->setResource(new ResourceObject('user', 42)); $resourceObject = new ResourceObject('user', 42); diff --git a/tests/profiles/CursorPaginationProfileTest.php b/tests/profiles/CursorPaginationProfileTest.php index 41fa3ef..5b9a8c2 100644 --- a/tests/profiles/CursorPaginationProfileTest.php +++ b/tests/profiles/CursorPaginationProfileTest.php @@ -1,5 +1,7 @@