8
8
9
9
namespace Magento \AdobeStockImage \Test \Integration \Model ;
10
10
11
- use AdobeStock \Api \Models \StockFile ;
12
11
use Magento \AdobeStockAssetApi \Api \AssetRepositoryInterface ;
13
- use Magento \AdobeStockClient \Model \StockFileToDocument ;
14
- use Magento \AdobeStockImage \Model \SaveImageFile ;
15
- use Magento \AdobeStockImage \Model \Storage \Save ;
16
12
use Magento \AdobeStockImageApi \Api \SaveImageInterface ;
17
13
use Magento \Framework \Api \AttributeValueFactory ;
18
14
use Magento \Framework \Api \Search \Document ;
@@ -37,11 +33,6 @@ class SaveImageTest extends TestCase
37
33
*/
38
34
private $ saveImage ;
39
35
40
- /**
41
- * @var string
42
- */
43
- private $ saveDestination = 'catalog/category/tmp.png ' ;
44
-
45
36
/**
46
37
* @var DriverInterface
47
38
*/
@@ -62,6 +53,43 @@ class SaveImageTest extends TestCase
62
53
*/
63
54
private $ criteriaBuilder ;
64
55
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
+
65
93
/**
66
94
* @inheritdoc
67
95
*/
@@ -71,46 +99,35 @@ protected function setUp(): void
71
99
$ this ->fileSystem = Bootstrap::getObjectManager ()->get (Filesystem::class);
72
100
$ this ->assetRepository = Bootstrap::getObjectManager ()->get (AssetRepositoryInterface::class);
73
101
$ 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);
97
108
}
98
109
99
110
/**
100
111
* Test with image.
101
112
*
113
+ * @param array $documentData
114
+ * @param string $sourceFile
115
+ * @param string $destinationPath
102
116
* @return void
117
+ * @dataProvider getSaveTestDataProvider
103
118
*/
104
- public function testSave (): void
119
+ public function testSave (array $ documentData , string $ sourceFile , string $ destinationPath ): void
105
120
{
106
- $ document = $ this ->getDocument ();
121
+ $ this ->deleteImage ($ destinationPath );
122
+ $ document = $ this ->getDocument ($ documentData );
123
+ $ mediaDir = $ this ->fileSystem ->getDirectoryWrite (DirectoryList::MEDIA );
107
124
$ this ->saveImage ->execute (
108
125
$ 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 )
111
128
);
112
129
self ::assertTrue (
113
- $ this ->fileSystem ->getDirectoryRead (DirectoryList::MEDIA )->isExist ($ this -> saveDestination ),
130
+ $ this ->fileSystem ->getDirectoryRead (DirectoryList::MEDIA )->isExist ($ destinationPath ),
114
131
'File was not saved by destination '
115
132
);
116
133
$ searchCriteria = $ this ->criteriaBuilder
@@ -120,43 +137,19 @@ public function testSave(): void
120
137
$ this ->assetRepository ->getList ($ searchCriteria ),
121
138
'Image asset was not saved '
122
139
);
140
+ $ this ->deleteImage ($ destinationPath );
123
141
}
124
142
125
143
/**
126
144
* Document for save.
127
145
*
146
+ * @param array $documentData
128
147
* @return Document
129
- * @throws IntegrationException
130
148
*/
131
- private function getDocument (): Document
149
+ private function getDocument (array $ documentData ): Document
132
150
{
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 ' ]);
160
153
return $ document ;
161
154
}
162
155
@@ -207,13 +200,14 @@ private function getImageFilePath(string $filename): string
207
200
/**
208
201
* Delete test image if exists
209
202
*
203
+ * @param string $destinationPath
210
204
* @return void
211
205
*/
212
- private function deleteImage (): void
206
+ private function deleteImage (string $ destinationPath ): void
213
207
{
214
208
$ 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 ));
217
211
}
218
212
}
219
213
}
0 commit comments