From 0411e1d73bc58813fe7258e5ef5ee0bcf4209a1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Mon, 16 Nov 2026 01:01:33 +0100 Subject: [PATCH] refactor(datatable): require column listing resolver dependency in DataTableService --- app/Providers/AppServiceProvider.php | 14 ++++++++++++++ app/Services/DataTableService.php | 9 +++------ .../GetPaginatedDiscountsQueryTest.php | 17 +++++++++++++++-- .../Queries/Faqs/GetPaginatedFaqsQueryTest.php | 9 ++++++++- .../GetPaginatedPickupExceptionsQueryTest.php | 7 ++++++- .../GetBoPaginatedProductsQueryTest.php | 7 ++++++- .../Ranges/GetPaginatedRangesQueryTest.php | 7 ++++++- .../Shops/GetPaginatedShopsQueryTest.php | 13 +++++++++++-- .../Slides/GetPaginatedSlidesQueryTest.php | 17 +++++++++++++++-- tests/Unit/Services/DataTableServiceTest.php | 6 +++--- 10 files changed, 87 insertions(+), 19 deletions(-) diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index ef5a68ff..d22bcd40 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -8,12 +8,17 @@ use App\Contracts\Executor; use App\Lib\PrivateS3Disk; use App\Lib\PublicS3Disk; use App\Services\ActionExecutorService; +use App\Services\DataTableService; use App\Services\IcsGeneratorService; use Carbon\CarbonInterface; +use Illuminate\Contracts\Foundation\Application; +use Illuminate\Contracts\Session\Session; use Illuminate\Database\Eloquent\Model; +use Illuminate\Http\Request; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Date; +use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\URL; use Illuminate\Support\Facades\Vite; use Illuminate\Support\ServiceProvider; @@ -64,6 +69,15 @@ class AppServiceProvider extends ServiceProvider Config::string('app.name', 'La Boutique de Cocotte'), ) ); + + $this->app->bind( + DataTableService::class, + fn (Application $app): DataTableService => new DataTableService( + request: $app->make(Request::class), + session: $app->make(Session::class), + columnListingResolver: static fn (string $table): array => Schema::getColumnListing($table), + ) + ); } /** diff --git a/app/Services/DataTableService.php b/app/Services/DataTableService.php index 0df45fdc..c696cd03 100644 --- a/app/Services/DataTableService.php +++ b/app/Services/DataTableService.php @@ -8,7 +8,6 @@ use Illuminate\Contracts\Database\Query\Builder; use Illuminate\Contracts\Session\Session; use Illuminate\Http\Request; use Illuminate\Pagination\LengthAwarePaginator; -use Illuminate\Support\Facades\Schema; use Shared\Enums\HasBaseEnum; use Shared\Pagination\Pagination; @@ -25,12 +24,12 @@ class DataTableService private const string CACHE_SORT_WAY_KEY = 'sort_way'; /** - * @param (\Closure(string): array)|null $columnListingResolver + * @param \Closure(string): array $columnListingResolver */ public function __construct( protected readonly Request $request, protected readonly Session $session, - protected readonly ?\Closure $columnListingResolver = null, + protected readonly \Closure $columnListingResolver, ) { } @@ -86,9 +85,7 @@ class DataTableService private function hasTableColumn(string $table, string $column): bool { if (! isset(self::$columnsCache[$table])) { - self::$columnsCache[$table] = $this->columnListingResolver !== null - ? ($this->columnListingResolver)($table) - : Schema::getColumnListing($table); + self::$columnsCache[$table] = ($this->columnListingResolver)($table); } return in_array($column, self::$columnsCache[$table], true); diff --git a/tests/Integration/WithApp/App/Queries/Discounts/GetPaginatedDiscountsQueryTest.php b/tests/Integration/WithApp/App/Queries/Discounts/GetPaginatedDiscountsQueryTest.php index 1d679126..9c3e00a8 100644 --- a/tests/Integration/WithApp/App/Queries/Discounts/GetPaginatedDiscountsQueryTest.php +++ b/tests/Integration/WithApp/App/Queries/Discounts/GetPaginatedDiscountsQueryTest.php @@ -15,6 +15,7 @@ use Domain\Models\ValueObjects\DiscountValue; use Illuminate\Http\Request; use Illuminate\Session\NullSessionHandler; use Illuminate\Session\Store; +use Illuminate\Support\Facades\Schema; use Shared\Pagination\PageNumber; use Shared\Pagination\PageSize; use Shared\Pagination\Pagination; @@ -38,7 +39,13 @@ describe('GetPaginatedDiscountsQuery', function (): void { $discount2->is_old_price_displayed = false; $discount2->save(); - $query = new GetPaginatedDiscountsQuery(new DataTableService(Request::create('/'), new Store('test', new NullSessionHandler()))); + $query = new GetPaginatedDiscountsQuery( + new DataTableService( + Request::create('/'), + new Store('test', new NullSessionHandler()), + static fn (string $table): array => Schema::getColumnListing($table), + ), + ); $input = new GetPaginatedDiscountsQueryInput( pagination: Pagination::ofPage(new PageNumber(1), new PageSize(10)), ); @@ -55,7 +62,13 @@ describe('GetPaginatedDiscountsQuery', function (): void { it('throws when an invalid input is passed', function (): void { // GIVEN - $query = new GetPaginatedDiscountsQuery(new DataTableService(Request::create('/'), new Store('test', new NullSessionHandler()))); + $query = new GetPaginatedDiscountsQuery( + new DataTableService( + Request::create('/'), + new Store('test', new NullSessionHandler()), + static fn (string $table): array => Schema::getColumnListing($table), + ), + ); $invalidInput = new class () implements QueryInput { }; diff --git a/tests/Integration/WithApp/App/Queries/Faqs/GetPaginatedFaqsQueryTest.php b/tests/Integration/WithApp/App/Queries/Faqs/GetPaginatedFaqsQueryTest.php index ce713cd9..f20b88f8 100644 --- a/tests/Integration/WithApp/App/Queries/Faqs/GetPaginatedFaqsQueryTest.php +++ b/tests/Integration/WithApp/App/Queries/Faqs/GetPaginatedFaqsQueryTest.php @@ -10,6 +10,7 @@ use App\Services\DataTableService; use Illuminate\Http\Request; use Illuminate\Session\NullSessionHandler; use Illuminate\Session\Store; +use Illuminate\Support\Facades\Schema; use Shared\Pagination\PageNumber; use Shared\Pagination\PageSize; use Shared\Pagination\Pagination; @@ -31,7 +32,13 @@ describe('GetPaginatedFaqsQuery', function (): void { $faq2->answer = 'Second answer.'; $faq2->save(); - $query = new GetPaginatedFaqsQuery(new DataTableService(Request::create('/'), new Store('test', new NullSessionHandler()))); + $query = new GetPaginatedFaqsQuery( + new DataTableService( + Request::create('/'), + new Store('test', new NullSessionHandler()), + static fn (string $table): array => Schema::getColumnListing($table), + ), + ); $input = new GetPaginatedFaqsQueryInput( pagination: Pagination::ofPage(new PageNumber(1), new PageSize(10)), ); diff --git a/tests/Integration/WithApp/App/Queries/PickupExceptions/GetPaginatedPickupExceptionsQueryTest.php b/tests/Integration/WithApp/App/Queries/PickupExceptions/GetPaginatedPickupExceptionsQueryTest.php index 6b9611aa..a17b5796 100644 --- a/tests/Integration/WithApp/App/Queries/PickupExceptions/GetPaginatedPickupExceptionsQueryTest.php +++ b/tests/Integration/WithApp/App/Queries/PickupExceptions/GetPaginatedPickupExceptionsQueryTest.php @@ -12,6 +12,7 @@ use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Http\Request; use Illuminate\Session\NullSessionHandler; use Illuminate\Session\Store; +use Illuminate\Support\Facades\Schema; use Shared\Pagination\PageNumber; use Shared\Pagination\PageSize; use Shared\Pagination\Pagination; @@ -27,7 +28,11 @@ describe('GetPaginatedPickupExceptionsQuery', function () { // GIVEN PickupException::factory()->count(3)->create(); - $dataTableService = new DataTableService(Request::create('/'), new Store('test', new NullSessionHandler())); + $dataTableService = new DataTableService( + Request::create('/'), + new Store('test', new NullSessionHandler()), + static fn (string $table): array => Schema::getColumnListing($table), + ); $query = new GetPaginatedPickupExceptionsQuery($dataTableService); $input = new GetPaginatedPickupExceptionsQueryInput( pagination: Pagination::ofPage(new PageNumber(1), new PageSize(10)), diff --git a/tests/Integration/WithApp/App/Queries/Products/GetBoPaginatedProductsQueryTest.php b/tests/Integration/WithApp/App/Queries/Products/GetBoPaginatedProductsQueryTest.php index 86e5ad67..29d94092 100644 --- a/tests/Integration/WithApp/App/Queries/Products/GetBoPaginatedProductsQueryTest.php +++ b/tests/Integration/WithApp/App/Queries/Products/GetBoPaginatedProductsQueryTest.php @@ -21,6 +21,7 @@ use Domain\Models\ValueObjects\DiscountValue; use Illuminate\Http\Request; use Illuminate\Session\NullSessionHandler; use Illuminate\Session\Store; +use Illuminate\Support\Facades\Schema; use Infrastructure\Adapters\Repositories\EloquentProductRepository; use Money\Currencies\ISOCurrencies; use Money\Formatter\IntlMoneyFormatter; @@ -90,7 +91,11 @@ describe('GetBoPaginatedProductsQuery', function (): void { $prod3 = $createProduct('Prod without Discount', $range, null); $query = new GetBoPaginatedProductsQuery( - new DataTableService(Request::create('/'), new Store('test', new NullSessionHandler())), + new DataTableService( + Request::create('/'), + new Store('test', new NullSessionHandler()), + static fn (string $table): array => Schema::getColumnListing($table), + ), new IntlMoneyFormatter(new \NumberFormatter('fr_FR', \NumberFormatter::CURRENCY), new ISOCurrencies()), new ProductDtoAssemblerService(new EloquentProductRepository()), ); diff --git a/tests/Integration/WithApp/App/Queries/Ranges/GetPaginatedRangesQueryTest.php b/tests/Integration/WithApp/App/Queries/Ranges/GetPaginatedRangesQueryTest.php index 100b4983..eae5777b 100644 --- a/tests/Integration/WithApp/App/Queries/Ranges/GetPaginatedRangesQueryTest.php +++ b/tests/Integration/WithApp/App/Queries/Ranges/GetPaginatedRangesQueryTest.php @@ -11,6 +11,7 @@ use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Http\Request; use Illuminate\Session\NullSessionHandler; use Illuminate\Session\Store; +use Illuminate\Support\Facades\Schema; use Shared\Pagination\PageNumber; use Shared\Pagination\PageSize; use Shared\Pagination\Pagination; @@ -38,7 +39,11 @@ describe('GetPaginatedRangesQuery', function () { $range2->status = true; $range2->save(); - $dataTableService = new DataTableService(Request::create('/'), new Store('test', new NullSessionHandler())); + $dataTableService = new DataTableService( + Request::create('/'), + new Store('test', new NullSessionHandler()), + static fn (string $table): array => Schema::getColumnListing($table), + ); $query = new GetPaginatedRangesQuery($dataTableService); $input = new GetPaginatedRangesQueryInput( pagination: Pagination::ofPage(new PageNumber(1), new PageSize(10)), diff --git a/tests/Integration/WithApp/App/Queries/Shops/GetPaginatedShopsQueryTest.php b/tests/Integration/WithApp/App/Queries/Shops/GetPaginatedShopsQueryTest.php index f00f872d..3c04a648 100644 --- a/tests/Integration/WithApp/App/Queries/Shops/GetPaginatedShopsQueryTest.php +++ b/tests/Integration/WithApp/App/Queries/Shops/GetPaginatedShopsQueryTest.php @@ -14,6 +14,7 @@ use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Http\Request; use Illuminate\Session\NullSessionHandler; use Illuminate\Session\Store; +use Illuminate\Support\Facades\Schema; use Shared\Pagination\PageNumber; use Shared\Pagination\PageSize; use Shared\Pagination\Pagination; @@ -31,7 +32,11 @@ describe('GetPaginatedShopsQuery', function () { $shop2 = Shop::factory()->create(['name' => 'Shop B']); $exception = PickupException::factory()->create(); - $dataTableService = new DataTableService(Request::create('/'), new Store('test', new NullSessionHandler())); + $dataTableService = new DataTableService( + Request::create('/'), + new Store('test', new NullSessionHandler()), + static fn (string $table): array => Schema::getColumnListing($table), + ); $query = new GetPaginatedShopsQuery($dataTableService); $input = new GetPaginatedShopsQueryInput( pagination: Pagination::ofPage(new PageNumber(1), new PageSize(10)), @@ -57,7 +62,11 @@ describe('GetPaginatedShopsQuery', function () { $shop1->pickupExceptions()->attach($exception->id); - $dataTableService = new DataTableService(Request::create('/'), new Store('test', new NullSessionHandler())); + $dataTableService = new DataTableService( + Request::create('/'), + new Store('test', new NullSessionHandler()), + static fn (string $table): array => Schema::getColumnListing($table), + ); $query = new GetPaginatedShopsQuery($dataTableService); $input = new GetPaginatedShopsQueryInput( pagination: Pagination::ofPage(new PageNumber(1), new PageSize(10)), diff --git a/tests/Integration/WithApp/App/Queries/Slides/GetPaginatedSlidesQueryTest.php b/tests/Integration/WithApp/App/Queries/Slides/GetPaginatedSlidesQueryTest.php index bb7089a0..4fc1c9b7 100644 --- a/tests/Integration/WithApp/App/Queries/Slides/GetPaginatedSlidesQueryTest.php +++ b/tests/Integration/WithApp/App/Queries/Slides/GetPaginatedSlidesQueryTest.php @@ -11,6 +11,7 @@ use Illuminate\Http\Request; use Illuminate\Session\NullSessionHandler; use Illuminate\Session\Store; use Illuminate\Support\Facades\Date; +use Illuminate\Support\Facades\Schema; use Shared\Pagination\PageNumber; use Shared\Pagination\PageSize; use Shared\Pagination\Pagination; @@ -34,7 +35,13 @@ describe('GetPaginatedSlidesQuery', function (): void { $slide2->order = 2; $slide2->save(); - $query = new GetPaginatedSlidesQuery(new DataTableService(Request::create('/'), new Store('test', new NullSessionHandler()))); + $query = new GetPaginatedSlidesQuery( + new DataTableService( + Request::create('/'), + new Store('test', new NullSessionHandler()), + static fn (string $table): array => Schema::getColumnListing($table), + ), + ); $input = new GetPaginatedSlidesQueryInput( pagination: Pagination::ofPage(new PageNumber(1), new PageSize(10)), ); @@ -75,7 +82,13 @@ describe('GetPaginatedSlidesQuery', function (): void { $futureSlide->date_end = $now->copy()->addDays(5); $futureSlide->save(); - $query = new GetPaginatedSlidesQuery(new DataTableService(Request::create('/'), new Store('test', new NullSessionHandler()))); + $query = new GetPaginatedSlidesQuery( + new DataTableService( + Request::create('/'), + new Store('test', new NullSessionHandler()), + static fn (string $table): array => Schema::getColumnListing($table), + ), + ); $input = new GetPaginatedSlidesQueryInput( pagination: Pagination::ofPage(new PageNumber(1), new PageSize(10)), publishedOnly: true, diff --git a/tests/Unit/Services/DataTableServiceTest.php b/tests/Unit/Services/DataTableServiceTest.php index fbd43175..1ed54d24 100644 --- a/tests/Unit/Services/DataTableServiceTest.php +++ b/tests/Unit/Services/DataTableServiceTest.php @@ -25,7 +25,7 @@ describe('DataTableService', function () { $session = mock(Session::class); $session->shouldReceive('get')->andReturn(null); - $service = new DataTableService($request, $session); + $service = new DataTableService($request, $session, static fn (string $table): array => []); // WHEN $sort = $service->getSortRequest(); @@ -42,7 +42,7 @@ describe('DataTableService', function () { $session->shouldReceive('get')->with('noroute.sort_col', 'undefined')->andReturn('name'); $session->shouldReceive('get')->with('noroute.sort_way')->andReturn('asc'); - $service = new DataTableService($request, $session); + $service = new DataTableService($request, $session, static fn (string $table): array => []); // WHEN $sort = $service->getSortRequest(); @@ -63,7 +63,7 @@ describe('DataTableService', function () { ])->once(); $session->shouldReceive('get')->andReturn(null); - $service = new DataTableService($request, $session); + $service = new DataTableService($request, $session, static fn (string $table): array => []); // WHEN $service->resetSortSession('noroute');