5
5
*/
6
6
namespace Magento \Catalog \Test \Unit \Model \Category \Attribute \Backend ;
7
7
8
+ use Magento \Framework \App \Filesystem \DirectoryList ;
9
+ use Magento \Framework \Filesystem \Directory \WriteInterface ;
10
+
11
+ /**
12
+ * Test for Magento\Catalog\Model\Category\Attribute\Backend\Image class.
13
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
14
+ */
8
15
class ImageTest extends \PHPUnit \Framework \TestCase
9
16
{
10
17
/**
@@ -27,6 +34,14 @@ class ImageTest extends \PHPUnit\Framework\TestCase
27
34
*/
28
35
private $ logger ;
29
36
37
+ /**
38
+ * @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
39
+ */
40
+ private $ filesystem ;
41
+
42
+ /**
43
+ * @inheritdoc
44
+ */
30
45
protected function setUp ()
31
46
{
32
47
$ this ->objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
@@ -57,8 +72,12 @@ protected function setUp()
57
72
58
73
$ this ->imageUploader = $ this ->createPartialMock (
59
74
\Magento \Catalog \Model \ImageUploader::class,
60
- ['moveFileFromTmp ' ]
75
+ ['moveFileFromTmp ' , ' getBasePath ' ]
61
76
);
77
+
78
+ $ this ->filesystem = $ this ->getMockBuilder (\Magento \Framework \Filesystem::class)
79
+ ->disableOriginalConstructor ()
80
+ ->getMock ();
62
81
}
63
82
64
83
/**
@@ -82,9 +101,7 @@ public function testBeforeSaveValueDeletion($value)
82
101
$ model = $ this ->objectManager ->getObject (\Magento \Catalog \Model \Category \Attribute \Backend \Image::class);
83
102
$ model ->setAttribute ($ this ->attribute );
84
103
85
- $ object = new \Magento \Framework \DataObject ([
86
- 'test_attribute ' => $ value
87
- ]);
104
+ $ object = new \Magento \Framework \DataObject (['test_attribute ' => $ value ]);
88
105
89
106
$ model ->beforeSave ($ object );
90
107
@@ -119,57 +136,84 @@ public function testBeforeSaveValueInvalid($value)
119
136
$ model = $ this ->objectManager ->getObject (\Magento \Catalog \Model \Category \Attribute \Backend \Image::class);
120
137
$ model ->setAttribute ($ this ->attribute );
121
138
122
- $ object = new \Magento \Framework \DataObject ([
123
- 'test_attribute ' => $ value
124
- ]);
139
+ $ object = new \Magento \Framework \DataObject (['test_attribute ' => $ value ]);
125
140
126
141
$ model ->beforeSave ($ object );
127
142
128
143
$ this ->assertEquals ('' , $ object ->getTestAttribute ());
129
144
}
130
145
146
+ /**
147
+ * Test beforeSaveAttributeFileName.
148
+ */
131
149
public function testBeforeSaveAttributeFileName ()
132
150
{
133
- $ model = $ this ->objectManager ->getObject (\Magento \Catalog \Model \Category \Attribute \Backend \Image::class);
134
- $ 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 ' );
135
162
136
- $ object = new \Magento \Framework \DataObject ([
137
- 'test_attribute ' => [
138
- ['name ' => 'test123.jpg ' ]
163
+ $ object = new \Magento \Framework \DataObject (
164
+ [
165
+ 'test_attribute ' => [
166
+ ['name ' => 'test123.jpg ' ],
167
+ ],
139
168
]
140
- ] );
169
+ );
141
170
142
171
$ model ->beforeSave ($ object );
143
172
144
173
$ this ->assertEquals ('test123.jpg ' , $ object ->getTestAttribute ());
145
174
}
146
175
176
+ /**
177
+ * Test beforeSaveTemporaryAttribute.
178
+ */
147
179
public function testBeforeSaveTemporaryAttribute ()
148
180
{
149
- $ model = $ this ->objectManager -> getObject (\ Magento \ Catalog \ Model \ Category \ Attribute \ Backend \Image::class );
181
+ $ model = $ this ->setUpModelForAfterSave ( );
150
182
$ model ->setAttribute ($ this ->attribute );
151
183
152
- $ object = new \Magento \Framework \DataObject ([
153
- 'test_attribute ' => [
154
- ['name ' => 'test123.jpg ' , 'tmp_name ' => 'abc123 ' , 'url ' => 'http://www.example.com/test123.jpg ' ]
184
+ $ mediaDirectoryMock = $ this ->createMock (WriteInterface::class);
185
+ $ this ->filesystem ->expects ($ this ->once ())
186
+ ->method ('getDirectoryWrite ' )
187
+ ->with (DirectoryList::MEDIA )
188
+ ->willReturn ($ mediaDirectoryMock );
189
+
190
+ $ object = new \Magento \Framework \DataObject (
191
+ [
192
+ 'test_attribute ' => [
193
+ ['name ' => 'test123.jpg ' , 'tmp_name ' => 'abc123 ' , 'url ' => 'http://www.example.com/test123.jpg ' ],
194
+ ],
155
195
]
156
- ] );
196
+ );
157
197
158
198
$ model ->beforeSave ($ object );
159
199
160
- $ this ->assertEquals ([
161
- ['name ' => 'test123.jpg ' , 'tmp_name ' => 'abc123 ' , 'url ' => 'http://www.example.com/test123.jpg ' ]
162
- ], $ object ->getData ('_additional_data_test_attribute ' ));
200
+ $ this ->assertEquals (
201
+ [
202
+ ['name ' => 'test123.jpg ' , 'tmp_name ' => 'abc123 ' , 'url ' => 'http://www.example.com/test123.jpg ' ],
203
+ ],
204
+ $ object ->getData ('_additional_data_test_attribute ' )
205
+ );
163
206
}
164
207
208
+ /**
209
+ * Test beforeSaveAttributeStringValue.
210
+ */
165
211
public function testBeforeSaveAttributeStringValue ()
166
212
{
167
213
$ model = $ this ->objectManager ->getObject (\Magento \Catalog \Model \Category \Attribute \Backend \Image::class);
168
214
$ model ->setAttribute ($ this ->attribute );
169
215
170
- $ object = new \Magento \Framework \DataObject ([
171
- 'test_attribute ' => 'test123.jpg '
172
- ]);
216
+ $ object = new \Magento \Framework \DataObject (['test_attribute ' => 'test123.jpg ' ]);
173
217
174
218
$ model ->beforeSave ($ object );
175
219
@@ -188,18 +232,26 @@ private function setUpModelForAfterSave()
188
232
189
233
$ objectManagerMock ->expects ($ this ->any ())
190
234
->method ('get ' )
191
- ->will ($ this ->returnCallback (function ($ class , $ params = []) use ($ imageUploaderMock ) {
192
- if ($ class == \Magento \Catalog \CategoryImageUpload::class) {
193
- return $ imageUploaderMock ;
194
- }
195
-
196
- return $ this ->objectManager ->get ($ class , $ params );
197
- }));
198
-
199
- $ model = $ this ->objectManager ->getObject (\Magento \Catalog \Model \Category \Attribute \Backend \Image::class, [
200
- 'objectManager ' => $ objectManagerMock ,
201
- 'logger ' => $ this ->logger
202
- ]);
235
+ ->will (
236
+ $ this ->returnCallback (
237
+ function ($ class , $ params = []) use ($ imageUploaderMock ) {
238
+ if ($ class == \Magento \Catalog \CategoryImageUpload::class) {
239
+ return $ imageUploaderMock ;
240
+ }
241
+
242
+ return $ this ->objectManager ->get ($ class , $ params );
243
+ }
244
+ )
245
+ );
246
+
247
+ $ model = $ this ->objectManager ->getObject (
248
+ \Magento \Catalog \Model \Category \Attribute \Backend \Image::class,
249
+ [
250
+ 'objectManager ' => $ objectManagerMock ,
251
+ 'logger ' => $ this ->logger ,
252
+ 'filesystem ' => $ this ->filesystem ,
253
+ ]
254
+ );
203
255
$ this ->objectManager ->setBackwardCompatibleProperty ($ model , 'imageUploader ' , $ this ->imageUploader );
204
256
205
257
return $ model ->setAttribute ($ this ->attribute );
@@ -262,6 +314,9 @@ public function testAfterSaveWithoutAdditionalData($value)
262
314
$ model ->afterSave ($ object );
263
315
}
264
316
317
+ /**
318
+ * Test afterSaveWithExceptions.
319
+ */
265
320
public function testAfterSaveWithExceptions ()
266
321
{
267
322
$ model = $ this ->setUpModelForAfterSave ();
0 commit comments