10000 Merge branch 'hotfix/0.11.3' · startupengine/laravel-json-api@145fcb2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 145fcb2

Browse files
committed
Merge branch 'hotfix/0.11.3'
2 parents 6ea8203 + 5851631 commit 145fcb2

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
All notable changes to this project will be documented in this file. This project adheres to
33
[Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/).
44

5+
## [0.11.3] - 2017-12-01
6+
7+
### Fixed
8+
- [#125](https://github.com/cloudcreativity/laravel-json-api/issues/125)
9+
Refresh the Eloquent model after hydrating so that any cached relationships are reloaded.
10+
511
## [0.11.2] - 2017-11-14
612

713
### Added

src/Http/Controllers/EloquentController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ protected function commit(Model $model, ResourceObjectInterface $resource)
236236
$this->beforeCommit($model, $resource, $isUpdating);
237237

238238
$result = $this->save($model, $resource);
239+
$model->refresh();
239240

240241
if ($result) {
241242
$this->afterCommit($model, $resource, $isUpdating);

tests/Http/Controllers/PostsController.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55
use CloudCreativity\LaravelJsonApi\Http\Controllers\EloquentController;
66
use CloudCreativity\LaravelJsonApi\Tests\JsonApi\Posts\Hydrator;
77
use CloudCreativity\LaravelJsonApi\Tests\Models\Post;
8+
use Illuminate\Support\Collection;
89

910
class PostsController extends EloquentController
1011
{
1112

13+
/**
14+
* @var Collection
15+
*/
16+
private $invokables;
17+
1218
/**
1319
* PostsController constructor.
1420
*
@@ -17,5 +23,41 @@ class PostsController extends EloquentController
1723
public function __construct(Hydrator $hydrator)
1824
{
1925
parent::__construct(new Post(), $hydrator);
26+
$this->invokables = collect();
27+
}
28+
29+
/**
30+
* Set a callback for testing purposes.
31+
*
32+
* @param $event
33+
* @param callable $fn
34+
* @return $this
35+
*/
36+
public function on($event, callable $fn)
37+
{
38+
$this->invokables[$event] = $fn;
39+
40+
return $this;
41+
}
42+
43+
/**
44+
* @param array ...$args
45+
*/
46+
protected function saving(...$args)
47+
{
48+
$this->invoke('saving', $args);
49+
}
50+
51+
/**
52+
* Invoke a test callback.
53+
*
54+
* @param $event
55+
* @param array $args
56+
*/
57+
private function invoke($event, array $args)
58+
{
59+
if ($fn = $this->invokables->get($event)) {
60+
call_user_func_array($fn, $args);
61+
}
2062
}
2163
}

tests/Integration/Eloquent/PostsTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace CloudCreativity\LaravelJsonApi\Tests\Integration\Eloquent;
44

5+
use CloudCreativity\LaravelJsonApi\Tests\Http\Controllers\PostsController;
56
use CloudCreativity\LaravelJsonApi\Tests\Models\Post;
67
use CloudCreativity\LaravelJsonApi\Tests\Models\Tag;
78

@@ -86,6 +87,10 @@ public function testCreate()
8687
->assertCreateResponse($data);
8788

8889
$this->assertModelCreated($model, $id, ['title', 'slug', 'content', 'author_id']);
90+
$this->assertDatabaseHas('post_tag', [
91+
'post_id' => $id,
92+
'tag_id' => $tag->getKey(),
93+
]);
8994
}
9095

9196
/**
@@ -119,6 +124,49 @@ public function testUpdate()
119124
$this->assertModelPatched($model, $data['attributes'], ['content']);
120125
}
121126

127+
/**
128+
* Issue 125.
129+
*
130+
* If the model caches any of its relationships prior to a hydrator being invoked,
131+
* any changes to that relationship will not be serialized when the schema serializes
132+
* the model.
133+
*
134+
* @see https://github.com/cloudcreativity/laravel-json-api/issues/125
135+
*/
136+
public function testUpdateRefreshes()
137+
{
138+
/** @var PostsController $controller */
139+
$controller = $this->app->make(PostsController::class);
140+
$this->app->instance(PostsController::class, $controller);
141+
142+
$controller->on('saving', function ($model) {
143+
$model->tags; // causes the model to cache the tags relationship
144+
});
145+
146+
$model = $this->createPost();
147+
/** @var Tag $tag */
148+
$tag = factory(Tag::class)->create();
149+
150+
$data = [
151+
'type' => 'posts',
152+
'id' => (string) $model->getKey(),
153+
'relationships' => [
154+
'tags' => [
155+
'data' => [
156+
['type' => 'tags', 'id' => (string) $tag->getKey()],
157+
],
158+
],
159+
],
160+
];
161+
162+
$this->doUpdate($data)->assertUpdateResponse($data);
163+
164+
$this->assertDatabaseHas('post_tag', [
165+
'post_id' => $model->getKey(),
166+
'tag_id' => $tag->getKey(),
167+
]);
168+
}
169+
122170
/**
123171
* Test the delete resource route.
124172
*/

0 commit comments

Comments
 (0)
0