How to get base url in Unit Test?Tools for Unit Testing in MagentoUnit Test for overwrite collection class in magento2Magento Unit Test ErrorUnit test case for custom module REST APIUnit test Issue on getting valueWhat we can achieve by unit test in Magento 2 ? How it is useful?unit test with phpunitUnit test in magento 2 Error: Call to a member function getAllVisibleItems() on nullHow to post form data from Unit Test controller to custom controller | Magento 2Class 'MagentoTestFrameworkTestCaseAbstractController' not found when running unit test for custom module

How can I review my manager, who is fine?

Why am I getting unevenly-spread results when using $RANDOM?

force:lightningQuickAction . Will implementing this open the component as modalpop up directly?

What does "spinning upon the shoals" mean?

How to have a filled pattern

In layman's terms, does the Luckstone just give a passive +1 to all d20 rolls and saves except for death saves?

What is the meaning of "prairie-dog" in this sentence?

How many Jimmys can fit?

Sorting a list according to some pre-specified rules

As a supervisor, what feedback would you expect from a PhD who quits?

Array or vector? Two dimensional array or matrix?

Quotients of a ring of integers

How to reclaim personal item I've lent to the office without burning bridges?

How should I ask for a "pint" in countries that use metric?

NOLOCK or Read Uncommitted locking / latching behaviours

Why do Martians have to wear space helmets?

This LM317 diagram doesn't make any sense to me

What purpose does mercury dichloride have in fireworks?

Jimmy needs your help!

Computer name naming convention for security

How to understand flavors and when to use combination of them?

Sense of humor in your sci-fi stories

Four ships at the ocean with the same distance

Can one block with a protection from color creature?



How to get base url in Unit Test?


Tools for Unit Testing in MagentoUnit Test for overwrite collection class in magento2Magento Unit Test ErrorUnit test case for custom module REST APIUnit test Issue on getting valueWhat we can achieve by unit test in Magento 2 ? How it is useful?unit test with phpunitUnit test in magento 2 Error: Call to a member function getAllVisibleItems() on nullHow to post form data from Unit Test controller to custom controller | Magento 2Class 'MagentoTestFrameworkTestCaseAbstractController' not found when running unit test for custom module






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4















This is my unit test in my custom module:



namespace StarTrekTestUnitModel;

class BeyondTest extends PHPUnitFrameworkTestCase





how can i get my magento 2 base url in there?










share|improve this question















This question has an open bounty worth +100
reputation from mileven ending ending at 2019-07-08 02:02:17Z">in 2 days.


The question is widely applicable to a large audience. A detailed canonical answer is required to address all the concerns.
















  • Sounds like you need an integration test, not a unit test.

    – Shawn Abramson
    Jul 1 at 3:35

















4















This is my unit test in my custom module:



namespace StarTrekTestUnitModel;

class BeyondTest extends PHPUnitFrameworkTestCase





how can i get my magento 2 base url in there?










share|improve this question















This question has an open bounty worth +100
reputation from mileven ending ending at 2019-07-08 02:02:17Z">in 2 days.


The question is widely applicable to a large audience. A detailed canonical answer is required to address all the concerns.
















  • Sounds like you need an integration test, not a unit test.

    – Shawn Abramson
    Jul 1 at 3:35













4












4








4








This is my unit test in my custom module:



namespace StarTrekTestUnitModel;

class BeyondTest extends PHPUnitFrameworkTestCase





how can i get my magento 2 base url in there?










share|improve this question














This is my unit test in my custom module:



namespace StarTrekTestUnitModel;

class BeyondTest extends PHPUnitFrameworkTestCase





how can i get my magento 2 base url in there?







magento2 base-url unit-tests






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jun 28 at 10:07









milevenmileven

898 bronze badges




898 bronze badges






This question has an open bounty worth +100
reputation from mileven ending ending at 2019-07-08 02:02:17Z">in 2 days.


The question is widely applicable to a large audience. A detailed canonical answer is required to address all the concerns.








This question has an open bounty worth +100
reputation from mileven ending ending at 2019-07-08 02:02:17Z">in 2 days.


The question is widely applicable to a large audience. A detailed canonical answer is required to address all the concerns.














  • Sounds like you need an integration test, not a unit test.

    – Shawn Abramson
    Jul 1 at 3:35

















  • Sounds like you need an integration test, not a unit test.

    – Shawn Abramson
    Jul 1 at 3:35
















Sounds like you need an integration test, not a unit test.

– Shawn Abramson
Jul 1 at 3:35





Sounds like you need an integration test, not a unit test.

– Shawn Abramson
Jul 1 at 3:35










2 Answers
2






active

oldest

votes


















3














namespace StarTrekTestUnitModel;
class BeyondTest extends PHPUnitFrameworkTestCase

