Skip to content

Commit 59dac90

Browse files
ENGCOM-6294: 585: cover with unit test the GetById media asset command #25547
2 parents 059eef0 + 25b3546 commit 59dac90

File tree

5 files changed

+416
-12
lines changed

5 files changed

+416
-12
lines changed

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

Lines changed: 22 additions & 12 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
/**
@@ -66,24 +66,34 @@ 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);
73-
$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+
}
7483

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

80-
return $this->assetFactory->create(['data' => $data]);
89+
try {
90+
return $this->assetFactory->create(['data' => $mediaAssetData]);
8191
} catch (\Exception $exception) {
92+
$this->logger->critical($exception->getMessage());
8293
$message = __(
83-
'En error occurred during get media asset with id %id by id: %error',
94+
'En error occurred during initialize media asset with id %id: %error',
8495
['id' => $mediaAssetId, 'error' => $exception->getMessage()]
8596
);
86-
$this->logger->critical($message);
8797
throw new IntegrationException($message, $exception);
8898
}
8999
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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\AssetInterfaceFactory;
17+
use PHPUnit\Framework\MockObject\MockObject;
18+
use Psr\Log\LoggerInterface;
19+
use Zend\Db\Adapter\Driver\Pdo\Statement;
20+
21+
/**
22+
* Test the GetById command with exception during media asset initialization
23+
*/
24+
class GetByIdExceptionDuringMediaAssetInitializationTest extends \PHPUnit\Framework\TestCase
25+
{
26+
private const MEDIA_ASSET_STUB_ID = 1;
27+
28+
private const MEDIA_ASSET_DATA = ['id' => 1];
29+
30+
/**
31+
* @var GetById|MockObject
32+
*/
33+
private $getMediaAssetById;
34+
35+
/**
36+
* @var AssetInterfaceFactory|MockObject
37+
*/
38+
private $assetFactory;
39+
40+
/**
41+
* @var AdapterInterface|MockObject
42+
*/
43+
private $adapter;
44+
45+
/**
46+
* @var Select|MockObject
47+
*/
48+
private $selectStub;
49+
50+
/**
51+
* @var Statement|MockObject
52+
*/
53+
private $statementMock;
54+
55+
/**
56+
* @var LoggerInterface|MockObject
57+
*/
58+
private $logger;
59+
60+
/**
61+
* Initialize basic test class mocks
62+
*/
63+
protected function setUp(): void
64+
{
65+
$resourceConnection = $this->createMock(ResourceConnection::class);
66+
$this->assetFactory = $this->createMock(AssetInterfaceFactory::class);
67+
$this->logger = $this->createMock(LoggerInterface::class);
68+
69+
$this->getMediaAssetById = (new ObjectManager($this))->getObject(
70+
GetById::class,
71+
[
72+
'resourceConnection' => $resourceConnection,
73+
'assetFactory' => $this->assetFactory,
74+
'logger' => $this->logger,
75+
]
76+
);
77+
$this->adapter = $this->createMock(AdapterInterface::class);
78+
$resourceConnection->method('getConnection')->willReturn($this->adapter);
79+
80+
$this->selectStub = $this->createMock(Select::class);
81+
$this->selectStub->method('from')->willReturnSelf();
82+
$this->selectStub->method('where')->willReturnSelf();
83+
$this->adapter->method('select')->willReturn($this->selectStub);
84+
85+
$this->statementMock = $this->getMockBuilder(\Zend_Db_Statement_Interface::class)->getMock();
86+
}
87+
88+
/**
89+
* Test case when a problem occurred during asset initialization from received data.
90+
*/
91+
public function testErrorDuringMediaAssetInitializationException(): void
92+
{
93+
$this->statementMock->method('fetch')->willReturn(self::MEDIA_ASSET_DATA);
94+
$this->adapter->method('query')->willReturn($this->statementMock);
95+
96+
$this->assetFactory->expects($this->once())->method('create')->willThrowException(new \Exception());
97+
98+
$this->expectException(IntegrationException::class);
99+
$this->logger->expects($this->any())
100+
->method('critical')
101+
->willReturnSelf();
102+
103+
$this->getMediaAssetById->execute(self::MEDIA_ASSET_STUB_ID);
104+
}
105+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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\NoSuchEntityException;
14+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15+
use Magento\MediaGallery\Model\Asset\Command\GetById;
16+
use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory;
17+
use PHPUnit\Framework\MockObject\MockObject;
18+
use Psr\Log\LoggerInterface;
19+
use Zend\Db\Adapter\Driver\Pdo\Statement;
20+
21+
/**
22+
* Test the GetById command with exception thrown in case when there is no such entity
23+
*/
24+
class GetByIdExceptionNoSuchEntityTest extends \PHPUnit\Framework\TestCase
25+
{
26+
private const MEDIA_ASSET_STUB_ID = 1;
27+
28+
/**
29+
* @var GetById|MockObject
30+
*/
31+
private $getMediaAssetById;
32+
33+
/**
34+
* @var AssetInterfaceFactory|MockObject
35+
*/
36+
private $assetFactory;
37+
38+
/**
39+
* @var AdapterInterface|MockObject
40+
*/
41+
private $adapter;
42+
43+
/**
44+
* @var Select|MockObject
45+
*/
46+
private $selectStub;
47+
48+
/**
49+
* @var Statement|MockObject
50+
*/
51+
private $statementMock;
52+
53+
/**
54+
* Initialize basic test class mocks
55+
*/
56+
protected function setUp(): void
57+
{
58+
$resourceConnection = $this->createMock(ResourceConnection::class);
59+
$this->assetFactory = $this->createMock(AssetInterfaceFactory::class);
60+
$logger = $this->createMock(LoggerInterface::class);
61+
62+
$this->getMediaAssetById = (new ObjectManager($this))->getObject(
63+
GetById::class,
64+
[
65+
'resourceConnection' => $resourceConnection,
66+
'assetFactory' => $this->assetFactory,
67+
'logger' => $logger,
68+
]
69+
);
70+
$this->adapter = $this->createMock(AdapterInterface::class);
71+
$resourceConnection->method('getConnection')->willReturn($this->adapter);
72+
73+
$this->selectStub = $this->createMock(Select::class);
74+
$this->selectStub->method('from')->willReturnSelf();
75+
$this->selectStub->method('where')->willReturnSelf();
76+
$this->adapter->method('select')->willReturn($this->selectStub);
77+
78+
$this->statementMock = $this->getMockBuilder(\Zend_Db_Statement_Interface::class)->getMock();
79+
}
80+
81+
/**
82+
* Test case when there is no found media asset by id.
83+
*/
84+
public function testNotFoundMediaAssetException(): void
85+
{
86+
$this->statementMock->method('fetch')->willReturn([]);
87+
$this->adapter->method('query')->willReturn($this->statementMock);
88+
89+
$this->expectException(NoSuchEntityException::class);
90+
91+
$this->getMediaAssetById->execute(self::MEDIA_ASSET_STUB_ID);
92+
}
93+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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\AssetInterfaceFactory;
17+
use PHPUnit\Framework\MockObject\MockObject;
18+
use Psr\Log\LoggerInterface;
19+
use Zend\Db\Adapter\Driver\Pdo\Statement;
20+
21+
/**
22+
* Test the GetById command with exception during get media data
23+
*/
24+
class GetByIdExceptionOnGetDataTest extends \PHPUnit\Framework\TestCase
25+
{
26+
private const MEDIA_ASSET_STUB_ID = 1;
27+
28+
private const MEDIA_ASSET_DATA = ['id' => 1];
29+
30+
/**
31+
* @var GetById|MockObject
32+
*/
33+
private $getMediaAssetById;
34+
/**
35+
* @var AdapterInterface|MockObject
36+
*/
37+
private $adapter;
38+
39+
/**
40+
* @var Select|MockObject
41+
*/
42+
private $selectStub;
43+
44+
/**
45+
* @var LoggerInterface|MockObject
46+
*/
47+
private $logger;
48+
49+
/**
50+
* @var Statement|MockObject
51+
*/
52+
private $statementMock;
53+
54+
/**
55+
* Initialize basic test class mocks
56+
*/
57+
protected function setUp(): void
58+
{
59+
$resourceConnection = $this->createMock(ResourceConnection::class);
60+
$assetFactory = $this->createMock(AssetInterfaceFactory::class);
61+
$this->logger = $this->createMock(LoggerInterface::class);
62+
63+
$this->getMediaAssetById = (new ObjectManager($this))->getObject(
64+
GetById::class,
65+
[
66+
'resourceConnection' => $resourceConnection,
67+
'assetFactory' => $assetFactory,
68+
'logger' => $this->logger,
69+
]
70+
);
71+
$this->adapter = $this->createMock(AdapterInterface::class);
72+
$resourceConnection->method('getConnection')->willReturn($this->adapter);
73+
74+
$this->selectStub = $this->createMock(Select::class);
75+
$this->selectStub->method('from')->willReturnSelf();
76+
$this->selectStub->method('where')->willReturnSelf();
77+
$this->adapter->method('select')->willReturn($this->selectStub);
78+
79+
$this->statementMock = $this->getMockBuilder(\Zend_Db_Statement_Interface::class)->getMock();
80+
}
81+
82+
/**
83+
* Test an exception during the get asset data query.
84+
*/
85+
public function testExceptionDuringGetMediaAssetData(): void
86+
{
87+
$this->statementMock->method('fetch')->willReturn(self::MEDIA_ASSET_DATA);
88+
$this->adapter->method('query')->willThrowException(new \Exception());
89+
90+
$this->expectException(IntegrationException::class);
91+
$this->logger->expects($this->any())
92+
->method('critical')
93+
->willReturnSelf();
94+
95+
$this->getMediaAssetById->execute(self::MEDIA_ASSET_STUB_ID);
96+
}
97+
}

0 commit comments

Comments
 (0)