diff --git a/docs/source/variants.rst b/docs/source/variants.rst index 283b356..810ad73 100644 --- a/docs/source/variants.rst +++ b/docs/source/variants.rst @@ -152,14 +152,22 @@ Variants can be created from the ``ImageManipulator`` class. This will create a $variantMedia = ImageManipulator::createImageVariant($originalMedia, 'thumbnail'); -Depending on the size of the files and the nature of the manipulations, creating variants may be a time consuming operation. As such, it may be more beneficial to perform the operation asynchronously. The ``CreateImageVariants`` job can be used to easily queue variants to be processed. Multiple variant names can be provided in order to process the creation of several variants as part of the same worker process. +Depending on the size of the files and the nature of the manipulations, creating variants may be a time consuming operation. As such, it may be more beneficial to perform the operation asynchronously. The ``CreateImageVariants`` job can be used to easily queue variants to be processed. A collection of ``Media`` records and multiple variant names can be provided in order to process the creation of several variants as part of the same worker process. :: collect($models); $variantNames = (array) $variantNames; - $this->validate($model, $variantNames); + $this->validate($models, $variantNames); $this->variantNames = $variantNames; - $this->model = $model; + $this->models = $models; $this->forceRecreate = $forceRecreate; } public function handle() { - foreach ($this->getVariantNames() as $variantName) { - $this->getImageManipulator()->createImageVariant( - $this->getModel(), - $variantName, - $this->getForceRecreate() - ); + foreach ($this->getModels() as $model) { + foreach ($this->getVariantNames() as $variantName) { + $this->getImageManipulator()->createImageVariant( + $model, + $variantName, + $this->getForceRecreate() + ); + } } } @@ -65,11 +69,11 @@ public function getVariantNames(): array } /** - * @return Media + * @return Collection|Media[] */ - public function getModel(): Media + public function getModels(): Collection { - return $this->model; + return $this->models; } /** @@ -77,10 +81,12 @@ public function getModel(): Media * @param array $variantNames * @throws ImageManipulationException */ - private function validate(Media $media, array $variantNames): void + private function validate(Collection $models, array $variantNames): void { $imageManipulator = $this->getImageManipulator(); - $imageManipulator->validateMedia($media); + foreach ($models as $media) { + $imageManipulator->validateMedia($media); + } foreach ($variantNames as $variantName) { $imageManipulator->getVariantDefinition($variantName); } @@ -98,4 +104,16 @@ public function getForceRecreate(): bool { return $this->forceRecreate; } + + /** + * @param Media|Collection|Media[] $models + * @return bool + */ + private function collect($models): Collection + { + if ($models instanceof Media) { + $models = [$models]; + } + return new Collection($models); + } } diff --git a/tests/Integration/Jobs/CreateImageVariantsTest.php b/tests/Integration/Jobs/CreateImageVariantsTest.php index 04a4354..eb6c5c5 100644 --- a/tests/Integration/Jobs/CreateImageVariantsTest.php +++ b/tests/Integration/Jobs/CreateImageVariantsTest.php @@ -2,6 +2,7 @@ namespace Plank\Mediable\Tests\Integration\Jobs; +use Illuminate\Database\Eloquent\Collection; use Plank\Mediable\ImageManipulation; use Plank\Mediable\ImageManipulator; use Plank\Mediable\Jobs\CreateImageVariants; @@ -12,23 +13,53 @@ class CreateImageVariantsTest extends TestCase public function test_it_will_trigger_image_manipulation() { $model = $this->makeMedia(['aggregate_type' => 'image']); - $variant1 = 'foo'; - $variant2 = 'bar'; + $variant = 'foo'; $manipulator = $this->createMock(ImageManipulator::class); $manipulator->expects($this->once()) ->method('validateMedia') ->with($model); + $manipulator->expects($this->once()) + ->method('getVariantDefinition') + ->with($variant) + ->willReturn($this->createMock(ImageManipulation::class)); + $manipulator->expects($this->once()) + ->method('createImageVariant') + ->withConsecutive([$model, $variant, false]); + app()->instance(ImageManipulator::class, $manipulator); + + $job = new CreateImageVariants($model, $variant); + $job->handle(); + } + + public function test_it_will_trigger_image_manipulation_multiple() + { + $model1 = $this->makeMedia(['aggregate_type' => 'image']); + $model2 = $this->makeMedia(['aggregate_type' => 'image']); + $variant1 = 'foo'; + $variant2 = 'bar'; + + $manipulator = $this->createMock(ImageManipulator::class); + $manipulator->expects($this->exactly(2)) + ->method('validateMedia') + ->withConsecutive([$model1],[$model2]); $manipulator->expects($this->exactly(2)) ->method('getVariantDefinition') ->withConsecutive([$variant1], [$variant2]) ->willReturn($this->createMock(ImageManipulation::class)); - $manipulator->expects($this->exactly(2)) + $manipulator->expects($this->exactly(4)) ->method('createImageVariant') - ->withConsecutive([$model, $variant1, false], [$model, $variant2, false]); + ->withConsecutive( + [$model1, $variant1, false], + [$model1, $variant2, false], + [$model2, $variant1, false], + [$model2, $variant2, false] + ); app()->instance(ImageManipulator::class, $manipulator); - $job = new CreateImageVariants($model, [$variant1, $variant2]); + $job = new CreateImageVariants( + new Collection([$model1, $model2]), [$variant1, $variant2] + ); $job->handle(); } @@ -48,7 +79,10 @@ public function test_it_will_trigger_image_manipulation_recreate() ->willReturn($this->createMock(ImageManipulation::class)); $manipulator->expects($this->exactly(2)) ->method('createImageVariant') - ->withConsecutive([$model, $variant1, true], [$model, $variant2, true]); + ->withConsecutive( + [$model, $variant1, true], + [$model, $variant2, true] + ); app()->instance(ImageManipulator::class, $manipulator); $job = new CreateImageVariants($model, [$variant1, $variant2], true);