2020
2121class UploadTest extends TestCase
2222{
23- /**
24- * Directory paths sample
25- */
26- const DIRECTORY_ABSOLUTE_PATH = 'dasdad ' ;
27- /**
28- * Allowed Extensions to Upload a file
29- */
30- const ALLOWED_EXTENSIONS = ['csv ' , 'zip ' ];
31- /**
32- * The name to use when saving the uploaded file
33- */
34- const SAVED_FILE_NAME = 'testString ' ;
35- /**
36- * The ID of the file being imported.
37- */
38- const IMPORT_FILE_ID = 'import_file ' ;
39-
4023 /**
4124 * @var Upload
4225 */
@@ -48,17 +31,17 @@ class UploadTest extends TestCase
4831 protected FileTransferFactory |MockObject $ httpFactoryMock ;
4932
5033 /**
51- * @var DataHelper|\PHPUnit\Framework\MockObject\ MockObject
34+ * @var DataHelper|MockObject
5235 */
5336 private DataHelper |MockObject $ importExportDataMock ;
5437
5538 /**
56- * @var UploaderFactory|\PHPUnit\Framework\MockObject\ MockObject
39+ * @var UploaderFactory|MockObject
5740 */
5841 private UploaderFactory |MockObject $ uploaderFactoryMock ;
5942
6043 /**
61- * @var Random|\PHPUnit\Framework\MockObject\ MockObject
44+ * @var Random|MockObject
6245 */
6346 private Random |MockObject $ randomMock ;
6447
@@ -73,19 +56,14 @@ class UploadTest extends TestCase
7356 private Http |MockObject $ adapterMock ;
7457
7558 /**
76- * @var Uploader
59+ * @var Uploader|MockObject
7760 */
78- private Uploader $ uploaderMock ;
61+ private Uploader | MockObject $ uploaderMock ;
7962
80- /**
81- * @inheritDoc
82- */
8363 protected function setUp (): void
8464 {
85- $ this ->httpFactoryMock = $ this ->createPartialMock (
86- FileTransferFactory::class,
87- ['create ' ]
88- );
65+ $ directoryAbsolutePath = 'importexport/ ' ;
66+ $ this ->httpFactoryMock = $ this ->createPartialMock (FileTransferFactory::class, ['create ' ]);
8967 $ this ->importExportDataMock = $ this ->createMock (DataHelper::class);
9068 $ this ->uploaderFactoryMock = $ this ->getMockBuilder (UploaderFactory::class)
9169 ->disableOriginalConstructor ()
@@ -95,13 +73,11 @@ protected function setUp(): void
9573 ->getMock ();
9674 $ this ->filesystemMock = $ this ->createMock (Filesystem::class);
9775 $ this ->adapterMock = $ this ->createMock (Http::class);
98- $ directoryWrite = $ this ->getMockBuilder (WriteInterface::class)
76+ $ directoryWriteMock = $ this ->getMockBuilder (WriteInterface::class)
9977 ->disableOriginalConstructor ()
10078 ->getMock ();
101- $ directoryWrite ->expects ($ this ->once ())->method ('getAbsolutePath ' )
102- ->willReturn (self ::DIRECTORY_ABSOLUTE_PATH );
103- $ this ->filesystemMock ->expects ($ this ->once ())->method ('getDirectoryWrite ' )
104- ->willReturn ($ directoryWrite );
79+ $ directoryWriteMock ->expects ($ this ->once ())->method ('getAbsolutePath ' )->willReturn ($ directoryAbsolutePath );
80+ $ this ->filesystemMock ->expects ($ this ->once ())->method ('getDirectoryWrite ' )->willReturn ($ directoryWriteMock );
10581 $ this ->upload = new Upload (
10682 $ this ->httpFactoryMock ,
10783 $ this ->importExportDataMock ,
@@ -112,24 +88,26 @@ protected function setUp(): void
11288 }
11389
11490 /**
115- * Test that the uploadSource method uploads a file and returns an array.
116- *
11791 * @return void
118- * @throws \Magento\Framework\Exception\LocalizedException
11992 */
120- public function testUploadSource (): void
93+ public function testValidateFileUploadReturnsSavedFileArray (): void
12194 {
95+ $ allowedExtensions = ['csv ' , 'zip ' ];
96+ $ savedFileName = 'testString ' ;
97+ $ importFileId = 'import_file ' ;
98+ $ randomStringLength =32 ;
12299 $ this ->adapterMock ->method ('isValid ' )->willReturn (true );
123100 $ this ->httpFactoryMock ->method ('create ' )->willReturn ($ this ->adapterMock );
124101 $ this ->uploaderMock = $ this ->createMock (Uploader::class);
125- $ this ->uploaderMock ->method ('setAllowedExtensions ' )->with (self :: ALLOWED_EXTENSIONS );
102+ $ this ->uploaderMock ->method ('setAllowedExtensions ' )->with ($ allowedExtensions );
126103 $ this ->uploaderMock ->method ('skipDbProcessing ' )->with (true );
127104 $ this ->uploaderFactoryMock ->method ('create ' )
128- ->with (['fileId ' => self :: IMPORT_FILE_ID ])
105+ ->with (['fileId ' => $ importFileId ])
129106 ->willReturn ($ this ->uploaderMock );
130- $ this ->randomMock ->method ('getRandomString ' )->with (32 );
131- $ this ->uploaderMock ->method ('save ' )->willReturn (['file ' => self :: SAVED_FILE_NAME ]);
132- $ result = $ this ->upload ->uploadSource (self :: SAVED_FILE_NAME );
107+ $ this ->randomMock ->method ('getRandomString ' )->with ($ randomStringLength );
108+ $ this ->uploaderMock ->method ('save ' )->willReturn (['file ' => $ savedFileName ]);
109+ $ result = $ this ->upload ->uploadSource ($ savedFileName );
133110 $ this ->assertIsArray ($ result );
111+ $ this ->assertEquals ($ savedFileName , $ result ['file ' ]);
134112 }
135113}
0 commit comments