Skip to content

Commit

Permalink
[Core] Fix issue with deleted images after try to remove the product
Browse files Browse the repository at this point in the history
  • Loading branch information
GSadee committed Oct 26, 2017
1 parent 9e179de commit 5af2ced
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Feature: Deleting a product
Then I should be notified that this product cannot be deleted
And the product "Lamborghini Gallardo model" should still be in the shop

@ui @javascript @todo
@ui @javascript
Scenario: Deleting used product should not remove the image
Given this product has an image "lamborghini.jpg" with "thumbnail" type
And there is a customer "batman@dc.com" that placed an order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Doctrine\ORM\Event\LifecycleEventArgs;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
use Sylius\Component\Core\Model\ImageInterface;
use Sylius\Component\Core\Model\ImagesAwareInterface;
use Sylius\Component\Core\Uploader\ImageUploaderInterface;

final class ImagesRemoveListener
Expand All @@ -39,11 +39,16 @@ public function __construct(ImageUploaderInterface $imageUploader, CacheManager

public function postRemove(LifecycleEventArgs $event): void
{
$image = $event->getEntity();

if ($image instanceof ImageInterface) {
$this->imageUploader->remove($image->getPath());
$this->cacheManager->remove($image->getPath(), array_keys($this->filterManager->getFilterConfiguration()->all()));
$entity = $event->getEntity();

if ($entity instanceof ImagesAwareInterface) {
foreach ($entity->getImages() as $image) {
$this->imageUploader->remove($image->getPath());
$this->cacheManager->remove(
$image->getPath(),
array_keys($this->filterManager->getFilterConfiguration()->all())
);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace spec\Sylius\Bundle\CoreBundle\EventListener;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
Expand All @@ -21,6 +22,7 @@
use Prophecy\Argument;
use Sylius\Bundle\CoreBundle\EventListener\ImagesRemoveListener;
use Sylius\Component\Core\Model\ImageInterface;
use Sylius\Component\Core\Model\ImagesAwareInterface;
use Sylius\Component\Core\Uploader\ImageUploaderInterface;

final class ImagesRemoveListenerSpec extends ObjectBehavior
Expand All @@ -40,10 +42,12 @@ function it_removes_file_on_post_remove_event(
CacheManager $cacheManager,
FilterManager $filterManager,
LifecycleEventArgs $event,
ImagesAwareInterface $imagesAwareEntity,
ImageInterface $image,
FilterConfiguration $filterConfiguration
): void {
$event->getEntity()->willReturn($image);
$event->getEntity()->willReturn($imagesAwareEntity);
$imagesAwareEntity->getImages()->willReturn(new ArrayCollection([$image->getWrappedObject()]));
$image->getPath()->willReturn('image/path');

$filterManager->getFilterConfiguration()->willReturn($filterConfiguration);
Expand All @@ -54,7 +58,7 @@ function it_removes_file_on_post_remove_event(
$this->postRemove($event);
}

function it_does_nothing_if_entity_is_not_image(
function it_does_nothing_if_entity_is_not_image_aware(
ImageUploaderInterface $imageUploader,
CacheManager $cacheManager,
FilterManager $filterManager,
Expand Down

0 comments on commit 5af2ced

Please sign in to comment.