Skip to content

Commit 1b2a501

Browse files
authored
Merge pull request magento#1836 from engcom-Golf/asi-1824
Cover SaveImageInterface with integration test
2 parents 59873a2 + e8cbe3e commit 1b2a501

File tree

3 files changed

+258
-0
lines changed

3 files changed

+258
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\AdobeStockImage\Test\Integration\Model;
10+
11+
use Magento\Framework\Filesystem\Driver\Https;
12+
13+
/**
14+
* Class DriverMock represents overwritten methods
15+
*/
16+
class HttpsDriverMock extends Https
17+
{
18+
/**
19+
* Retrieve file contents from given path
20+
*
21+
* @param string $path
22+
* @param string|null $flags
23+
* @param resource|null $context
24+
* @return string
25+
*/
26+
public function fileGetContents($path, $flags = null, $context = null)
27+
{
28+
return file_get_contents($path, (bool)$flags, $context);
29+
}
30+
}
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\AdobeStockImage\Test\Integration\Model;
10+
11+
use Magento\AdobeStockAssetApi\Api\AssetRepositoryInterface;
12+
use Magento\AdobeStockImageApi\Api\SaveImageInterface;
13+
use Magento\Framework\Api\AttributeValueFactory;
14+
use Magento\Framework\Api\Search\Document;
15+
use Magento\Framework\Api\SearchCriteriaBuilder;
16+
use Magento\Framework\App\Filesystem\DirectoryList;
17+
use Magento\Framework\Filesystem;
18+
use Magento\Framework\Filesystem\Driver\Https;
19+
use Magento\MediaGalleryApi\Api\GetAssetsByPathsInterface;
20+
use Magento\TestFramework\Helper\Bootstrap;
21+
use PHPUnit\Framework\TestCase;
22+
23+
/**
24+
* Test client for communication to Adobe Stock API.
25+
*/
26+
class SaveImageTest extends TestCase
27+
{
28+
/**
29+
* @var Filesystem
30+
*/
31+
private $fileSystem;
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
protected function setUp(): void
37+
{
38+
$this->fileSystem = Bootstrap::getObjectManager()->get(Filesystem::class);
39+
Bootstrap::getObjectManager()->configure([
40+
'preferences' => [
41+
Https::class => HttpsDriverMock::class
42+
]
43+
]);
44+
}
45+
46+
/**
47+
* Test with image.
48+
*
49+
* @param array $documentData
50+
* @param string $sourceFile
51+
* @param string $destinationPath
52+
* @return void
53+
* @dataProvider getSaveTestDataProvider
54+
*/
55+
public function testSave(array $documentData, string $sourceFile, string $destinationPath): void
56+
{
57+
$this->deleteImage($destinationPath);
58+
$document = $this->getDocument($documentData);
59+
$saveImage = Bootstrap::getObjectManager()->create(SaveImageInterface::class);
60+
$saveImage->execute(
61+
$document,
62+
$this->getImageFilePath($sourceFile),
63+
$destinationPath
64+
);
65+
$this->assertImageSavedToDirectory($destinationPath);
66+
$this->assertAssets($destinationPath, $documentData);
67+
$this->deleteImage($destinationPath);
68+
}
69+
70+
/**
71+
* @return array
72+
*/
73+
public function getSaveTestDataProvider(): array
74+
{
75+
return [
76+
'image_save' => [
77+
'documentData' => [
78+
'id' => 1,
79+
'comp_url' => 'https://test.url/magento-logo.png',
80+
'width' => 110,
81+
'title' => 'test adobe image title',
82+
'content_type' => 'image/png',
83+
'height' => 210,
84+
'some_bool_param' => false,
85+
'some_nullable_param' => null,
86+
'extension_attributes' => [
87+
'title' => 'test adobe image title',
88+
'is_downloaded' => 0,
89+
'is_licensed_locally' => 0,
90+
'thumbnail_240_url' => 'https://test.url/magento-logo.png',
91+
'creator_id' => random_int(0, 2147483647),
92+
'creator_name' => 'Test',
93+
'path' => 'catalog/category/tmp.png',
94+
'content_type' => 'image/png',
95+
'category' => [
96+
'id' => random_int(0, 2147483647),
97+
'name' => 'Test'
98+
],
99+
]
100+
],
101+
'sourcePath' => 'magento-logo.png',
102+
'destinationPath' => 'catalog/category/adobe-stock-save-image-test.png',
103+
]
104+
];
105+
}
106+
107+
/**
108+
* Document for save.
109+
*
110+
* @param array $documentData
111+
* @return Document
112+
*/
113+
private function getDocument(array $documentData): Document
114+
{
115+
$document = new Document($documentData);
116+
$this->addAttributes($document, $documentData['extension_attributes']);
117+
return $document;
118+
}
119+
120+
/**
121+
* Check if image saved by destination path
122+
*
123+
* @param string $destinationPath
124+
* @return void
125+
*/
126+
private function assertImageSavedToDirectory(string $destinationPath): void
127+
{
128+
self::assertTrue(
129+
$this->fileSystem->getDirectoryRead(DirectoryList::MEDIA)->isExist($destinationPath),
130+
'File was not saved by destination'
131+
);
132+
}
133+
134+
/**
135+
* Assert saved assets data
136+
*
137+
* @param string $destinationPath
138+
* @param array $documentData
139+
* @return void
140+
*/
141+
private function assertAssets(string $destinationPath, array $documentData): void
142+
{
143+
$galleryAssets = Bootstrap::getObjectManager()->get(GetAssetsByPathsInterface::class);
144+
$mediaAssets = $galleryAssets->execute([$destinationPath]);
145+
self::assertCount(1, $mediaAssets, 'Wrong gallery assets count');
146+
self::assertEquals(
147+
$documentData['extension_attributes']['title'],
148+
$mediaAssets[0]->getTitle(),
149+
'Wrong gallery assets image title saved'
150+
);
151+
$criteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class);
152+
$searchCriteria = $criteriaBuilder
153+
->addFilter('media_gallery_id', $mediaAssets[0]->getId())
154+
->create();
155+
/** @var AssetRepositoryInterface $stockAssets */
156+
$stockAssets = Bootstrap::getObjectManager()->get(AssetRepositoryInterface::class);
157+
$items = $stockAssets->getList($searchCriteria)->getItems();
158+
self::assertNotEmpty(
159+
$items,
160+
'Image asset was not saved'
161+
);
162+
$item = reset($items);
163+
self::assertEquals(
164+
$documentData['extension_attributes']['creator_id'],
165+
$item->getCreatorId(),
166+
'Wrong stock asset creator id saved'
167+
);
168+
self::assertEquals(
169+
$documentData['extension_attributes']['category']['id'],
170+
$item->getCategoryId(),
171+
'Wrong stock asset category id saved'
172+
);
173+
}
174+
175+
/**
176+
* Add attributes to document
177+
*
178+
* @param Document $document
179+
* @param array $attributes [code => value]
180+
* @return Document
181+
*/
182+
private function addAttributes(Document $document, array $attributes): Document
183+
{
184+
$customAttributes = $document->getCustomAttributes();
185+
$valueFactory = Bootstrap::getObjectManager()->create(
186+
AttributeValueFactory::class
187+
);
188+
foreach ($attributes as $code => $value) {
189+
$attribute = $valueFactory->create();
190+
$attribute->setAttributeCode($code);
191+
$attribute->setValue($value);
192+
$customAttributes[$code] = $attribute;
193+
}
194+
195+
$document->setCustomAttributes($customAttributes);
196+
197+
return $document;
198+
}
199+
200+
/**
201+
* Return image file path
202+
*
203+
* @param string $filename
204+
* @return string
205+
*/
206+
private function getImageFilePath(string $filename): string
207+
{
208+
return implode(
209+
DIRECTORY_SEPARATOR,
210+
[
211+
dirname(__DIR__, 1),
212+
'_files',
213+
$filename
214+
]
215+
);
216+
}
217+
218+
/**
219+
* Delete test image if exists
220+
*
221+
* @param string $destinationPath
222+
* @return void
223+
*/
224+
private function deleteImage(string $destinationPath): void
225+
{
226+
$this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA)->delete($destinationPath);
227+
}
228+
}
9.66 KB
Loading

0 commit comments

Comments
 (0)