Skip to content

Commit 8132d97

Browse files
committed
Added unit tests
1 parent 9d0f830 commit 8132d97

File tree

3 files changed

+219
-25
lines changed

3 files changed

+219
-25
lines changed

app/code/Magento/MediaGallery/Model/Keyword/Command/SaveAssetLinks.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,16 @@ public function execute(int $assetId, array $keywordIds): void
5555
$values[] = [$assetId, $keywordId];
5656
}
5757

58-
/** @var Mysql $connection */
59-
$connection = $this->resourceConnection->getConnection();
60-
$connection->insertArray(
61-
$this->resourceConnection->getTableName(self::TABLE_ASSET_KEYWORD),
62-
[self::FIELD_ASSET_ID, self::FIELD_KEYWORD_ID],
63-
$values,
64-
AdapterInterface::INSERT_IGNORE
65-
);
58+
if (!empty($values)) {
59+
/** @var Mysql $connection */
60+
$connection = $this->resourceConnection->getConnection();
61+
$connection->insertArray(
62+
$this->resourceConnection->getTableName(self::TABLE_ASSET_KEYWORD),
63+
[self::FIELD_ASSET_ID, self::FIELD_KEYWORD_ID],
64+
$values,
65+
AdapterInterface::INSERT_IGNORE
66+
);
67+
}
6668
} catch (\Exception $exception) {
6769
$message = __('An error occurred during save asset keyword links: %1', $exception->getMessage());
6870
throw new CouldNotSaveException($message, $exception);
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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+
}

app/code/Magento/MediaGallery/Test/Unit/Model/Keyword/Command/SaveAssetLinksTest.php

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,22 @@ class SaveAssetLinksTest extends TestCase
2929
*/
3030
private $connectionMock;
3131

32+
/**
33+
* @var ResourceConnection|MockObject
34+
*/
35+
private $resourceConnectionMock;
36+
3237
/**
3338
* Prepare test objects.
3439
*/
3540
public function setUp(): void
3641
{
42+
3743
$this->connectionMock = $this->createMock(AdapterInterface::class);
38-
$resourceConnectionMock = $this->createMock(ResourceConnection::class);
39-
$resourceConnectionMock->expects($this->once())
40-
->method('getConnection')
41-
->willReturn($this->connectionMock);
42-
$resourceConnectionMock->expects($this->once())
43-
->method('getTableName')
44-
->with('media_gallery_asset_keyword')
45-
->willReturn('prefix_media_gallery_asset_keyword');
44+
$this->resourceConnectionMock = $this->createMock(ResourceConnection::class);
4645

4746
$this->sut = new SaveAssetLinks(
48-
$resourceConnectionMock
47+
$this->resourceConnectionMock
4948
);
5049
}
5150

@@ -60,14 +59,25 @@ public function setUp(): void
6059
*/
6160
public function testAssetKeywordsSave(int $assetId, array $keywordIds, array $values): void
6261
{
63-
$this->connectionMock->expects($this->once())
64-
->method('insertArray')
65-
->with(
66-
'prefix_media_gallery_asset_keyword',
67-
['asset_id', 'keyword_id'],
68-
$values,
69-
2
70-
);
62+
$expectedCalls = (int) (count($keywordIds));
63+
64+
if ($expectedCalls) {
65+
$this->resourceConnectionMock->expects($this->once())
66+
->method('getConnection')
67+
->willReturn($this->connectionMock);
68+
$this->resourceConnectionMock->expects($this->once())
69+
->method('getTableName')
70+
->with('media_gallery_asset_keyword')
71+
->willReturn('prefix_media_gallery_asset_keyword');
72+
$this->connectionMock->expects($this->once())
73+
->method('insertArray')
74+
->with(
75+
'prefix_media_gallery_asset_keyword',
76+
['asset_id', 'keyword_id'],
77+
$values,
78+
2
79+
);
80+
}
7181

7282
$this->sut->execute($assetId, $keywordIds);
7383
}
@@ -79,6 +89,9 @@ public function testAssetKeywordsSave(int $assetId, array $keywordIds, array $va
7989
*/
8090
public function testAssetNotSavingCausedByError(): void
8191
{
92+
$this->resourceConnectionMock->expects($this->once())
93+
->method('getConnection')
94+
->willReturn($this->connectionMock);
8295
$this->connectionMock->expects($this->once())
8396
->method('insertArray')
8497
->willThrowException((new \Exception()));

0 commit comments

Comments
 (0)