Skip to content

Commit 45c6c09

Browse files
author
Viktor Kopin
committed
magento#1824 update after review
1 parent 38da086 commit 45c6c09

File tree

2 files changed

+93
-69
lines changed

2 files changed

+93
-69
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+
}

AdobeStockImage/Test/Integration/Model/SaveImageTest.php

Lines changed: 63 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@
88

99
namespace Magento\AdobeStockImage\Test\Integration\Model;
1010

11-
use AdobeStock\Api\Models\StockFile;
1211
use Magento\AdobeStockAssetApi\Api\AssetRepositoryInterface;
13-
use Magento\AdobeStockClient\Model\StockFileToDocument;
14-
use Magento\AdobeStockImage\Model\SaveImageFile;
15-
use Magento\AdobeStockImage\Model\Storage\Save;
1612
use Magento\AdobeStockImageApi\Api\SaveImageInterface;
1713
use Magento\Framework\Api\AttributeValueFactory;
1814
use Magento\Framework\Api\Search\Document;
@@ -37,11 +33,6 @@ class SaveImageTest extends TestCase
3733
*/
3834
private $saveImage;
3935

40-
/**
41-
* @var string
42-
*/
43-
private $saveDestination = 'catalog/category/tmp.png';
44-
4536
/**
4637
* @var DriverInterface
4738
*/
@@ -62,6 +53,43 @@ class SaveImageTest extends TestCase
6253
*/
6354
private $criteriaBuilder;
6455

56+
/**
57+
* @return array
58+
*/
59+
public function getSaveTestDataProvider(): array
60+
{
61+
return [
62+
'image_save' => [
63+
'documentData' => [
64+
'id' => 1,
65+
'comp_url' => 'https://test.url/magento-logo.png',
66+
'width' => 110,
67+
'title' => 'test',
68+
'content_type' => 'image/png',
69+
'height' => 210,
70+
'some_bool_param' => false,
71+
'some_nullable_param' => null,
72+
'extension_attributes' => [
73+
'title' => 'test',
74+
'is_downloaded' => 0,
75+
'is_licensed_locally' => 0,
76+
'thumbnail_240_url' => 'https://test.url/magento-logo.png',
77+
'creator_id' => 1122,
78+
'creator_name' => 'Test',
79+
'path' => 'catalog/category/tmp.png',
80+
'content_type' => 'image/png',
81+
'category' => [
82+
'id' => 1,
83+
'name' => 'Test'
84+
],
85+
]
86+
],
87+
'sourcePath' => 'magento-logo.png',
88+
'destinationPath' => 'catalog/category/tmp.png',
89+
]
90+
];
91+
}
92+
6593
/**
6694
* @inheritdoc
6795
*/
@@ -71,46 +99,35 @@ protected function setUp(): void
7199
$this->fileSystem = Bootstrap::getObjectManager()->get(Filesystem::class);
72100
$this->assetRepository = Bootstrap::getObjectManager()->get(AssetRepositoryInterface::class);
73101
$this->criteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class);
74-
75-
$this->deleteImage();
76-
$https = $this->createMock(Https::class);
77-
$https->expects($this->once())
78-
->method('fileGetContents')
79-
->willReturnCallback(function ($filePath) {
80-
return file_get_contents($filePath);
81-
});
82-
$storageSave = Bootstrap::getObjectManager()->create(Save::class, ['driver' => $https]);
83-
$saveImageFile = Bootstrap::getObjectManager()->create(SaveImageFile::class, ['storageSave' => $storageSave]);
84-
$this->saveImage = Bootstrap::getObjectManager()->create(
85-
SaveImageInterface::class,
86-
['saveImageFile' => $saveImageFile]
87-
);
88-
}
89-
90-
/**
91-
* @inheridoc
92-
*/
93-
protected function tearDown(): void
94-
{
95-
$this->deleteImage();
96-
parent::tearDown();
102+
Bootstrap::getObjectManager()->configure([
103+
'preferences' => [
104+
Https::class => HttpsDriverMock::class
105+
]
106+
]);
107+
$this->saveImage = Bootstrap::getObjectManager()->create(SaveImageInterface::class);
97108
}
98109

