ldc-cocotte/tests/Integration/WithApp/App/Actions/Ranges/DeleteRangeTest.php

80 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
use App\Contracts\ActionInput;
use App\Cqs\Actions\Ranges\DeleteRange;
use App\Cqs\Actions\Ranges\DeleteRangeInput;
use App\Lib\PublicS3Disk;
use App\Models\Range;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Uwil\Storage\Contracts\StorageService;
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
uses(RefreshDatabase::class);
describe('DeleteRange action', function () {
test('it deletes an existing range from database', function () {
/** @var \Tests\TestCase $this */
// GIVEN
$range = new Range();
$range->name = 'Gamme Temporaire';
$range->picture_alt = 'Alt';
$range->picture_title = 'Title';
$range->status = true;
$range->save();
// Range has no picture: storage must stay untouched.
$storage = \Mockery::mock(StorageService::class);
$storage->shouldNotReceive('fileExistsOnDisk');
$storage->shouldNotReceive('removeFromDisk');
$action = new DeleteRange($storage, new PublicS3Disk('https://cdn.test'));
$input = new DeleteRangeInput(range: $range);
// WHEN
$result = $action($input);
// THEN
expect($result)->toBeTrue();
$this->assertDatabaseMissing('ranges', [
'id' => $range->id,
]);
});
test('it removes the picture from storage when the range has one', function () {
/** @var \Tests\TestCase $this */
// GIVEN
$range = new Range();
$range->name = 'Gamme Avec Image';
$range->picture_alt = 'Alt';
$range->picture_title = 'Title';
$range->status = true;
$range->picture = new StorageFilePath('media/range.jpg');
$range->save();
$storage = \Mockery::mock(StorageService::class);
$storage->shouldReceive('fileExistsOnDisk')->once()->andReturnTrue();
$storage->shouldReceive('removeFromDisk')->once()->andReturnTrue();
$action = new DeleteRange($storage, new PublicS3Disk('https://cdn.test'));
// WHEN
$result = $action(new DeleteRangeInput(range: $range));
// THEN
expect($result)->toBeTrue();
$this->assertDatabaseMissing('ranges', ['id' => $range->id]);
});
test('it throws when an invalid input is passed', function () {
// GIVEN
$storage = \Mockery::mock(StorageService::class);
$action = new DeleteRange($storage, new PublicS3Disk('https://cdn.test'));
$invalidInput = new class () implements ActionInput
{
};
// WHEN & THEN
expect(fn () => $action($invalidInput))
->toThrow(\InvalidArgumentException::class);
});
});