feat(attachment): refactor image cropping functionality to use range objects and update related tests

This commit is contained in:
2026-08-18 12:54:47 -03:00
parent 58cc35fd61
commit 9cfd49f233
16 changed files with 496 additions and 54 deletions

View File

@@ -77,15 +77,21 @@ class AttachmentTest extends TestCase
$original = app(AttachmentService::class)->storeCroppedImage(
UploadedFile::fake()->image('product.jpg', 200, 100),
'attachments/acme',
50,
25,
['start_percentage' => 25, 'end_percentage' => 75],
['start_percentage' => 10, 'end_percentage' => 85],
);
$cropped = $original->croppedAttachment;
$this->assertNotNull($cropped);
$this->assertSame(50.0, $original->crop_horizontal_start_percent);
$this->assertSame(25.0, $original->crop_vertical_start_percent);
$this->assertEquals(
['start_percentage' => 25.0, 'end_percentage' => 75.0],
$original->crop_horizontal,
);
$this->assertEquals(
['start_percentage' => 10.0, 'end_percentage' => 85.0],
$original->crop_vertical,
);
$this->assertTrue($cropped->originalAttachment->is($original));
$this->assertDatabaseCount('attachments', 2);
$this->assertDatabaseHas('attachments', [
@@ -110,8 +116,8 @@ class AttachmentTest extends TestCase
app(AttachmentService::class)->storeCroppedImage(
UploadedFile::fake()->image('product.png'),
'attachments/acme',
100,
0,
['start_percentage' => 75, 'end_percentage' => 25],
['start_percentage' => 0, 'end_percentage' => 100],
);
$this->fail('Expected an AttachmentStorageException to be thrown.');
@@ -129,8 +135,8 @@ class AttachmentTest extends TestCase
app(AttachmentService::class)->storeCroppedImage(
UploadedFile::fake()->createWithContent('invalid.png', 'not-an-image'),
'attachments/acme',
10,
10,
['start_percentage' => 10, 'end_percentage' => 90],
['start_percentage' => 10, 'end_percentage' => 90],
);
$this->fail('Expected an AttachmentStorageException to be thrown.');
@@ -147,8 +153,8 @@ class AttachmentTest extends TestCase
$original = app(AttachmentService::class)->storeCroppedImage(
UploadedFile::fake()->image('product.png'),
'attachments/acme',
10,
10,
['start_percentage' => 10, 'end_percentage' => 90],
['start_percentage' => 10, 'end_percentage' => 90],
);
$cropped = $original->croppedAttachment;
@@ -159,6 +165,37 @@ class AttachmentTest extends TestCase
Storage::disk('s3')->assertMissing($cropped->path);
}
public function test_it_replaces_the_crop_of_an_existing_image(): void
{
Storage::fake('s3');
$original = app(AttachmentService::class)->storeCroppedImage(
UploadedFile::fake()->image('product.jpg', 200, 100),
'attachments/acme',
['start_percentage' => 0, 'end_percentage' => 100],
['start_percentage' => 0, 'end_percentage' => 100],
);
$previousCrop = $original->croppedAttachment;
$updated = app(AttachmentService::class)->updateImageCrop(
$original,
['start_percentage' => 25, 'end_percentage' => 75],
['start_percentage' => 10, 'end_percentage' => 85],
);
$this->assertFalse($updated->croppedAttachment->is($previousCrop));
$this->assertDatabaseMissing('attachments', ['id' => $previousCrop->id]);
Storage::disk('s3')->assertMissing($previousCrop->path);
$croppedSize = getimagesizefromstring(
Storage::disk('s3')->get($updated->croppedAttachment->path)
);
$this->assertIsArray($croppedSize);
$this->assertSame(100, $croppedSize[0]);
$this->assertSame(75, $croppedSize[1]);
}
public function test_it_deletes_from_s3_before_removing_the_database_record(): void
{
Storage::fake('s3');