Skip to content

Commit

Permalink
Close #209
Browse files Browse the repository at this point in the history
  • Loading branch information
neomerx committed Apr 15, 2018
1 parent d8aeae9 commit c97646e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/Document/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
30 changes: 27 additions & 3 deletions src/Encoder/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
];

/**
Expand Down Expand Up @@ -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);
}

Expand Down
24 changes: 23 additions & 1 deletion tests/Encoder/EncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)]);
Expand Down

0 comments on commit c97646e

Please sign in to comment.