10
10
use Magento \Framework \File \Uploader ;
11
11
use PHPUnit \Framework \TestCase ;
12
12
use PHPUnit \Framework \MockObject \MockObject ;
13
+ use Magento \Framework \App \Filesystem \DirectoryList ;
14
+ use Magento \Framework \Filesystem ;
15
+ use Magento \Framework \Filesystem \Directory \TargetDirectory ;
16
+ use Magento \Framework \Filesystem \DriverPool ;
13
17
14
18
/**
15
19
* Unit Test class for \Magento\Framework\File\Uploader
@@ -21,9 +25,37 @@ class UploaderTest extends TestCase
21
25
*/
22
26
private $ uploader ;
23
27
28
+ /**
29
+ * Allowed extensions array
30
+ *
31
+ * @var array
32
+ */
33
+ private $ _allowedMimeTypes = [
34
+ 'php ' => 'text/plain ' ,
35
+ 'txt ' => 'text/plain '
36
+ ];
37
+
24
38
protected function setUp (): void
25
39
{
26
- $ this ->uploader = $ this ->createMock (Uploader::class);
40
+ $ class = new \ReflectionObject ($ this );
41
+ $ fileName = $ class ->getFilename ();
42
+ $ fileType = 'php ' ;
43
+ $ this ->setupFiles (1230123 , $ fileName , $ fileType );
44
+
45
+ $ driverPool = $ this ->createMock (DriverPool::class);
46
+ $ directoryList = $ this ->createMock (DirectoryList::class);
47
+ $ filesystem = $ this ->createMock (Filesystem::class);
48
+ $ targetDirectory = $ this ->createMock (TargetDirectory::class);
49
+
50
+ $ this ->uploader = new Uploader (
51
+ "fileId " ,
52
+ null ,
53
+ $ directoryList ,
54
+ $ driverPool ,
55
+ $ targetDirectory ,
56
+ $ filesystem
57
+ );
58
+ $ this ->uploader ->setAllowedExtensions (array_keys ($ this ->_allowedMimeTypes ));
27
59
}
28
60
29
61
/**
@@ -87,11 +119,10 @@ public function getCorrectFileNameProvider()
87
119
*/
88
120
public function testCheckAllowedExtension (bool $ isValid , string $ extension )
89
121
{
90
- $ this ->uploader
91
- ->expects ($ this ->never ())
92
- ->method ('checkAllowedExtension ' )
93
- ->with ($ extension )
94
- ->willReturn ($ isValid );
122
+ $ this ->assertEquals (
123
+ $ isValid ,
124
+ $ this ->uploader ->checkAllowedExtension ($ extension )
125
+ );
95
126
}
96
127
97
128
/**
@@ -102,23 +133,51 @@ public function checkAllowedExtensionProvider(): array
102
133
return [
103
134
[
104
135
true ,
105
- 'jpeg '
136
+ 'txt '
137
+ ],
138
+ [
139
+ false ,
140
+ 'png '
106
141
],
107
142
[
108
143
false ,
109
144
'$#@$#@$3 '
110
145
],
111
146
[
112
- true ,
113
- '4324324324jpeg '
147
+ false ,
148
+ '4324324324txt '
114
149
],
115
150
[
116
151
false ,
117
152
'$#$#$jpeg..$#2$#@$#@$ '
118
153
],
119
154
[
120
155
false ,
121
- '../../jpeg '
156
+ '../../txt '
157
+ ],
158
+ [
159
+ true ,
160
+ 'php '
161
+ ]
162
+ ];
163
+ }
164
+
165
+ /**
166
+ * Setup global variable $_FILES.
167
+ *
168
+ * @param int $fileSize
169
+ * @param string $fileName
170
+ * @param string $fileType
171
+ * @return void
172
+ */
173
+ private function setupFiles ($ fileSize , $ fileName , $ fileType ) {
174
+ $ _FILES = [
175
+ 'fileId ' => [
176
+ 'name ' => $ fileName ,
177
+ 'type ' => $ fileType ,
178
+ 'tmp_name ' => $ fileName ,
179
+ 'error ' => 0 ,
180
+ 'size ' => $ fileSize ,
122
181
]
123
182
];
124
183
}
0 commit comments