9
9
use Magento \Framework \App \Filesystem \DirectoryList ;
10
10
use Magento \Framework \File \Mime ;
11
11
use Magento \Framework \Filesystem ;
12
- use Magento \Framework \Filesystem \Directory \WriteInterface ;
13
12
use Magento \Framework \Filesystem \Directory \ReadInterface ;
13
+ use Magento \Framework \Filesystem \Directory \WriteInterface ;
14
+ use PHPUnit \Framework \MockObject \MockObject ;
15
+ use PHPUnit \Framework \TestCase ;
14
16
15
- class FileInfoTest extends \PHPUnit \Framework \TestCase
17
+ /**
18
+ * Test for Magento\Catalog\Model\Category\FileInfo class.
19
+ */
20
+ class FileInfoTest extends TestCase
16
21
{
17
22
/**
18
- * @var Filesystem|\PHPUnit_Framework_MockObject_MockObject
23
+ * @var Filesystem|MockObject
19
24
*/
20
25
private $ filesystem ;
21
26
22
27
/**
23
- * @var Mime|\PHPUnit_Framework_MockObject_MockObject
28
+ * @var Mime|MockObject
24
29
*/
25
30
private $ mime ;
26
31
27
32
/**
28
- * @var WriteInterface|\PHPUnit_Framework_MockObject_MockObject
33
+ * @var WriteInterface|MockObject
29
34
*/
30
35
private $ mediaDirectory ;
31
36
32
37
/**
33
- * @var ReadInterface|\PHPUnit_Framework_MockObject_MockObject
38
+ * @var ReadInterface|MockObject
34
39
*/
35
40
private $ baseDirectory ;
36
41
42
+ /**
43
+ * @var ReadInterface|MockObject
44
+ */
45
+ private $ pubDirectory ;
46
+
37
47
/**
38
48
* @var FileInfo
39
49
*/
@@ -44,30 +54,43 @@ protected function setUp()
44
54
$ this ->mediaDirectory = $ this ->getMockBuilder (WriteInterface::class)
45
55
->getMockForAbstractClass ();
46
56
47
- $ this ->baseDirectory = $ this ->getMockBuilder (ReadInterface::class)
57
+ $ this ->baseDirectory = $ baseDirectory = $ this ->getMockBuilder (ReadInterface::class)
58
+ ->getMockForAbstractClass ();
59
+
60
+ $ this ->pubDirectory = $ pubDirectory = $ this ->getMockBuilder (ReadInterface::class)
48
61
->getMockForAbstractClass ();
49
62
50
63
$ this ->filesystem = $ this ->getMockBuilder (Filesystem::class)
51
64
->disableOriginalConstructor ()
52
65
->getMock ();
53
- $ this -> filesystem -> expects ( $ this -> any ())
54
- ->method ('getDirectoryWrite ' )
66
+
67
+ $ this -> filesystem ->method ('getDirectoryWrite ' )
55
68
->with (DirectoryList::MEDIA )
56
69
->willReturn ($ this ->mediaDirectory );
57
70
58
- $ this ->filesystem ->expects ($ this ->any ())
59
- ->method ('getDirectoryRead ' )
60
- ->with (DirectoryList::ROOT )
61
- ->willReturn ($ this ->baseDirectory );
71
+ $ this ->filesystem ->method ('getDirectoryRead ' )
72
+ ->willReturnCallback (
73
+ function ($ arg ) use ($ baseDirectory , $ pubDirectory ) {
74
+ if ($ arg === DirectoryList::PUB ) {
75
+ return $ pubDirectory ;
76
+ }
77
+ return $ baseDirectory ;
78
+ }
79
+ );
62
80
63
81
$ this ->mime = $ this ->getMockBuilder (Mime::class)
64
82
->disableOriginalConstructor ()
65
83
->getMock ();
66
84
67
- $ this ->baseDirectory ->expects ($ this ->any ())
68
- ->method ('getAbsolutePath ' )
69
- ->with (null )
70
- ->willReturn ('/a/b/c ' );
85
+ $ this ->baseDirectory ->method ('getAbsolutePath ' )
86
+ ->willReturn ('/a/b/c/ ' );
87
+
88
+ $ this ->baseDirectory ->method ('getRelativePath ' )
89
+ ->with ('/a/b/c/pub/ ' )
90
+ ->willReturn ('pub/ ' );
91
+
92
+ $ this ->pubDirectory ->method ('getAbsolutePath ' )
93
+ ->willReturn ('/a/b/c/pub/ ' );
71
94
72
95
$ this ->model = new FileInfo (
73
96
$ this ->filesystem ,
@@ -85,12 +108,12 @@ public function testGetMimeType()
85
108
$ this ->mediaDirectory ->expects ($ this ->at (0 ))
86
109
->method ('getAbsolutePath ' )
87
110
->with (null )
88
- ->willReturn ('/a/b/c/pub/media ' );
111
+ ->willReturn ('/a/b/c/pub/media/ ' );
89
112
90
113
$ this ->mediaDirectory ->expects ($ this ->at (1 ))
91
114
->method ('getAbsolutePath ' )
92
115
->with (null )
93
- ->willReturn ('/a/b/c/pub/media ' );
116
+ ->willReturn ('/a/b/c/pub/media/ ' );
94
117
95
118
$ this ->mediaDirectory ->expects ($ this ->at (2 ))
96
119
->method ('getAbsolutePath ' )
@@ -113,13 +136,11 @@ public function testGetStat()
113
136
114
137
$ expected = ['size ' => 1 ];
115
138
116
- $ this ->mediaDirectory ->expects ($ this ->any ())
117
- ->method ('getAbsolutePath ' )
139
+ $ this ->mediaDirectory ->method ('getAbsolutePath ' )
118
140
->with (null )
119
- ->willReturn ('/a/b/c/pub/media ' );
141
+ ->willReturn ('/a/b/c/pub/media/ ' );
120
142
121
- $ this ->mediaDirectory ->expects ($ this ->once ())
122
- ->method ('stat ' )
143
+ $ this ->mediaDirectory ->method ('stat ' )
123
144
->with ($ mediaPath . $ fileName )
124
145
->willReturn ($ expected );
125
146
@@ -130,22 +151,52 @@ public function testGetStat()
130
151
$ this ->assertEquals (1 , $ result ['size ' ]);
131
152
}
132
153
133
- public function testIsExist ()
154
+ /**
155
+ * @param $fileName
156
+ * @param $fileMediaPath
157
+ * @dataProvider isExistProvider
158
+ */
159
+ public function testIsExist ($ fileName , $ fileMediaPath )
134
160
{
135
- $ mediaPath = '/catalog/category ' ;
161
+ $ this ->mediaDirectory ->method ('getAbsolutePath ' )
162
+ ->willReturn ('/a/b/c/pub/media/ ' );
136
163
137
- $ fileName = '/filename.ext1 ' ;
138
-
139
- $ this ->mediaDirectory ->expects ($ this ->any ())
140
- ->method ('getAbsolutePath ' )
141
- ->with (null )
142
- ->willReturn ('/a/b/c/pub/media ' );
143
-
144
- $ this ->mediaDirectory ->expects ($ this ->once ())
145
- ->method ('isExist ' )
146
- ->with ($ mediaPath . $ fileName )
164
+ $ this ->mediaDirectory ->method ('isExist ' )
165
+ ->with ($ fileMediaPath )
147
166
->willReturn (true );
148
167
149
168
$ this ->assertTrue ($ this ->model ->isExist ($ fileName ));
150
169
}
170
+
171
+ public function isExistProvider ()
172
+ {
173
+ return [
174
+ ['/filename.ext1 ' , '/catalog/category/filename.ext1 ' ],
175
+ ['/pub/media/filename.ext1 ' , 'filename.ext1 ' ],
176
+ ['/media/filename.ext1 ' , 'filename.ext1 ' ]
177
+ ];
178
+ }
179
+
180
+ /**
181
+ * @param $fileName
182
+ * @param $expected
183
+ * @dataProvider isBeginsWithMediaDirectoryPathProvider
184
+ */
185
+ public function testIsBeginsWithMediaDirectoryPath ($ fileName , $ expected )
186
+ {
187
+ $ this ->mediaDirectory ->method ('getAbsolutePath ' )
188
+ ->willReturn ('/a/b/c/pub/media/ ' );
189
+
190
+ $ this ->assertEquals ($ expected , $ this ->model ->isBeginsWithMediaDirectoryPath ($ fileName ));
191
+ }
192
+
193
+ public function isBeginsWithMediaDirectoryPathProvider ()
194
+ {
195
+ return [
196
+ ['/pub/media/test/filename.ext1 ' , true ],
197
+ ['/media/test/filename.ext1 ' , true ],
198
+ ['/test/filename.ext1 ' , false ],
199
+ ['test2/filename.ext1 ' , false ]
200
+ ];
201
+ }
151
202
}
0 commit comments