|
5 | 5 | */
|
6 | 6 | namespace Magento\Customer\Test\Unit\Model\Plugin;
|
7 | 7 |
|
| 8 | +use Magento\Backend\App\AbstractAction; |
| 9 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 10 | +use Magento\Customer\Api\Data\CustomerInterface; |
8 | 11 | use Magento\Customer\Model\Customer\NotificationStorage;
|
9 | 12 | use Magento\Customer\Model\Plugin\CustomerNotification;
|
| 13 | +use Magento\Customer\Model\Session; |
| 14 | +use Magento\Framework\App\Area; |
| 15 | +use Magento\Framework\App\RequestInterface; |
| 16 | +use Magento\Framework\App\State; |
| 17 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 18 | +use Psr\Log\LoggerInterface; |
10 | 19 |
|
11 | 20 | class CustomerNotificationTest extends \PHPUnit\Framework\TestCase
|
12 | 21 | {
|
13 |
| - /** @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject */ |
14 |
| - protected $session; |
| 22 | + /** @var Session|\PHPUnit_Framework_MockObject_MockObject */ |
| 23 | + private $sessionMock; |
15 | 24 |
|
16 |
| - /** @var \Magento\Customer\Model\Customer\NotificationStorage|\PHPUnit_Framework_MockObject_MockObject */ |
17 |
| - protected $notificationStorage; |
| 25 | + /** @var NotificationStorage|\PHPUnit_Framework_MockObject_MockObject */ |
| 26 | + private $notificationStorageMock; |
18 | 27 |
|
19 |
| - /** @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */ |
20 |
| - protected $customerRepository; |
| 28 | + /** @var CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 29 | + private $customerRepositoryMock; |
21 | 30 |
|
22 |
| - /** @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject */ |
23 |
| - protected $appState; |
| 31 | + /** @var State|\PHPUnit_Framework_MockObject_MockObject */ |
| 32 | + private $appStateMock; |
24 | 33 |
|
25 |
| - /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */ |
26 |
| - protected $request; |
| 34 | + /** @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 35 | + private $requestMock; |
27 | 36 |
|
28 |
| - /** @var \Magento\Backend\App\AbstractAction|\PHPUnit_Framework_MockObject_MockObject */ |
29 |
| - protected $abstractAction; |
| 37 | + /** @var AbstractAction|\PHPUnit_Framework_MockObject_MockObject */ |
| 38 | + private $abstractActionMock; |
| 39 | + |
| 40 | + /** @var LoggerInterface */ |
| 41 | + private $loggerMock; |
30 | 42 |
|
31 | 43 | /** @var CustomerNotification */
|
32 |
| - protected $plugin; |
| 44 | + private $plugin; |
| 45 | + |
| 46 | + /** @var int */ |
| 47 | + private static $customerId = 1; |
33 | 48 |
|
34 | 49 | protected function setUp()
|
35 | 50 | {
|
36 |
| - $this->session = $this->getMockBuilder(\Magento\Customer\Model\Session::class) |
| 51 | + $this->sessionMock = $this->getMockBuilder(Session::class) |
37 | 52 | ->disableOriginalConstructor()
|
| 53 | + ->setMethods(['getCustomerId', 'setCustomerData', 'setCustomerGroupId', 'regenerateId']) |
38 | 54 | ->getMock();
|
39 |
| - $this->notificationStorage = $this->getMockBuilder(\Magento\Customer\Model\Customer\NotificationStorage::class) |
| 55 | + $this->notificationStorageMock = $this->getMockBuilder(NotificationStorage::class) |
40 | 56 | ->disableOriginalConstructor()
|
| 57 | + ->setMethods(['isExists', 'remove']) |
41 | 58 | ->getMock();
|
42 |
| - $this->customerRepository = $this->getMockBuilder(\Magento\Customer\Api\CustomerRepositoryInterface::class) |
| 59 | + $this->customerRepositoryMock = $this->getMockBuilder(CustomerRepositoryInterface::class) |
43 | 60 | ->getMockForAbstractClass();
|
44 |
| - $this->abstractAction = $this->getMockBuilder(\Magento\Backend\App\AbstractAction::class) |
| 61 | + $this->abstractActionMock = $this->getMockBuilder(AbstractAction::class) |
45 | 62 | ->disableOriginalConstructor()
|
46 | 63 | ->getMockForAbstractClass();
|
47 |
| - $this->request = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class) |
| 64 | + $this->requestMock = $this->getMockBuilder(RequestInterface::class) |
48 | 65 | ->setMethods(['isPost'])
|
49 | 66 | ->getMockForAbstractClass();
|
50 |
| - $this->appState = $this->getMockBuilder(\Magento\Framework\App\State::class) |
51 |
| - ->disableOriginalConstructor()->getMock(); |
| 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') |
| 78 | + ->with(NotificationStorage::UPDATE_CUSTOMER_SESSION, self::$customerId) |
| 79 | + ->willReturn(true); |
| 80 | + |
52 | 81 | $this->plugin = new CustomerNotification(
|
53 |
| - $this->session, |
54 |
| - $this->notificationStorage, |
55 |
| - $this->appState, |
56 |
| - $this->customerRepository |
| 82 | + $this->sessionMock, |
| 83 | + $this->notificationStorageMock, |
| 84 | + $this->appStateMock, |
| 85 | + $this->customerRepositoryMock, |
| 86 | + $this->loggerMock |
57 | 87 | );
|
58 | 88 | }
|
59 | 89 |
|
60 | 90 | public function testBeforeDispatch()
|
61 | 91 | {
|
62 |
| - $customerId = 1; |
63 | 92 | $customerGroupId =1;
|
64 |
| - $this->appState->expects($this->any()) |
65 |
| - ->method('getAreaCode') |
66 |
| - ->willReturn(\Magento\Framework\App\Area::AREA_FRONTEND); |
67 |
| - $this->request->expects($this->any())->method('isPost')->willReturn(true); |
68 |
| - $customerMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class) |
69 |
| - ->getMockForAbstractClass(); |
70 |
| - $customerMock->expects($this->any())->method('getGroupId')->willReturn($customerGroupId); |
71 |
| - $this->customerRepository->expects($this->any()) |
| 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()) |
72 | 99 | ->method('getById')
|
73 |
| - ->with($customerId) |
| 100 | + ->with(self::$customerId) |
74 | 101 | ->willReturn($customerMock);
|
75 |
| - $this->session->expects($this->any())->method('getCustomerId')->willReturn($customerId); |
76 |
| - $this->session->expects($this->any())->method('setCustomerData')->with($customerMock); |
77 |
| - $this->session->expects($this->any())->method('setCustomerGroupId')->with($customerGroupId); |
78 |
| - $this->session->expects($this->once())->method('regenerateId'); |
79 |
| - $this->notificationStorage->expects($this->any()) |
80 |
| - ->method('isExists') |
81 |
| - ->with(NotificationStorage::UPDATE_CUSTOMER_SESSION, $customerId) |
82 |
| - ->willReturn(true); |
| 102 | + $this->notificationStorageMock->expects($this->once()) |
| 103 | + ->method('remove') |
| 104 | + ->with(NotificationStorage::UPDATE_CUSTOMER_SESSION, self::$customerId); |
| 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 | + |
| 113 | + public function testBeforeDispatchWithNoCustomerFound() |
| 114 | + { |
| 115 | + $this->customerRepositoryMock->method('getById') |
| 116 | + ->with(self::$customerId) |
| 117 | + ->willThrowException(new NoSuchEntityException()); |
| 118 | + $this->loggerMock->expects($this->once()) |
| 119 | + ->method('error'); |
83 | 120 |
|
84 |
| - $this->plugin->beforeDispatch($this->abstractAction, $this->request); |
| 121 | + $this->plugin->beforeDispatch($this->abstractActionMock, $this->requestMock); |
85 | 122 | }
|
86 | 123 | }
|
0 commit comments