protected $_storeManager;

public function __construct(
MagentoStoreModelStoreManagerInterface $storeManager,
)

$this->_storeManager = $storeManager;



/**
* Prining URLs using StoreManagerInterface
*/
public function getStoreManagerData()

echo $this->_storeManager->getStore()->getBaseUrl() . '<br />';
echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_WEB) . '<br />';
echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_DIRECT_LINK) . '<br />';
echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA) . '<br />';
echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_STATIC) . '<br />';








share|improve this answer






























    1














    I think you have to look into the core Magento unit test code how they use and get base URL.



    I am not sure about this is what I shared is exactly you want, but I think it will help you.



    Look into the Magento core test module.




    vendor/magento/module-cms/Test/Unit/Helper/Wysiwyg/ImagesTest.php




    Find the function public function testGetBaseUrl() inside this function they get base URL $this->imagesHelper->getBaseUrl(); using imageHelper they are use protected variable protected $imagesHelper; by class MagentoCmsHelperWysiwygImages




    UPDATE:



    There is one anoter example of to get base URL using storeRepository look into the core module.




    vendor/magento/magento2-base/dev/tests/integration/testsuite/Magento/UrlRewrite/Model/StoreSwitcher/RewriteUrlTest.php




    See the public function testSwitchToNonExistingPage() they are get store using object manager using $storeRepository = $this->objectManager->create(MagentoStoreApiStoreRepositoryInterface::class); pass the storecode using $toStore = $storeRepository->get($toStoreCode); if you wan to get URL store specific.and finally get URL using $toStore->getBaseUrl();



    I think it will help to solve out your issue.






    share|improve this answer



























      Your Answer








      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "479"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f280055%2fhow-to-get-base-url-in-unit-test%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      namespace StarTrekTestUnitModel;
      class BeyondTest extends PHPUnitFrameworkTestCase

      protected $_storeManager;

      public function __construct(
      MagentoStoreModelStoreManagerInterface $storeManager,
      )

      $this->_storeManager = $storeManager;



      /**
      * Prining URLs using StoreManagerInterface
      */
      public function getStoreManagerData()

      echo $this->_storeManager->getStore()->getBaseUrl() . '<br />';
      echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_WEB) . '<br />';
      echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_DIRECT_LINK) . '<br />';
      echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA) . '<br />';
      echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_STATIC) . '<br />';








      share|improve this answer



























        3














        namespace StarTrekTestUnitModel;
        class BeyondTest extends PHPUnitFrameworkTestCase

        protected $_storeManager;

        public function __construct(
        MagentoStoreModelStoreManagerInterface $storeManager,
        )

        $this->_storeManager = $storeManager;



        /**
        * Prining URLs using StoreManagerInterface
        */
        public function getStoreManagerData()

        echo $this->_storeManager->getStore()->getBaseUrl() . '<br />';
        echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_WEB) . '<br />';
        echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_DIRECT_LINK) . '<br />';
        echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA) . '<br />';
        echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_STATIC) . '<br />';








        share|improve this answer

























          3












          3








          3







          namespace StarTrekTestUnitModel;
          class BeyondTest extends PHPUnitFrameworkTestCase

          protected $_storeManager;

          public function __construct(
          MagentoStoreModelStoreManagerInterface $storeManager,
          )

          $this->_storeManager = $storeManager;



          /**
          * Prining URLs using StoreManagerInterface
          */
          public function getStoreManagerData()

          echo $this->_storeManager->getStore()->getBaseUrl() . '<br />';
          echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_WEB) . '<br />';
          echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_DIRECT_LINK) . '<br />';
          echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA) . '<br />';
          echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_STATIC) . '<br />';








          share|improve this answer













          namespace StarTrekTestUnitModel;
          class BeyondTest extends PHPUnitFrameworkTestCase

          protected $_storeManager;

          public function __construct(
          MagentoStoreModelStoreManagerInterface $storeManager,
          )

          $this->_storeManager = $storeManager;



          /**
          * Prining URLs using StoreManagerInterface
          */
          public function getStoreManagerData()

          echo $this->_storeManager->getStore()->getBaseUrl() . '<br />';
          echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_WEB) . '<br />';
          echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_DIRECT_LINK) . '<br />';
          echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA) . '<br />';
          echo $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_STATIC) . '<br />';









          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jul 1 at 4:28









          Ketan BoradaKetan Borada

          90511 silver badges40 bronze badges




          90511 silver badges40 bronze badges























              1














              I think you have to look into the core Magento unit test code how they use and get base URL.



              I am not sure about this is what I shared is exactly you want, but I think it will help you.



              Look into the Magento core test module.




              vendor/magento/module-cms/Test/Unit/Helper/Wysiwyg/ImagesTest.php




              Find the function public function testGetBaseUrl() inside this function they get base URL $this->imagesHelper->getBaseUrl(); using imageHelper they are use protected variable protected $imagesHelper; by class MagentoCmsHelperWysiwygImages




              UPDATE:



              There is one anoter example of to get base URL using storeRepository look into the core module.




              vendor/magento/magento2-base/dev/tests/integration/testsuite/Magento/UrlRewrite/Model/StoreSwitcher/RewriteUrlTest.php




              See the public function testSwitchToNonExistingPage() they are get store using object manager using $storeRepository = $this->objectManager->create(MagentoStoreApiStoreRepositoryInterface::class); pass the storecode using $toStore = $storeRepository->get($toStoreCode); if you wan to get URL store specific.and finally get URL using $toStore->getBaseUrl();



              I think it will help to solve out your issue.






              share|improve this answer





























                1














                I think you have to look into the core Magento unit test code how they use and get base URL.



                I am not sure about this is what I shared is exactly you want, but I think it will help you.



                Look into the Magento core test module.




                vendor/magento/module-cms/Test/Unit/Helper/Wysiwyg/ImagesTest.php




                Find the function public function testGetBaseUrl() inside this function they get base URL $this->imagesHelper->getBaseUrl(); using imageHelper they are use protected variable protected $imagesHelper; by class MagentoCmsHelperWysiwygImages




                UPDATE:



                There is one anoter example of to get base URL using storeRepository look into the core module.




                vendor/magento/magento2-base/dev/tests/integration/testsuite/Magento/UrlRewrite/Model/StoreSwitcher/RewriteUrlTest.php




                See the public function testSwitchToNonExistingPage() they are get store using object manager using $storeRepository = $this->objectManager->create(MagentoStoreApiStoreRepositoryInterface::class); pass the storecode using $toStore = $storeRepository->get($toStoreCode); if you wan to get URL store specific.and finally get URL using $toStore->getBaseUrl();



                I think it will help to solve out your issue.






                share|improve this answer



























                  1












                  1








                  1







                  I think you have to look into the core Magento unit test code how they use and get base URL.



                  I am not sure about this is what I shared is exactly you want, but I think it will help you.



                  Look into the Magento core test module.




                  vendor/magento/module-cms/Test/Unit/Helper/Wysiwyg/ImagesTest.php




                  Find the function public function testGetBaseUrl() inside this function they get base URL $this->imagesHelper->getBaseUrl(); using imageHelper they are use protected variable protected $imagesHelper; by class MagentoCmsHelperWysiwygImages




                  UPDATE:



                  There is one anoter example of to get base URL using storeRepository look into the core module.




                  vendor/magento/magento2-base/dev/tests/integration/testsuite/Magento/UrlRewrite/Model/StoreSwitcher/RewriteUrlTest.php




                  See the public function testSwitchToNonExistingPage() they are get store using object manager using $storeRepository = $this->objectManager->create(MagentoStoreApiStoreRepositoryInterface::class); pass the storecode using $toStore = $storeRepository->get($toStoreCode); if you wan to get URL store specific.and finally get URL using $toStore->getBaseUrl();



                  I think it will help to solve out your issue.






                  share|improve this answer















                  I think you have to look into the core Magento unit test code how they use and get base URL.



                  I am not sure about this is what I shared is exactly you want, but I think it will help you.



                  Look into the Magento core test module.




                  vendor/magento/module-cms/Test/Unit/Helper/Wysiwyg/ImagesTest.php




                  Find the function public function testGetBaseUrl() inside this function they get base URL $this->imagesHelper->getBaseUrl(); using imageHelper they are use protected variable protected $imagesHelper; by class MagentoCmsHelperWysiwygImages




                  UPDATE:



                  There is one anoter example of to get base URL using storeRepository look into the core module.




                  vendor/magento/magento2-base/dev/tests/integration/testsuite/Magento/UrlRewrite/Model/StoreSwitcher/RewriteUrlTest.php




                  See the public function testSwitchToNonExistingPage() they are get store using object manager using $storeRepository = $this->objectManager->create(MagentoStoreApiStoreRepositoryInterface::class); pass the storecode using $toStore = $storeRepository->get($toStoreCode); if you wan to get URL store specific.and finally get URL using $toStore->getBaseUrl();



                  I think it will help to solve out your issue.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jul 1 at 5:32

























                  answered Jul 1 at 5:14









                  Chirag PatelChirag Patel

                  3,6796 silver badges28 bronze badges




                  3,6796 silver badges28 bronze badges



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Magento Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f280055%2fhow-to-get-base-url-in-unit-test%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                      Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

                      Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?