1111use Magento \Framework \Filesystem ;
1212use Magento \Framework \Filesystem \Directory \WriteInterface ;
1313use Magento \Framework \Filesystem \Directory \ReadInterface ;
14+ use PHPUnit \Framework \MockObject \MockObject ;
15+ use PHPUnit \Framework \TestCase ;
1416
15- class FileInfoTest extends \ PHPUnit \ Framework \ TestCase
17+ class FileInfoTest extends TestCase
1618{
1719 /**
18- * @var Filesystem|\PHPUnit_Framework_MockObject_MockObject
20+ * @var Filesystem|MockObject
1921 */
2022 private $ filesystem ;
2123
2224 /**
23- * @var Mime|\PHPUnit_Framework_MockObject_MockObject
25+ * @var Mime|MockObject
2426 */
2527 private $ mime ;
2628
2729 /**
28- * @var WriteInterface|\PHPUnit_Framework_MockObject_MockObject
30+ * @var WriteInterface|MockObject
2931 */
3032 private $ mediaDirectory ;
3133
3234 /**
33- * @var ReadInterface|\PHPUnit_Framework_MockObject_MockObject
35+ * @var ReadInterface|MockObject
3436 */
3537 private $ baseDirectory ;
3638
39+ /**
40+ * @var ReadInterface|MockObject
41+ */
42+ private $ pubDirectory ;
43+
3744 /**
3845 * @var FileInfo
3946 */
@@ -44,31 +51,42 @@ protected function setUp()
4451 $ this ->mediaDirectory = $ this ->getMockBuilder (WriteInterface::class)
4552 ->getMockForAbstractClass ();
4653
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)
4858 ->getMockForAbstractClass ();
4959
5060 $ this ->filesystem = $ this ->getMockBuilder (Filesystem::class)
5161 ->disableOriginalConstructor ()
5262 ->getMock ();
53- $ this -> filesystem -> expects ( $ this -> any ())
54- ->method ('getDirectoryWrite ' )
63+
64+ $ this -> filesystem ->method ('getDirectoryWrite ' )
5565 ->with (DirectoryList::MEDIA )
5666 ->willReturn ($ this ->mediaDirectory );
5767
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+ });
6275
6376 $ this ->mime = $ this ->getMockBuilder (Mime::class)
6477 ->disableOriginalConstructor ()
6578 ->getMock ();
6679
67- $ this ->baseDirectory ->expects ($ this ->any ())
68- ->method ('getAbsolutePath ' )
69- ->with (null )
80+ $ this ->baseDirectory ->method ('getAbsolutePath ' )
7081 ->willReturn ('/a/b/c/ ' );
7182
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/ ' );
89+
7290 $ this ->model = new FileInfo (
7391 $ this ->filesystem ,
7492 $ this ->mime
@@ -113,13 +131,11 @@ public function testGetStat()
113131
114132 $ expected = ['size ' => 1 ];
115133
116- $ this ->mediaDirectory ->expects ($ this ->any ())
117- ->method ('getAbsolutePath ' )
134+ $ this ->mediaDirectory ->method ('getAbsolutePath ' )
118135 ->with (null )
119136 ->willReturn ('/a/b/c/pub/media/ ' );
120137
121- $ this ->mediaDirectory ->expects ($ this ->once ())
122- ->method ('stat ' )
138+ $ this ->mediaDirectory ->method ('stat ' )
123139 ->with ($ mediaPath . $ fileName )
124140 ->willReturn ($ expected );
125141
@@ -137,12 +153,10 @@ public function testGetStat()
137153 */
138154 public function testIsExist ($ fileName , $ fileMediaPath )
139155 {
140- $ this ->mediaDirectory ->expects ($ this ->any ())
141- ->method ('getAbsolutePath ' )
156+ $ this ->mediaDirectory ->method ('getAbsolutePath ' )
142157 ->willReturn ('/a/b/c/pub/media/ ' );
143158
144- $ this ->mediaDirectory ->expects ($ this ->once ())
145- ->method ('isExist ' )
159+ $ this ->mediaDirectory ->method ('isExist ' )
146160 ->with ($ fileMediaPath )
147161 ->willReturn (true );
148162
@@ -165,8 +179,7 @@ public function isExistProvider()
165179 */
166180 public function testIsBeginsWithMediaDirectoryPath ($ fileName , $ expected )
167181 {
168- $ this ->mediaDirectory ->expects ($ this ->any ())
169- ->method ('getAbsolutePath ' )
182+ $ this ->mediaDirectory ->method ('getAbsolutePath ' )
170183 ->willReturn ('/a/b/c/pub/media/ ' );
171184
172185 $ this ->assertEquals ($ expected , $ this ->model ->isBeginsWithMediaDirectoryPath ($ fileName ));
0 commit comments