Skip to content

Commit

Permalink
Merge 2a0ffdd into 7bf4ae6
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-trozowski committed Feb 13, 2021
2 parents 7bf4ae6 + 2a0ffdd commit c63dc20
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/ResourceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Mezzio\Hal;

use Mezzio\Hal\Metadata\AbstractMetadata;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;

Expand Down Expand Up @@ -122,12 +123,7 @@ public function fromObject(object $instance, ServerRequestInterface $request, in
throw Exception\InvalidObjectException::forNonObject($instance);
}

$class = get_class($instance);
if (! $this->metadataMap->has($class)) {
throw Exception\InvalidObjectException::forUnknownType($class);
}

$metadata = $this->metadataMap->get($class);
$metadata = $this->getClassMetadata($instance);
$metadataType = get_class($metadata);

if (! isset($this->strategies[$metadataType])) {
Expand All @@ -143,4 +139,19 @@ public function fromObject(object $instance, ServerRequestInterface $request, in
$depth
);
}

private function getClassMetadata(object $instance): AbstractMetadata
{
$class = get_class($instance);
if (! $this->metadataMap->has($class)) {
foreach (class_parents($instance) as $parent) {
if ($this->metadataMap->has($parent)) {
return $this->metadataMap->get($parent);
}
}
throw Exception\InvalidObjectException::forUnknownType($class);
}

return $this->metadataMap->get($class);
}
}
53 changes: 53 additions & 0 deletions test/ResourceGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use stdClass;

use function array_key_exists;
use function class_parents;

/**
* @todo Create tests for cases where resources embed other resources.
Expand Down Expand Up @@ -672,6 +673,9 @@ public function testGeneratorAcceptsOnePageWhenCollectionHasNoEmbedded(): void
public function testGeneratorRaisesExceptionForUnknownObjectType(): void
{
$this->metadataMap->has(self::class)->willReturn(false);
foreach (class_parents(self::class) as $parent) {
$this->metadataMap->has($parent)->willReturn(false);
}
$this->expectException(InvalidObjectException::class);
$this->expectExceptionMessage('not in metadata map');
$this->generator->fromObject($this, $this->request->reveal());
Expand Down Expand Up @@ -852,4 +856,53 @@ public function testUsesConfiguredRoutePlaceholderMapToSpecifyRouteParams(): voi
$self = $this->getLinkByRel('self', $resource);
$this->assertLink('self', '/api/foo-bar/XXXX-YYYY-ZZZZ/foo/BAR/bar/BAZ', $self);
}

public function testParentClassesAreUsedWhenInstanceMetadataDoesNotExist(): void
{
$instance = new TestAsset\InheritedClass();
$instance->id = 'XXXX-YYYY-ZZZZ';
$instance->foo = 'BAR';
$instance->bar = 'BAZ';

$metadata = new Metadata\RouteBasedResourceMetadata(
TestAsset\FooBar::class,
'foo-bar',
self::getObjectPropertyHydratorClass(),
'id',
[],
[
'id' => 'foo_bar_id',
'foo' => 'foo_value',
'bar' => 'bar_value',
]
);

$this->metadataMap->has(TestAsset\InheritedClass::class)->willReturn(false);
$this->metadataMap->has(TestAsset\InheritFooBar::class)->willReturn(false);
$this->metadataMap->has(TestAsset\FooBar::class)->willReturn(true);
$this->metadataMap->get(TestAsset\FooBar::class)->willReturn($metadata);

$hydratorClass = self::getObjectPropertyHydratorClass();

$this->hydrators->get($hydratorClass)->willReturn(new $hydratorClass());
$this->linkGenerator
->fromRoute(
'self',
$this->request->reveal(),
'foo-bar',
[
'foo_bar_id' => 'XXXX-YYYY-ZZZZ',
'foo_value' => 'BAR',
'bar_value' => 'BAZ',
]
)
->willReturn(new Link('self', '/api/foo-bar/XXXX-YYYY-ZZZZ/foo/BAR/bar/BAZ'));

$resource = $this->generator->fromObject($instance, $this->request->reveal());

$this->assertInstanceOf(HalResource::class, $resource);

$self = $this->getLinkByRel('self', $resource);
$this->assertLink('self', '/api/foo-bar/XXXX-YYYY-ZZZZ/foo/BAR/bar/BAZ', $self);
}
}
9 changes: 9 additions & 0 deletions test/TestAsset/InheritFooBar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace MezzioTest\Hal\TestAsset;

class InheritFooBar extends FooBar
{
}
9 changes: 9 additions & 0 deletions test/TestAsset/InheritedClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace MezzioTest\Hal\TestAsset;

class InheritedClass extends InheritFooBar
{
}

0 comments on commit c63dc20

Please sign in to comment.