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