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

                      Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

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

                      Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form