88namespace Magento \Catalog \Model ;
99
1010use 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 ;
1117
1218/**
1319 * Tests for the \Magento\Catalog\Model\ImageUploader class
1420 */
15- class ImageUploaderTest extends \ PHPUnit \ Framework \ TestCase
21+ class ImageUploaderTest extends TestCase
1622{
23+ private const BASE_TMP_PATH = 'catalog/tmp/category ' ;
24+
25+ private const BASE_PATH = 'catalog/category ' ;
26+
1727 /**
18- * @var \Magento\Framework\ ObjectManagerInterface
28+ * @var ObjectManagerInterface
1929 */
2030 private $ objectManager ;
2131
2232 /**
23- * @var \Magento\Catalog\Model\ ImageUploader
33+ * @var ImageUploader
2434 */
2535 private $ imageUploader ;
2636
2737 /**
28- * @var \Magento\Framework\ Filesystem
38+ * @var Filesystem
2939 */
3040 private $ filesystem ;
3141
3242 /**
33- * @var \Magento\Framework\Filesystem\Directory\ WriteInterface
43+ * @var WriteInterface
3444 */
3545 private $ mediaDirectory ;
3646
47+ /**
48+ * @var WriteInterface
49+ */
50+ private $ tmpDirectory ;
51+
3752 /**
3853 * @inheritdoc
3954 */
4055 protected function setUp (): void
4156 {
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);
4559 $ this ->mediaDirectory = $ this ->filesystem ->getDirectoryWrite (DirectoryList::MEDIA );
46- /** @var $uploader \Magento\MediaStorage\Model\File\Uploader */
60+ $ this -> tmpDirectory = $ this -> filesystem -> getDirectoryWrite (DirectoryList:: SYS_TMP );
4761 $ this ->imageUploader = $ this ->objectManager ->create (
48- \ Magento \ Catalog \ Model \ ImageUploader::class,
62+ ImageUploader::class,
4963 [
50- 'baseTmpPath ' => ' catalog/tmp/category ' ,
51- 'basePath ' => ' catalog/category ' ,
64+ 'baseTmpPath ' => self :: BASE_TMP_PATH ,
65+ 'basePath ' => self :: BASE_PATH ,
5266 'allowedExtensions ' => ['jpg ' , 'jpeg ' , 'gif ' , 'png ' ],
5367 'allowedMimeTypes ' => ['image/jpg ' , 'image/jpeg ' , 'image/gif ' , 'image/png ' ]
5468 ]
5569 );
5670 }
5771
5872 /**
73+ * @dataProvider saveFileToTmpDirProvider
74+ * @param string $fileName
75+ * @param string $expectedName
5976 * @return void
6077 */
61- public function testSaveFileToTmpDir (): void
78+ public function testSaveFileToTmpDir (string $ fileName , string $ expectedName ): void
6279 {
63- $ fileName = 'magento_small_image.jpg ' ;
64- $ tmpDirectory = $ this ->filesystem ->getDirectoryWrite (\Magento \Framework \App \Filesystem \DirectoryList::SYS_TMP );
6580 $ fixtureDir = realpath (__DIR__ . '/../_files ' );
66- $ filePath = $ tmpDirectory ->getAbsolutePath ($ fileName );
81+ $ filePath = $ this -> tmpDirectory ->getAbsolutePath ($ fileName );
6782 copy ($ fixtureDir . DIRECTORY_SEPARATOR . $ fileName , $ filePath );
6883
6984 $ _FILES ['image ' ] = [
@@ -75,10 +90,27 @@ public function testSaveFileToTmpDir(): void
7590 ];
7691
7792 $ this ->imageUploader ->saveFileToTmpDir ('image ' );
78- $ filePath = $ this ->imageUploader ->getBaseTmpPath () . DIRECTORY_SEPARATOR . $ fileName ;
93+ $ filePath = $ this ->imageUploader ->getBaseTmpPath () . DIRECTORY_SEPARATOR . $ expectedName ;
7994 $ this ->assertTrue (is_file ($ this ->mediaDirectory ->getAbsolutePath ($ filePath )));
8095 }
8196
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+
82114 /**
83115 * Test that method rename files when move it with the same name into base directory.
84116 *
@@ -90,7 +122,7 @@ public function testMoveFileFromTmp(): void
90122 {
91123 $ expectedFilePath = $ this ->imageUploader ->getBasePath () . DIRECTORY_SEPARATOR . 'magento_small_image_1.jpg ' ;
92124
93- $ this ->assertFileNotExists ($ this ->mediaDirectory ->getAbsolutePath ($ expectedFilePath ));
125+ $ this ->assertFileDoesNotExist ($ this ->mediaDirectory ->getAbsolutePath ($ expectedFilePath ));
94126
95127 $ this ->imageUploader ->moveFileFromTmp ('magento_small_image.jpg ' );
96128
@@ -102,12 +134,11 @@ public function testMoveFileFromTmp(): void
102134 */
103135 public function testSaveFileToTmpDirWithWrongExtension (): void
104136 {
105- $ this ->expectException (\ Magento \ Framework \ Exception \ LocalizedException::class);
137+ $ this ->expectException (LocalizedException::class);
106138 $ this ->expectExceptionMessage ('File validation failed. ' );
107139
108140 $ 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 );
111142 $ file = fopen ($ filePath , "wb " );
112143 fwrite ($ file , 'just a text ' );
113144
@@ -120,7 +151,7 @@ public function testSaveFileToTmpDirWithWrongExtension(): void
120151 ];
121152
122153 $ this ->imageUploader ->saveFileToTmpDir ('image ' );
123- $ filePath = $ this ->imageUploader ->getBaseTmpPath () . DIRECTORY_SEPARATOR . $ fileName ;
154+ $ filePath = $ this ->imageUploader ->getBaseTmpPath () . DIRECTORY_SEPARATOR . $ fileName ;
124155 $ this ->assertFalse (is_file ($ this ->mediaDirectory ->getAbsolutePath ($ filePath )));
125156 }
126157
@@ -129,12 +160,11 @@ public function testSaveFileToTmpDirWithWrongExtension(): void
129160 */
130161 public function testSaveFileToTmpDirWithWrongFile (): void
131162 {
132- $ this ->expectException (\ Magento \ Framework \ Exception \ LocalizedException::class);
163+ $ this ->expectException (LocalizedException::class);
133164 $ this ->expectExceptionMessage ('File validation failed. ' );
134165
135166 $ 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 );
138168 $ file = fopen ($ filePath , "wb " );
139169 fwrite ($ file , 'just a text ' );
140170
@@ -147,7 +177,7 @@ public function testSaveFileToTmpDirWithWrongFile(): void
147177 ];
148178
149179 $ this ->imageUploader ->saveFileToTmpDir ('image ' );
150- $ filePath = $ this ->imageUploader ->getBaseTmpPath () . DIRECTORY_SEPARATOR . $ fileName ;
180+ $ filePath = $ this ->imageUploader ->getBaseTmpPath () . DIRECTORY_SEPARATOR . $ fileName ;
151181 $ this ->assertFalse (is_file ($ this ->mediaDirectory ->getAbsolutePath ($ filePath )));
152182 }
153183
@@ -157,11 +187,10 @@ public function testSaveFileToTmpDirWithWrongFile(): void
157187 public static function tearDownAfterClass (): void
158188 {
159189 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 */
164192 $ mediaDirectory = $ filesystem ->getDirectoryWrite (DirectoryList::MEDIA );
165- $ mediaDirectory ->delete ('tmp ' );
193+ $ mediaDirectory ->delete (self ::BASE_TMP_PATH );
194+ $ mediaDirectory ->delete (self ::BASE_PATH );
166195 }
167196}
0 commit comments