From 7582f1f57474831a1b3e703d34e1b89b877f8bae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20S=2E=20Campos?= Date: Sat, 2 Dec 2023 19:28:30 -0500 Subject: [PATCH] feature: install spatie/laravel-json-api-paginate and support current_sort --- composer.json | 1 + composer.lock | 69 ++++++++- config/json-api-paginate.php | 58 ++++++++ .../Controllers/PublicQuotesController.php | 8 +- .../Queries/PublicQuoteIndexQuery.php | 1 + .../Quotes/Controllers/QuoteController.php | 8 +- .../Api/Quotes/Queries/IndexQuoteQuery.php | 1 + .../Ratings/Controllers/RatingController.php | 10 +- .../Api/Ratings/Queries/RatingIndexQuery.php | 1 + .../Api/Users/Controllers/UserController.php | 8 +- src/App/Api/Users/Queries/UserIndexQuery.php | 3 +- .../Controllers/ActivityController.php | 8 +- .../Users/Controllers/UserController.php | 8 +- .../ApiAdmin/Users/Queries/UserIndexQuery.php | 3 +- .../Web/Quotes/Queries/QuoteIndexQuery.php | 3 +- .../Metadata/GetQueryMetaDataAction.php | 15 ++ .../Api/PublicQuotes/IndexPublicQuoteTest.php | 137 +++++++++++++++--- tests/Feature/Api/Quotes/IndexQuoteTest.php | 2 +- .../Feature/Api/Ratings/IndexRatingsTest.php | 2 +- .../Api/Users/IndexUserControllerTest.php | 2 +- .../IndexActivityControllerTest.php | 4 +- .../Feature/ApiAdmin/Users/IndexUserTest.php | 2 +- .../Web/Quotes/QuoteControllerTest.php | 2 +- 23 files changed, 314 insertions(+), 42 deletions(-) create mode 100644 config/json-api-paginate.php create mode 100644 src/Support/Metadata/GetQueryMetaDataAction.php diff --git a/composer.json b/composer.json index a95c9ee..3debc99 100644 --- a/composer.json +++ b/composer.json @@ -35,6 +35,7 @@ "rebing/graphql-laravel": "^9.1", "spatie/laravel-activitylog": "^4.7", "spatie/laravel-data": "^2.1", + "spatie/laravel-json-api-paginate": "^1.13", "spatie/laravel-model-states": "^2.3", "spatie/laravel-permission": "^5.9", "spatie/laravel-query-builder": "5.1.2", diff --git a/composer.lock b/composer.lock index 8baebb5..f68dec5 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e878775ce451412fdc9a259532bc3b61", + "content-hash": "c7d64a8db14fa3038ac9da8df4753197", "packages": [ { "name": "aws/aws-crt-php", @@ -5761,6 +5761,71 @@ ], "time": "2023-01-24T08:23:58+00:00" }, + { + "name": "spatie/laravel-json-api-paginate", + "version": "1.13.0", + "source": { + "type": "git", + "url": "https://github.com/spatie/laravel-json-api-paginate.git", + "reference": "e3cc9b9505e998da312ea87570e215ecd40a005b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/laravel-json-api-paginate/zipball/e3cc9b9505e998da312ea87570e215ecd40a005b", + "reference": "e3cc9b9505e998da312ea87570e215ecd40a005b", + "shasum": "" + }, + "require": { + "illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0", + "php": "^7.3|^8.0" + }, + "require-dev": { + "orchestra/testbench": "^4.0|^5.0|^6.0|^7.0|^8.0", + "pestphp/pest": "^1.22", + "phpunit/phpunit": "^8.0|^9.0" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Spatie\\JsonApiPaginate\\JsonApiPaginateServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Spatie\\JsonApiPaginate\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Freek Van der Herten", + "email": "freek@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + } + ], + "description": "A paginator that plays nice with the JSON API spec", + "homepage": "https://github.com/spatie/laravel-json-api-paginate", + "keywords": [ + "laravel-json-api-paginate", + "spatie" + ], + "support": { + "source": "https://github.com/spatie/laravel-json-api-paginate/tree/1.13.0" + }, + "funding": [ + { + "url": "https://spatie.be/open-source/support-us", + "type": "custom" + } + ], + "time": "2023-03-27T13:33:07+00:00" + }, { "name": "spatie/laravel-model-states", "version": "2.6.0", @@ -15267,5 +15332,5 @@ "php": "^8.1" }, "platform-dev": [], - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.6.0" } diff --git a/config/json-api-paginate.php b/config/json-api-paginate.php new file mode 100644 index 0000000..f4ba1b0 --- /dev/null +++ b/config/json-api-paginate.php @@ -0,0 +1,58 @@ + 50, + + /* + * The default number of results that will be returned + * when using the JSON API paginator. + */ + 'default_size' => 20, + + /* + * The key of the page[x] query string parameter for page number. + */ + 'number_parameter' => 'number', + + /* + * The key of the page[x] query string parameter for page size. + */ + 'size_parameter' => 'size', + + /* + * The key of the page[x] query string parameter for cursor. + */ + 'cursor_parameter' => 'cursor', + + /* + * The name of the macro that is added to the Eloquent query builder. + */ + 'method_name' => 'jsonPaginate', + + /* + * If you only need to display Next and Previous links, you may use + * simple pagination to perform a more efficient query. + */ + 'use_simple_pagination' => false, + + /* + * If you want to use cursor pagination, set this to true. + * This would override use_simple_pagination. + */ + 'use_cursor_pagination' => false, + + /* + * Here you can override the base url to be used in the link items. + */ + 'base_url' => null, + + /* + * The name of the query parameter used for pagination + */ + 'pagination_parameter' => 'page', +]; diff --git a/src/App/Api/PublicQuotes/Controllers/PublicQuotesController.php b/src/App/Api/PublicQuotes/Controllers/PublicQuotesController.php index fec1c29..4062adf 100644 --- a/src/App/Api/PublicQuotes/Controllers/PublicQuotesController.php +++ b/src/App/Api/PublicQuotes/Controllers/PublicQuotesController.php @@ -7,6 +7,7 @@ use App\Api\PublicQuotes\Resources\PublicQuoteResource; use App\Controller; use Illuminate\Http\Resources\Json\AnonymousResourceCollection; +use Support\Metadata\GetQueryMetaDataAction; /** @authenticated */ class PublicQuotesController extends Controller @@ -19,9 +20,12 @@ class PublicQuotesController extends Controller */ public function index(PublicQuoteIndexQuery $quoteQuery): AnonymousResourceCollection { - $quotes = $quoteQuery->paginate(); + $quotes = $quoteQuery + ->jsonPaginate() + ->withQueryString(); - return PublicQuoteResource::collection($quotes); + return PublicQuoteResource::collection($quotes) + ->additional(['meta' => (new GetQueryMetaDataAction())->__invoke($quoteQuery->getQuery())]); } /** diff --git a/src/App/Api/PublicQuotes/Queries/PublicQuoteIndexQuery.php b/src/App/Api/PublicQuotes/Queries/PublicQuoteIndexQuery.php index 366e6dc..70e0dc9 100644 --- a/src/App/Api/PublicQuotes/Queries/PublicQuoteIndexQuery.php +++ b/src/App/Api/PublicQuotes/Queries/PublicQuoteIndexQuery.php @@ -26,6 +26,7 @@ public function __construct(Request $request) $this->allowedFilters(['title', 'content', 'user_id']) ->allowedIncludes('user') + ->defaultSort('created_at') ->allowedSorts('id', 'title', 'created_at'); } } diff --git a/src/App/Api/Quotes/Controllers/QuoteController.php b/src/App/Api/Quotes/Controllers/QuoteController.php index 6d1d385..8602810 100644 --- a/src/App/Api/Quotes/Controllers/QuoteController.php +++ b/src/App/Api/Quotes/Controllers/QuoteController.php @@ -16,6 +16,7 @@ use Illuminate\Http\JsonResponse; use Illuminate\Http\Resources\Json\AnonymousResourceCollection; use Spatie\ModelStates\Exceptions\CouldNotPerformTransition; +use Support\Metadata\GetQueryMetaDataAction; /** @authenticated */ class QuoteController extends Controller @@ -28,9 +29,12 @@ class QuoteController extends Controller */ public function index(IndexQuoteQuery $quoteQuery): AnonymousResourceCollection { - $quotes = $quoteQuery->paginate(); + $quotes = $quoteQuery + ->jsonPaginate() + ->withQueryString(); - return QuoteResource::collection($quotes); + return QuoteResource::collection($quotes) + ->additional(['meta' => (new GetQueryMetaDataAction())->__invoke($quoteQuery->getQuery())]); } /** diff --git a/src/App/Api/Quotes/Queries/IndexQuoteQuery.php b/src/App/Api/Quotes/Queries/IndexQuoteQuery.php index c23751b..ad50ea2 100644 --- a/src/App/Api/Quotes/Queries/IndexQuoteQuery.php +++ b/src/App/Api/Quotes/Queries/IndexQuoteQuery.php @@ -29,6 +29,7 @@ public function __construct(Request $request) parent::__construct($query, $request); $this->allowedFilters(['title', 'content', 'state']) + ->defaultSort('created_at') ->allowedSorts('id', 'title', 'created_at'); } } diff --git a/src/App/Api/Ratings/Controllers/RatingController.php b/src/App/Api/Ratings/Controllers/RatingController.php index db18777..03cfff2 100644 --- a/src/App/Api/Ratings/Controllers/RatingController.php +++ b/src/App/Api/Ratings/Controllers/RatingController.php @@ -16,6 +16,7 @@ use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Http\JsonResponse; use Illuminate\Http\Resources\Json\AnonymousResourceCollection; +use Support\Metadata\GetQueryMetaDataAction; /** @authenticated */ class RatingController extends Controller @@ -26,11 +27,14 @@ class RatingController extends Controller * @bodyParam include string Include qualifier and rateable Example: qualifier,rateable * @bodyParam sort string Sort by fields Example: id,created_at */ - public function index(RatingIndexQuery $quoteQuery): AnonymousResourceCollection + public function index(RatingIndexQuery $ratingQuery): AnonymousResourceCollection { - $quotes = $quoteQuery->paginate(); + $quotes = $ratingQuery + ->jsonPaginate() + ->withQueryString(); - return RatingResource::collection($quotes); + return RatingResource::collection($quotes) + ->additional(['meta' => (new GetQueryMetaDataAction())->__invoke($ratingQuery->getQuery())]); } /** diff --git a/src/App/Api/Ratings/Queries/RatingIndexQuery.php b/src/App/Api/Ratings/Queries/RatingIndexQuery.php index aaec4ff..f04cb88 100644 --- a/src/App/Api/Ratings/Queries/RatingIndexQuery.php +++ b/src/App/Api/Ratings/Queries/RatingIndexQuery.php @@ -25,6 +25,7 @@ public function __construct(Request $request) $this->allowedFilters(['qualifier_type', 'rateable_type']) ->allowedIncludes(['qualifier', 'rateable']) + ->defaultSort('created_at') ->allowedSorts('id', 'created_at'); } } diff --git a/src/App/Api/Users/Controllers/UserController.php b/src/App/Api/Users/Controllers/UserController.php index b25e2b8..6b680d2 100644 --- a/src/App/Api/Users/Controllers/UserController.php +++ b/src/App/Api/Users/Controllers/UserController.php @@ -7,6 +7,7 @@ use App\Api\Users\Resources\UserResource; use App\Controller; use Illuminate\Http\Resources\Json\AnonymousResourceCollection; +use Support\Metadata\GetQueryMetaDataAction; /** @authenticated */ class UserController extends Controller @@ -19,9 +20,12 @@ class UserController extends Controller */ public function index(UserIndexQuery $userQuery): AnonymousResourceCollection { - $users = $userQuery->paginate(); + $users = $userQuery + ->jsonPaginate() + ->withQueryString(); - return UserResource::collection($users); + return UserResource::collection($users) + ->additional(['meta' => (new GetQueryMetaDataAction())->__invoke($userQuery->getQuery())]); } /** diff --git a/src/App/Api/Users/Queries/UserIndexQuery.php b/src/App/Api/Users/Queries/UserIndexQuery.php index 37d6c99..28cd3ae 100644 --- a/src/App/Api/Users/Queries/UserIndexQuery.php +++ b/src/App/Api/Users/Queries/UserIndexQuery.php @@ -21,6 +21,7 @@ public function __construct(Request $request) $this->allowedFilters(['id', 'name']) ->allowedIncludes('quotes') - ->allowedSorts('id', 'name'); + ->defaultSort('created_at') + ->allowedSorts('id', 'name', 'created_at'); } } diff --git a/src/App/ApiAdmin/Activities/Controllers/ActivityController.php b/src/App/ApiAdmin/Activities/Controllers/ActivityController.php index 53ab35e..9324c0e 100644 --- a/src/App/ApiAdmin/Activities/Controllers/ActivityController.php +++ b/src/App/ApiAdmin/Activities/Controllers/ActivityController.php @@ -12,6 +12,7 @@ use Illuminate\Http\Resources\Json\AnonymousResourceCollection; use Maatwebsite\Excel\Facades\Excel; use Spatie\Activitylog\Models\Activity; +use Support\Metadata\GetQueryMetaDataAction; use Symfony\Component\HttpFoundation\BinaryFileResponse; /** @authenticated */ @@ -27,9 +28,12 @@ public function index(ActivityIndexQuery $activityQuery): AnonymousResourceColle { $this->authorize('viewAny', Activity::class); - $activities = $activityQuery->paginate(); + $activities = $activityQuery + ->jsonPaginate() + ->withQueryString(); - return ActivityResource::collection($activities); + return ActivityResource::collection($activities) + ->additional(['meta' => (new GetQueryMetaDataAction())->__invoke($activityQuery->getQuery())]); } /** diff --git a/src/App/ApiAdmin/Users/Controllers/UserController.php b/src/App/ApiAdmin/Users/Controllers/UserController.php index 862f80f..751e9a6 100644 --- a/src/App/ApiAdmin/Users/Controllers/UserController.php +++ b/src/App/ApiAdmin/Users/Controllers/UserController.php @@ -12,6 +12,7 @@ use Illuminate\Http\JsonResponse; use Illuminate\Http\Resources\Json\AnonymousResourceCollection; use Illuminate\Support\Facades\Auth; +use Support\Metadata\GetQueryMetaDataAction; /** @authenticated */ class UserController extends Controller @@ -29,9 +30,12 @@ public function index(UserIndexQuery $userQuery): AnonymousResourceCollection { $this->authorize('viewAny', User::class); - $users = $userQuery->paginate(); + $users = $userQuery + ->jsonPaginate() + ->withQueryString(); - return UserResource::collection($users); + return UserResource::collection($users) + ->additional(['meta' => (new GetQueryMetaDataAction())->__invoke($userQuery->getQuery())]); } /** diff --git a/src/App/ApiAdmin/Users/Queries/UserIndexQuery.php b/src/App/ApiAdmin/Users/Queries/UserIndexQuery.php index a7e4b51..a66860c 100644 --- a/src/App/ApiAdmin/Users/Queries/UserIndexQuery.php +++ b/src/App/ApiAdmin/Users/Queries/UserIndexQuery.php @@ -24,6 +24,7 @@ public function __construct(Request $request) $this->allowedFilters(['id', 'name', AllowedFilter::trashed()]) ->allowedIncludes(['permissions', 'roles']) - ->allowedSorts('id', 'name'); + ->defaultSort('created_at') + ->allowedSorts('id', 'name', 'created_at'); } } diff --git a/src/App/Web/Quotes/Queries/QuoteIndexQuery.php b/src/App/Web/Quotes/Queries/QuoteIndexQuery.php index c131c0a..e9c4163 100644 --- a/src/App/Web/Quotes/Queries/QuoteIndexQuery.php +++ b/src/App/Web/Quotes/Queries/QuoteIndexQuery.php @@ -19,6 +19,7 @@ public function __construct(Request $request) parent::__construct($query, $request); $this->allowedFilters(['title', 'content']) - ->allowedSorts('title'); + ->defaultSort('created_at') + ->allowedSorts('title', 'created_at'); } } diff --git a/src/Support/Metadata/GetQueryMetaDataAction.php b/src/Support/Metadata/GetQueryMetaDataAction.php new file mode 100644 index 0000000..c7a5e3f --- /dev/null +++ b/src/Support/Metadata/GetQueryMetaDataAction.php @@ -0,0 +1,15 @@ + $query->orders, + ]; + } +} diff --git a/tests/Feature/Api/PublicQuotes/IndexPublicQuoteTest.php b/tests/Feature/Api/PublicQuotes/IndexPublicQuoteTest.php index 0bfdff2..cd2c4b2 100644 --- a/tests/Feature/Api/PublicQuotes/IndexPublicQuoteTest.php +++ b/tests/Feature/Api/PublicQuotes/IndexPublicQuoteTest.php @@ -7,10 +7,14 @@ use Illuminate\Support\Facades\DB; use Illuminate\Testing\Fluent\AssertableJson; use function Pest\Laravel\getJson; -use function PHPUnit\Framework\assertEquals; use function PHPUnit\Framework\assertLessThan; +/** + * Test chosen to test all query params as page[size], page[number] and metadata + */ beforeEach(function () { + config()->set('app.url', 'http://localhost'); + $this->user = User::factory()->create(); (new QuoteFactory)->setAmount(5)->withUser($this->user)->withState(Published::$name)->create(); @@ -21,17 +25,47 @@ it('can index', function () { getJson(route('api.public.quotes.index')) ->assertOk() - ->assertJsonStructure([ - 'data' => [ - '*' => ['id', 'title', 'content', 'state', 'average_rating', 'excerpt', 'created_at', 'updated_at']], - ]); + ->assertJson(function (AssertableJson $json) { + $json->has('data', 5) + ->has('data.0', function (AssertableJson $data) { + $data->whereAllType([ + 'id' => 'integer', + 'title' => 'string', + 'content' => 'string', + 'state' => 'string', + 'average_rating' => 'null', + 'excerpt' => 'string', + 'created_at' => 'string', + 'updated_at' => 'string', + ])->etc(); + }) + ->has('links', function (AssertableJson $links) { + $links->where('first', 'http://localhost/api/public/quotes?page%5Bnumber%5D=1') + ->where('last', 'http://localhost/api/public/quotes?page%5Bnumber%5D=1') + ->where('prev', null) + ->where('next', null); + }) + ->has('meta', function (AssertableJson $meta) { + $meta->where('current_page', 1) + ->where('from', 1) + ->where('last_page', 1) + ->where('current_sort.0.column', 'created_at') + ->where('current_sort.0.direction', 'asc') + ->has('links') + ->where('path', 'http://localhost/api/public/quotes') + ->where('per_page', 20) + ->where('to', 5) + ->where('total', 5); + }); + }); }); it('can filter by title', function () { /** @var Quote $quote */ - $quote = (new QuoteFactory)->withUser($this->user)->withState(Published::$name)->create([ - 'title' => 'Hamlet', - ]); + $quote = (new QuoteFactory) + ->withUser($this->user) + ->withState(Published::$name) + ->create(['title' => 'Hamlet']); getJson(route('api.public.quotes.index', ['filter[title]' => 'hamlet'])) ->assertJson(function (AssertableJson $json) use ($quote) { @@ -44,15 +78,16 @@ 'title' => 'string', ]) ->etc(); - })->etc(); + })->where('links.first', 'http://localhost/api/public/quotes?filter%5Btitle%5D=hamlet&page%5Bnumber%5D=1')->etc(); }); }); it('can filter by content', function () { /** @var Quote $quote */ - $quote = (new QuoteFactory)->withUser($this->user)->withState(Published::$name)->create([ - 'content' => 'Some text about something', - ]); + $quote = (new QuoteFactory) + ->withUser($this->user) + ->withState(Published::$name) + ->create(['content' => 'Some text about something']); getJson(route('api.public.quotes.index', ['filter[content]' => 'Some text about something'])) ->assertSuccessful() @@ -82,9 +117,7 @@ $json->has('data', 1) ->has('data.0', function (AssertableJson $data) use ($quote) { $data->where('id', $quote->getKey()) - ->whereAllType([ - 'id' => 'integer', - ]) + ->whereAllType(['id' => 'integer']) ->etc(); })->etc(); }); @@ -99,7 +132,7 @@ ->whereAllType([ 'user' => 'array', ])->etc(); - })->etc(); + })->where('links.first', 'http://localhost/api/public/quotes?include=user&page%5Bnumber%5D=1')->etc(); }); $newUser = User::factory()->create(); @@ -135,6 +168,76 @@ ->json('data'); assertLessThan($responseDataTwo[0]['id'], $responseDataTwo[4]['id']); + + getJson(route('api.public.quotes.index', ['sort' => 'id,title'])) + ->assertJson(function (AssertableJson $json) { + $json->where('links.first', 'http://localhost/api/public/quotes?sort=id%2Ctitle&page%5Bnumber%5D=1') + ->where('meta.current_sort.0.column', 'id') + ->where('meta.current_sort.1.direction', 'asc') + ->etc(); + }); +}); + +it('can sort by two columns', function () { + DB::enableQueryLog(); + + getJson(route('api.public.quotes.index', ['sort' => 'title,created_at'])) + ->assertSuccessful() + ->assertJson(function (AssertableJson $json) { + $json->where('links.first', 'http://localhost/api/public/quotes?sort=title%2Ccreated_at&page%5Bnumber%5D=1') + ->where('meta.current_sort.0.column', 'title') + ->where('meta.current_sort.0.direction', 'asc') + ->where('meta.current_sort.1.column', 'created_at') + ->where('meta.current_sort.1.direction', 'asc') + ->etc(); + }); + + expect(formatQueries(DB::getQueryLog())) + ->toHaveCount(2) + ->sequence( + fn ($query) => $query->toBe('select count(*) as aggregate from `quotes` where `state` = ?'), + fn ($query) => $query->toBe('select `id`, `title`, `content`, `state`, `average_score`, `user_id`, `created_at`, `updated_at` from `quotes` where `state` = ? order by `created_at` asc limit 20 offset 0'), + ); + + DB::disableQueryLog(); +}); + +it('can get by page size', function () { + (new QuoteFactory)->setAmount(5)->withUser($this->user)->withState(Published::$name)->create(); + + getJson(route('api.public.quotes.index', ['page[size]' => 5])) + ->assertOk() + ->assertJson(function (AssertableJson $json) { + $json->has('data', 5)->etc(); + }); + + getJson(route('api.public.quotes.index', ['page[size]' => 10])) + ->assertOk() + ->assertJson(function (AssertableJson $json) { + $json->has('data', 10)->etc(); + }); +}); + +it('can get by page number', function () { + (new QuoteFactory)->setAmount(18)->withUser($this->user)->withState(Published::$name)->create(); + + getJson(route('api.public.quotes.index', ['page[number]' => 1])) + ->assertOk() + ->assertJson(function (AssertableJson $json) { + $json->has('data', 20)->etc(); + }); + + getJson(route('api.public.quotes.index', ['page[number]' => 2])) + ->assertOk() + ->assertJson(function (AssertableJson $json) { + $json->has('data', 3)->etc(); + }); + + getJson(route('api.public.quotes.index', ['page' => 2])) // page is not valid param anymore + ->assertOk() + ->assertJson(function (AssertableJson $json) { + $json->has('data', 20)->etc(); + }); }); test('sql queries optimization test', function () { @@ -145,7 +248,7 @@ ->toHaveCount(2) ->sequence( fn ($query) => $query->toBe('select count(*) as aggregate from `quotes` where `state` = ?'), - fn ($query) => $query->toBe('select `id`, `title`, `content`, `state`, `average_score`, `user_id`, `created_at`, `updated_at` from `quotes` where `state` = ? limit 15 offset 0'), + fn ($query) => $query->toBe('select `id`, `title`, `content`, `state`, `average_score`, `user_id`, `created_at`, `updated_at` from `quotes` where `state` = ? order by `created_at` asc limit 20 offset 0'), ); DB::disableQueryLog(); diff --git a/tests/Feature/Api/Quotes/IndexQuoteTest.php b/tests/Feature/Api/Quotes/IndexQuoteTest.php index de9e54a..1e962da 100644 --- a/tests/Feature/Api/Quotes/IndexQuoteTest.php +++ b/tests/Feature/Api/Quotes/IndexQuoteTest.php @@ -125,7 +125,7 @@ ->toHaveCount(2) ->sequence( fn ($query) => $query->toBe('select count(*) as aggregate from `quotes` where `user_id` = ?'), - fn ($query) => $query->toBe('select `id`, `title`, `content`, `state`, `average_score`, `created_at`, `updated_at` from `quotes` where `user_id` = ? limit 15 offset 0'), + fn ($query) => $query->toBe('select `id`, `title`, `content`, `state`, `average_score`, `created_at`, `updated_at` from `quotes` where `user_id` = ? order by `created_at` asc limit 20 offset 0'), ); DB::disableQueryLog(); diff --git a/tests/Feature/Api/Ratings/IndexRatingsTest.php b/tests/Feature/Api/Ratings/IndexRatingsTest.php index 2b16663..b3cc54f 100644 --- a/tests/Feature/Api/Ratings/IndexRatingsTest.php +++ b/tests/Feature/Api/Ratings/IndexRatingsTest.php @@ -84,7 +84,7 @@ ->toHaveCount(2) ->sequence( fn ($query) => $query->toBe('select count(*) as aggregate from `ratings`'), - fn ($query) => $query->toBe('select `id`, `score`, `qualifier_id`, `qualifier_type`, `rateable_id`, `rateable_type`, `created_at`, `updated_at` from `ratings` limit 15 offset 0'), + fn ($query) => $query->toBe('select `id`, `score`, `qualifier_id`, `qualifier_type`, `rateable_id`, `rateable_type`, `created_at`, `updated_at` from `ratings` order by `created_at` asc limit 20 offset 0'), ); DB::disableQueryLog(); diff --git a/tests/Feature/Api/Users/IndexUserControllerTest.php b/tests/Feature/Api/Users/IndexUserControllerTest.php index 6e7bff2..d071d8f 100644 --- a/tests/Feature/Api/Users/IndexUserControllerTest.php +++ b/tests/Feature/Api/Users/IndexUserControllerTest.php @@ -108,7 +108,7 @@ ->toHaveCount(2) ->sequence( fn ($query) => $query->toBe('select count(*) as aggregate from `users` where `users`.`deleted_at` is null'), - fn ($query) => $query->toBe('select `id`, `name`, `email`, `created_at` from `users` where `users`.`deleted_at` is null limit 15 offset 0'), + fn ($query) => $query->toBe('select `id`, `name`, `email`, `created_at` from `users` where `users`.`deleted_at` is null order by `created_at` asc limit 20 offset 0'), ); DB::disableQueryLog(); diff --git a/tests/Feature/ApiAdmin/Activities/IndexActivityControllerTest.php b/tests/Feature/ApiAdmin/Activities/IndexActivityControllerTest.php index bd6554f..5c36d5c 100644 --- a/tests/Feature/ApiAdmin/Activities/IndexActivityControllerTest.php +++ b/tests/Feature/ApiAdmin/Activities/IndexActivityControllerTest.php @@ -72,10 +72,10 @@ fn ($query) => $query->toContain('select `roles`.*, `role_has_permissions`.`permission_id` as `pivot_permission_id`, `role_has_permissions`.`role_id` as `pivot_role_id` from `roles` inner join `role_has_permissions` on `roles`.`id` = `role_has_permissions`.`role_id` where `role_has_permissions`.`permission_id`'), fn ($query) => $query->toBe('select `permissions`.*, `model_has_permissions`.`model_id` as `pivot_model_id`, `model_has_permissions`.`permission_id` as `pivot_permission_id`, `model_has_permissions`.`model_type` as `pivot_model_type` from `permissions` inner join `model_has_permissions` on `permissions`.`id` = `model_has_permissions`.`permission_id` where `model_has_permissions`.`model_id` = ? and `model_has_permissions`.`model_type` = ?'), fn ($query) => $query->toBe('select count(*) as aggregate from `activity_log`'), - fn ($query) => $query->toBe('select `id`, `log_name`, `description`, `subject_type`, `subject_id`, `causer_type`, `causer_id`, `event`, `created_at`, `updated_at` from `activity_log` limit 15 offset 0'), + fn ($query) => $query->toBe('select `id`, `log_name`, `description`, `subject_type`, `subject_id`, `causer_type`, `causer_id`, `event`, `created_at`, `updated_at` from `activity_log` limit 20 offset 0'), // TODO: Validate cache in roles and permissions fn ($query) => $query->toBe('select count(*) as aggregate from `activity_log`'), - fn ($query) => $query->toBe('select `id`, `log_name`, `description`, `subject_type`, `subject_id`, `causer_type`, `causer_id`, `event`, `created_at`, `updated_at` from `activity_log` limit 15 offset 0'), + fn ($query) => $query->toBe('select `id`, `log_name`, `description`, `subject_type`, `subject_id`, `causer_type`, `causer_id`, `event`, `created_at`, `updated_at` from `activity_log` limit 20 offset 0'), ); DB::disableQueryLog(); diff --git a/tests/Feature/ApiAdmin/Users/IndexUserTest.php b/tests/Feature/ApiAdmin/Users/IndexUserTest.php index c6d5ad7..8875318 100644 --- a/tests/Feature/ApiAdmin/Users/IndexUserTest.php +++ b/tests/Feature/ApiAdmin/Users/IndexUserTest.php @@ -140,7 +140,7 @@ fn ($query) => $query->toContain('select `roles`.*, `role_has_permissions`.`permission_id` as `pivot_permission_id`, `role_has_permissions`.`role_id` as `pivot_role_id` from `roles` inner join `role_has_permissions` on `roles`.`id` = `role_has_permissions`.`role_id` where `role_has_permissions`.`permission_id` in'), fn ($query) => $query->toBe('select `permissions`.*, `model_has_permissions`.`model_id` as `pivot_model_id`, `model_has_permissions`.`permission_id` as `pivot_permission_id`, `model_has_permissions`.`model_type` as `pivot_model_type` from `permissions` inner join `model_has_permissions` on `permissions`.`id` = `model_has_permissions`.`permission_id` where `model_has_permissions`.`model_id` = ? and `model_has_permissions`.`model_type` = ?'), fn ($query) => $query->toBe('select count(*) as aggregate from `users` where `users`.`deleted_at` is null'), - fn ($query) => $query->toBe('select `id`, `name`, `email`, `deleted_at`, `created_at`, `updated_at` from `users` where `users`.`deleted_at` is null limit 15 offset 0'), + fn ($query) => $query->toBe('select `id`, `name`, `email`, `deleted_at`, `created_at`, `updated_at` from `users` where `users`.`deleted_at` is null order by `created_at` asc limit 20 offset 0'), ); DB::disableQueryLog(); diff --git a/tests/Feature/Web/Quotes/QuoteControllerTest.php b/tests/Feature/Web/Quotes/QuoteControllerTest.php index 8a9acea..2023239 100644 --- a/tests/Feature/Web/Quotes/QuoteControllerTest.php +++ b/tests/Feature/Web/Quotes/QuoteControllerTest.php @@ -25,7 +25,7 @@ expect(formatQueries(DB::getQueryLog())) ->toHaveCount(1) ->sequence( - fn ($query) => $query->toBe('select `title`, `content`, `created_at` from `quotes`'), + fn ($query) => $query->toBe('select `title`, `content`, `created_at` from `quotes` order by `created_at` asc'), ); DB::disableQueryLog();