8
8
namespace Magento \Catalog \Model ;
9
9
10
10
use Magento \Framework \App \Filesystem \DirectoryList ;
11
+ use Magento \Framework \Exception \LocalizedException ;
12
+ use Magento \Framework \Filesystem ;
13
+ use Magento \Framework \Filesystem \Directory \WriteInterface ;
14
+ use Magento \Framework \ObjectManagerInterface ;
15
+ use Magento \TestFramework \Helper \Bootstrap ;
16
+ use PHPUnit \Framework \TestCase ;
11
17
12
18
/**
13
19
* Tests for the \Magento\Catalog\Model\ImageUploader class
14
20
*/
15
- class ImageUploaderTest extends \ PHPUnit \ Framework \ TestCase
21
+ class ImageUploaderTest extends TestCase
16
22
{
23
+ private const BASE_TMP_PATH = 'catalog/tmp/category ' ;
24
+
25
+ private const BASE_PATH = 'catalog/category ' ;
26
+
17
27
/**
18
- * @var \Magento\Framework\ ObjectManagerInterface
28
+ * @var ObjectManagerInterface
19
29
*/
20
30
private $ objectManager ;
21
31
22
32
/**
23
- * @var \Magento\Catalog\Model\ ImageUploader
33
+ * @var ImageUploader
24
34
*/
25
35
private $ imageUploader ;
26
36
27
37
/**
28
- * @var \Magento\Framework\ Filesystem
38
+ * @var Filesystem
29
39
*/
30
40
private $ filesystem ;
31
41
32
42
/**
33
- * @var \Magento\Framework\Filesystem\Directory\ WriteInterface
43
+ * @var WriteInterface
34
44
*/
35
45
private $ mediaDirectory ;
36
46
47
+ /**
48
+ * @var WriteInterface
49
+ */
50
+ private $ tmpDirectory ;
51
+
37
52
/**
38
53
* @inheritdoc
39
54
*/
40
55
protected function setUp (): void
41
56
{
42
- $ this ->objectManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ();
43
- /** @var \Magento\Framework\Filesystem $filesystem */
44
- $ this ->filesystem = $ this ->objectManager ->get (\Magento \Framework \Filesystem::class);
57
+ $ this ->objectManager = Bootstrap::getObjectManager ();
58
+ $ this ->filesystem = $ this ->objectManager ->get (Filesystem::class);
45
59
$ this ->mediaDirectory = $ this ->filesystem ->getDirectoryWrite (DirectoryList::MEDIA );
46
- /** @var $uploader \Magento\MediaStorage\Model\File\Uploader */
60
+ $ this -> tmpDirectory = $ this -> filesystem -> getDirectoryWrite (DirectoryList:: SYS_TMP );
47
61
$ this ->imageUploader = $ this ->objectManager ->create (
48
- \ Magento \ Catalog \ Model \ ImageUploader::class,
62
+ ImageUploader::class,
49
63
[
50
- 'baseTmpPath ' => ' catalog/tmp/category ' ,
51
- 'basePath ' => ' catalog/category ' ,
64
+ 'baseTmpPath ' => self :: BASE_TMP_PATH ,
65
+ 'basePath ' => self :: BASE_PATH ,
52
66
'allowedExtensions ' => ['jpg ' , 'jpeg ' , 'gif ' , 'png ' ],
53
67
'allowedMimeTypes ' => ['image/jpg ' , 'image/jpeg ' , 'image/gif ' , 'image/png ' ]
54
68
]
55
69
);
56
70
}
57
71
58
72
/**
73
+ * @dataProvider saveFileToTmpDirProvider
74
+ * @param string $fileName
75
+ * @param string $expectedName
59
76
* @return void
60
77
*/
61
- public function testSaveFileToTmpDir (): void
78
+ public function testSaveFileToTmpDir (string $ fileName , string $ expectedName ): void
62
79
{
63
- $ fileName = 'magento_small_image.jpg ' ;
64
- $ tmpDirectory = $ this ->filesystem ->getDirectoryWrite (\Magento \Framework \App \Filesystem \DirectoryList::SYS_TMP );
65
80
$ fixtureDir = realpath (__DIR__ . '/../_files ' );
66
- $ filePath = $ tmpDirectory ->getAbsolutePath ($ fileName );
81
+ $ filePath = $ this -> tmpDirectory ->getAbsolutePath ($ fileName );
67
82
copy ($ fixtureDir . DIRECTORY_SEPARATOR . $ fileName , $ filePath );
68
83
69
84
$ _FILES ['image ' ] = [
@@ -75,10 +90,27 @@ public function testSaveFileToTmpDir(): void
75
90
];
76
91
77
92
$ this ->imageUploader ->saveFileToTmpDir ('image ' );
78
- $ filePath = $ this ->imageUploader ->getBaseTmpPath () . DIRECTORY_SEPARATOR . $ fileName ;
93
+ $ filePath = $ this ->imageUploader ->getBaseTmpPath () . DIRECTORY_SEPARATOR . $ expectedName ;
79
94
$ this ->assertTrue (is_file ($ this ->mediaDirectory ->getAbsolutePath ($ filePath )));
80
95
}
81
96
97
+ /**
98
+ * @return array
99
+ */
100
+ public function saveFileToTmpDirProvider (): array
101
+ {
102
+ return [
103
+ 'image_default_name ' => [
104
+ 'file_name ' => 'magento_small_image.jpg ' ,
105
+ 'expected_name ' => 'magento_small_image.jpg ' ,
106
+ ],
107
+ 'image_with_space_in_name ' => [
108
+ 'file_name ' => 'magento_image with space in name.jpg ' ,
109
+ 'expected_name ' => 'magento_image_with_space_in_name.jpg ' ,
110
+ ],
111
+ ];
112
+ }
113
+
82
114
/**
83
115
* Test that method rename files when move it with the same name into base directory.
84
116
*
@@ -90,7 +122,7 @@ public function testMoveFileFromTmp(): void
90
122
{
91
123
$ expectedFilePath = $ this ->imageUploader ->getBasePath () . DIRECTORY_SEPARATOR . 'magento_small_image_1.jpg ' ;
92
124
93
- $ this ->assertFileNotExists ($ this ->mediaDirectory ->getAbsolutePath ($ expectedFilePath ));
125
+ $ this ->assertFileDoesNotExist ($ this ->mediaDirectory ->getAbsolutePath ($ expectedFilePath ));
94
126
95
127
$ this ->imageUploader ->moveFileFromTmp ('magento_small_image.jpg ' );
96
128
@@ -102,12 +134,11 @@ public function testMoveFileFromTmp(): void
102
134
*/
103
135
public function testSaveFileToTmpDirWithWrongExtension (): void
104
136
{
105
- $ this ->expectException (\ Magento \ Framework \ Exception \ LocalizedException::class);
137
+ $ this ->expectException (LocalizedException::class);
106
138
$ this ->expectExceptionMessage ('File validation failed. ' );
107
139
108
140
$ fileName = 'text.txt ' ;
109
- $ tmpDirectory = $ this ->filesystem ->getDirectoryWrite (\Magento \Framework \App \Filesystem \DirectoryList::SYS_TMP );
110
- $ filePath = $ tmpDirectory ->getAbsolutePath ($ fileName );
141
+ $ filePath = $ this ->tmpDirectory ->getAbsolutePath ($ fileName );
111
142
$ file = fopen ($ filePath , "wb " );
112
143
fwrite ($ file , 'just a text ' );
113
144
@@ -120,7 +151,7 @@ public function testSaveFileToTmpDirWithWrongExtension(): void
120
151
];
121
152
122
153
$ this ->imageUploader ->saveFileToTmpDir ('image ' );
123
- $ filePath = $ this ->imageUploader ->getBaseTmpPath () . DIRECTORY_SEPARATOR . $ fileName ;
154
+ $ filePath = $ this ->imageUploader ->getBaseTmpPath () . DIRECTORY_SEPARATOR . $ fileName ;
124
155
$ this ->assertFalse (is_file ($ this ->mediaDirectory ->getAbsolutePath ($ filePath )));
125
156
}
126
157
@@ -129,12 +160,11 @@ public function testSaveFileToTmpDirWithWrongExtension(): void
129
160
*/
130
161
public function testSaveFileToTmpDirWithWrongFile (): void
131
162
{
132
- $ this ->expectException (\ Magento \ Framework \ Exception \ LocalizedException::class);
163
+ $ this ->expectException (LocalizedException::class);
133
164
$ this ->expectExceptionMessage ('File validation failed. ' );
134
165
135
166
$ fileName = 'file.gif ' ;
136
- $ tmpDirectory = $ this ->filesystem ->getDirectoryWrite (\Magento \Framework \App \Filesystem \DirectoryList::SYS_TMP );
137
- $ filePath = $ tmpDirectory ->getAbsolutePath ($ fileName );
167
+ $ filePath = $ this ->tmpDirectory ->getAbsolutePath ($ fileName );
138
168
$ file = fopen ($ filePath , "wb " );
139
169
fwrite ($ file , 'just a text ' );
140
170
@@ -147,7 +177,7 @@ public function testSaveFileToTmpDirWithWrongFile(): void
147
177
];
148
178
149
179
$ this ->imageUploader ->saveFileToTmpDir ('image ' );
150
- $ filePath = $ this ->imageUploader ->getBaseTmpPath () . DIRECTORY_SEPARATOR . $ fileName ;
180
+ $ filePath = $ this ->imageUploader ->getBaseTmpPath () . DIRECTORY_SEPARATOR . $ fileName ;
151
181
$ this ->assertFalse (is_file ($ this ->mediaDirectory ->getAbsolutePath ($ filePath )));
152
182
}
153
183
@@ -157,11 +187,10 @@ public function testSaveFileToTmpDirWithWrongFile(): void
157
187
public static function tearDownAfterClass (): void
158
188
{
159
189
parent ::tearDownAfterClass ();
160
- $ filesystem = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->get (
161
- \Magento \Framework \Filesystem::class
162
- );
163
- /** @var \Magento\Framework\Filesystem\Directory\WriteInterface $mediaDirectory */
190
+ $ filesystem = Bootstrap::getObjectManager ()->get (Filesystem::class);
191
+ /** @var WriteInterface $mediaDirectory */
164
192
$ mediaDirectory = $ filesystem ->getDirectoryWrite (DirectoryList::MEDIA );
165
- $ mediaDirectory ->delete ('tmp ' );
193
+ $ mediaDirectory ->delete (self ::BASE_TMP_PATH );
194
+ $ mediaDirectory ->delete (self ::BASE_PATH );
166
195
}
167
196
}
0 commit comments