CustomerNotificationTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Model\Plugin;
  7. use Magento\Backend\App\AbstractAction;
  8. use Magento\Customer\Api\CustomerRepositoryInterface;
  9. use Magento\Customer\Api\Data\CustomerInterface;
  10. use Magento\Customer\Model\Customer\NotificationStorage;
  11. use Magento\Customer\Model\Plugin\CustomerNotification;
  12. use Magento\Customer\Model\Session;
  13. use Magento\Framework\App\Area;
  14. use Magento\Framework\App\RequestInterface;
  15. use Magento\Framework\App\State;
  16. use Magento\Framework\Exception\NoSuchEntityException;
  17. use Psr\Log\LoggerInterface;
  18. class CustomerNotificationTest extends \PHPUnit\Framework\TestCase
  19. {
  20. /** @var Session|\PHPUnit_Framework_MockObject_MockObject */
  21. private $sessionMock;
  22. /** @var NotificationStorage|\PHPUnit_Framework_MockObject_MockObject */
  23. private $notificationStorageMock;
  24. /** @var CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
  25. private $customerRepositoryMock;
  26. /** @var State|\PHPUnit_Framework_MockObject_MockObject */
  27. private $appStateMock;
  28. /** @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
  29. private $requestMock;
  30. /** @var AbstractAction|\PHPUnit_Framework_MockObject_MockObject */
  31. private $abstractActionMock;
  32. /** @var LoggerInterface */
  33. private $loggerMock;
  34. /** @var CustomerNotification */
  35. private $plugin;
  36. /** @var int */
  37. private static $customerId = 1;
  38. protected function setUp()
  39. {
  40. $this->sessionMock = $this->getMockBuilder(Session::class)
  41. ->disableOriginalConstructor()
  42. ->setMethods(['getCustomerId', 'setCustomerData', 'setCustomerGroupId', 'regenerateId'])
  43. ->getMock();
  44. $this->notificationStorageMock = $this->getMockBuilder(NotificationStorage::class)
  45. ->disableOriginalConstructor()
  46. ->setMethods(['isExists', 'remove'])
  47. ->getMock();
  48. $this->customerRepositoryMock = $this->getMockBuilder(CustomerRepositoryInterface::class)
  49. ->getMockForAbstractClass();
  50. $this->abstractActionMock = $this->getMockBuilder(AbstractAction::class)
  51. ->disableOriginalConstructor()
  52. ->getMockForAbstractClass();
  53. $this->requestMock = $this->getMockBuilder(RequestInterface::class)
  54. ->setMethods(['isPost'])
  55. ->getMockForAbstractClass();
  56. $this->appStateMock = $this->getMockBuilder(State::class)
  57. ->disableOriginalConstructor()
  58. ->setMethods(['getAreaCode'])
  59. ->getMock();
  60. $this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
  61. $this->appStateMock->method('getAreaCode')->willReturn(Area::AREA_FRONTEND);
  62. $this->requestMock->method('isPost')->willReturn(true);
  63. $this->sessionMock->method('getCustomerId')->willReturn(self::$customerId);
  64. $this->notificationStorageMock->expects($this->any())
  65. ->method('isExists')
  66. ->with(NotificationStorage::UPDATE_CUSTOMER_SESSION, self::$customerId)
  67. ->willReturn(true);
  68. $this->plugin = new CustomerNotification(
  69. $this->sessionMock,
  70. $this->notificationStorageMock,
  71. $this->appStateMock,
  72. $this->customerRepositoryMock,
  73. $this->loggerMock
  74. );
  75. }
  76. public function testBeforeDispatch()
  77. {
  78. $customerGroupId =1;
  79. $customerMock = $this->getMockForAbstractClass(CustomerInterface::class);
  80. $customerMock->method('getGroupId')->willReturn($customerGroupId);
  81. $customerMock->method('getId')->willReturn(self::$customerId);
  82. $this->customerRepositoryMock->expects($this->once())
  83. ->method('getById')
  84. ->with(self::$customerId)
  85. ->willReturn($customerMock);
  86. $this->notificationStorageMock->expects($this->once())
  87. ->method('remove')
  88. ->with(NotificationStorage::UPDATE_CUSTOMER_SESSION, self::$customerId);
  89. $this->sessionMock->expects($this->once())->method('setCustomerData')->with($customerMock);
  90. $this->sessionMock->expects($this->once())->method('setCustomerGroupId')->with($customerGroupId);
  91. $this->sessionMock->expects($this->once())->method('regenerateId');
  92. $this->plugin->beforeDispatch($this->abstractActionMock, $this->requestMock);
  93. }
  94. public function testBeforeDispatchWithNoCustomerFound()
  95. {
  96. $this->customerRepositoryMock->method('getById')
  97. ->with(self::$customerId)
  98. ->willThrowException(new NoSuchEntityException());
  99. $this->loggerMock->expects($this->once())
  100. ->method('error');
  101. $this->plugin->beforeDispatch($this->abstractActionMock, $this->requestMock);
  102. }
  103. }