CustomerScopeDataTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Block;
  7. use Magento\Framework\App\Config\ScopeConfigInterface;
  8. use Magento\Framework\View\Element\Template\Context;
  9. use Magento\Store\Api\Data\StoreInterface;
  10. use Magento\Store\Model\StoreManagerInterface;
  11. use Magento\Customer\Block\CustomerScopeData;
  12. class CustomerScopeDataTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /** @var \Magento\Customer\Block\CustomerScopeData */
  15. private $model;
  16. /** @var \Magento\Framework\View\Element\Template\Context|\PHPUnit_Framework_MockObject_MockObject */
  17. private $contextMock;
  18. /** @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  19. private $storeManagerMock;
  20. /** @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
  21. private $scopeConfigMock;
  22. /** @var \Magento\Framework\Json\EncoderInterface|\PHPUnit_Framework_MockObject_MockObject */
  23. private $encoderMock;
  24. /** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
  25. private $serializerMock;
  26. protected function setUp()
  27. {
  28. $this->contextMock = $this->getMockBuilder(Context::class)
  29. ->disableOriginalConstructor()
  30. ->getMock();
  31. $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
  32. ->getMock();
  33. $this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
  34. ->getMock();
  35. $this->encoderMock = $this->getMockBuilder(\Magento\Framework\Json\EncoderInterface::class)
  36. ->getMock();
  37. $this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
  38. ->getMock();
  39. $this->contextMock->expects($this->exactly(2))
  40. ->method('getStoreManager')
  41. ->willReturn($this->storeManagerMock);
  42. $this->contextMock->expects($this->once())
  43. ->method('getScopeConfig')
  44. ->willReturn($this->scopeConfigMock);
  45. $this->model = new CustomerScopeData(
  46. $this->contextMock,
  47. $this->encoderMock,
  48. [],
  49. $this->serializerMock
  50. );
  51. }
  52. public function testGetWebsiteId()
  53. {
  54. $storeId = 1;
  55. $storeMock = $this->getMockBuilder(StoreInterface::class)
  56. ->setMethods(['getWebsiteId'])
  57. ->getMockForAbstractClass();
  58. $storeMock->expects($this->any())
  59. ->method('getWebsiteId')
  60. ->willReturn($storeId);
  61. $this->storeManagerMock->expects($this->any())
  62. ->method('getStore')
  63. ->with(null)
  64. ->willReturn($storeMock);
  65. $this->assertEquals($storeId, $this->model->getWebsiteId());
  66. }
  67. public function testEncodeConfiguration()
  68. {
  69. $rules = [
  70. '*' => [
  71. 'Magento_Customer/js/invalidation-processor' => [
  72. 'invalidationRules' => [
  73. 'website-rule' => [
  74. 'Magento_Customer/js/invalidation-rules/website-rule' => [
  75. 'scopeConfig' => [
  76. 'websiteId' => 1,
  77. ]
  78. ]
  79. ]
  80. ]
  81. ]
  82. ],
  83. ];
  84. $this->serializerMock->expects($this->any())
  85. ->method('serialize')
  86. ->with($rules)
  87. ->willReturn(json_encode($rules));
  88. $this->assertEquals(
  89. json_encode($rules),
  90. $this->model->encodeConfiguration($rules)
  91. );
  92. }
  93. }