|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Loevgaard\SyliusBrandPlugin\Tests\EventSubscriber; |
| 6 | + |
| 7 | +use Loevgaard\SyliusBrandPlugin\EventSubscriber\ImageUploadSubscriber; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Prophecy\PhpUnit\ProphecyTrait; |
| 10 | +use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent; |
| 11 | +use Sylius\Component\Core\Model\ImageInterface; |
| 12 | +use Sylius\Component\Core\Uploader\ImageUploaderInterface; |
| 13 | + |
| 14 | +class ImageUploadSubscriberTest extends TestCase |
| 15 | +{ |
| 16 | + use ProphecyTrait; |
| 17 | + |
| 18 | + /** @test */ |
| 19 | + public function it_subscribes_to_brand_image_pre_create_event(): void |
| 20 | + { |
| 21 | + $events = ImageUploadSubscriber::getSubscribedEvents(); |
| 22 | + |
| 23 | + self::assertArrayHasKey('loevgaard_sylius_brand.brand_image.pre_create', $events); |
| 24 | + self::assertSame('uploadImage', $events['loevgaard_sylius_brand.brand_image.pre_create']); |
| 25 | + } |
| 26 | + |
| 27 | + /** @test */ |
| 28 | + public function it_uploads_image_when_file_exists(): void |
| 29 | + { |
| 30 | + $image = $this->prophesize(ImageInterface::class); |
| 31 | + $image->hasFile()->willReturn(true); |
| 32 | + $image->getPath()->willReturn('/path/to/image.jpg'); |
| 33 | + |
| 34 | + $uploader = $this->prophesize(ImageUploaderInterface::class); |
| 35 | + $uploader->upload($image->reveal())->shouldBeCalledOnce(); |
| 36 | + |
| 37 | + $event = $this->prophesize(ResourceControllerEvent::class); |
| 38 | + $event->getSubject()->willReturn($image->reveal()); |
| 39 | + $event->stop()->shouldNotBeCalled(); |
| 40 | + |
| 41 | + $subscriber = new ImageUploadSubscriber($uploader->reveal()); |
| 42 | + $subscriber->uploadImage($event->reveal()); |
| 43 | + } |
| 44 | + |
| 45 | + /** @test */ |
| 46 | + public function it_does_not_upload_when_file_does_not_exist(): void |
| 47 | + { |
| 48 | + $image = $this->prophesize(ImageInterface::class); |
| 49 | + $image->hasFile()->willReturn(false); |
| 50 | + $image->getPath()->willReturn('/existing/path.jpg'); |
| 51 | + |
| 52 | + $uploader = $this->prophesize(ImageUploaderInterface::class); |
| 53 | + $uploader->upload()->shouldNotBeCalled(); |
| 54 | + |
| 55 | + $event = $this->prophesize(ResourceControllerEvent::class); |
| 56 | + $event->getSubject()->willReturn($image->reveal()); |
| 57 | + $event->stop()->shouldNotBeCalled(); |
| 58 | + |
| 59 | + $subscriber = new ImageUploadSubscriber($uploader->reveal()); |
| 60 | + $subscriber->uploadImage($event->reveal()); |
| 61 | + } |
| 62 | + |
| 63 | + /** @test */ |
| 64 | + public function it_stops_event_when_path_is_null_after_upload(): void |
| 65 | + { |
| 66 | + $image = $this->prophesize(ImageInterface::class); |
| 67 | + $image->hasFile()->willReturn(true); |
| 68 | + $image->getPath()->willReturn(null); |
| 69 | + |
| 70 | + $uploader = $this->prophesize(ImageUploaderInterface::class); |
| 71 | + $uploader->upload($image->reveal())->shouldBeCalledOnce(); |
| 72 | + |
| 73 | + $event = $this->prophesize(ResourceControllerEvent::class); |
| 74 | + $event->getSubject()->willReturn($image->reveal()); |
| 75 | + $event->stop('loevgaard_sylius_brand.brand_image.upload_error')->shouldBeCalledOnce(); |
| 76 | + |
| 77 | + $subscriber = new ImageUploadSubscriber($uploader->reveal()); |
| 78 | + $subscriber->uploadImage($event->reveal()); |
| 79 | + } |
| 80 | +} |
0 commit comments