|
| 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\Save; |
| 11 | +use Magento\MediaGalleryApi\Api\Data\AssetInterface; |
| 12 | +use Magento\MediaGalleryApi\Model\DataExtractorInterface; |
| 13 | +use Magento\Framework\App\ResourceConnection; |
| 14 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 15 | +use Magento\Framework\DB\Adapter\Pdo\Mysql; |
| 16 | +use Magento\Framework\Exception\CouldNotSaveException; |
| 17 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | +use Psr\Log\LoggerInterface; |
| 21 | + |
| 22 | +/** |
| 23 | + * Tests the Save model using PHPUnit |
| 24 | + */ |
| 25 | +class SaveTest extends TestCase |
| 26 | +{ |
| 27 | + |
| 28 | + /** |
| 29 | + * Constant for tablename of media gallery assets |
| 30 | + */ |
| 31 | + private const TABLE_MEDIA_GALLERY_ASSET = 'media_gallery_asset'; |
| 32 | + |
| 33 | + /** |
| 34 | + * Constant for prefixed tablename of media gallery assets |
| 35 | + */ |
| 36 | + private const PREFIXED_TABLE_MEDIA_GALLERY_ASSET = 'prefix_' . self::TABLE_MEDIA_GALLERY_ASSET; |
| 37 | + |
| 38 | + /** |
| 39 | + * Constant for last ID generated after data insertion |
| 40 | + */ |
| 41 | + private const INSERT_ID = '1'; |
| 42 | + |
| 43 | + /** |
| 44 | + * Constant for affected rows count after data insertion |
| 45 | + */ |
| 46 | + private const AFFECTED_ROWS = 1; |
| 47 | + |
| 48 | + /** |
| 49 | + * Constant for image data |
| 50 | + */ |
| 51 | + private const IMAGE_DATA = [ |
| 52 | + 'path' => '/test/path', |
| 53 | + 'title' => 'Test Title', |
| 54 | + 'source' => 'Adobe Stock', |
| 55 | + 'content_type' => 'image/jpeg', |
| 56 | + 'height' => 4863, |
| 57 | + 'width' => 12129 |
| 58 | + ]; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var MockObject | ResourceConnection |
| 62 | + */ |
| 63 | + private $resourceConnectionMock; |
| 64 | + |
| 65 | + /** |
| 66 | + * @var MockObject | DataExtractorInterface |
| 67 | + */ |
| 68 | + private $loggerMock; |
| 69 | + |
| 70 | + /** |
| 71 | + * @var MockObject | LoggerInterface |
| 72 | + */ |
| 73 | + private $extractorMock; |
| 74 | + |
| 75 | + /** |
| 76 | + * @var MockObject | AdapterInterface |
| 77 | + */ |
| 78 | + private $adapterMock; |
| 79 | + |
| 80 | + /** |
| 81 | + * @var MockObject | AssetInterface |
| 82 | + */ |
| 83 | + private $mediaAssetMock; |
| 84 | + |
| 85 | + /** |
| 86 | + * @var Save |
| 87 | + */ |
| 88 | + private $save; |
| 89 | + |
| 90 | + /** |
| 91 | + * Set up test mocks |
| 92 | + */ |
| 93 | + protected function setUp(): void |
| 94 | + { |
| 95 | + /* Intermediary mocks */ |
| 96 | + $this->adapterMock = $this->createMock(Mysql::class); |
| 97 | + $this->mediaAssetMock = $this->createMock(AssetInterface::class); |
| 98 | + |
| 99 | + /* Save constructor mocks */ |
| 100 | + $this->extractorMock = $this->createMock(DataExtractorInterface::class); |
| 101 | + $this->loggerMock = $this->createMock(LoggerInterface::class); |
| 102 | + $this->resourceConnectionMock = $this->createConfiguredMock( |
| 103 | + ResourceConnection::class, |
| 104 | + [ |
| 105 | + 'getConnection' => $this->adapterMock, |
| 106 | + 'getTableName' => self::PREFIXED_TABLE_MEDIA_GALLERY_ASSET |
| 107 | + ] |
| 108 | + ); |
| 109 | + |
| 110 | + /* Create Save instance with mocks */ |
| 111 | + $this->save = (new ObjectManager($this))->getObject( |
| 112 | + Save::class, |
| 113 | + [ |
| 114 | + 'resourceConnection' => $this->resourceConnectionMock, |
| 115 | + 'extractor' => $this->extractorMock, |
| 116 | + 'logger' => $this->loggerMock |
| 117 | + ] |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Tests a successful Save::execute method |
| 123 | + */ |
| 124 | + public function testSuccessfulExecute(): void |
| 125 | + { |
| 126 | + $this->resourceConnectionMock->expects(self::once())->method('getConnection'); |
| 127 | + $this->resourceConnectionMock->expects(self::once())->method('getTableName'); |
| 128 | + |
| 129 | + $this->extractorMock |
| 130 | + ->expects(self::once()) |
| 131 | + ->method('extract') |
| 132 | + ->with($this->mediaAssetMock, AssetInterface::class) |
| 133 | + ->willReturn(self::IMAGE_DATA); |
| 134 | + |
| 135 | + $this->adapterMock |
| 136 | + ->expects(self::once()) |
| 137 | + ->method('insertOnDuplicate') |
| 138 | + ->with(self::PREFIXED_TABLE_MEDIA_GALLERY_ASSET, self::IMAGE_DATA) |
| 139 | + ->willReturn(self::AFFECTED_ROWS); |
| 140 | + |
| 141 | + $this->adapterMock |
| 142 | + ->expects(self::once()) |
| 143 | + ->method('lastInsertId') |
| 144 | + ->with(self::PREFIXED_TABLE_MEDIA_GALLERY_ASSET) |
| 145 | + ->willReturn(self::INSERT_ID); |
| 146 | + |
| 147 | + $this->save->execute($this->mediaAssetMock); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Tests Save::execute method with an exception thrown |
| 152 | + */ |
| 153 | + public function testExceptionExecute(): void |
| 154 | + { |
| 155 | + $this->resourceConnectionMock->expects(self::once())->method('getConnection'); |
| 156 | + $this->resourceConnectionMock->expects(self::once())->method('getTableName'); |
| 157 | + |
| 158 | + $this->extractorMock |
| 159 | + ->expects(self::once()) |
| 160 | + ->method('extract') |
| 161 | + ->with($this->mediaAssetMock, AssetInterface::class) |
| 162 | + ->willReturn(self::IMAGE_DATA); |
| 163 | + |
| 164 | + $this->adapterMock |
| 165 | + ->expects(self::once()) |
| 166 | + ->method('insertOnDuplicate') |
| 167 | + ->with(self::PREFIXED_TABLE_MEDIA_GALLERY_ASSET, self::IMAGE_DATA) |
| 168 | + ->willThrowException(new \Zend_Db_Exception()); |
| 169 | + |
| 170 | + $this->loggerMock |
| 171 | + ->expects(self::once()) |
| 172 | + ->method('critical') |
| 173 | + ->willReturnSelf(); |
| 174 | + |
| 175 | + $this->expectException(CouldNotSaveException::class); |
| 176 | + |
| 177 | + $this->save->execute($this->mediaAssetMock); |
| 178 | + } |
| 179 | +} |
0 commit comments