55 */
66namespace Magento \Sitemap \Test \Unit \Model ;
77
8+ use Magento \Framework \App \Request \Http ;
89use Magento \Framework \DataObject ;
910use Magento \Framework \Filesystem ;
1011use Magento \Framework \Filesystem \Directory \Write as DirectoryWrite ;
@@ -86,6 +87,15 @@ class SitemapTest extends \PHPUnit\Framework\TestCase
8687 */
8788 private $ configReaderMock ;
8889
90+ /**
91+ * @var Http|\PHPUnit_Framework_MockObject_MockObject
92+ */
93+ private $ request ;
94+ /**
95+ * @var Store|\PHPUnit_Framework_MockObject_MockObject
96+ */
97+ private $ store ;
98+
8999 /**
90100 * @inheritdoc
91101 */
@@ -143,6 +153,12 @@ protected function setUp()
143153
144154 $ this ->configReaderMock = $ this ->getMockForAbstractClass (SitemapConfigReaderInterface::class);
145155 $ this ->itemProviderMock = $ this ->getMockForAbstractClass (ItemProviderInterface::class);
156+ $ this ->request = $ this ->createMock (Http::class);
157+ $ this ->store = $ this ->createPartialMock (Store::class, ['isFrontUrlSecure ' , 'getBaseUrl ' ]);
158+ $ this ->storeManagerMock = $ this ->createMock (StoreManagerInterface::class);
159+ $ this ->storeManagerMock ->expects ($ this ->any ())
160+ ->method ('getStore ' )
161+ ->willReturn ($ this ->store );
146162 }
147163
148164 /**
@@ -476,25 +492,15 @@ function ($from, $to) {
476492
477493 $ model = $ this ->getModelMock (true );
478494
479- $ storeMock = $ this ->getMockBuilder (Store::class)
480- ->setMethods (['isFrontUrlSecure ' , 'getBaseUrl ' ])
481- ->disableOriginalConstructor ()
482- ->getMock ();
483-
484- $ storeMock ->expects ($ this ->atLeastOnce ())
495+ $ this ->store ->expects ($ this ->atLeastOnce ())
485496 ->method ('isFrontUrlSecure ' )
486497 ->willReturn (false );
487498
488- $ storeMock ->expects ($ this ->atLeastOnce ())
499+ $ this -> store ->expects ($ this ->atLeastOnce ())
489500 ->method ('getBaseUrl ' )
490501 ->with ($ this ->isType ('string ' ), false )
491502 ->willReturn ('http://store.com/ ' );
492503
493- $ this ->storeManagerMock ->expects ($ this ->atLeastOnce ())
494- ->method ('getStore ' )
495- ->with (1 )
496- ->willReturn ($ storeMock );
497-
498504 return $ model ;
499505 }
500506
@@ -599,10 +605,6 @@ private function getModelConstructorArgs()
599605 ->disableOriginalConstructor ()
600606 ->getMock ();
601607
602- $ this ->storeManagerMock = $ this ->getMockBuilder (StoreManagerInterface::class)
603- ->setMethods (['getStore ' ])
604- ->getMockForAbstractClass ();
605-
606608 $ objectManager = new ObjectManager ($ this );
607609 $ escaper = $ objectManager ->getObject (\Magento \Framework \Escaper::class);
608610
@@ -617,7 +619,8 @@ private function getModelConstructorArgs()
617619 'filesystem ' => $ this ->filesystemMock ,
618620 'itemProvider ' => $ this ->itemProviderMock ,
619621 'configReader ' => $ this ->configReaderMock ,
620- 'escaper ' => $ escaper
622+ 'escaper ' => $ escaper ,
623+ 'request ' => $ this ->request ,
621624 ]
622625 );
623626 $ constructArguments ['resource ' ] = null ;
@@ -732,4 +735,62 @@ public static function siteUrlDataProvider()
732735 ]
733736 ];
734737 }
738+
739+ /**
740+ * Check site URL getter
741+ *
742+ * @param string $storeBaseUrl
743+ * @param string $baseDir
744+ * @param string $documentRoot
745+ * @dataProvider getDocumentRootFromBaseDirUrlDataProvider
746+ */
747+ public function testGetDocumentRootFromBaseDir (
748+ string $ storeBaseUrl ,
749+ string $ baseDir ,
750+ ?string $ documentRoot
751+ ) {
752+ $ this ->store ->setCode ('store ' );
753+ $ this ->store ->method ('getBaseUrl ' )->willReturn ($ storeBaseUrl );
754+ $ this ->directoryMock ->method ('getAbsolutePath ' )->willReturn ($ baseDir );
755+ /** @var $model Sitemap */
756+ $ model = $ this ->getMockBuilder (Sitemap::class)
757+ ->setMethods (['_construct ' ])
758+ ->setConstructorArgs ($ this ->getModelConstructorArgs ())
759+ ->getMock ();
760+
761+ $ method = new \ReflectionMethod ($ model , 'getDocumentRootFromBaseDir ' );
762+ $ method ->setAccessible (true );
763+ $ this ->assertSame ($ documentRoot , $ method ->invoke ($ model ));
764+ }
765+
766+ /**
767+ * Provides test cases for document root testing
768+ *
769+ * @return array
770+ */
771+ public function getDocumentRootFromBaseDirUrlDataProvider (): array
772+ {
773+ return [
774+ [
775+ 'http://magento.com/ ' ,
776+ '/var/www ' ,
777+ '/var/www ' ,
778+ ],
779+ [
780+ 'http://magento.com/usa ' ,
781+ '/var/www/usa ' ,
782+ '/var/www ' ,
783+ ],
784+ [
785+ 'http://magento.com/usa/tx ' ,
786+ '/var/www/usa/tx ' ,
787+ '/var/www ' ,
788+ ],
789+ 'symlink <document root>/usa/txt -> /var/www/html ' => [
790+ 'http://magento.com/usa/tx ' ,
791+ '/var/www/html ' ,
792+ null ,
793+ ],
794+ ];
795+ }
735796}
0 commit comments