|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\MediaGallery\Test\Unit\Plugin\Product\Gallery; |
| 10 | + |
| 11 | +use Magento\Catalog\Model\Product; |
| 12 | +use Magento\Catalog\Model\Product\Gallery\Processor as ProcessorSubject; |
| 13 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 14 | +use Magento\MediaGallery\Plugin\Product\Gallery\Processor; |
| 15 | +use Magento\MediaGalleryApi\Model\Asset\Command\DeleteByPathInterface; |
| 16 | +use PHPUnit\Framework\MockObject\MockObject; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use Psr\Log\LoggerInterface; |
| 19 | + |
| 20 | +/** |
| 21 | + * Unit test for \Magento\MediaGallery\Plugin\Product\Gallery\Processor |
| 22 | + */ |
| 23 | +class ProcessorTest extends TestCase |
| 24 | +{ |
| 25 | + private const STUB_FILE_NAME = 'file'; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var DeleteByPathInterface|MockObject |
| 29 | + */ |
| 30 | + private $deleteMediaAssetByPathMock; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var LoggerInterface|MockObject |
| 34 | + */ |
| 35 | + private $loggerMock; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var ProcessorSubject|MockObject |
| 39 | + */ |
| 40 | + private $processorSubjectMock; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var Product|MockObject |
| 44 | + */ |
| 45 | + private $productMock; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var Processor |
| 49 | + */ |
| 50 | + private $plugin; |
| 51 | + |
| 52 | + /** |
| 53 | + * @inheritDoc |
| 54 | + */ |
| 55 | + protected function setUp() |
| 56 | + { |
| 57 | + $this->processorSubjectMock = $this->createMock(ProcessorSubject::class); |
| 58 | + $this->productMock = $this->createMock(Product::class); |
| 59 | + |
| 60 | + $this->deleteMediaAssetByPathMock = $this->getMockBuilder(DeleteByPathInterface::class) |
| 61 | + ->disableOriginalConstructor() |
| 62 | + ->setMethods(['execute']) |
| 63 | + ->getMockForAbstractClass(); |
| 64 | + $this->loggerMock = $this->getMockBuilder(LoggerInterface::class) |
| 65 | + ->disableOriginalConstructor() |
| 66 | + ->setMethods(['critical']) |
| 67 | + ->getMockForAbstractClass(); |
| 68 | + |
| 69 | + $this->plugin = (new ObjectManagerHelper($this))->getObject( |
| 70 | + Processor::class, |
| 71 | + [ |
| 72 | + 'deleteMediaAssetByPath' => $this->deleteMediaAssetByPathMock, |
| 73 | + 'logger' => $this->loggerMock |
| 74 | + ] |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Successful test case. |
| 80 | + */ |
| 81 | + public function testAfterRemoveImageExpectsExecuteCalled() |
| 82 | + { |
| 83 | + $this->deleteMediaAssetByPathMock->expects($this->once()) |
| 84 | + ->method('execute') |
| 85 | + ->with(self::STUB_FILE_NAME); |
| 86 | + $this->loggerMock->expects($this->never())->method('critical'); |
| 87 | + |
| 88 | + $actualResult = $this->plugin->afterRemoveImage( |
| 89 | + $this->processorSubjectMock, |
| 90 | + $this->processorSubjectMock, |
| 91 | + $this->productMock, |
| 92 | + self::STUB_FILE_NAME |
| 93 | + ); |
| 94 | + $this->assertSame($this->processorSubjectMock, $actualResult); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Test case when passed File argument is not a string. |
| 99 | + */ |
| 100 | + public function testAfterRemoveImageWithIncorrectFile() |
| 101 | + { |
| 102 | + $this->deleteMediaAssetByPathMock->expects($this->never())->method('execute'); |
| 103 | + $this->loggerMock->expects($this->never())->method('critical'); |
| 104 | + |
| 105 | + $actualResult = $this->plugin->afterRemoveImage( |
| 106 | + $this->processorSubjectMock, |
| 107 | + $this->processorSubjectMock, |
| 108 | + $this->productMock, |
| 109 | + ['non-string-argument' => self::STUB_FILE_NAME] |
| 110 | + ); |
| 111 | + $this->assertSame($this->processorSubjectMock, $actualResult); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Test case when an Exception is thrown. |
| 116 | + */ |
| 117 | + public function testAfterRemoveImageExpectsExecuteWillThrowException() |
| 118 | + { |
| 119 | + $this->deleteMediaAssetByPathMock->expects($this->once()) |
| 120 | + ->method('execute') |
| 121 | + ->with(self::STUB_FILE_NAME) |
| 122 | + ->willThrowException(new \Exception('Some Exception')); |
| 123 | + $this->loggerMock->expects($this->once())->method('critical'); |
| 124 | + |
| 125 | + $actualResult = $this->plugin->afterRemoveImage( |
| 126 | + $this->processorSubjectMock, |
| 127 | + $this->processorSubjectMock, |
| 128 | + $this->productMock, |
| 129 | + self::STUB_FILE_NAME |
| 130 | + ); |
| 131 | + $this->assertSame($this->processorSubjectMock, $actualResult); |
| 132 | + } |
| 133 | +} |
0 commit comments