Skip to content

Commit

Permalink
more robust migration
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Oct 14, 2024
1 parent bd58d46 commit 3d7bc6d
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ return new class extends Migration
): array {

return collect($generatedConversions)
->flatMap(function (array $generatedConversion, string $conversionName) use ($parent) {
->flatMap(function ($generatedConversion, $conversionName) use ($parent) {

if(!is_array($generatedConversion)){
return [];
}

if(empty($generatedConversion)){
return [];
}

$fullName = $parent ? "{$parent}.{$conversionName}" : $conversionName;

Expand Down
108 changes: 108 additions & 0 deletions tests/Unit/MigrateGeneratedConversionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

it('transform an array of generated conversions into MediaConversion array', function () {

$generated_conversions = [
null,
[],
'',
'foo' => [
'state' => 'success',
'state_set_at' => now()->toJSON(),
'disk' => 's3',
'path' => 'uuid/foo.jpg',
'type' => 'image',
'name' => 'foo',
'extension' => 'jpg',
'file_name' => 'foo.jpg',
'mime_type' => 'image/jpeg',
'width' => 16,
'height' => 9,
'aspect_ratio' => 16 / 9,
'average_color' => null,
'size' => 100,
'duration' => null,
'metadata' => [],
'created_at' => now()->toJSON(),
'updated_at' => now()->toJSON(),
'generated_conversions' => [
null,
[],
'',
'nested' => [
'state' => 'success',
'state_set_at' => now()->toJSON(),
'disk' => 's3',
'path' => 'uuid/nested.jpg',
'type' => 'image',
'name' => 'nested',
'extension' => 'jpg',
'file_name' => 'nested.jpg',
'mime_type' => 'image/jpeg',
'width' => 16,
'height' => 9,
'aspect_ratio' => 16 / 9,
'average_color' => null,
'size' => 100,
'duration' => null,
'metadata' => [],
'created_at' => now()->toJSON(),
'updated_at' => now()->toJSON(),
'generated_conversions' => [
'supernested' => [
'state' => 'success',
'state_set_at' => now()->toJSON(),
'disk' => 's3',
'path' => 'uuid/supernested.jpg',
'type' => 'image',
'name' => 'supernested',
'extension' => 'jpg',
'file_name' => 'supernested.jpg',
'mime_type' => 'image/jpeg',
'width' => 16,
'height' => 9,
'aspect_ratio' => 16 / 9,
'average_color' => null,
'size' => 100,
'duration' => null,
'metadata' => [],
'created_at' => now()->toJSON(),
'updated_at' => now()->toJSON(),
],
],
],
],
],
'bar' => [
'state' => 'success',
'state_set_at' => now()->toJSON(),
'disk' => 's3',
'path' => 'uuid/bar.jpg',
'type' => 'image',
'name' => 'bar',
'extension' => 'jpg',
'file_name' => 'bar.jpg',
'mime_type' => 'image/jpeg',
'width' => 16,
'height' => 9,
'aspect_ratio' => 16 / 9,
'average_color' => null,
'size' => 100,
'duration' => null,
'metadata' => [],
'created_at' => now()->toJSON(),
'updated_at' => now()->toJSON(),
],
];

$migration = include __DIR__.'/../../database/migrations/migrate_generated_conversions_to_media_conversions_table.php.stub';

$conversions = collect($migration->generatedConversionsToMediaConversions($generated_conversions));

expect($conversions)->toHaveLength(4);
expect($conversions->firstWhere('conversion_name', 'foo'))->not->toBe(null);
expect($conversions->firstWhere('conversion_name', 'foo.nested'))->not->toBe(null);
expect($conversions->firstWhere('conversion_name', 'foo.nested.supernested'))->not->toBe(null);
expect($conversions->firstWhere('conversion_name', 'bar'))->not->toBe(null);

});

0 comments on commit 3d7bc6d

Please sign in to comment.