Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomerNotificationTest.php
Go to the documentation of this file.
1 <?php
7 
18 use Psr\Log\LoggerInterface;
19 
20 class CustomerNotificationTest extends \PHPUnit\Framework\TestCase
21 {
23  private $sessionMock;
24 
26  private $notificationStorageMock;
27 
29  private $customerRepositoryMock;
30 
32  private $appStateMock;
33 
35  private $requestMock;
36 
38  private $abstractActionMock;
39 
41  private $loggerMock;
42 
44  private $plugin;
45 
47  private static $customerId = 1;
48 
49  protected function setUp()
50  {
51  $this->sessionMock = $this->getMockBuilder(Session::class)
52  ->disableOriginalConstructor()
53  ->setMethods(['getCustomerId', 'setCustomerData', 'setCustomerGroupId', 'regenerateId'])
54  ->getMock();
55  $this->notificationStorageMock = $this->getMockBuilder(NotificationStorage::class)
56  ->disableOriginalConstructor()
57  ->setMethods(['isExists', 'remove'])
58  ->getMock();
59  $this->customerRepositoryMock = $this->getMockBuilder(CustomerRepositoryInterface::class)
60  ->getMockForAbstractClass();
61  $this->abstractActionMock = $this->getMockBuilder(AbstractAction::class)
62  ->disableOriginalConstructor()
63  ->getMockForAbstractClass();
64  $this->requestMock = $this->getMockBuilder(RequestInterface::class)
65  ->setMethods(['isPost'])
66  ->getMockForAbstractClass();
67  $this->appStateMock = $this->getMockBuilder(State::class)
68  ->disableOriginalConstructor()
69  ->setMethods(['getAreaCode'])
70  ->getMock();
71 
72  $this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
73  $this->appStateMock->method('getAreaCode')->willReturn(Area::AREA_FRONTEND);
74  $this->requestMock->method('isPost')->willReturn(true);
75  $this->sessionMock->method('getCustomerId')->willReturn(self::$customerId);
76  $this->notificationStorageMock->expects($this->any())
77  ->method('isExists')
79  ->willReturn(true);
80 
81  $this->plugin = new CustomerNotification(
82  $this->sessionMock,
83  $this->notificationStorageMock,
84  $this->appStateMock,
85  $this->customerRepositoryMock,
86  $this->loggerMock
87  );
88  }
89 
90  public function testBeforeDispatch()
91  {
92  $customerGroupId =1;
93 
94  $customerMock = $this->getMockForAbstractClass(CustomerInterface::class);
95  $customerMock->method('getGroupId')->willReturn($customerGroupId);
96  $customerMock->method('getId')->willReturn(self::$customerId);
97 
98  $this->customerRepositoryMock->expects($this->once())
99  ->method('getById')
100  ->with(self::$customerId)
101  ->willReturn($customerMock);
102  $this->notificationStorageMock->expects($this->once())
103  ->method('remove')
105 
106  $this->sessionMock->expects($this->once())->method('setCustomerData')->with($customerMock);
107  $this->sessionMock->expects($this->once())->method('setCustomerGroupId')->with($customerGroupId);
108  $this->sessionMock->expects($this->once())->method('regenerateId');
109 
110  $this->plugin->beforeDispatch($this->abstractActionMock, $this->requestMock);
111  }
112 
114  {
115  $this->customerRepositoryMock->method('getById')
116  ->with(self::$customerId)
117  ->willThrowException(new NoSuchEntityException());
118  $this->loggerMock->expects($this->once())
119  ->method('error');
120 
121  $this->plugin->beforeDispatch($this->abstractActionMock, $this->requestMock);
122  }
123 }