Skip to content

Commit d08eb44

Browse files
committed
Change the GetById command execution logic and cover with tests
1 parent 5c3424f commit d08eb44

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
namespace Magento\MediaGallery\Model\Asset\Command;
99

10-
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
11-
use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory;
12-
use Magento\MediaGalleryApi\Model\Asset\Command\GetByIdInterface;
1310
use Magento\Framework\App\ResourceConnection;
1411
use Magento\Framework\Exception\IntegrationException;
1512
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
14+
use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory;
15+
use Magento\MediaGalleryApi\Model\Asset\Command\GetByIdInterface;
1616
use Psr\Log\LoggerInterface;
1717

1818
/**
@@ -71,20 +71,29 @@ public function execute(int $mediaAssetId): AssetInterface
7171
$select = $connection->select()
7272
->from(['amg' => $mediaAssetTable])
7373
->where('amg.id = ?', $mediaAssetId);
74-
$data = $connection->query($select)->fetch();
74+
$mediaAssetData = $connection->query($select)->fetch();
75+
} catch (\Exception $exception) {
76+
$message = __(
77+
'En error occurred during get media asset data by id %id: %error',
78+
['id' => $mediaAssetId, 'error' => $exception->getMessage()]
79+
);
80+
$this->logger->critical($message);
81+
throw new IntegrationException($message, $exception);
82+
}
7583

76-
if (empty($data)) {
77-
$message = __('There is no such media asset with id "%1"', $mediaAssetId);
78-
throw new NoSuchEntityException($message);
79-
}
84+
if (empty($mediaAssetData)) {
85+
$message = __('There is no such media asset with id "%1"', $mediaAssetId);
86+
throw new NoSuchEntityException($message);
87+
}
8088

81-
return $this->assetFactory->create(['data' => $data]);
89+
try {
90+
return $this->assetFactory->create(['data' => $mediaAssetData]);
8291
} catch (\Exception $exception) {
92+
$this->logger->critical($exception->getMessage());
8393
$message = __(
84-
'En error occurred during get media asset with id %id: %error',
94+
'En error occurred during initialize media asset with id %id: %error',
8595
['id' => $mediaAssetId, 'error' => $exception->getMessage()]
8696
);
87-
$this->logger->critical($message);
8897
throw new IntegrationException($message, $exception);
8998
}
9099
}

app/code/Magento/MediaGallery/Test/Unit/Model/Asset/Command/GetByIdTest.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\DB\Adapter\AdapterInterface;
1212
use Magento\Framework\DB\Select;
1313
use Magento\Framework\Exception\IntegrationException;
14+
use Magento\Framework\Exception\NoSuchEntityException;
1415
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1516
use Magento\MediaGallery\Model\Asset\Command\GetById;
1617
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
@@ -22,6 +23,7 @@
2223

2324
/**
2425
* Test the GetById command model
26+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2527
*/
2628
class GetByIdTest extends TestCase
2729
{
@@ -98,6 +100,22 @@ public function testSuccessfulGetByIdExecution(): void
98100
$mediaAssetStub = $this->getMockBuilder(AssetInterface::class)->getMock();
99101
$this->assetFactory->expects($this->once())->method('create')->willReturn($mediaAssetStub);
100102

103+
$this->assertEquals(
104+
$mediaAssetStub,
105+
$this->getMediaAssetById->execute(self::MEDIA_ASSET_STUB_ID)
106+
);
107+
}
108+
109+
/**
110+
* Test an exception during the get asset data query.
111+
*/
112+
public function testExceptionDuringGetMediaAssetData(): void
113+
{
114+
$this->statementMock->method('fetch')->willReturn(self::MEDIA_ASSET_DATA);
115+
$this->adapter->method('query')->willThrowException(new \Exception());
116+
117+
$this->expectException(IntegrationException::class);
118+
101119
$this->getMediaAssetById->execute(self::MEDIA_ASSET_STUB_ID);
102120
}
103121

@@ -109,8 +127,23 @@ public function testNotFoundMediaAssetException(): void
109127
$this->statementMock->method('fetch')->willReturn([]);
110128
$this->adapter->method('query')->willReturn($this->statementMock);
111129

130+
$this->expectException(NoSuchEntityException::class);
131+
132+
$this->getMediaAssetById->execute(self::MEDIA_ASSET_STUB_ID);
133+
}
134+
135+
/**
136+
* Test case when a problem occurred during asset initialization from received data.
137+
*/
138+
public function testErrorDuringMediaAssetInitializationException(): void
139+
{
140+
$this->statementMock->method('fetch')->willReturn(self::MEDIA_ASSET_DATA);
141+
$this->adapter->method('query')->willReturn($this->statementMock);
142+
143+
$this->assetFactory->expects($this->once())->method('create')->willThrowException(new \Exception());
144+
112145
$this->expectException(IntegrationException::class);
113-
$this->logger->expects($this->once())
146+
$this->logger->expects($this->any())
114147
->method('critical')
115148
->willReturnSelf();
116149

0 commit comments

Comments
 (0)