test: apply Given/When/Then structure to remaining Pest tests

This commit is contained in:
Jérémy Munsch 2026-10-03 10:09:58 +02:00
parent d29561fe64
commit ff831beb01
No known key found for this signature in database
GPG key ID: 2AAECDA6B7330F91
7 changed files with 132 additions and 25 deletions

View file

@ -26,7 +26,8 @@ describe('Employee Sync Feature E2E', function (): void {
it('configures sites in back office and synchronizes employees from Arcadia API into database', function (): void {
/** @var \Tests\TestCase $this */
// 1. GIVEN: An admin user updates the employee sites in Back Office
// GIVEN
// An admin user updates the employee sites in Back Office
$admin = User::factory()->createOne();
$updateResponse = $this->actingAs($admin, 'backend')->put(route('bo.settings.update'), [
@ -39,7 +40,7 @@ describe('Employee Sync Feature E2E', function (): void {
$setting = Setting::getSingleton();
expect($setting->employee_sites)->toBe(['PA', 'LY']);
// 2. Mock ArcadiaEmployeeService to return employee data for site PA
// Mock ArcadiaEmployeeService to return employee data for site PA
$mockArcadia = \Mockery::mock(ArcadiaEmployeeService::class);
$mockArcadia->shouldReceive('getEmployeesOfSiteSince')
->once()
@ -64,12 +65,14 @@ describe('Employee Sync Feature E2E', function (): void {
$this->app->instance(ArcadiaEmployeeService::class, $mockArcadia);
// 3. Admin launches sync job via controller endpoint (executed synchronously by default queue driver)
// WHEN
// Admin launches sync job via controller endpoint (executed synchronously by default queue driver)
$syncResponse = $this->actingAs($admin, 'backend')->post(route('bo.settings.employees.sync'));
$syncResponse->assertRedirect(route('bo.dashboard'))
->assertSessionHas('success');
// 4. THEN: Employee exists in database with all attributes populated
// THEN
// Employee exists in database with all attributes populated
$employee = Employee::query()->where('matricule', '7654321')->first();
expect($employee)->not->toBeNull()
->and($employee->nom)->toBe('Martin')
@ -83,7 +86,7 @@ describe('Employee Sync Feature E2E', function (): void {
->and($employee->code)->not->toBeNull()
->and($employee->last_sync)->not->toBeNull();
// 6. Verify sync timestamps
// Verify sync timestamps
$syncService = resolve(EmployeeSyncService::class);
expect($syncService->getLastSync())->not->toBeNull();

View file

@ -6,18 +6,21 @@ use App\Dto\RangeDto;
describe('RangeDto', function () {
test('it can be instantiated directly and converts to array', function () {
// GIVEN & WHEN
$dto = new RangeDto(
id: 1,
name: 'Gamme Découverte',
slug: 'gamme-decouverte',
description: '<p>Description</p>',
picture: 'ranges/pic.jpg',
picture_alt: 'Alt text',
picture_title: 'Title text',
order: 2,
status: true,
);
// GIVEN
$data = [
'id' => 1,
'name' => 'Gamme Découverte',
'slug' => 'gamme-decouverte',
'description' => '<p>Description</p>',
'picture' => 'ranges/pic.jpg',
'picture_alt' => 'Alt text',
'picture_title' => 'Title text',
'order' => 2,
'status' => true,
];
// WHEN
$dto = new RangeDto(...$data);
// THEN
expect($dto->id)->toBe(1)

View file

@ -40,47 +40,60 @@ function createDummyBasketProduct(): BasketProduct
describe('BasketPolicy', function () {
describe('viewTotal', function () {
it('returns true when user is a Customer instance', function () {
// GIVEN
$productService = \Mockery::mock(ProductAvailabilityService::class);
$policy = new BasketPolicy($productService);
$customer = \Mockery::mock(Customer::class);
// WHEN
$result = $policy->viewTotal($customer);
// THEN
expect($result)->toBeTrue();
});
it('returns true when user is a User instance', function () {
// GIVEN
$productService = \Mockery::mock(ProductAvailabilityService::class);
$policy = new BasketPolicy($productService);
$user = \Mockery::mock(User::class);
// WHEN
$result = $policy->viewTotal($user);
// THEN
expect($result)->toBeTrue();
});
it('returns false when user is null', function () {
// GIVEN
$productService = \Mockery::mock(ProductAvailabilityService::class);
$policy = new BasketPolicy($productService);
// WHEN
$result = $policy->viewTotal(null);
// THEN
expect($result)->toBeFalse();
});
});
describe('viewPrices', function () {
it('returns false when user is null', function () {
// GIVEN
$productService = \Mockery::mock(ProductAvailabilityService::class);
$policy = new BasketPolicy($productService);
$product = createDummyBasketProduct();
// WHEN
$result = $policy->viewPrices(null, $product);
// THEN
expect($result)->toBeFalse();
});
it('returns true when user is not null and product is published', function () {
// GIVEN
$productService = \Mockery::mock(ProductAvailabilityService::class);
$policy = new BasketPolicy($productService);
$customer = \Mockery::mock(Customer::class);
@ -91,12 +104,15 @@ describe('BasketPolicy', function () {
->with($product)
->andReturn(true);
// WHEN
$result = $policy->viewPrices($customer, $product);
// THEN
expect($result)->toBeTrue();
});
it('returns false when user is not null and product is not published', function () {
// GIVEN
$productService = \Mockery::mock(ProductAvailabilityService::class);
$policy = new BasketPolicy($productService);
$user = \Mockery::mock(User::class);
@ -107,8 +123,10 @@ describe('BasketPolicy', function () {
->with($product)
->andReturn(false);
// WHEN
$result = $policy->viewPrices($user, $product);
// THEN
expect($result)->toBeFalse();
});
});

View file

@ -29,25 +29,35 @@ function createFakeGate(bool $returns): object
describe('OrderTunnelStepPolicy', function () {
describe('canReviewBasketSummary', function () {
it('returns false when user is a User instance', function () {
// GIVEN
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
$policy = new OrderTunnelStepPolicy($orderTunnelService);
$user = new User();
$result = $policy->canReviewBasketSummary($user);
// WHEN
$result = $policy->canReviewBasketSummary($user);
// THEN
expect($result)->toBeFalse();
});
it('returns false when gate denies customerPassOrders', function () {
// GIVEN
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
$policy = new OrderTunnelStepPolicy($orderTunnelService);
$customer = new Customer();
Gate::swap(createFakeGate(false));
// WHEN
$result = $policy->canReviewBasketSummary($customer);
// THEN
expect($result)->toBeFalse();
});
it('returns false when orderTunnelService denies access to REVIEW step', function () {
// GIVEN
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
$policy = new OrderTunnelStepPolicy($orderTunnelService);
$customer = new Customer();
@ -59,11 +69,15 @@ describe('OrderTunnelStepPolicy', function () {
->with(OrderTunnelStep::REVIEW)
->andReturn(false);
// WHEN
$result = $policy->canReviewBasketSummary($customer);
// THEN
expect($result)->toBeFalse();
});
it('returns true when gate allows and orderTunnelService allows', function () {
// GIVEN
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
$policy = new OrderTunnelStepPolicy($orderTunnelService);
$customer = new Customer();
@ -75,32 +89,45 @@ describe('OrderTunnelStepPolicy', function () {
->with(OrderTunnelStep::REVIEW)
->andReturn(true);
// WHEN
$result = $policy->canReviewBasketSummary($customer);
// THEN
expect($result)->toBeTrue();
});
});
describe('canPickTimeSlot', function () {
it('returns false when user is a User instance', function () {
// GIVEN
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
$policy = new OrderTunnelStepPolicy($orderTunnelService);
$user = new User();
$result = $policy->canPickTimeSlot($user);
// WHEN
$result = $policy->canPickTimeSlot($user);
// THEN
expect($result)->toBeFalse();
});
it('returns false when gate denies customerPassOrders', function () {
// GIVEN
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
$policy = new OrderTunnelStepPolicy($orderTunnelService);
$customer = new Customer();
Gate::swap(createFakeGate(false));
// WHEN
$result = $policy->canPickTimeSlot($customer);
// THEN
expect($result)->toBeFalse();
});
it('returns false when orderTunnelService denies access to BOOKING step', function () {
// GIVEN
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
$policy = new OrderTunnelStepPolicy($orderTunnelService);
$customer = new Customer();
@ -112,11 +139,15 @@ describe('OrderTunnelStepPolicy', function () {
->with(OrderTunnelStep::BOOKING)
->andReturn(false);
// WHEN
$result = $policy->canPickTimeSlot($customer);
// THEN
expect($result)->toBeFalse();
});
it('returns true when gate allows and orderTunnelService allows', function () {
// GIVEN
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
$policy = new OrderTunnelStepPolicy($orderTunnelService);
$customer = new Customer();
@ -128,32 +159,45 @@ describe('OrderTunnelStepPolicy', function () {
->with(OrderTunnelStep::BOOKING)
->andReturn(true);
// WHEN
$result = $policy->canPickTimeSlot($customer);
// THEN
expect($result)->toBeTrue();
});
});
describe('canFinalizeOrder', function () {
it('returns false when user is a User instance', function () {
// GIVEN
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
$policy = new OrderTunnelStepPolicy($orderTunnelService);
$user = new User();
$result = $policy->canFinalizeOrder($user);
// WHEN
$result = $policy->canFinalizeOrder($user);
// THEN
expect($result)->toBeFalse();
});
it('returns false when gate denies customerPassOrders', function () {
// GIVEN
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
$policy = new OrderTunnelStepPolicy($orderTunnelService);
$customer = new Customer();
Gate::swap(createFakeGate(false));
// WHEN
$result = $policy->canFinalizeOrder($customer);
// THEN
expect($result)->toBeFalse();
});
it('returns false when orderTunnelService denies access to CONFIRMATION step', function () {
// GIVEN
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
$policy = new OrderTunnelStepPolicy($orderTunnelService);
$customer = new Customer();
@ -165,11 +209,15 @@ describe('OrderTunnelStepPolicy', function () {
->with(OrderTunnelStep::CONFIRMATION)
->andReturn(false);
// WHEN
$result = $policy->canFinalizeOrder($customer);
// THEN
expect($result)->toBeFalse();
});
it('returns true when gate allows and orderTunnelService allows', function () {
// GIVEN
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
$policy = new OrderTunnelStepPolicy($orderTunnelService);
$customer = new Customer();
@ -181,7 +229,10 @@ describe('OrderTunnelStepPolicy', function () {
->with(OrderTunnelStep::CONFIRMATION)
->andReturn(true);
// WHEN
$result = $policy->canFinalizeOrder($customer);
// THEN
expect($result)->toBeTrue();
});
});

View file

@ -8,7 +8,10 @@ use App\Queries\Shops\GetShopQueryInput;
describe('GetShopQueryInput', function (): void {
it('creates an instance of GetShopQueryInput', function (): void {
// GIVEN & WHEN
// GIVEN
// (no precondition: the input has no argument)
// WHEN
$input = new GetShopQueryInput();
// THEN

View file

@ -18,6 +18,7 @@ describe('DataTableService', function () {
});
it('retrieves sort request from request inputs', function () {
// GIVEN
$request = Request::create('/bo/products', 'GET', [
'sort_col' => 'name',
'sort_way' => 'desc',
@ -26,26 +27,34 @@ describe('DataTableService', function () {
$session->shouldReceive('get')->andReturn(null);
$service = new DataTableService($request, $session);
$sort = $service->getSortRequest();
// 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 = 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);
$sort = $service->getSortRequest();
// 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 = mock(Session::class);
$session->shouldReceive('forget')->with([
@ -56,10 +65,16 @@ describe('DataTableService', function () {
$session->shouldReceive('get')->andReturn(null);
$service = new DataTableService($request, $session);
// 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',
@ -76,18 +91,24 @@ describe('DataTableService', function () {
$query = mock(Builder::class);
$query->shouldReceive('orderBy')->once()->with('products.name', 'asc');
$query2 = mock(Builder::class);
$query2->shouldReceive('orderBy')->once()->with('products.name', 'asc');
$service = new DataTableService($request, $session);
// WHEN
// First call triggers getColumnListing
$service->sortQuery(Product::class, $query);
// Second call uses columnsCache without re-calling getColumnListing
$query2 = mock(Builder::class);
$query2->shouldReceive('orderBy')->once()->with('products.name', 'asc');
$service->sortQuery(Product::class, $query2);
// THEN
// Mockery verifies on teardown: getColumnListing() once, orderBy() once per query.
});
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 = mock(Session::class);
$session->shouldReceive('get')->andReturn(null);
@ -101,6 +122,11 @@ describe('DataTableService', function () {
$query->shouldReceive('orderBy')->once()->with('order', 'asc');
$service = new DataTableService($request, $session);
// WHEN
$service->sortQuery(Product::class, $query);
// THEN
// Mockery verifies on teardown that orderBy('order', 'asc') was called once.
});
});

View file

@ -7,6 +7,7 @@ use App\Services\IcsGeneratorService;
use Carbon\CarbonImmutable;
it('generates a valid ICS string', function () {
// GIVEN
$service = new IcsGeneratorService('La Boutique de Cocotte');
// Fix time to ensure DTSTAMP doesn't fail assertion
@ -21,8 +22,10 @@ it('generates a valid ICS string', function () {
url: 'https://laboutiquedecocotte.fr'
);
// WHEN
$ics = $service->generate($event);
// THEN
expect($ics)->toContain('BEGIN:VCALENDAR')
->toContain('VERSION:2.0')
->toContain('PRODID:-//La Boutique de Cocotte//NONSGML v1.0//EN')