CurrentCustomerTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Helper\Session;
  7. class CurrentCustomerTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Customer\Helper\Session\CurrentCustomer
  11. */
  12. protected $currentCustomer;
  13. /**
  14. * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $customerSessionMock;
  17. /**
  18. * @var \Magento\Framework\View\LayoutInterface|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $layoutMock;
  21. /**
  22. * @var \Magento\Customer\Api\Data\CustomerInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $customerInterfaceFactoryMock;
  25. /**
  26. * @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $customerDataMock;
  29. /**
  30. * @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $customerRepositoryMock;
  33. /**
  34. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $requestMock;
  37. /**
  38. * @var \Magento\Framework\Module\Manager|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $moduleManagerMock;
  41. /**
  42. * @var \Magento\Framework\App\ViewInterface|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected $viewMock;
  45. /**
  46. * @var int
  47. */
  48. protected $customerId = 100;
  49. /**
  50. * @var int
  51. */
  52. protected $customerGroupId = 500;
  53. /**
  54. * Test setup
  55. */
  56. protected function setUp()
  57. {
  58. $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
  59. $this->layoutMock = $this->createMock(\Magento\Framework\View\Layout::class);
  60. $this->customerInterfaceFactoryMock = $this->createPartialMock(
  61. \Magento\Customer\Api\Data\CustomerInterfaceFactory::class,
  62. ['create', 'setGroupId']
  63. );
  64. $this->customerDataMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  65. $this->customerRepositoryMock = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
  66. $this->requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
  67. $this->moduleManagerMock = $this->createMock(\Magento\Framework\Module\Manager::class);
  68. $this->viewMock = $this->createMock(\Magento\Framework\App\View::class);
  69. $this->currentCustomer = new \Magento\Customer\Helper\Session\CurrentCustomer(
  70. $this->customerSessionMock,
  71. $this->layoutMock,
  72. $this->customerInterfaceFactoryMock,
  73. $this->customerRepositoryMock,
  74. $this->requestMock,
  75. $this->moduleManagerMock,
  76. $this->viewMock
  77. );
  78. }
  79. /**
  80. * test getCustomer method, method returns depersonalized customer Data
  81. */
  82. public function testGetCustomerDepersonalizeCustomerData()
  83. {
  84. $this->requestMock->expects($this->once())->method('isAjax')->will($this->returnValue(false));
  85. $this->layoutMock->expects($this->once())->method('isCacheable')->will($this->returnValue(true));
  86. $this->viewMock->expects($this->once())->method('isLayoutLoaded')->will($this->returnValue(true));
  87. $this->moduleManagerMock->expects($this->once())
  88. ->method('isEnabled')
  89. ->with($this->equalTo('Magento_PageCache'))
  90. ->will($this->returnValue(true));
  91. $this->customerSessionMock->expects($this->once())
  92. ->method('getCustomerGroupId')
  93. ->will($this->returnValue($this->customerGroupId));
  94. $this->customerInterfaceFactoryMock->expects($this->once())
  95. ->method('create')
  96. ->will($this->returnValue($this->customerDataMock));
  97. $this->customerDataMock->expects($this->once())
  98. ->method('setGroupId')
  99. ->with($this->equalTo($this->customerGroupId))
  100. ->will($this->returnSelf());
  101. $this->assertEquals($this->customerDataMock, $this->currentCustomer->getCustomer());
  102. }
  103. /**
  104. * test get customer method, method returns customer from service
  105. */
  106. public function testGetCustomerLoadCustomerFromService()
  107. {
  108. $this->moduleManagerMock->expects($this->once())
  109. ->method('isEnabled')
  110. ->with($this->equalTo('Magento_PageCache'))
  111. ->will($this->returnValue(false));
  112. $this->customerSessionMock->expects($this->once())
  113. ->method('getId')
  114. ->will($this->returnValue($this->customerId));
  115. $this->customerRepositoryMock->expects($this->once())
  116. ->method('getById')
  117. ->with($this->equalTo($this->customerId))
  118. ->will($this->returnValue($this->customerDataMock));
  119. $this->assertEquals($this->customerDataMock, $this->currentCustomer->getCustomer());
  120. }
  121. }