Skip to content

Commit 5c3424f

Browse files
committed
585: cover with unit test the GetById media asset command
1 parent 8112a38 commit 5c3424f

File tree

2 files changed

+122
-2
lines changed

2 files changed

+122
-2
lines changed

app/code/Magento/MediaGallery/Model/Asset/Command/GetById.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ public function __construct(
6666
public function execute(int $mediaAssetId): AssetInterface
6767
{
6868
try {
69+
$mediaAssetTable = $this->resourceConnection->getTableName(self::TABLE_MEDIA_GALLERY_ASSET);
6970
$connection = $this->resourceConnection->getConnection();
7071
$select = $connection->select()
71-
->from(['amg' => $this->resourceConnection->getTableName(self::TABLE_MEDIA_GALLERY_ASSET)])
72+
->from(['amg' => $mediaAssetTable])
7273
->where('amg.id = ?', $mediaAssetId);
7374
$data = $connection->query($select)->fetch();
7475

@@ -80,7 +81,7 @@ public function execute(int $mediaAssetId): AssetInterface
8081
return $this->assetFactory->create(['data' => $data]);
8182
} catch (\Exception $exception) {
8283
$message = __(
83-
'En error occurred during get media asset with id %id by id: %error',
84+
'En error occurred during get media asset with id %id: %error',
8485
['id' => $mediaAssetId, 'error' => $exception->getMessage()]
8586
);
8687
$this->logger->critical($message);
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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

Comments
 (0)