diff --git a/src/Sylius/Behat/Service/Checker/ImageExistenceChecker.php b/src/Sylius/Behat/Service/Checker/ImageExistenceChecker.php index 1dc326f6c9e..a042d884093 100644 --- a/src/Sylius/Behat/Service/Checker/ImageExistenceChecker.php +++ b/src/Sylius/Behat/Service/Checker/ImageExistenceChecker.php @@ -1,5 +1,14 @@ filterService->getUrlOfFilteredImage($imageUrl, $liipImagineFilter); - return file_exists($this->mediaRootPath.parse_url($browserImagePath, PHP_URL_PATH)); + return file_exists($this->mediaRootPath . parse_url($browserImagePath, PHP_URL_PATH)); } } diff --git a/src/Sylius/Behat/Service/Checker/ImageExistenceCheckerInterface.php b/src/Sylius/Behat/Service/Checker/ImageExistenceCheckerInterface.php index d5bca627015..8de400404d1 100644 --- a/src/Sylius/Behat/Service/Checker/ImageExistenceCheckerInterface.php +++ b/src/Sylius/Behat/Service/Checker/ImageExistenceCheckerInterface.php @@ -1,5 +1,14 @@ imageUploader = $imageUploader; @@ -37,13 +44,24 @@ public function __construct(ImageUploaderInterface $imageUploader, CacheManager $this->filterManager = $filterManager; } - public function postRemove(LifecycleEventArgs $event): void + public function onFlush(OnFlushEventArgs $event): void { - $image = $event->getEntity(); + foreach ($event->getEntityManager()->getUnitOfWork()->getScheduledEntityDeletions() as $entityDeletion) { + if (!$entityDeletion instanceof ImageInterface) { + continue; + } + + if (!in_array($entityDeletion->getPath(), $this->imagesToDelete)) { + $this->imagesToDelete[] = $entityDeletion->getPath(); + } + } + } - if ($image instanceof ImageInterface) { - $this->imageUploader->remove($image->getPath()); - $this->cacheManager->remove($image->getPath(), array_keys($this->filterManager->getFilterConfiguration()->all())); + public function postFlush(PostFlushEventArgs $event): void + { + foreach ($this->imagesToDelete as $imagePath) { + $this->imageUploader->remove($imagePath); + $this->cacheManager->remove($imagePath, array_keys($this->filterManager->getFilterConfiguration()->all())); } } } diff --git a/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml b/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml index d825a3a164c..60a8eccd449 100644 --- a/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml +++ b/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml @@ -36,7 +36,8 @@ - + + diff --git a/src/Sylius/Bundle/CoreBundle/Resources/config/test_services.xml b/src/Sylius/Bundle/CoreBundle/Resources/config/test_services.xml index 7bfd70d5054..698cd74fcf6 100644 --- a/src/Sylius/Bundle/CoreBundle/Resources/config/test_services.xml +++ b/src/Sylius/Bundle/CoreBundle/Resources/config/test_services.xml @@ -52,6 +52,7 @@ - + + diff --git a/src/Sylius/Bundle/CoreBundle/spec/EventListener/ImagesRemoveListenerSpec.php b/src/Sylius/Bundle/CoreBundle/spec/EventListener/ImagesRemoveListenerSpec.php index 8c9d1bc80f9..515d8ef54bd 100644 --- a/src/Sylius/Bundle/CoreBundle/spec/EventListener/ImagesRemoveListenerSpec.php +++ b/src/Sylius/Bundle/CoreBundle/spec/EventListener/ImagesRemoveListenerSpec.php @@ -13,14 +13,17 @@ namespace spec\Sylius\Bundle\CoreBundle\EventListener; -use Doctrine\ORM\Event\LifecycleEventArgs; +use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\Event\OnFlushEventArgs; +use Doctrine\ORM\Event\PostFlushEventArgs; +use Doctrine\ORM\UnitOfWork; use Liip\ImagineBundle\Imagine\Cache\CacheManager; use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration; use Liip\ImagineBundle\Imagine\Filter\FilterManager; use PhpSpec\ObjectBehavior; -use Prophecy\Argument; use Sylius\Bundle\CoreBundle\EventListener\ImagesRemoveListener; use Sylius\Component\Core\Model\ImageInterface; +use Sylius\Component\Core\Model\ProductInterface; use Sylius\Component\Core\Uploader\ImageUploaderInterface; final class ImagesRemoveListenerSpec extends ObjectBehavior @@ -35,36 +38,49 @@ function it_is_initializable(): void $this->shouldHaveType(ImagesRemoveListener::class); } - function it_removes_file_on_post_remove_event( + function it_saves_scheduled_entity_deletions_images_paths( + OnFlushEventArgs $event, + EntityManagerInterface $entityManager, + UnitOfWork $unitOfWork, + ImageInterface $image, + ProductInterface $product + ): void { + $event->getEntityManager()->willReturn($entityManager); + $entityManager->getUnitOfWork()->willReturn($unitOfWork); + $unitOfWork->getScheduledEntityDeletions()->willReturn([$image, $product]); + + $image->getPath()->shouldBeCalled(); + + $this->onFlush($event); + } + + function it_removes_saved_images_paths( ImageUploaderInterface $imageUploader, CacheManager $cacheManager, FilterManager $filterManager, - LifecycleEventArgs $event, + OnFlushEventArgs $onFlushEvent, + PostFlushEventArgs $postFlushEvent, + EntityManagerInterface $entityManager, + UnitOfWork $unitOfWork, ImageInterface $image, + ProductInterface $product, FilterConfiguration $filterConfiguration ): void { - $event->getEntity()->willReturn($image); + $onFlushEvent->getEntityManager()->willReturn($entityManager); + $entityManager->getUnitOfWork()->willReturn($unitOfWork); + $unitOfWork->getScheduledEntityDeletions()->willReturn([$image, $product]); + $image->getPath()->willReturn('image/path'); - $filterManager->getFilterConfiguration()->willReturn($filterConfiguration); - $filterConfiguration->all()->willReturn(['sylius_small' => 'thumbnalis']); + $this->onFlush($onFlushEvent); + $imageUploader->remove('image/path')->shouldBeCalled(); - $cacheManager->remove('image/path', ['sylius_small'])->shouldBeCalled(); - $this->postRemove($event); - } + $filterManager->getFilterConfiguration()->willReturn($filterConfiguration); + $filterConfiguration->all()->willReturn(['test' => 'Test']); - function it_does_nothing_if_entity_is_not_image( - ImageUploaderInterface $imageUploader, - CacheManager $cacheManager, - FilterManager $filterManager, - LifecycleEventArgs $event - ): void { - $event->getEntity()->willReturn(new \stdClass()); - $filterManager->getFilterConfiguration()->shouldNotBeCalled(); - $imageUploader->remove(Argument::any())->shouldNotBeCalled(); - $cacheManager->remove(Argument::any(), Argument::any())->shouldNotBeCalled(); + $cacheManager->remove('image/path', ['test'])->shouldBeCalled(); - $this->postRemove($event); + $this->postFlush($postFlushEvent); } }