From c97646e266a52199a15d3286a4d7ed91d177ef56 Mon Sep 17 00:00:00 2001 From: EK Date: Sun, 15 Apr 2018 13:46:02 +0300 Subject: [PATCH] Close #209 --- src/Document/Error.php | 2 ++ src/Encoder/Parser/Parser.php | 30 +++++++++++++++++++++++++++--- tests/Encoder/EncoderTest.php | 24 +++++++++++++++++++++++- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/Document/Error.php b/src/Document/Error.php index 87b158c3..0be61452 100644 --- a/src/Document/Error.php +++ b/src/Document/Error.php @@ -133,6 +133,8 @@ public function getLinks(): ?array * @param LinkInterface|null $link * * @return self + * + * @SuppressWarnings(PHPMD.ElseExpression) */ public function setLink(string $name, ?LinkInterface $link): self { diff --git a/src/Encoder/Parser/Parser.php b/src/Encoder/Parser/Parser.php index 298ad5dc..8accf9f1 100644 --- a/src/Encoder/Parser/Parser.php +++ b/src/Encoder/Parser/Parser.php @@ -70,16 +70,32 @@ class Parser implements ParserInterface, LoggerAwareInterface { use LoggerAwareTrait; + /** @deprecated Use `MSG_GET_SCHEMA_FAILED_FOR_RESOURCE_AT_XXX` instead + * Message code. + */ + const MSG_SCHEME_NOT_REGISTERED = self::MSG_GET_SCHEMA_FAILED_FOR_RESOURCE_AT_PATH; + /** * Message code. */ - const MSG_SCHEME_NOT_REGISTERED = 0; + const MSG_GET_SCHEMA_FAILED_FOR_RESOURCE_AT_ROOT = 0; + + /** + * Message code. + */ + const MSG_GET_SCHEMA_FAILED_FOR_RESOURCE_AT_PATH = self::MSG_GET_SCHEMA_FAILED_FOR_RESOURCE_AT_ROOT + 1; /** * Default messages. */ const MESSAGES = [ - self::MSG_SCHEME_NOT_REGISTERED => 'Schema is not registered for a resource at path \'%s\'.', + self::MSG_GET_SCHEMA_FAILED_FOR_RESOURCE_AT_ROOT => + 'Getting Schema for a top-level resource of type `%s` failed. ' . + 'Please check you have added a Schema for this type.', + + self::MSG_GET_SCHEMA_FAILED_FOR_RESOURCE_AT_PATH => + 'Getting Schema for a resource of type `%s` at path `%s` failed. ' . + 'Please check you have added a Schema for this type.', ]; /** @@ -291,13 +307,21 @@ protected function analyzeData($data): array * @return SchemaInterface * * @SuppressWarnings(PHPMD.StaticAccess) + * @SuppressWarnings(PHPMD.ElseExpression) */ private function getSchema($resource, StackFrameReadOnlyInterface $frame): SchemaInterface { try { $schema = $this->container->getSchema($resource); } catch (InvalidArgumentException $exception) { - $message = _($this->messages[self::MSG_SCHEME_NOT_REGISTERED], $frame->getPath()); + $path = $frame->getPath(); + $typeName = is_object($resource) === true ? get_class($resource) : gettype($resource); + if ($path === null) { + $message = _($this->messages[static::MSG_GET_SCHEMA_FAILED_FOR_RESOURCE_AT_ROOT], $typeName); + } else { + $message = _($this->messages[static::MSG_GET_SCHEMA_FAILED_FOR_RESOURCE_AT_PATH], $typeName, $path); + } + throw new InvalidArgumentException($message, 0, $exception); } diff --git a/tests/Encoder/EncoderTest.php b/tests/Encoder/EncoderTest.php index 58cf54b2..3a5f3d41 100644 --- a/tests/Encoder/EncoderTest.php +++ b/tests/Encoder/EncoderTest.php @@ -858,7 +858,29 @@ public function testEncodeWithRelationshipRelatedLink() /** * Test encode unrecognized resource (no registered Schema). */ - public function testEncodeUnrecognizedResource() + public function testEncodeUnrecognizedResourceAtRoot() + { + $author = Author::instance(9, 'Dan', 'Gebhardt'); + + /** @var InvalidArgumentException $catch */ + $catch = null; + try { + Encoder::instance([ + Post::class => PostSchema::class, + ], $this->encoderOptions)->encodeData($author); + } catch (InvalidArgumentException $exception) { + $catch = $exception; + } + + $this->assertNotNull($catch); + $this->assertContains('top-level', $catch->getMessage()); + $this->assertNotNull($catch->getPrevious()); + } + + /** + * Test encode unrecognized resource (no registered Schema). + */ + public function testEncodeUnrecognizedResourceInRelationship() { $author = Author::instance(9, 'Dan', 'Gebhardt'); $post = Post::instance(1, 'Title', 'Body', null, [Comment::instance(5, 'First!', $author)]);