|
| 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\Wysiwyg\Images; |
| 10 | + |
| 11 | +use Magento\Cms\Model\Wysiwyg\Images\Storage as StorageSubject; |
| 12 | +use Magento\Framework\Filesystem; |
| 13 | +use Magento\Framework\Filesystem\Directory\ReadInterface; |
| 14 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 15 | +use Magento\MediaGallery\Plugin\Wysiwyg\Images\Storage; |
| 16 | +use Magento\MediaGalleryApi\Model\Asset\Command\DeleteByPathInterface; |
| 17 | +use Magento\MediaGalleryApi\Model\Asset\Command\GetByPathInterface; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | +use Psr\Log\LoggerInterface; |
| 21 | + |
| 22 | +/** |
| 23 | + * Unit test for \Magento\MediaGallery\Plugin\Wysiwyg\Images\Storage |
| 24 | + */ |
| 25 | +class StorageTest extends TestCase |
| 26 | +{ |
| 27 | + const STUB_TARGET = '/stub/test.png'; |
| 28 | + const STUB_RELATIVE_PATH = 'test.png'; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var Storage |
| 32 | + */ |
| 33 | + private $storage; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var GetByPathInterface|MockObject |
| 37 | + */ |
| 38 | + private $getMediaAssetByPathMock; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var DeleteByPathInterface|MockObject |
| 42 | + */ |
| 43 | + private $deleteMediaAssetByPathMock; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var Filesystem|MockObject |
| 47 | + */ |
| 48 | + private $filesystemMock; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var LoggerInterface|MockObject |
| 52 | + */ |
| 53 | + private $loggerMock; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var StorageSubject|MockObject |
| 57 | + */ |
| 58 | + private $storageSubjectMock; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var ReadInterface|MockObject |
| 62 | + */ |
| 63 | + private $readInterfaceMock; |
| 64 | + |
| 65 | + /** |
| 66 | + * @inheritDoc |
| 67 | + */ |
| 68 | + protected function setUp() |
| 69 | + { |
| 70 | + $this->storageSubjectMock = $this->createMock(StorageSubject::class); |
| 71 | + $this->filesystemMock = $this->createMock(Filesystem::class); |
| 72 | + $this->getMediaAssetByPathMock = $this->createMock(GetByPathInterface::class); |
| 73 | + $this->deleteMediaAssetByPathMock = $this->getMockBuilder(DeleteByPathInterface::class) |
| 74 | + ->disableOriginalConstructor() |
| 75 | + ->setMethods(['execute']) |
| 76 | + ->getMockForAbstractClass(); |
| 77 | + $this->loggerMock = $this->getMockBuilder(LoggerInterface::class) |
| 78 | + ->disableOriginalConstructor() |
| 79 | + ->setMethods(['critical']) |
| 80 | + ->getMockForAbstractClass(); |
| 81 | + $this->readInterfaceMock = $this->getMockBuilder(ReadInterface::class) |
| 82 | + ->disableOriginalConstructor() |
| 83 | + ->setMethods(['getRelativePath']) |
| 84 | + ->getMockForAbstractClass(); |
| 85 | + |
| 86 | + $this->storage = (new ObjectManagerHelper($this))->getObject( |
| 87 | + Storage::class, |
| 88 | + [ |
| 89 | + 'getMediaAssetByPath' => $this->getMediaAssetByPathMock, |
| 90 | + 'deleteMediaAssetByPath' => $this->deleteMediaAssetByPathMock, |
| 91 | + 'filesystem' => $this->filesystemMock, |
| 92 | + 'logger' => $this->loggerMock |
| 93 | + ] |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Test case when an exception is thrown during the method execution. |
| 99 | + */ |
| 100 | + public function testAfterDeleteFileExpectsDeleteMediaAssetExecuted() |
| 101 | + { |
| 102 | + $this->setupMocksToReturnCorrectRelativePath(); |
| 103 | + $this->deleteMediaAssetByPathMock->expects($this->once())->method('execute'); |
| 104 | + $this->loggerMock->expects($this->never())->method('critical'); |
| 105 | + |
| 106 | + $this->executeOriginalMethodWithCorrectTarget(); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Test case when an exception is thrown during the method execution. |
| 111 | + */ |
| 112 | + public function testAfterDeleteFileWithException() |
| 113 | + { |
| 114 | + $this->setupMocksToReturnCorrectRelativePath(); |
| 115 | + $this->deleteMediaAssetByPathMock->expects($this->once()) |
| 116 | + ->method('execute') |
| 117 | + ->willThrowException(new \Exception()); |
| 118 | + $this->loggerMock->expects($this->once())->method('critical'); |
| 119 | + |
| 120 | + $this->executeOriginalMethodWithCorrectTarget(); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Test case when the target is not a string. |
| 125 | + */ |
| 126 | + public function testAfterDeleteFileWhenTargetIsNotString() |
| 127 | + { |
| 128 | + $target = []; |
| 129 | + $this->filesystemMock->expects($this->never())->method('getDirectoryRead'); |
| 130 | + $this->deleteMediaAssetByPathMock->expects($this->never())->method('execute'); |
| 131 | + $this->assertSame( |
| 132 | + $this->storageSubjectMock, |
| 133 | + $this->storage->afterDeleteFile($this->storageSubjectMock, $this->storageSubjectMock, $target) |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Test case when there is no Relative Path which is need to be deleted. |
| 139 | + */ |
| 140 | + public function testAfterDeleteFileWhenRelativePathIsEmpty() |
| 141 | + { |
| 142 | + $this->readInterfaceMock->expects($this->once()) |
| 143 | + ->method('getRelativePath') |
| 144 | + ->willReturn(''); |
| 145 | + $this->filesystemMock->expects($this->once()) |
| 146 | + ->method('getDirectoryRead') |
| 147 | + ->willReturn($this->readInterfaceMock); |
| 148 | + |
| 149 | + $this->deleteMediaAssetByPathMock->expects($this->never())->method('execute'); |
| 150 | + $this->executeOriginalMethodWithCorrectTarget(); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Call the tested method |
| 155 | + */ |
| 156 | + private function executeOriginalMethodWithCorrectTarget() |
| 157 | + { |
| 158 | + $this->assertSame( |
| 159 | + $this->storageSubjectMock, |
| 160 | + $this->storage->afterDeleteFile($this->storageSubjectMock, $this->storageSubjectMock, self::STUB_TARGET) |
| 161 | + ); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Set mocks in order to return the relative path |
| 166 | + */ |
| 167 | + private function setupMocksToReturnCorrectRelativePath() |
| 168 | + { |
| 169 | + $this->readInterfaceMock->expects($this->once()) |
| 170 | + ->method('getRelativePath') |
| 171 | + ->willReturn(self::STUB_RELATIVE_PATH); |
| 172 | + $this->filesystemMock->expects($this->once()) |
| 173 | + ->method('getDirectoryRead') |
| 174 | + ->willReturn($this->readInterfaceMock); |
| 175 | + } |
| 176 | +} |
0 commit comments