From d6bcc827aee2e3bd8de56eac7658328c2d60f42b Mon Sep 17 00:00:00 2001 From: Rodrigo Pedra Brum Date: Thu, 21 Jun 2018 00:02:41 -0300 Subject: [PATCH] Use value() helper in whenLoaded() --- .../ConditionallyLoadsAttributes.php | 2 +- ...AuthorResourceWithOptionalRelationship.php | 21 +++++++++++++++ .../Fixtures/PostResourceWithOptionalData.php | 4 +++ tests/Integration/Http/ResourceTest.php | 26 +++++++++++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 tests/Integration/Http/Fixtures/AuthorResourceWithOptionalRelationship.php diff --git a/src/Illuminate/Http/Resources/ConditionallyLoadsAttributes.php b/src/Illuminate/Http/Resources/ConditionallyLoadsAttributes.php index 6c9ba02fbde2..1aa61e6d82a5 100644 --- a/src/Illuminate/Http/Resources/ConditionallyLoadsAttributes.php +++ b/src/Illuminate/Http/Resources/ConditionallyLoadsAttributes.php @@ -141,7 +141,7 @@ protected function whenLoaded($relationship, $value = null, $default = null) } if (! $this->resource->relationLoaded($relationship)) { - return $default; + return value($default); } if (func_num_args() === 1) { diff --git a/tests/Integration/Http/Fixtures/AuthorResourceWithOptionalRelationship.php b/tests/Integration/Http/Fixtures/AuthorResourceWithOptionalRelationship.php new file mode 100644 index 000000000000..d51d345d5450 --- /dev/null +++ b/tests/Integration/Http/Fixtures/AuthorResourceWithOptionalRelationship.php @@ -0,0 +1,21 @@ + $this->name, + 'posts_count' => $this->whenLoaded('posts', function () { + return $this->posts->count().' posts'; + }, function () { + return 'not loaded'; + }), + 'latest_post_title' => $this->whenLoaded('posts', function () { + return optional($this->posts->first())->title ?: 'no posts yet'; + }, 'not loaded'), + ]; + } +} diff --git a/tests/Integration/Http/Fixtures/PostResourceWithOptionalData.php b/tests/Integration/Http/Fixtures/PostResourceWithOptionalData.php index 86bb515a18af..8d110a6e69cf 100644 --- a/tests/Integration/Http/Fixtures/PostResourceWithOptionalData.php +++ b/tests/Integration/Http/Fixtures/PostResourceWithOptionalData.php @@ -15,6 +15,10 @@ public function toArray($request) 'third' => $this->when(true, function () { return 'value'; }), + 'fourth' => $this->when(false, 'value', 'default'), + 'fifth' => $this->when(false, 'value', function () { + return 'default'; + }), ]; } } diff --git a/tests/Integration/Http/ResourceTest.php b/tests/Integration/Http/ResourceTest.php index 52988619c9c6..28fa76242e1f 100644 --- a/tests/Integration/Http/ResourceTest.php +++ b/tests/Integration/Http/ResourceTest.php @@ -20,6 +20,7 @@ use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalData; use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalMerging; use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalRelationship; +use Illuminate\Tests\Integration\Http\Fixtures\AuthorResourceWithOptionalRelationship; use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalPivotRelationship; /** @@ -88,6 +89,8 @@ public function test_resources_may_have_optional_values() 'id' => 5, 'second' => 'value', 'third' => 'value', + 'fourth' => 'default', + 'fifth' => 'default', ], ]); } @@ -192,6 +195,29 @@ public function test_resources_may_shows_null_for_loaded_relationship_with_value ]); } + public function test_resources_may_have_optional_relationships_with_default_values() + { + Route::get('/', function () { + return new AuthorResourceWithOptionalRelationship(new Author([ + 'name' => 'jrrmartin', + ])); + }); + + $response = $this->withoutExceptionHandling()->get( + '/', ['Accept' => 'application/json'] + ); + + $response->assertStatus(200); + + $response->assertExactJson([ + 'data' => [ + 'name' => 'jrrmartin', + 'posts_count' => 'not loaded', + 'latest_post_title' => 'not loaded', + ], + ]); + } + public function test_resources_may_have_optional_pivot_relationships() { Route::get('/', function () {