99110
/**
100111
* Test with image.
101112
*
113+
* @param array $documentData
114+
* @param string $sourceFile
115+
* @param string $destinationPath
102116
* @return void
117+
* @dataProvider getSaveTestDataProvider
103118
*/
104-
public function testSave(): void
119+
public function testSave(array $documentData, string $sourceFile, string $destinationPath): void
105120
{
106-
$document = $this->getDocument();
121+
$this->deleteImage($destinationPath);
122+
$document = $this->getDocument($documentData);
123+
$mediaDir = $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA);
107124
$this->saveImage->execute(
108125
$document,
109-
$document->getCustomAttribute(self::URL_FIELD)->getValue(),
110-
$this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA)->getAbsolutePath($this->saveDestination)
126+
$this->getImageFilePath($sourceFile),
127+
$mediaDir->getAbsolutePath($destinationPath)
111128
);
112129
self::assertTrue(
113-
$this->fileSystem->getDirectoryRead(DirectoryList::MEDIA)->isExist($this->saveDestination),
130+
$this->fileSystem->getDirectoryRead(DirectoryList::MEDIA)->isExist($destinationPath),
114131
'File was not saved by destination'
115132
);
116133
$searchCriteria = $this->criteriaBuilder
@@ -120,43 +137,19 @@ public function testSave(): void
120137
$this->assetRepository->getList($searchCriteria),
121138
'Image asset was not saved'
122139
);
140+
$this->deleteImage($destinationPath);
123141
}
124142

125143
/**
126144
* Document for save.
127145
*
146+
* @param array $documentData
128147
* @return Document
129-
* @throws IntegrationException
130148
*/
131-
private function getDocument(): Document
149+
private function getDocument(array $documentData): Document
132150
{
133-
$stockFileData = [
134-
'id' => 1,
135-
'comp_url' => 'https://test.url/1.png',
136-
'width' => 110,
137-
'title' => 'test',
138-
'content_type' => 'image/png',
139-
'height' => 210,
140-
'some_bool_param' => false,
141-
'some_nullable_param' => null,
142-
'category' => [
143-
'id' => 1,
144-
'name' => 'Test'
145-
],
146-
];
147-
148-
$stockFile = new StockFile($stockFileData);
149-
/** @var StockFileToDocument $stockFileToDocument */
150-
$stockFileToDocument = Bootstrap::getObjectManager()->create(StockFileToDocument::class);
151-
$document = $stockFileToDocument->convert($stockFile);
152-
$this->addAttributes($document, [
153-
'is_downloaded' => 0,
154-
'path' => '',
155-
'is_licensed_locally' => 0,
156-
self::URL_FIELD => $this->getImageFilePath('magento-logo.png'),
157-
'creator_id' => 1122,
158-
'creator_name' => 'Test'
159-
]);
151+
$document = new Document($documentData);
152+
$this->addAttributes($document, $documentData['extension_attributes']);
160153
return $document;
161154
}
162155

@@ -207,13 +200,14 @@ private function getImageFilePath(string $filename): string
207200
/**
208201
* Delete test image if exists
209202
*
203+
* @param string $destinationPath
210204
* @return void
211205
*/
212-
private function deleteImage(): void
206+
private function deleteImage(string $destinationPath): void
213207
{
214208
$mediaDir = $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA);
215-
if ($mediaDir->isExist($this->saveDestination)) {
216-
$this->driver->deleteFile($mediaDir->getAbsolutePath($this->saveDestination));
209+
if ($mediaDir->isExist($destinationPath)) {
210+
$this->driver->deleteFile($mediaDir->getAbsolutePath($destinationPath));
217211
}
218212
}
219213
}

0 commit comments

Comments
 (0)