SwitcherTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\Test\Unit\Block;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. class SwitcherTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /** @var \Magento\Store\Block\Switcher */
  11. protected $switcher;
  12. /** @var \Magento\Framework\View\Element\Template\Context|\PHPUnit_Framework_MockObject_MockObject */
  13. protected $context;
  14. /** @var \Magento\Framework\Data\Helper\PostHelper|\PHPUnit_Framework_MockObject_MockObject */
  15. protected $corePostDataHelper;
  16. /** @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  17. protected $storeManager;
  18. /** @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $urlBuilder;
  20. /** @var \Magento\Store\Api\Data\StoreInterface|\PHPUnit_Framework_MockObject_MockObject */
  21. private $store;
  22. /**
  23. * @return void
  24. */
  25. protected function setUp()
  26. {
  27. $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)->getMock();
  28. $this->urlBuilder = $this->createMock(\Magento\Framework\UrlInterface::class);
  29. $this->context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
  30. $this->context->expects($this->any())->method('getStoreManager')->will($this->returnValue($this->storeManager));
  31. $this->context->expects($this->any())->method('getUrlBuilder')->will($this->returnValue($this->urlBuilder));
  32. $this->corePostDataHelper = $this->createMock(\Magento\Framework\Data\Helper\PostHelper::class);
  33. $this->store = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
  34. ->disableOriginalConstructor()
  35. ->getMockForAbstractClass();
  36. $this->switcher = (new ObjectManager($this))->getObject(
  37. \Magento\Store\Block\Switcher::class,
  38. [
  39. 'context' => $this->context,
  40. 'postDataHelper' => $this->corePostDataHelper,
  41. ]
  42. );
  43. }
  44. /**
  45. * @return void
  46. */
  47. public function testGetTargetStorePostData()
  48. {
  49. $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $store->expects($this->any())
  53. ->method('getCode')
  54. ->willReturn('new-store');
  55. $storeSwitchUrl = 'http://domain.com/stores/store/redirect';
  56. $store->expects($this->atLeastOnce())
  57. ->method('getCurrentUrl')
  58. ->with(false)
  59. ->willReturn($storeSwitchUrl);
  60. $this->storeManager->expects($this->once())
  61. ->method('getStore')
  62. ->willReturn($this->store);
  63. $this->store->expects($this->once())
  64. ->method('getCode')
  65. ->willReturn('old-store');
  66. $this->urlBuilder->expects($this->once())
  67. ->method('getUrl')
  68. ->willReturn($storeSwitchUrl);
  69. $this->corePostDataHelper->expects($this->any())
  70. ->method('getPostData')
  71. ->with($storeSwitchUrl, ['___store' => 'new-store', 'uenc' => null, '___from_store' => 'old-store']);
  72. $this->switcher->getTargetStorePostData($store);
  73. }
  74. /**
  75. * @dataProvider isStoreInUrlDataProvider
  76. * @param bool $isUseStoreInUrl
  77. */
  78. public function testIsStoreInUrl($isUseStoreInUrl)
  79. {
  80. $storeMock = $this->createMock(\Magento\Store\Model\Store::class);
  81. $storeMock->expects($this->once())->method('isUseStoreInUrl')->will($this->returnValue($isUseStoreInUrl));
  82. $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($storeMock));
  83. $this->assertEquals($this->switcher->isStoreInUrl(), $isUseStoreInUrl);
  84. // check value is cached
  85. $this->assertEquals($this->switcher->isStoreInUrl(), $isUseStoreInUrl);
  86. }
  87. /**
  88. * @see self::testIsStoreInUrlDataProvider()
  89. * @return array
  90. */
  91. public function isStoreInUrlDataProvider()
  92. {
  93. return [[true], [false]];
  94. }
  95. }