Skip to content

Commit

Permalink
Merge pull request #247 from bearsunday/patch
Browse files Browse the repository at this point in the history
Fix link override failed test
  • Loading branch information
koriym committed Aug 12, 2017
2 parents 6d5e6d2 + d714c5b commit 45e5b49
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/Provide/Representation/HalRenderer.php
Expand Up @@ -216,8 +216,10 @@ private function getHalLink(array $body, array $methodAnnotations, Hal $hal)
$hal->addLink($annotation->rel, $reverseUri);
}
if (isset($body['_links'])) {
foreach ($body['_links'] as $rel => $annotation) {
$hal->addLink($rel, $annotation);
foreach ($body['_links'] as $rel => $link) {
$attr = $link;
unset($attr['href']);
$hal->addLink($rel, $link['href'], $attr);
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions tests/Fake/fake-app/src/Resource/App/Hal.php
@@ -0,0 +1,27 @@
<?php
namespace FakeVendor\HelloWorld\Resource\App;

use BEAR\Resource\ResourceObject;

class Hal extends ResourceObject
{
public $body = [
'message' => 'Welcome',
'_links' => [
'self' => [
'href' => '/',
],
'curies' => [
'href' => 'http://localhost:8080/docs/{?rel}',
'name' => 'pt',
'templated' => true
],
'pt:todo' => ['href' => '/todo']
]
];

public function onGet()
{
return $this;
}
}
10 changes: 7 additions & 3 deletions tests/Fake/fake-app/src/Resource/App/Post.php
Expand Up @@ -20,9 +20,13 @@ class Post extends ResourceObject
*/
public function onGet($id)
{
$this['id'] = $id;
$this['name'] = 'user_' .$id;
$this['_links'] =['test' => '/test'];
$this->body = [
'id' => $id,
'name' => 'user_' .$id,
'_links' => [
'test' => ['href' => '/test']
]
];

return $this;
}
Expand Down
26 changes: 26 additions & 0 deletions tests/Provide/Representation/HalRendererTest.php
Expand Up @@ -192,4 +192,30 @@ public function test201Created()
$this->assertSame(201, $ro->code);
$this->assertSame('/post?id=10', $ro->headers['Location']);
}

public function testLinksAlreadyExists()
{
$ro = $this->resource->get->uri('app://self/hal')->eager->request();
$result = (string) $ro;
$expect = '{
"message": "Welcome",
"_links": {
"self": {
"href": "/"
},
"curies": [
{
"href": "http://localhost:8080/docs/{?rel}",
"name": "pt",
"templated": true
}
],
"pt:todo": {
"href": "/todo"
}
}
}
';
$this->assertSame($expect, $result);
}
}

0 comments on commit 45e5b49

Please sign in to comment.