|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\MediaGallery\Test\Unit\Model\Asset\Command; |
| 9 | + |
| 10 | +use Magento\MediaGallery\Model\Asset\Command\DeleteByDirectoryPath; |
| 11 | +use Magento\Framework\App\ResourceConnection; |
| 12 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 13 | +use Magento\Framework\Exception\CouldNotDeleteException; |
| 14 | +use PHPUnit\Framework\MockObject\MockObject; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 17 | +use Psr\Log\LoggerInterface; |
| 18 | + |
| 19 | +/** |
| 20 | + * Test the DeleteByDirectoryPath command model |
| 21 | + */ |
| 22 | +class DeleteByDirectoryPathTest extends TestCase |
| 23 | +{ |
| 24 | + private const TABLE_NAME = 'media_gallery_asset'; |
| 25 | + private const DIRECTORY_PATH = 'test-directory-path/'; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var ResourceConnection|MockObject |
| 29 | + */ |
| 30 | + private $resourceConnection; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var DeleteByDirectoryPath |
| 34 | + */ |
| 35 | + private $deleteMediaAssetByDirectoryPath; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var AdapterInterface|MockObject |
| 39 | + */ |
| 40 | + private $adapter; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var LoggerInterface|MockObject |
| 44 | + */ |
| 45 | + private $logger; |
| 46 | + |
| 47 | + /** |
| 48 | + * Initialize basic test class mocks |
| 49 | + */ |
| 50 | + protected function setUp(): void |
| 51 | + { |
| 52 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 53 | + $this->resourceConnection = $this->createMock(ResourceConnection::class); |
| 54 | + |
| 55 | + $this->deleteMediaAssetByDirectoryPath = (new ObjectManager($this))->getObject( |
| 56 | + DeleteByDirectoryPath::class, |
| 57 | + [ |
| 58 | + 'resourceConnection' => $this->resourceConnection, |
| 59 | + 'logger' => $this->logger, |
| 60 | + ] |
| 61 | + ); |
| 62 | + |
| 63 | + $this->adapter = $this->createMock(AdapterInterface::class); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Test delete media asset by path command |
| 68 | + * |
| 69 | + * @param string $directoryPath |
| 70 | + * @throws CouldNotDeleteException |
| 71 | + * @dataProvider directoryPathDataProvider |
| 72 | + */ |
| 73 | + public function testDeleteByDirectoryPath(string $directoryPath): void |
| 74 | + { |
| 75 | + if (!empty($directoryPath)) { |
| 76 | + $this->resourceConnection->expects($this->once()) |
| 77 | + ->method('getConnection') |
| 78 | + ->willReturn($this->adapter); |
| 79 | + $this->resourceConnection->expects($this->once()) |
| 80 | + ->method('getTableName') |
| 81 | + ->with(self::TABLE_NAME) |
| 82 | + ->willReturn('prefix_' . self::TABLE_NAME); |
| 83 | + $this->adapter->expects($this->once()) |
| 84 | + ->method('delete') |
| 85 | + ->with('prefix_' . self::TABLE_NAME, ['path LIKE ?' => self::DIRECTORY_PATH . '%']); |
| 86 | + } else { |
| 87 | + self::expectException('\Magento\Framework\Exception\CouldNotDeleteException'); |
| 88 | + } |
| 89 | + |
| 90 | + $this->deleteMediaAssetByDirectoryPath->execute($directoryPath); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Data provider for directory path |
| 95 | + * |
| 96 | + * @return array |
| 97 | + */ |
| 98 | + public function directoryPathDataProvider(): array |
| 99 | + { |
| 100 | + return [ |
| 101 | + 'Existing path' => [self::DIRECTORY_PATH], |
| 102 | + 'Empty path' => [''] |
| 103 | + ]; |
| 104 | + } |
| 105 | +} |
0 commit comments