5
5
*/
6
6
namespace Magento \Catalog \Test \Unit \Model \Category \Attribute \Backend ;
7
7
8
+ use Magento \Catalog \Model \ImageUploader ;
8
9
use Magento \Framework \App \Filesystem \DirectoryList ;
10
+ use Magento \Framework \Filesystem \Directory \WriteInterface ;
9
11
10
12
class ImageTest extends \PHPUnit \Framework \TestCase
11
13
{
@@ -67,7 +69,7 @@ protected function setUp()
67
69
68
70
$ this ->imageUploader = $ this ->createPartialMock (
69
71
\Magento \Catalog \Model \ImageUploader::class,
70
- ['moveFileFromTmp ' ]
72
+ ['moveFileFromTmp ' , ' getBasePath ' ]
71
73
);
72
74
73
75
$ this ->filesystem = $ this ->getMockBuilder (\Magento \Framework \Filesystem::class)->disableOriginalConstructor ()
@@ -146,8 +148,21 @@ public function testBeforeSaveValueInvalid($value)
146
148
*/
147
149
public function testBeforeSaveAttributeFileName ()
148
150
{
149
- $ model = $ this ->objectManager ->getObject (\Magento \Catalog \Model \Category \Attribute \Backend \Image::class);
150
- $ model ->setAttribute ($ this ->attribute );
151
+ $ model = $ this ->setUpModelForAfterSave ();
152
+ $ mediaDirectoryMock = $ this ->createMock (WriteInterface::class);
153
+ $ this ->filesystem ->expects ($ this ->once ())
154
+ ->method ('getDirectoryWrite ' )
155
+ ->with (DirectoryList::MEDIA )
156
+ ->willReturn ($ mediaDirectoryMock );
157
+ $ this ->imageUploader ->expects ($ this ->once ())->method ('getBasePath ' )->willReturn ('base/path ' );
158
+ $ mediaDirectoryMock ->expects ($ this ->once ())
159
+ ->method ('getAbsolutePath ' )
160
+ ->with ('base/path/test123.jpg ' )
161
+ ->willReturn ('absolute/path/base/path/test123.jpg ' );
162
+ $ mediaDirectoryMock ->expects ($ this ->once ())
163
+ ->method ('isExist ' )
164
+ ->with ('absolute/path/base/path/test123.jpg ' )
165
+ ->willReturn (false );
151
166
152
167
$ object = new \Magento \Framework \DataObject ([
153
168
'test_attribute ' => [
@@ -165,12 +180,14 @@ public function testBeforeSaveAttributeFileName()
165
180
*/
166
181
public function testBeforeSaveAttributeFileNameOutsideOfCategoryDir ()
167
182
{
168
- $ model = $ this ->objectManager ->getObject (\Magento \Catalog \Model \Category \Attribute \Backend \Image::class, [
169
- 'filesystem ' => $ this ->filesystem
170
- ]);
171
-
183
+ $ model = $ this ->setUpModelForAfterSave ();
172
184
$ model ->setAttribute ($ this ->attribute );
173
185
186
+ $ mediaDirectoryMock = $ this ->createMock (WriteInterface::class);
187
+ $ this ->filesystem ->expects ($ this ->once ())
188
+ ->method ('getDirectoryWrite ' )
189
+ ->with (DirectoryList::MEDIA )
190
+ ->willReturn ($ mediaDirectoryMock );
174
191
$ this ->filesystem
175
192
->expects ($ this ->once ())
176
193
->method ('getUri ' )
@@ -200,9 +217,15 @@ public function testBeforeSaveAttributeFileNameOutsideOfCategoryDir()
200
217
*/
201
218
public function testBeforeSaveTemporaryAttribute ()
202
219
{
203
- $ model = $ this ->objectManager -> getObject (\ Magento \ Catalog \ Model \ Category \ Attribute \ Backend \Image::class );
220
+ $ model = $ this ->setUpModelForAfterSave ( );
204
221
$ model ->setAttribute ($ this ->attribute );
205
222
223
+ $ mediaDirectoryMock = $ this ->createMock (WriteInterface::class);
224
+ $ this ->filesystem ->expects ($ this ->once ())
225
+ ->method ('getDirectoryWrite ' )
226
+ ->with (DirectoryList::MEDIA )
227
+ ->willReturn ($ mediaDirectoryMock );
228
+
206
229
$ object = new \Magento \Framework \DataObject ([
207
230
'test_attribute ' => [
208
231
['name ' => 'test123.jpg ' , 'tmp_name ' => 'abc123 ' , 'url ' => 'http://www.example.com/test123.jpg ' ]
@@ -246,7 +269,7 @@ private function setUpModelForAfterSave()
246
269
$ objectManagerMock ->expects ($ this ->any ())
247
270
->method ('get ' )
248
271
->will ($ this ->returnCallback (function ($ class , $ params = []) use ($ imageUploaderMock ) {
249
- if ($ class == \ Magento \ Catalog \CategoryImageUpload ::class) {
272
+ if ($ class == ImageUploader ::class) {
250
273
return $ imageUploaderMock ;
251
274
}
252
275
@@ -255,7 +278,8 @@ private function setUpModelForAfterSave()
255
278
256
279
$ model = $ this ->objectManager ->getObject (\Magento \Catalog \Model \Category \Attribute \Backend \Image::class, [
257
280
'objectManager ' => $ objectManagerMock ,
258
- 'logger ' => $ this ->logger
281
+ 'logger ' => $ this ->logger ,
282
+ 'filesystem ' => $ this ->filesystem ,
259
283
]);
260
284
$ this ->objectManager ->setBackwardCompatibleProperty ($ model , 'imageUploader ' , $ this ->imageUploader );
261
285
0 commit comments