diff --git a/src/Drivers/Standard/QueryBuilder.php b/src/Drivers/Standard/QueryBuilder.php index 8de3476f..756ba79b 100644 --- a/src/Drivers/Standard/QueryBuilder.php +++ b/src/Drivers/Standard/QueryBuilder.php @@ -72,6 +72,9 @@ public function buildQuery($query, Request $request) $actionMethod = $request->route()->getActionMethod(); if (!$this->intermediateMode) { + $this->applyIncludesToQuery($query, $request); + $this->applyAggregatesToQuery($query, $request); + if (in_array($actionMethod, ['index', 'search', 'show'])) { if ($actionMethod === 'search') { $this->applyScopesToQuery($query, $request); @@ -81,9 +84,6 @@ public function buildQuery($query, Request $request) } $this->applySoftDeletesToQuery($query, $request); } - - $this->applyIncludesToQuery($query, $request); - $this->applyAggregatesToQuery($query, $request); } return $query; diff --git a/tests/Fixtures/app/Models/Post.php b/tests/Fixtures/app/Models/Post.php index d44a3ca6..f70fc8a9 100644 --- a/tests/Fixtures/app/Models/Post.php +++ b/tests/Fixtures/app/Models/Post.php @@ -94,11 +94,28 @@ public function scopePublished(Builder $query) return $query->where('publish_at', '<', Carbon::now()); } + /** + * @param Builder $query + * @param string $dateTime + * @return Builder|\Illuminate\Database\Query\Builder + */ public function scopePublishedAt(Builder $query, string $dateTime) { return $query->where('publish_at', $dateTime); } + /** + * @param Builder $query + * @param string $direction + * @return Builder|\Illuminate\Database\Query\Builder + */ + public function scopeOrderComments(Builder $query, string $direction = 'asc') + { + return $query->with(['comments' => function (MorphMany $query) use ($direction) { + $query->orderBy('created_at', $direction); + }]); + } + /** * @param Builder $query * @return Builder|\Illuminate\Database\Query\Builder diff --git a/tests/Unit/Drivers/Standard/QueryBuilderTest.php b/tests/Unit/Drivers/Standard/QueryBuilderTest.php index 570bb130..1cadc234 100644 --- a/tests/Unit/Drivers/Standard/QueryBuilderTest.php +++ b/tests/Unit/Drivers/Standard/QueryBuilderTest.php @@ -2,6 +2,7 @@ namespace Orion\Tests\Unit\Drivers\Standard; +use Carbon\Carbon; use Illuminate\Routing\Route; use Mockery; use Orion\Drivers\Standard\ParamsValidator; @@ -9,6 +10,7 @@ use Orion\Drivers\Standard\RelationsResolver; use Orion\Drivers\Standard\SearchBuilder; use Orion\Http\Requests\Request; +use Orion\Tests\Fixtures\App\Models\Comment; use Orion\Tests\Fixtures\App\Models\Post; use Orion\Tests\Fixtures\App\Models\Team; use Orion\Tests\Fixtures\App\Models\User; @@ -127,6 +129,51 @@ function () { $this->assertSame($postA->id, $posts->first()->id); } + /** @test */ + public function include_relations_do_not_overwrite_scopes_with_same_relations(): void + { + $request = new Request(['include' => 'comments']); + $request->setRouteResolver( + function () { + return new Route('POST', '/api/posts/search', [ControllerStub::class, 'search']); + } + ); + $request->query->set( + 'scopes', + [ + ['name' => 'orderComments', 'parameters' => ['asc']], + ] + ); + + $post = factory(Post::class)->create(); + + $commentA = factory(Comment::class)->make(['created_at' => Carbon::parse('2019-01-01 09:35:14')]); + $commentA->commentable()->associate($post); + $commentA->save(); + + $commentB = factory(Comment::class)->make(['created_at' => Carbon::parse('2018-01-01 09:35:14')]); + $commentB->commentable()->associate($post); + $commentB->save(); + + $query = Post::query(); + + $queryBuilder = new QueryBuilder( + Post::class, + new ParamsValidator(['orderComments'], [], [], [], ['comments']), + new RelationsResolver(['comments'], []), + new SearchBuilder([]) + ); + $queryBuilder->buildQuery($query, $request); + + $posts = $query->get(); + + $dates = $posts->first()->comments->map(function(Comment $comment) { + return $comment->created_at; + }); + + $this->assertTrue(Carbon::parse($dates->first())->isBefore(Carbon::parse($dates->last()))); + } + /** @test */ public function applying_model_level_fields_filters_with_singular_values(): void {