seed(RolesAndPermissionsSeeder::class); }); function failingPublicDisk(): FilesystemAdapter { $disk = Storage::fake('public'); // Proxy auf den echten Fake: nur put() schlägt fehl, alles andere — // exists(), delete(), Livewires Temp-Upload — läuft echt. $failingDisk = Mockery::mock($disk); $failingDisk->shouldReceive('put')->andReturn(false); Storage::set('public', $failingDisk); return $disk; } test('the image service refuses to report a logo that was not written', function () { /** @var TestCase $this */ failingPublicDisk(); expect(fn () => app(ImageService::class)->storeCompanyLogo(UploadedFile::fake()->image('logo.png', 400, 400), 'presseecho', 1)) ->toThrow(RuntimeException::class, 'Das Logo konnte nicht gespeichert werden.'); }); test('the image service refuses to report a press release image that was not written', function () { /** @var TestCase $this */ failingPublicDisk(); expect(fn () => app(ImageService::class)->storePressReleaseImage(UploadedFile::fake()->image('bild.jpg', 800, 600), 1, 'public')) ->toThrow(RuntimeException::class, 'Das Bild konnte nicht gespeichert werden.'); }); test('a failed logo write in the admin form keeps the previous logo and reports a validation error', function () { /** @var TestCase $this */ $disk = failingPublicDisk(); $disk->put('company-logos/presseecho/9/alt.png', 'alt'); $disk->put('company-logos/presseecho/9/alt-sq.png', 'alt-sq'); $company = Company::factory()->presseecho()->create([ 'logo_path' => 'company-logos/presseecho/9/alt.png', 'logo_variants' => ['sq' => 'company-logos/presseecho/9/alt-sq.png'], ]); $admin = User::factory()->create(['is_active' => true]); $admin->assignRole('admin'); Livewire::actingAs($admin) ->test('admin.companies.edit', ['id' => $company->id]) ->set('logo', UploadedFile::fake()->image('neu.png', 400, 400)) ->call('update') ->assertHasErrors(['logo']); $company->refresh(); expect($company->logo_path)->toBe('company-logos/presseecho/9/alt.png') ->and($company->logo_variants)->toBe(['sq' => 'company-logos/presseecho/9/alt-sq.png']) ->and($disk->exists('company-logos/presseecho/9/alt.png'))->toBeTrue() ->and($disk->exists('company-logos/presseecho/9/alt-sq.png'))->toBeTrue(); }); test('a successful logo replacement in the admin form removes the previous files only afterwards', function () { /** @var TestCase $this */ $disk = Storage::fake('public'); $disk->put('company-logos/presseecho/9/alt.png', 'alt'); $disk->put('company-logos/presseecho/9/alt-sq.png', 'alt-sq'); $company = Company::factory()->presseecho()->create([ 'logo_path' => 'company-logos/presseecho/9/alt.png', 'logo_variants' => ['sq' => 'company-logos/presseecho/9/alt-sq.png'], ]); $admin = User::factory()->create(['is_active' => true]); $admin->assignRole('admin'); Livewire::actingAs($admin) ->test('admin.companies.edit', ['id' => $company->id]) ->set('logo', UploadedFile::fake()->image('neu.png', 400, 400)) ->call('update') ->assertHasNoErrors(); $company->refresh(); expect($company->logo_path)->not->toBe('company-logos/presseecho/9/alt.png') ->and($disk->exists((string) $company->logo_path))->toBeTrue() ->and($disk->exists('company-logos/presseecho/9/alt.png'))->toBeFalse() ->and($disk->exists('company-logos/presseecho/9/alt-sq.png'))->toBeFalse(); });