|
| 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\Framework\App\ResourceConnection; |
| 11 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 12 | +use Magento\Framework\DB\Select; |
| 13 | +use Magento\Framework\Exception\IntegrationException; |
| 14 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 15 | +use Magento\MediaGallery\Model\Asset\Command\GetById; |
| 16 | +use Magento\MediaGalleryApi\Api\Data\AssetInterface; |
| 17 | +use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | +use Psr\Log\LoggerInterface; |
| 21 | +use Zend\Db\Adapter\Driver\Pdo\Statement; |
| 22 | + |
| 23 | +/** |
| 24 | + * Test the GetById command model |
| 25 | + */ |
| 26 | +class GetByIdTest extends TestCase |
| 27 | +{ |
| 28 | + private const MEDIA_ASSET_STUB_ID = 1; |
| 29 | + |
| 30 | + private const MEDIA_ASSET_DATA = ['id' => 1]; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var GetById|MockObject |
| 34 | + */ |
| 35 | + private $getMediaAssetById; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var AssetInterfaceFactory|MockObject |
| 39 | + */ |
| 40 | + private $assetFactory; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var AdapterInterface|MockObject |
| 44 | + */ |
| 45 | + private $adapter; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var Select|MockObject |
| 49 | + */ |
| 50 | + private $selectStub; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var Statement|MockObject |
| 54 | + */ |
| 55 | + private $statementMock; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var LoggerInterface|MockObject |
| 59 | + */ |
| 60 | + private $logger; |
| 61 | + |
| 62 | + /** |
| 63 | + * Initialize basic test class mocks |
| 64 | + */ |
| 65 | + protected function setUp(): void |
| 66 | + { |
| 67 | + $resourceConnection = $this->createMock(ResourceConnection::class); |
| 68 | + $this->assetFactory = $this->createMock(AssetInterfaceFactory::class); |
| 69 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 70 | + |
| 71 | + $this->getMediaAssetById = (new ObjectManager($this))->getObject( |
| 72 | + GetById::class, |
| 73 | + [ |
| 74 | + 'resourceConnection' => $resourceConnection, |
| 75 | + 'assetFactory' => $this->assetFactory, |
| 76 | + 'logger' => $this->logger, |
| 77 | + ] |
| 78 | + ); |
| 79 | + $this->adapter = $this->createMock(AdapterInterface::class); |
| 80 | + $resourceConnection->method('getConnection')->willReturn($this->adapter); |
| 81 | + |
| 82 | + $this->selectStub = $this->createMock(Select::class); |
| 83 | + $this->selectStub->method('from')->willReturnSelf(); |
| 84 | + $this->selectStub->method('where')->willReturnSelf(); |
| 85 | + $this->adapter->method('select')->willReturn($this->selectStub); |
| 86 | + |
| 87 | + $this->statementMock = $this->getMockBuilder(\Zend_Db_Statement_Interface::class)->getMock(); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Test successful get media asset by id command execution. |
| 92 | + */ |
| 93 | + public function testSuccessfulGetByIdExecution(): void |
| 94 | + { |
| 95 | + $this->statementMock->method('fetch')->willReturn(self::MEDIA_ASSET_DATA); |
| 96 | + $this->adapter->method('query')->willReturn($this->statementMock); |
| 97 | + |
| 98 | + $mediaAssetStub = $this->getMockBuilder(AssetInterface::class)->getMock(); |
| 99 | + $this->assetFactory->expects($this->once())->method('create')->willReturn($mediaAssetStub); |
| 100 | + |
| 101 | + $this->getMediaAssetById->execute(self::MEDIA_ASSET_STUB_ID); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Test case when there is no found media asset by id. |
| 106 | + */ |
| 107 | + public function testNotFoundMediaAssetException(): void |
| 108 | + { |
| 109 | + $this->statementMock->method('fetch')->willReturn([]); |
| 110 | + $this->adapter->method('query')->willReturn($this->statementMock); |
| 111 | + |
| 112 | + $this->expectException(IntegrationException::class); |
| 113 | + $this->logger->expects($this->once()) |
| 114 | + ->method('critical') |
| 115 | + ->willReturnSelf(); |
| 116 | + |
| 117 | + $this->getMediaAssetById->execute(self::MEDIA_ASSET_STUB_ID); |
| 118 | + } |
| 119 | +} |
0 commit comments