ldc-cocotte/tests/Integration/Standalones/App/Services/DataTableServiceTest.php
Jérémy Munsch 29b9a87d78
test(tests): update mock usage in DataTableServiceTest
Refactor mock creation to use \Mockery::mock for consistency.
2026-11-16 12:54:52 +01:00

199 lines
6.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Integration\Standalones\App\Services;
use App\Models\Order;
use App\Models\Product;
use App\Services\DataTableService;
use Illuminate\Contracts\Database\Query\Builder;
use Illuminate\Contracts\Session\Session;
use Illuminate\Http\Request;
describe('DataTableService', function () {
beforeEach(function () {
DataTableService::clearColumnsCache();
});
it('retrieves sort request from request inputs', function () {
// GIVEN
$request = Request::create('/bo/products', 'GET', [
'sort_col' => 'name',
'sort_way' => 'desc',
]);
$session = \Mockery::mock(Session::class);
$session->shouldReceive('get')->andReturn(null);
$service = new DataTableService($request, $session, static fn (string $table): array => []);
// WHEN
$sort = $service->getSortRequest();
// THEN
expect($sort['sort_col'])->toBe('name');
expect($sort['sort_way'])->toBe(\SortDirection::Descending);
});
it('falls back to session when sort request input is not provided', function () {
// GIVEN
$request = Request::create('/bo/products', 'GET');
$session = \Mockery::mock(Session::class);
$session->shouldReceive('get')->with('noroute.sort_col', 'undefined')->andReturn('name');
$session->shouldReceive('get')->with('noroute.sort_way')->andReturn('asc');
$service = new DataTableService($request, $session, static fn (string $table): array => []);
// WHEN
$sort = $service->getSortRequest();
// THEN
expect($sort['sort_col'])->toBe('name');
expect($sort['sort_way'])->toBe(\SortDirection::Ascending);
});
it('resets sort session when rst parameter is present', function () {
// GIVEN
$request = Request::create('/bo/products', 'GET', ['rst' => '1']);
$session = \Mockery::mock(Session::class);
$session->shouldReceive('forget')->with([
'noroute.sort_col',
'noroute.sort_way',
'noroute.sorted',
])->once();
$session->shouldReceive('get')->andReturn(null);
$service = new DataTableService($request, $session, static fn (string $table): array => []);
// WHEN
$service->resetSortSession('noroute');
// THEN
// Mockery verifies on teardown that forget() was called exactly once with the sort keys.
});
it('sorts query using cached table column listing', function () {
// GIVEN
$request = Request::create('/bo/products', 'GET', [
'sort_col' => 'name',
'sort_way' => 'asc',
]);
$session = \Mockery::mock(Session::class);
$session->shouldReceive('get')->andReturn(null);
$session->shouldReceive('put')->times(4);
$schemaCalls = [];
$columnResolver = function (string $table) use (&$schemaCalls): array {
$schemaCalls[] = $table;
return ['id', 'name', 'order', 'created_at'];
};
$query = \Mockery::mock(Builder::class);
$query->shouldReceive('orderBy')->once()->with('products.name', 'asc');
$query2 = \Mockery::mock(Builder::class);
$query2->shouldReceive('orderBy')->once()->with('products.name', 'asc');
$service = new DataTableService($request, $session, $columnResolver);
// WHEN
// First call triggers column listing resolver
$service->sortQuery(Product::class, $query);
// Second call uses columnsCache without re-calling column listing resolver
$service->sortQuery(Product::class, $query2);
// THEN
expect($schemaCalls)->toBe(['products']);
});
it('sorts by default order column if model has order attribute and no sort column is specified', function () {
// GIVEN
$request = Request::create('/bo/products', 'GET');
$session = \Mockery::mock(Session::class);
$session->shouldReceive('get')->andReturn(null);
$schemaCalls = [];
$columnResolver = function (string $table) use (&$schemaCalls): array {
$schemaCalls[] = $table;
return ['id', 'name', 'order', 'created_at'];
};
$query = \Mockery::mock(Builder::class);
$query->shouldReceive('orderBy')->once()->with('order', 'asc');
$service = new DataTableService($request, $session, $columnResolver);
// WHEN
$service->sortQuery(Product::class, $query);
// THEN
expect($schemaCalls)->toBe(['products']);
});
it('sorts by custom default column and way when no sort column is specified', function () {
// GIVEN
$request = Request::create('/bo/orders', 'GET');
$session = \Mockery::mock(Session::class);
$session->shouldReceive('get')->andReturn(null);
$schemaCalls = [];
$columnResolver = function (string $table) use (&$schemaCalls): array {
$schemaCalls[] = $table;
return ['id', 'reference', 'status', 'total'];
};
$query = \Mockery::mock(Builder::class);
$query->shouldReceive('orderBy')->once()->with('orders.id', 'desc');
$service = new DataTableService($request, $session, $columnResolver);
// WHEN
$service->sortQuery(
Order::class,
$query,
defaultSortCol: 'date',
defaultSortWay: \SortDirection::Descending,
columnMap: ['date' => 'id'],
);
// THEN
expect($schemaCalls)->toBe(['orders']);
});
it('uses columnMap when sorting by an explicit virtual column', function () {
// GIVEN
$request = Request::create('/bo/orders', 'GET', [
'sort_col' => 'date',
'sort_way' => 'asc',
]);
$session = \Mockery::mock(Session::class);
$session->shouldReceive('get')->andReturn(null);
$session->shouldReceive('put')->twice();
$schemaCalls = [];
$columnResolver = function (string $table) use (&$schemaCalls): array {
$schemaCalls[] = $table;
return ['id', 'reference', 'status', 'total'];
};
$query = \Mockery::mock(Builder::class);
$query->shouldReceive('orderBy')->once()->with('orders.id', 'asc');
$service = new DataTableService($request, $session, $columnResolver);
// WHEN
$service->sortQuery(
Order::class,
$query,
columnMap: ['date' => 'id'],
);
// THEN
expect($schemaCalls)->toBe(['orders']);
});
});