test(tests): apply step 1 hygiene, clock reset, and explicit Arcadia policy skips

This commit is contained in:
Jérémy Munsch 2026-11-16 12:39:21 +01:00
parent 8a3d6a8156
commit 875d6dd01d
No known key found for this signature in database
GPG key ID: 2AAECDA6B7330F91
7 changed files with 103 additions and 78 deletions

View file

@ -2,39 +2,48 @@
declare(strict_types=1);
use App\Jobs\FetchAndUpsertArcadiaEmployees;
use App\Models\User;
use Domain\Enums\UserRole;
use Illuminate\Support\Facades\Queue;
describe('EmployeesController', function () {
beforeEach(function () {
use function Pest\Laravel\actingAs;
use function Pest\Laravel\post;
describe('EmployeesController', function (): void {
beforeEach(function (): void {
// GIVEN
/** @var \Tests\TestCase $this */
$user = User::factory()->create(['role' => UserRole::SuperAdmin]);
$this->actingAs($user, 'backend');
$user = User::factory()->createOne(['role' => UserRole::SuperAdmin]);
actingAs($user, 'backend');
});
// FIXME: The Arcadia refresh action is inhibited, uncomment this test when api acrcadia is up.
// it('can launch employee sync job', function () {
// // GIVEN
// /** @var \Tests\TestCase $this */
// Queue::fake();
it('forbids launching employee sync job while EmployeePolicy is disabled', function (): void {
// WHEN
$response = post('/bo/settings/employees/sync');
// // WHEN
// $response = $this->post('/bo/settings/employees/sync');
// THEN
$response->assertForbidden();
});
// // THEN
// $response->assertSessionHasNoErrors();
// $response->assertRedirect(route('bo.dashboard'));
// Queue::assertPushed(FetchAndUpsertArcadiaEmployees::class);
// });
it('redirects unauthenticated users to login', function () {
it('can launch employee sync job', function (): void {
// GIVEN
Queue::fake();
// WHEN
$response = post('/bo/settings/employees/sync');
// THEN
$response->assertSessionHasNoErrors();
$response->assertRedirect(route('bo.dashboard'));
Queue::assertPushed(FetchAndUpsertArcadiaEmployees::class);
})->skip('Arcadia API is offline; SettingPolicy and EmployeePolicy currently return false.');
it('redirects unauthenticated users to login', function (): void {
// GIVEN
/** @var \Tests\TestCase $this */
auth()->guard('backend')->logout();
// WHEN
$response = $this->post('/bo/settings/employees/sync');
$response = post('/bo/settings/employees/sync');
// THEN
$response->assertRedirect('/bo/login');

View file

@ -16,15 +16,8 @@ use Domain\Models\ValueObjects\ShopperEstablishmentCode;
use Domain\Models\ValueObjects\ShopperServiceCode;
use Domain\Models\ValueObjects\ShopperSiteCode;
use Illuminate\Contracts\Cache\Repository as CacheRepository;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Psr\Log\NullLogger;
uses(RefreshDatabase::class);
afterEach(function (): void {
\Mockery::close();
});
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 */

View file

@ -2,27 +2,25 @@
declare(strict_types=1);
use App\Models\Setting;
use App\Models\User;
use function Pest\Laravel\actingAs;
describe('SettingsController', function (): void {
it('displays the employee sites edit form to an admin', function (): void {
/** @var \Tests\TestCase $this */
it('forbids accessing the employee sites edit form while SettingPolicy is disabled', function (): void {
// GIVEN
$user = User::factory()->createOne();
$route = route('bo.settings.edit');
// WHEN
$response = $this->actingAs($user, 'backend')->get($route);
$response = actingAs($user, 'backend')->get($route);
// THEN
$response->assertForbidden();
// $response->assertOk()
// ->assertViewIs('bo.settings.edit')
// ->assertViewHas('setting');
});
it('updates employee sites with array input and redirects', function (): void {
/** @var \Tests\TestCase $this */
it('forbids updating employee sites while SettingPolicy is disabled', function (): void {
// GIVEN
$user = User::factory()->createOne();
$route = route('bo.settings.update');
@ -31,19 +29,46 @@ describe('SettingsController', function (): void {
];
// WHEN
$response = $this->actingAs($user, 'backend')->put($route, $formData);
$response = actingAs($user, 'backend')->put($route, $formData);
// THEN
$response->assertForbidden();
// $response->assertRedirect(route('bo.settings.edit'))
// ->assertSessionHas('success');
// $setting = Setting::getSingleton();
// expect($setting->employee_sites)->toBe(['PA', 'LY', 'NA']);
});
it('displays the employee sites edit form to an admin', function (): void {
// GIVEN
$user = User::factory()->createOne();
$route = route('bo.settings.edit');
// WHEN
$response = actingAs($user, 'backend')->get($route);
// THEN
$response->assertOk()
->assertViewIs('bo.settings.edit')
->assertViewHas('setting');
})->skip('Arcadia API is offline; SettingPolicy and EmployeePolicy currently return false.');
it('updates employee sites with array input and redirects', function (): void {
// GIVEN
$user = User::factory()->createOne();
$route = route('bo.settings.update');
$formData = [
'employee_sites' => ['PA', 'LY', 'NA'],
];
// WHEN
$response = actingAs($user, 'backend')->put($route, $formData);
// THEN
$response->assertRedirect(route('bo.settings.edit'))
->assertSessionHas('success');
$setting = Setting::getSingleton();
expect($setting->employee_sites)->toBe(['PA', 'LY', 'NA']);
})->skip('Arcadia API is offline; SettingPolicy and EmployeePolicy currently return false.');
it('updates employee sites with comma string input and redirects', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$user = User::factory()->createOne();
$route = route('bo.settings.update');
@ -52,19 +77,17 @@ describe('SettingsController', function (): void {
];
// WHEN
$response = $this->actingAs($user, 'backend')->put($route, $formData);
$response = actingAs($user, 'backend')->put($route, $formData);
// THEN
$response->assertForbidden();
// $response->assertRedirect(route('bo.settings.edit'))
// ->assertSessionHas('success');
$response->assertRedirect(route('bo.settings.edit'))
->assertSessionHas('success');
// $setting = Setting::getSingleton();
// expect($setting->employee_sites)->toBe(['PA', 'LY', 'NA']);
});
$setting = Setting::getSingleton();
expect($setting->employee_sites)->toBe(['PA', 'LY', 'NA']);
})->skip('Arcadia API is offline; SettingPolicy and EmployeePolicy currently return false.');
it('rejects invalid site code formats', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$user = User::factory()->createOne();
$route = route('bo.settings.update');
@ -73,7 +96,7 @@ describe('SettingsController', function (): void {
];
// WHEN
$response = $this->actingAs($user, 'backend')->put($route, $formData);
$response = actingAs($user, 'backend')->put($route, $formData);
// THEN
$response->assertSessionHasErrors(['employee_sites.1', 'employee_sites.2']);

View file

@ -4,11 +4,17 @@ declare(strict_types=1);
use App\Dto\IcsContentDto;
use App\Services\IcsGeneratorService;
use Carbon\CarbonImmutable;
use Domain\Models\ValueObjects\IcsContent;
use Illuminate\Support\Carbon;
covers(IcsGeneratorService::class);
afterEach(function (): void {
Carbon::setTestNow();
CarbonImmutable::setTestNow();
});
describe('IcsGeneratorService', function (): void {
it('throws exception if app name is empty', function (): void {
// GIVEN
@ -49,9 +55,7 @@ describe('IcsGeneratorService', function (): void {
->toContain('DTEND:20260611T110000Z')
->toContain('SUMMARY:My Event\, with comma')
->toContain('DESCRIPTION:Line 1\nLine 2\;')
->not->toContain('URL:');
Carbon::setTestNow();
->and($ics->value)->not->toContain('URL:');
});
it('generates ics format with url', function (): void {

View file

@ -9,7 +9,6 @@ use Domain\Exceptions\OrderTunnel\MissingOrderBuilderException;
use Domain\Exceptions\OrderTunnel\OrderLimitExceededException;
use Domain\Exceptions\OrderTunnel\WeeklyLimitExceededException;
use Domain\Exceptions\UnknownCountryIsoCodeException;
use RuntimeException;
describe('Exceptions', function () {
it('creates MissingOrderBuilderException correctly', function () {
@ -27,7 +26,7 @@ describe('Exceptions', function () {
// GIVEN
$message = 'Custom error';
$code = 123;
$previous = new RuntimeException('Prev');
$previous = new \RuntimeException('Prev');
// WHEN
$e = new MissingOrderBuilderException($message, $code, $previous);

View file

@ -6,7 +6,6 @@ namespace Tests\Unit\Domain\Services;
use Carbon\CarbonImmutable;
use Carbon\WeekDay;
use DateInterval;
use Domain\Builders\OrderBuilder;
use Domain\Builders\OrderBuilderCompleted;
use Domain\Builders\OrderBuilderWithBasket;
@ -101,7 +100,7 @@ function createTestBuilder(bool $completed = false): OrderBuilderWithBasket
if ($completed) {
$shopId = new ShopId(Uuid::uuid7());
$nextFriday = CarbonImmutable::now('Europe/Paris')->next(WeekDay::Friday->value)->addWeek()->startOfDay();
$timeSlot = new PickupTimeSlot($nextFriday, new DateInterval('PT1H'));
$timeSlot = new PickupTimeSlot($nextFriday, new \DateInterval('PT1H'));
return $builder->withPickupTimeSlot($shopId, $timeSlot);
}
@ -272,7 +271,7 @@ describe('OrderTunnelService', function (): void {
// GIVEN
$shopId = new ShopId(Uuid::uuid7());
$nextFriday = CarbonImmutable::now('Europe/Paris')->next(WeekDay::Friday->value)->addWeek()->startOfDay();
$timeSlot = new PickupTimeSlot($nextFriday, new DateInterval('PT1H'));
$timeSlot = new PickupTimeSlot($nextFriday, new \DateInterval('PT1H'));
$builder = OrderBuilder::fromBasket(createTestBasket(0), new ShopperId(Uuid::uuid7()))->withPickupTimeSlot($shopId, $timeSlot);
$orderBuilderRepository = mock(OrderBuilderSessionManager::class);
$orderBuilderRepository->shouldReceive('get')->andReturn($builder);
@ -474,7 +473,7 @@ describe('OrderTunnelService', function (): void {
// GIVEN
$shopId = new ShopId(Uuid::uuid7());
$nextFriday = CarbonImmutable::now('Europe/Paris')->next(WeekDay::Friday->value)->addWeek()->startOfDay();
$timeSlot = new PickupTimeSlot($nextFriday, new DateInterval('PT1H'));
$timeSlot = new PickupTimeSlot($nextFriday, new \DateInterval('PT1H'));
$builder = OrderBuilder::fromBasket(createTestBasket(0), new ShopperId(Uuid::uuid7()))->withPickupTimeSlot($shopId, $timeSlot);
$orderBuilderRepo = mock(OrderBuilderSessionManager::class);
@ -494,8 +493,8 @@ describe('OrderTunnelService', function (): void {
$builder = createTestBuilder(completed: false);
$shop = new Shop(new ShopId(Uuid::uuid7()), 'Shop', 'Address', '75000', 'Paris');
$selectedDate = CarbonImmutable::create(2025, 1, 1);
$ts1 = new PickupTimeSlot($selectedDate, new DateInterval('PT1H'));
$ts2 = new PickupTimeSlot(CarbonImmutable::create(2025, 1, 2), new DateInterval('PT1H'));
$ts1 = new PickupTimeSlot($selectedDate, new \DateInterval('PT1H'));
$ts2 = new PickupTimeSlot(CarbonImmutable::create(2025, 1, 2), new \DateInterval('PT1H'));
$availableSlots = [$ts1, $ts2];
$orderBuilderRepo = mock(OrderBuilderSessionManager::class);
@ -515,7 +514,7 @@ describe('OrderTunnelService', function (): void {
$builder = createTestBuilder(completed: false);
$shop = new Shop(new ShopId(Uuid::uuid7()), 'Shop', 'Address', '75000', 'Paris');
$selectedDate = CarbonImmutable::create(2025, 1, 1);
$ts1 = new PickupTimeSlot(CarbonImmutable::create(2025, 1, 2), new DateInterval('PT1H'));
$ts1 = new PickupTimeSlot(CarbonImmutable::create(2025, 1, 2), new \DateInterval('PT1H'));
$availableSlots = [$ts1];
$service = createOrderTunnelService();
@ -747,7 +746,7 @@ describe('OrderTunnelService', function (): void {
$basket = createTestBasket(1);
$shopperId = new ShopperId(Uuid::uuid7());
$shopId = new ShopId(Uuid::uuid7());
$expiredSlot = new PickupTimeSlot(CarbonImmutable::create(2025, 1, 1), new DateInterval('PT1H'));
$expiredSlot = new PickupTimeSlot(CarbonImmutable::create(2025, 1, 1), new \DateInterval('PT1H'));
$builder = OrderBuilder::fromBasket($basket, $shopperId)->withPickupTimeSlot($shopId, $expiredSlot);
$shopper = new Shopper(
@ -984,7 +983,7 @@ describe('OrderTunnelService', function (): void {
new OrderId(Uuid::uuid7()),
new OrderReference(Uuid::uuid7()),
$basket,
new PickupTimeSlot(CarbonImmutable::now()->next('Friday'), new DateInterval('PT1H')),
new PickupTimeSlot(CarbonImmutable::now()->next('Friday'), new \DateInterval('PT1H')),
$shopper,
$shop
);

View file

@ -10,18 +10,15 @@ use Shared\Filesystem\TemporaryFileFactory;
covers(ImageOptimizerService::class);
describe('ImageOptimizerService', function (): void {
$service = \Mockery::mock(ImageOptimizerService::class);
$makeService = static fn (): ImageOptimizerService => new ImageOptimizerService(
new TemporaryFileFactory(),
new NullLogger(),
);
beforeEach(function () use (&$service) {
$service = new ImageOptimizerService(
new TemporaryFileFactory(),
new NullLogger(),
);
});
it('creates a real temp file for optimized images with the original extension', function () use (&$service): void {
it('creates a real temp file for optimized images with the original extension', function () use ($makeService): void {
// GIVEN
$file = UploadedFile::fake()->image('picture.jpg', 800, 600);
$service = $makeService();
$file = UploadedFile::fake()->image('picture.jpg', 800, 600);
// WHEN
$optimized = $service->optimize($file, quality: 70);
@ -37,9 +34,10 @@ describe('ImageOptimizerService', function (): void {
->and($content)->toBeString();
});
it('creates a real temp file for non-image uploads with a readable path', function () use (&$service): void {
it('creates a real temp file for non-image uploads with a readable path', function () use ($makeService): void {
// GIVEN
$pdf = UploadedFile::fake()->create('document.pdf', 250, 'application/pdf');
$service = $makeService();
$pdf = UploadedFile::fake()->create('document.pdf', 250, 'application/pdf');
// WHEN
$result = $service->optimize($pdf);