5
5
*/
6
6
namespace Magento \Theme \Test \Unit \Model \Design \Backend ;
7
7
8
- use Magento \Framework \UrlInterface ;
9
- use Magento \Theme \Model \Design \Backend \File ;
10
8
use Magento \Framework \App \Filesystem \DirectoryList ;
9
+ use Magento \Framework \Exception \LocalizedException ;
11
10
use Magento \Framework \Filesystem \Io \File as IoFileSystem ;
11
+ use Magento \Framework \UrlInterface ;
12
+ use Magento \Theme \Model \Design \Backend \File ;
12
13
13
14
/**
14
15
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -24,7 +25,7 @@ class FileTest extends \PHPUnit\Framework\TestCase
24
25
/** @var File */
25
26
protected $ fileBackend ;
26
27
27
- /** @var IoFileSystem|\PHPUnit_Framework_MockObject_MockObject */
28
+ /** @var IoFileSystem */
28
29
private $ ioFileSystem ;
29
30
30
31
/**
@@ -41,43 +42,48 @@ public function setUp()
41
42
{
42
43
$ context = $ this ->getMockObject (\Magento \Framework \Model \Context::class);
43
44
$ registry = $ this ->getMockObject (\Magento \Framework \Registry::class);
44
- $ config = $ this ->getMockObjectForAbstractClass (\Magento \Framework \App \Config \ScopeConfigInterface::class);
45
- $ cacheTypeList = $ this ->getMockObjectForAbstractClass (\Magento \Framework \App \Cache \TypeListInterface::class);
46
- $ uploaderFactory = $ this ->getMockObject (\Magento \MediaStorage \Model \File \UploaderFactory::class, ['create ' ]);
45
+ $ config = $ this ->getMockObjectForAbstractClass (
46
+ \Magento \Framework \App \Config \ScopeConfigInterface::class
47
+ );
48
+ $ cacheTypeList = $ this ->getMockObjectForAbstractClass (
49
+ \Magento \Framework \App \Cache \TypeListInterface::class
50
+ );
51
+ $ uploaderFactory = $ this ->getMockObject (
52
+ \Magento \MediaStorage \Model \File \UploaderFactory::class,
53
+ ['create ' ]
54
+ );
47
55
$ requestData = $ this ->getMockObjectForAbstractClass (
48
56
\Magento \Config \Model \Config \Backend \File \RequestData \RequestDataInterface::class
49
57
);
50
58
$ filesystem = $ this ->getMockBuilder (\Magento \Framework \Filesystem::class)
51
59
->disableOriginalConstructor ()
52
60
->getMock ();
53
- $ this ->mediaDirectory = $ this ->getMockBuilder (\Magento \Framework \Filesystem \Directory \WriteInterface::class)
61
+ $ this ->mediaDirectory = $ this ->getMockBuilder (
62
+ \Magento \Framework \Filesystem \Directory \WriteInterface::class
63
+ )
54
64
->getMockForAbstractClass ();
55
-
56
65
$ filesystem ->expects ($ this ->once ())
57
66
->method ('getDirectoryWrite ' )
58
67
->with (DirectoryList::MEDIA )
59
68
->willReturn ($ this ->mediaDirectory );
60
69
$ this ->urlBuilder = $ this ->getMockBuilder (\Magento \Framework \UrlInterface::class)
61
70
->getMockForAbstractClass ();
62
-
63
- $ this ->ioFileSystem = $ this ->getMockBuilder (\Magento \Framework \Filesystem \Io \File::class)
64
- ->getMockForAbstractClass ();
65
-
66
71
$ this ->mime = $ this ->getMockBuilder (\Magento \Framework \File \Mime::class)
67
72
->disableOriginalConstructor ()
68
73
->getMock ();
69
-
70
- $ this ->databaseHelper = $ this ->getMockBuilder (\Magento \MediaStorage \Helper \File \Storage \Database::class)
74
+ $ this ->databaseHelper = $ this ->getMockBuilder (
75
+ \Magento \MediaStorage \Helper \File \Storage \Database::class
76
+ )
71
77
->disableOriginalConstructor ()
72
78
->getMock ();
73
-
74
- $ abstractResource = $ this ->getMockBuilder (\Magento \Framework \Model \ResourceModel \AbstractResource::class)
79
+ $ abstractResource = $ this ->getMockBuilder (
80
+ \Magento \Framework \Model \ResourceModel \AbstractResource::class
81
+ )
75
82
->getMockForAbstractClass ();
76
-
77
83
$ abstractDb = $ this ->getMockBuilder (\Magento \Framework \Data \Collection \AbstractDb::class)
78
84
->disableOriginalConstructor ()
79
85
->getMockForAbstractClass ();
80
-
86
+ $ this -> ioFileSystem = new IoFileSystem ();
81
87
$ this ->fileBackend = new File (
82
88
$ context ,
83
89
$ registry ,
@@ -93,7 +99,6 @@ public function setUp()
93
99
$ this ->databaseHelper ,
94
100
$ this ->ioFileSystem
95
101
);
96
-
97
102
$ objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
98
103
$ objectManager ->setBackwardCompatibleProperty (
99
104
$ this ->fileBackend ,
@@ -139,16 +144,18 @@ public function testAfterLoad()
139
144
140
145
$ absoluteFilePath = '/absolute_path/ ' . $ value ;
141
146
142
- $ this ->fileBackend ->setValue ($ value );
143
- $ this ->fileBackend ->setFieldConfig (
147
+ $ this ->fileBackend ->setData (
144
148
[
145
- 'upload_dir ' => [
146
- 'value ' => 'value ' ,
147
- 'config ' => 'system/filesystem/media ' ,
148
- ],
149
- 'base_url ' => [
150
- 'type ' => 'media ' ,
151
- 'value ' => 'design/file '
149
+ 'value ' => $ value ,
150
+ 'field_config ' => [
151
+ 'upload_dir ' => [
152
+ 'value ' => 'value ' ,
153
+ 'config ' => 'system/filesystem/media ' ,
154
+ ],
155
+ 'base_url ' => [
156
+ 'type ' => 'media ' ,
157
+ 'value ' => 'design/file '
158
+ ],
152
159
],
153
160
]
154
161
);
@@ -161,7 +168,6 @@ public function testAfterLoad()
161
168
->method ('getAbsolutePath ' )
162
169
->with ('value/ ' . $ value )
163
170
->willReturn ($ absoluteFilePath );
164
-
165
171
$ this ->urlBuilder ->expects ($ this ->once ())
166
172
->method ('getBaseUrl ' )
167
173
->with (['_type ' => UrlInterface::URL_TYPE_MEDIA ])
@@ -174,12 +180,10 @@ public function testAfterLoad()
174
180
->method ('stat ' )
175
181
->with ('value/ ' . $ value )
176
182
->willReturn (['size ' => 234234 ]);
177
-
178
183
$ this ->mime ->expects ($ this ->once ())
179
184
->method ('getMimeType ' )
180
185
->with ($ absoluteFilePath )
181
186
->willReturn ($ mime );
182
-
183
187
$ this ->fileBackend ->afterLoad ();
184
188
$ this ->assertEquals (
185
189
[
@@ -199,27 +203,28 @@ public function testAfterLoad()
199
203
/**
200
204
* @dataProvider beforeSaveDataProvider
201
205
* @param string $fileName
206
+ * @throws LocalizedException
202
207
*/
203
208
public function testBeforeSave ($ fileName )
204
209
{
205
210
$ expectedFileName = basename ($ fileName );
206
211
$ expectedTmpMediaPath = 'tmp/design/file/ ' . $ expectedFileName ;
207
- $ this ->fileBackend ->setScope ('store ' );
208
- $ this ->fileBackend ->setScopeId (1 );
209
- $ this ->fileBackend ->setValue (
210
- [
211
- [
212
- 'url ' => 'http://magento2.com/pub/media/tmp/image/ ' . $ fileName ,
213
- 'file ' => $ fileName ,
214
- 'size ' => 234234 ,
215
- ]
216
- ]
217
- );
218
- $ this ->fileBackend ->setFieldConfig (
212
+ $ this ->fileBackend ->setData (
219
213
[
220
- 'upload_dir ' => [
221
- 'value ' => 'value ' ,
222
- 'config ' => 'system/filesystem/media ' ,
214
+ 'scope ' => 'store ' ,
215
+ 'scope_id ' => 1 ,
216
+ 'value ' => [
217
+ [
218
+ 'url ' => 'http://magento2.com/pub/media/tmp/image/ ' . $ fileName ,
219
+ 'file ' => $ fileName ,
220
+ 'size ' => 234234 ,
221
+ ]
222
+ ],
223
+ 'field_config ' => [
224
+ 'upload_dir ' => [
225
+ 'value ' => 'value ' ,
226
+ 'config ' => 'system/filesystem/media ' ,
227
+ ],
223
228
],
224
229
]
225
230
);
@@ -274,16 +279,19 @@ public function testBeforeSaveWithoutFile()
274
279
public function testBeforeSaveWithExistingFile ()
275
280
{
276
281
$ value = 'filename.jpg ' ;
277
- $ this ->fileBackend ->setValue (
282
+ $ this ->fileBackend ->setData (
278
283
[
279
- [
280
- 'url ' => 'http://magento2.com/pub/media/tmp/image/ ' . $ value ,
281
- 'file ' => $ value ,
282
- 'size ' => 234234 ,
283
- 'exists ' => true
284
- ]
284
+ 'value ' => [
285
+ [
286
+ 'url ' => 'http://magento2.com/pub/media/tmp/image/ ' . $ value ,
287
+ 'file ' => $ value ,
288
+ 'size ' => 234234 ,
289
+ 'exists ' => true
290
+ ]
291
+ ],
285
292
]
286
293
);
294
+
287
295
$ this ->fileBackend ->beforeSave ();
288
296
$ this ->assertEquals (
289
297
$ value ,
@@ -297,6 +305,7 @@ public function testBeforeSaveWithExistingFile()
297
305
* @param string $path
298
306
* @param string $filename
299
307
* @dataProvider getRelativeMediaPathDataProvider
308
+ * @throws \ReflectionException
300
309
*/
301
310
public function testGetRelativeMediaPath (string $ path , string $ filename )
302
311
{
0 commit comments