Skip to content

Commit

Permalink
qa: add test to cover automated route parameter injection
Browse files Browse the repository at this point in the history
Adds a test to cover automated route parameter injection in generated URIs.
In doing so, I realized we need to specifically only use scalar values.

Signed-off-by: Matthew Weier O'Phinney <[email protected]>
  • Loading branch information
weierophinney committed Dec 18, 2020
1 parent db06ef2 commit a05bd84
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/ResourceGenerator/RouteBasedResourceStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ public function createResource(
$routeParams[$routeIdentifier] = $data[$resourceIdentifier];
}

// Inject all entity keys automatically into route parameters
// Inject all scalar entity keys automatically into route parameters
foreach ($data as $key => $value) {
$routeParams[$key] = $value;
if (is_scalar($value)) {
$routeParams[$key] = $value;
}
}

return new HalResource($data, [
Expand Down
46 changes: 46 additions & 0 deletions test/ResourceGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -764,4 +764,50 @@ public function testAddStrategyRaisesExceptionIfInvalidStrategyClass()
$this->expectExceptionMessage('does not exist, or does not implement');
$this->generator->addStrategy(TestMetadata::class, 'invalid-strategy');
}

public function testPassesAllScalarEntityPropertiesAsRouteParametersWhenGeneratingUri()
{
$instance = new TestAsset\FooBar;
$instance->id = 'XXXX-YYYY-ZZZZ';
$instance->foo = 'BAR';
$instance->bar = [
'value' => 'baz',
];

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

$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',
'id' => 'XXXX-YYYY-ZZZZ',
'foo' => 'BAR',
'test' => 'param',
]
)
->willReturn(new Link('self', '/api/foo-bar/XXXX-YYYY-ZZZZ'));

$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', $self);
}
}

0 comments on commit a05bd84

Please sign in to comment.