99use Magento \Framework \App \Filesystem \DirectoryList ;
1010use Magento \Framework \File \Mime ;
1111use Magento \Framework \Filesystem ;
12- use Magento \Framework \Filesystem \Directory \WriteInterface ;
1312use Magento \Framework \Filesystem \Directory \ReadInterface ;
13+ use Magento \Framework \Filesystem \Directory \WriteInterface ;
14+ use PHPUnit \Framework \MockObject \MockObject ;
15+ use PHPUnit \Framework \TestCase ;
1416
15- class FileInfoTest extends \PHPUnit \Framework \TestCase
17+ /**
18+ * Test for Magento\Catalog\Model\Category\FileInfo class.
19+ */
20+ class FileInfoTest extends TestCase
1621{
1722 /**
18- * @var Filesystem|\PHPUnit_Framework_MockObject_MockObject
23+ * @var Filesystem|MockObject
1924 */
2025 private $ filesystem ;
2126
2227 /**
23- * @var Mime|\PHPUnit_Framework_MockObject_MockObject
28+ * @var Mime|MockObject
2429 */
2530 private $ mime ;
2631
2732 /**
28- * @var WriteInterface|\PHPUnit_Framework_MockObject_MockObject
33+ * @var WriteInterface|MockObject
2934 */
3035 private $ mediaDirectory ;
3136
3237 /**
33- * @var ReadInterface|\PHPUnit_Framework_MockObject_MockObject
38+ * @var ReadInterface|MockObject
3439 */
3540 private $ baseDirectory ;
3641
42+ /**
43+ * @var ReadInterface|MockObject
44+ */
45+ private $ pubDirectory ;
46+
3747 /**
3848 * @var FileInfo
3949 */
@@ -44,30 +54,43 @@ protected function setUp()
4454 $ this ->mediaDirectory = $ this ->getMockBuilder (WriteInterface::class)
4555 ->getMockForAbstractClass ();
4656
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)
4861 ->getMockForAbstractClass ();
4962
5063 $ this ->filesystem = $ this ->getMockBuilder (Filesystem::class)
5164 ->disableOriginalConstructor ()
5265 ->getMock ();
53- $ this -> filesystem -> expects ( $ this -> any ())
54- ->method ('getDirectoryWrite ' )
66+
67+ $ this -> filesystem ->method ('getDirectoryWrite ' )
5568 ->with (DirectoryList::MEDIA )
5669 ->willReturn ($ this ->mediaDirectory );
5770
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+ );
6280
6381 $ this ->mime = $ this ->getMockBuilder (Mime::class)
6482 ->disableOriginalConstructor ()
6583 ->getMock ();
6684
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/ ' );
7194
7295 $ this ->model = new FileInfo (
7396 $ this ->filesystem ,
@@ -85,12 +108,12 @@ public function testGetMimeType()
85108 $ this ->mediaDirectory ->expects ($ this ->at (0 ))
86109 ->method ('getAbsolutePath ' )
87110 ->with (null )
88- ->willReturn ('/a/b/c/pub/media ' );
111+ ->willReturn ('/a/b/c/pub/media/ ' );
89112
90113 $ this ->mediaDirectory ->expects ($ this ->at (1 ))
91114 ->method ('getAbsolutePath ' )
92115 ->with (null )
93- ->willReturn ('/a/b/c/pub/media ' );
116+ ->willReturn ('/a/b/c/pub/media/ ' );
94117
95118 $ this ->mediaDirectory ->expects ($ this ->at (2 ))
96119 ->method ('getAbsolutePath ' )
@@ -113,13 +136,11 @@ public function testGetStat()
113136
114137 $ expected = ['size ' => 1 ];
115138
116- $ this ->mediaDirectory ->expects ($ this ->any ())
117- ->method ('getAbsolutePath ' )
139+ $ this ->mediaDirectory ->method ('getAbsolutePath ' )
118140 ->with (null )
119- ->willReturn ('/a/b/c/pub/media ' );
141+ ->willReturn ('/a/b/c/pub/media/ ' );
120142
121- $ this ->mediaDirectory ->expects ($ this ->once ())
122- ->method ('stat ' )
143+ $ this ->mediaDirectory ->method ('stat ' )
123144 ->with ($ mediaPath . $ fileName )
124145 ->willReturn ($ expected );
125146
@@ -130,22 +151,52 @@ public function testGetStat()
130151 $ this ->assertEquals (1 , $ result ['size ' ]);
131152 }
132153
133- public function testIsExist ()
154+ /**
155+ * @param $fileName
156+ * @param $fileMediaPath
157+ * @dataProvider isExistProvider
158+ */
159+ public function testIsExist ($ fileName , $ fileMediaPath )
134160 {
135- $ mediaPath = '/catalog/category ' ;
161+ $ this ->mediaDirectory ->method ('getAbsolutePath ' )
162+ ->willReturn ('/a/b/c/pub/media/ ' );
136163
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 )
147166 ->willReturn (true );
148167
149168 $ this ->assertTrue ($ this ->model ->isExist ($ fileName ));
150169 }
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+ }
151202}
0 commit comments