Refactor attachment cropping functionality to support multiple variants

- Introduced AttachmentCrop model to manage crop variants for attachments.
- Updated Attachment model to remove direct crop fields and establish relationships with AttachmentCrop.
- Modified AttachmentService to handle storing and updating multiple crop variants (desktop and mobile).
- Adjusted validation rules to accommodate new crop structure.
- Updated database migration to create attachment_crops table and migrate existing crop data.
- Refactored tests to ensure compatibility with the new cropping structure and validate multiple crop variants.
- Enhanced documentation to reflect changes in attachment handling and cropping capabilities.
This commit is contained in:
2026-08-18 14:40:22 -03:00
parent c00b3d609c
commit e619c51ac9
14 changed files with 526 additions and 150 deletions

View File

@@ -5,6 +5,7 @@ namespace Tests\Feature\Attachable;
use App\Domains\Attachable\Enums\AttachmentType;
use App\Domains\Attachable\Exceptions\AttachmentStorageException;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Attachable\Models\AttachmentCrop;
use App\Domains\Attachable\Services\AttachmentService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
@@ -74,32 +75,45 @@ class AttachmentTest extends TestCase
{
Storage::fake('s3');
$original = app(AttachmentService::class)->storeCroppedImage(
$original = app(AttachmentService::class)->storeCroppedImageVariants(
UploadedFile::fake()->image('product.jpg', 200, 100),
'attachments/acme',
['start_percentage' => 25, 'end_percentage' => 75],
['start_percentage' => 10, 'end_percentage' => 85],
[
'desktop' => [
'crop_horizontal' => ['start_percentage' => 25, 'end_percentage' => 75],
'crop_vertical' => ['start_percentage' => 10, 'end_percentage' => 85],
],
'mobile' => [
'crop_horizontal' => ['start_percentage' => 20, 'end_percentage' => 80],
'crop_vertical' => ['start_percentage' => 0, 'end_percentage' => 100],
],
],
);
$cropped = $original->croppedAttachment;
$desktopCrop = $original->cropVariants->firstWhere('variant', AttachmentCrop::DESKTOP);
$mobileCrop = $original->cropVariants->firstWhere('variant', AttachmentCrop::MOBILE);
$cropped = $desktopCrop->croppedAttachment;
$this->assertNotNull($cropped);
$this->assertEquals(
['start_percentage' => 25.0, 'end_percentage' => 75.0],
$original->crop_horizontal,
$desktopCrop->crop_horizontal,
);
$this->assertEquals(
['start_percentage' => 10.0, 'end_percentage' => 85.0],
$original->crop_vertical,
$desktopCrop->crop_vertical,
);
$this->assertTrue($cropped->originalAttachment->is($original));
$this->assertDatabaseCount('attachments', 2);
$this->assertDatabaseHas('attachments', [
'id' => $original->id,
$this->assertTrue($cropped->cropSource->attachment->is($original));
$this->assertDatabaseCount('attachments', 3);
$this->assertDatabaseCount('attachment_crops', 2);
$this->assertDatabaseHas('attachment_crops', [
'attachment_id' => $original->id,
'variant' => AttachmentCrop::DESKTOP,
'cropped_attachment_id' => $cropped->id,
]);
Storage::disk('s3')->assertExists($original->path);
Storage::disk('s3')->assertExists($cropped->path);
Storage::disk('s3')->assertExists($mobileCrop->croppedAttachment->path);
$croppedSize = getimagesizefromstring(Storage::disk('s3')->get($cropped->path));
@@ -113,11 +127,19 @@ class AttachmentTest extends TestCase
Storage::fake('s3');
try {
app(AttachmentService::class)->storeCroppedImage(
app(AttachmentService::class)->storeCroppedImageVariants(
UploadedFile::fake()->image('product.png'),
'attachments/acme',
['start_percentage' => 75, 'end_percentage' => 25],
['start_percentage' => 0, 'end_percentage' => 100],
[
'desktop' => [
'crop_horizontal' => ['start_percentage' => 75, 'end_percentage' => 25],
'crop_vertical' => ['start_percentage' => 0, 'end_percentage' => 100],
],
'mobile' => [
'crop_horizontal' => ['start_percentage' => 0, 'end_percentage' => 100],
'crop_vertical' => ['start_percentage' => 0, 'end_percentage' => 100],
],
],
);
$this->fail('Expected an AttachmentStorageException to be thrown.');
@@ -132,11 +154,10 @@ class AttachmentTest extends TestCase
Storage::fake('s3');
try {
app(AttachmentService::class)->storeCroppedImage(
app(AttachmentService::class)->storeCroppedImageVariants(
UploadedFile::fake()->createWithContent('invalid.png', 'not-an-image'),
'attachments/acme',
['start_percentage' => 10, 'end_percentage' => 90],
['start_percentage' => 10, 'end_percentage' => 90],
$this->fullCropVariants(),
);
$this->fail('Expected an AttachmentStorageException to be thrown.');
@@ -150,45 +171,56 @@ class AttachmentTest extends TestCase
{
Storage::fake('s3');
$original = app(AttachmentService::class)->storeCroppedImage(
$original = app(AttachmentService::class)->storeCroppedImageVariants(
UploadedFile::fake()->image('product.png'),
'attachments/acme',
['start_percentage' => 10, 'end_percentage' => 90],
['start_percentage' => 10, 'end_percentage' => 90],
$this->fullCropVariants(),
);
$cropped = $original->croppedAttachment;
$cropped = $original->cropVariants->pluck('croppedAttachment');
app(AttachmentService::class)->delete($original);
$this->assertDatabaseCount('attachments', 0);
Storage::disk('s3')->assertMissing($original->path);
Storage::disk('s3')->assertMissing($cropped->path);
foreach ($cropped as $variant) {
Storage::disk('s3')->assertMissing($variant->path);
}
}
public function test_it_replaces_the_crop_of_an_existing_image(): void
{
Storage::fake('s3');
$original = app(AttachmentService::class)->storeCroppedImage(
$original = app(AttachmentService::class)->storeCroppedImageVariants(
UploadedFile::fake()->image('product.jpg', 200, 100),
'attachments/acme',
['start_percentage' => 0, 'end_percentage' => 100],
['start_percentage' => 0, 'end_percentage' => 100],
$this->fullCropVariants(),
);
$previousCrop = $original->croppedAttachment;
$previousCrops = $original->cropVariants->pluck('croppedAttachment');
$updated = app(AttachmentService::class)->updateImageCrop(
$updated = app(AttachmentService::class)->updateImageCropVariants(
$original,
['start_percentage' => 25, 'end_percentage' => 75],
['start_percentage' => 10, 'end_percentage' => 85],
[
'desktop' => [
'crop_horizontal' => ['start_percentage' => 25, 'end_percentage' => 75],
'crop_vertical' => ['start_percentage' => 10, 'end_percentage' => 85],
],
'mobile' => [
'crop_horizontal' => ['start_percentage' => 30, 'end_percentage' => 70],
'crop_vertical' => ['start_percentage' => 0, 'end_percentage' => 100],
],
],
);
$this->assertFalse($updated->croppedAttachment->is($previousCrop));
$this->assertDatabaseMissing('attachments', ['id' => $previousCrop->id]);
Storage::disk('s3')->assertMissing($previousCrop->path);
foreach ($previousCrops as $previousCrop) {
$this->assertDatabaseMissing('attachments', ['id' => $previousCrop->id]);
Storage::disk('s3')->assertMissing($previousCrop->path);
}
$desktop = $updated->cropVariants->firstWhere('variant', AttachmentCrop::DESKTOP);
$croppedSize = getimagesizefromstring(
Storage::disk('s3')->get($updated->croppedAttachment->path)
Storage::disk('s3')->get($desktop->croppedAttachment->path)
);
$this->assertIsArray($croppedSize);
@@ -252,4 +284,14 @@ class AttachmentTest extends TestCase
]);
}
}
private function fullCropVariants(): array
{
$crop = [
'crop_horizontal' => ['start_percentage' => 0, 'end_percentage' => 100],
'crop_vertical' => ['start_percentage' => 0, 'end_percentage' => 100],
];
return ['desktop' => $crop, 'mobile' => $crop];
}
}