ContextPluginTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Test\Unit\App\Action;
  7. class ContextPluginTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Tax\Helper\Data
  11. */
  12. protected $taxHelperMock;
  13. /**
  14. * @var \Magento\Weee\Helper\Data
  15. */
  16. protected $weeeHelperMock;
  17. /**
  18. * @var \Magento\Weee\Model\Tax
  19. */
  20. protected $weeeTaxMock;
  21. /**
  22. * @var \Magento\Framework\App\Http\Context
  23. */
  24. protected $httpContextMock;
  25. /**
  26. * @var \Magento\Tax\Model\Calculation\Proxy
  27. */
  28. protected $taxCalculationMock;
  29. /**
  30. * Module manager
  31. *
  32. * @var \Magento\Framework\Module\Manager
  33. */
  34. private $moduleManagerMock;
  35. /**
  36. * Cache config
  37. *
  38. * @var \Magento\PageCache\Model\Config
  39. */
  40. private $cacheConfigMock;
  41. /**
  42. * @var \Magento\Tax\Model\App\Action\ContextPlugin
  43. */
  44. protected $contextPlugin;
  45. protected function setUp()
  46. {
  47. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  48. $this->taxHelperMock = $this->getMockBuilder(\Magento\Tax\Helper\Data::class)
  49. ->disableOriginalConstructor()
  50. ->getMock();
  51. $this->weeeHelperMock = $this->getMockBuilder(\Magento\Weee\Helper\Data::class)
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $this->weeeTaxMock = $this->getMockBuilder(\Magento\Weee\Model\Tax::class)
  55. ->disableOriginalConstructor()
  56. ->getMock();
  57. $this->httpContextMock = $this->getMockBuilder(\Magento\Framework\App\Http\Context::class)
  58. ->disableOriginalConstructor()
  59. ->getMock();
  60. $this->taxCalculationMock = $this->getMockBuilder(\Magento\Tax\Model\Calculation\Proxy::class)
  61. ->disableOriginalConstructor()
  62. ->setMethods(['getTaxRates'])
  63. ->getMock();
  64. $this->customerSessionMock = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
  65. ->disableOriginalConstructor()
  66. ->setMethods([
  67. 'getDefaultTaxBillingAddress', 'getDefaultTaxShippingAddress', 'getCustomerTaxClassId',
  68. 'getWebsiteId', 'isLoggedIn'
  69. ])
  70. ->getMock();
  71. $this->moduleManagerMock = $this->getMockBuilder(\Magento\Framework\Module\Manager::class)
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $this->cacheConfigMock = $this->getMockBuilder(\Magento\PageCache\Model\Config::class)
  75. ->disableOriginalConstructor()
  76. ->getMock();
  77. $this->contextPlugin = $this->objectManager->getObject(
  78. \Magento\Tax\Model\App\Action\ContextPlugin::class,
  79. [
  80. 'customerSession' => $this->customerSessionMock,
  81. 'httpContext' => $this->httpContextMock,
  82. 'calculation' => $this->taxCalculationMock,
  83. 'weeeTax' => $this->weeeTaxMock,
  84. 'taxHelper' => $this->taxHelperMock,
  85. 'weeeHelper' => $this->weeeHelperMock,
  86. 'moduleManager' => $this->moduleManagerMock,
  87. 'cacheConfig' => $this->cacheConfigMock
  88. ]
  89. );
  90. }
  91. /**
  92. * @param bool $cache
  93. * @param bool $taxEnabled
  94. * @param bool $loggedIn
  95. * @dataProvider beforeDispatchDataProvider
  96. */
  97. public function testBeforeDispatch($cache, $taxEnabled, $loggedIn)
  98. {
  99. $this->customerSessionMock->expects($this->any())
  100. ->method('isLoggedIn')
  101. ->willReturn($loggedIn);
  102. $this->moduleManagerMock->expects($this->any())
  103. ->method('isEnabled')
  104. ->with('Magento_PageCache')
  105. ->willReturn($cache);
  106. $this->cacheConfigMock->expects($this->any())
  107. ->method('isEnabled')
  108. ->willReturn($cache);
  109. if ($cache && $loggedIn) {
  110. $this->taxHelperMock->expects($this->any())
  111. ->method('isCatalogPriceDisplayAffectedByTax')
  112. ->willReturn($taxEnabled);
  113. if ($taxEnabled) {
  114. $this->customerSessionMock->expects($this->once())
  115. ->method('getDefaultTaxBillingAddress')
  116. ->willReturn(['country_id' => 1, 'region_id' => 1, 'postcode' => 11111]);
  117. $this->customerSessionMock->expects($this->once())
  118. ->method('getDefaultTaxShippingAddress')
  119. ->willReturn(['country_id' => 1, 'region_id' => 1, 'postcode' => 11111]);
  120. $this->customerSessionMock->expects($this->once())
  121. ->method('getCustomerTaxClassId')
  122. ->willReturn(1);
  123. $this->taxCalculationMock->expects($this->once())
  124. ->method('getTaxRates')
  125. ->with(
  126. ['country_id' => 1, 'region_id' => 1, 'postcode' => 11111],
  127. ['country_id' => 1, 'region_id' => 1, 'postcode' => 11111],
  128. 1
  129. )
  130. ->willReturn([]);
  131. $this->httpContextMock->expects($this->any())
  132. ->method('setValue')
  133. ->with('tax_rates', [], 0);
  134. }
  135. $action = $this->objectManager->getObject(\Magento\Framework\App\Test\Unit\Action\Stub\ActionStub::class);
  136. $request = $this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getActionName']);
  137. $result = $this->contextPlugin->beforeDispatch($action, $request);
  138. $this->assertNull($result);
  139. } else {
  140. $this->assertFalse($loggedIn);
  141. }
  142. }
  143. /**
  144. * @return array
  145. */
  146. public function beforeDispatchDataProvider()
  147. {
  148. return [
  149. [false, false, false],
  150. [true, true, false],
  151. [true, true, true],
  152. [true, false, true],
  153. [true, true, true]
  154. ];
  155. }
  156. }