@@ -24,6 +24,11 @@ class ImageTest extends \PHPUnit_Framework_TestCase
24
24
*/
25
25
protected $ imageUploader ;
26
26
27
+ /**
28
+ * @var \Psr\Log\LoggerInterface
29
+ */
30
+ protected $ logger ;
31
+
27
32
protected function setUp ()
28
33
{
29
34
$ this ->objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
@@ -42,6 +47,16 @@ protected function setUp()
42
47
->method ('getName ' )
43
48
->will ($ this ->returnValue ('test_attribute ' ));
44
49
50
+ $ this ->logger = $ this ->getMockForAbstractClass (
51
+ \Psr \Log \LoggerInterface::class,
52
+ [],
53
+ 'TestLogger ' ,
54
+ false ,
55
+ false ,
56
+ true ,
57
+ ['critical ' ]
58
+ );
59
+
45
60
$ this ->imageUploader = $ this ->getMock (
46
61
\Magento \Catalog \Model \ImageUploader::class,
47
62
['moveFileFromTmp ' ],
@@ -51,7 +66,7 @@ protected function setUp()
51
66
);
52
67
}
53
68
54
- public function deletionValuesProvider ()
69
+ public function deletionValueProvider ()
55
70
{
56
71
return [
57
72
[false ],
@@ -60,7 +75,7 @@ public function deletionValuesProvider()
60
75
}
61
76
62
77
/**
63
- * @dataProvider deletionValuesProvider
78
+ * @dataProvider deletionValueProvider
64
79
*
65
80
* @param $value
66
81
*/
@@ -78,6 +93,36 @@ public function testBeforeSaveShouldSetAttributeValueToBlankWhenImageValueRequir
78
93
$ this ->assertEquals ('' , $ object ->getTestAttribute ());
79
94
}
80
95
96
+ public function invalidValueProvider ()
97
+ {
98
+ return [
99
+ [1234 ],
100
+ [true ],
101
+ [new \stdClass ()],
102
+ [function () {}],
103
+ [['a ' => 1 , 'b ' => 2 ]]
104
+ ];
105
+ }
106
+
107
+ /**
108
+ * @dataProvider invalidValueProvider
109
+ *
110
+ * @param $value
111
+ */
112
+ public function testBeforeSaveShouldSetAttributeValueToBlankWhenImageValueInvalid ($ value )
113
+ {
114
+ $ model = $ this ->objectManager ->getObject (Model::class);
115
+ $ model ->setAttribute ($ this ->attribute );
116
+
117
+ $ object = new \Magento \Framework \DataObject ([
118
+ 'test_attribute ' => $ value
119
+ ]);
120
+
121
+ $ model ->beforeSave ($ object );
122
+
123
+ $ this ->assertEquals ('' , $ object ->getTestAttribute ());
124
+ }
125
+
81
126
public function testBeforeSaveShouldSetAttributeValueToUploadedImageName ()
82
127
{
83
128
$ model = $ this ->objectManager ->getObject (Model::class);
@@ -109,55 +154,35 @@ public function testBeforeSaveShouldSetAttributeUploadInformationToTemporaryAttr
109
154
110
155
$ this ->assertEquals ([
111
156
['name ' => 'test123.jpg ' , 'tmp_name ' => 'abc123 ' , 'url ' => 'http://www.test.com/test123.jpg ' ]
112
- ], $ object ->getTestAttributeAdditionalData ( ));
157
+ ], $ object ->getData ( ' _additional_data_test_attribute ' ));
113
158
}
114
159
115
- public function stringValueProvider ()
116
- {
117
- return [
118
- ['test123 ' ],
119
- [12345 ],
120
- [true ],
121
- ['some ' => 'value ' ]
122
- ];
123
- }
124
-
125
- /**
126
- * @dataProvider stringValueProvider
127
- *
128
- * @param $value
129
- */
130
- public function testBeforeSaveShouldNotModifyAttributeValueWhenNotUploadData ($ value )
160
+ public function testBeforeSaveShouldNotModifyAttributeValueWhenStringValue ()
131
161
{
132
162
$ model = $ this ->objectManager ->getObject (Model::class);
133
163
$ model ->setAttribute ($ this ->attribute );
134
164
135
165
$ object = new \Magento \Framework \DataObject ([
136
- 'test_attribute ' => $ value
166
+ 'test_attribute ' => ' test123.jpg '
137
167
]);
138
168
139
169
$ model ->beforeSave ($ object );
140
170
141
- $ this ->assertEquals ($ value , $ object ->getTestAttribute ());
171
+ $ this ->assertEquals (' test123.jpg ' , $ object ->getTestAttribute ());
142
172
}
143
173
144
- /**
145
- * @dataProvider stringValueProvider
146
- *
147
- * @param $value
148
- */
149
- public function testBeforeSaveShouldNotSetAdditionalDataWhenNotUploadData ($ value )
174
+ public function testBeforeSaveShouldNotSetAdditionalDataWhenStringValue ()
150
175
{
151
176
$ model = $ this ->objectManager ->getObject (Model::class);
152
177
$ model ->setAttribute ($ this ->attribute );
153
178
154
179
$ object = new \Magento \Framework \DataObject ([
155
- 'test_attribute ' => $ value
180
+ 'test_attribute ' => ' test123.jpg '
156
181
]);
157
182
158
183
$ model ->beforeSave ($ object );
159
184
160
- $ this ->assertNull ($ object ->getTestAttributeAdditionalData ( ));
185
+ $ this ->assertNull ($ object ->getData ( ' _additional_data_test_attribute ' ));
161
186
}
162
187
163
188
protected function setUpModelForAfterSave ()
@@ -174,7 +199,7 @@ protected function setUpModelForAfterSave()
174
199
175
200
$ objectManagerMock ->expects ($ this ->any ())
176
201
->method ('get ' )
177
- ->will ($ this ->returnCallback (function ($ class , $ params = []) use ($ imageUploaderMock ) {
202
+ ->will ($ this ->returnCallback (function ($ class , $ params = []) use ($ imageUploaderMock ) {
178
203
if ($ class == \Magento \Catalog \CategoryImageUpload::class) {
179
204
return $ imageUploaderMock ;
180
205
}
@@ -183,13 +208,29 @@ protected function setUpModelForAfterSave()
183
208
}));
184
209
185
210
$ model = $ this ->objectManager ->getObject (Model::class, [
186
- 'objectManager ' => $ objectManagerMock
211
+ 'objectManager ' => $ objectManagerMock ,
212
+ 'logger ' => $ this ->logger
187
213
]);
188
214
189
215
return $ model ->setAttribute ($ this ->attribute );
190
216
}
191
217
192
- public function testAfterSaveShouldUploadImageWhenAdditionalDataSet ()
218
+ public function attributeValidValueProvider ()
219
+ {
220
+ return [
221
+ [[['name ' => 'test1234.jpg ' ]]],
222
+ ['test1234.jpg ' ],
223
+ ['' ],
224
+ [false ]
225
+ ];
226
+ }
227
+
228
+ /**
229
+ * @dataProvider attributeValidValueProvider
230
+ *
231
+ * @param $value
232
+ */
233
+ public function testAfterSaveShouldUploadImageWhenAdditionalDataSet ($ value )
193
234
{
194
235
$ model = $ this ->setUpModelForAfterSave ();
195
236
@@ -198,23 +239,50 @@ public function testAfterSaveShouldUploadImageWhenAdditionalDataSet()
198
239
->with ($ this ->equalTo ('test1234.jpg ' ));
199
240
200
241
$ object = new \Magento \Framework \DataObject ([
201
- 'test_attribute_additional_data ' => [
242
+ 'test_attribute ' => $ value ,
243
+ '_additional_data_test_attribute ' => [
202
244
['name ' => 'test1234.jpg ' ]
203
245
]
204
246
]);
205
247
206
248
$ model ->afterSave ($ object );
207
249
}
208
250
209
- public function testAfterSaveShouldNotUploadImageWhenAdditionalDataNotSet ()
251
+ /**
252
+ * @dataProvider attributeValidValueProvider
253
+ *
254
+ * @param $value
255
+ */
256
+ public function testAfterSaveShouldNotUploadImageWhenAdditionalDataNotSet ($ value )
210
257
{
211
258
$ model = $ this ->setUpModelForAfterSave ();
212
259
213
260
$ this ->imageUploader ->expects ($ this ->never ())
214
261
->method ('moveFileFromTmp ' );
215
262
216
263
$ object = new \Magento \Framework \DataObject ([
217
- 'test_attribute ' => [
264
+ 'test_attribute ' => $ value
265
+ ]);
266
+
267
+ $ model ->afterSave ($ object );
268
+ }
269
+
270
+ public function testAfterSaveShouldCreateCriticalLogEntryOnUploadExceptions ()
271
+ {
272
+ $ model = $ this ->setUpModelForAfterSave ();
273
+
274
+ $ exception = new \Exception ();
275
+
276
+ $ this ->imageUploader ->expects ($ this ->any ())
277
+ ->method ('moveFileFromTmp ' )
278
+ ->will ($ this ->throwException ($ exception ));
279
+
280
+ $ this ->logger ->expects ($ this ->once ())
281
+ ->method ('critical ' )
282
+ ->with ($ this ->equalTo ($ exception ));
283
+
284
+ $ object = new \Magento \Framework \DataObject ([
285
+ '_additional_data_test_attribute ' => [
218
286
['name ' => 'test1234.jpg ' ]
219
287
]
220
288
]);
0 commit comments