|
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 $session; |
15 | 24 |
|
16 | 25 | /** @var \Magento\Customer\Model\Customer\NotificationStorage|\PHPUnit_Framework_MockObject_MockObject */
|
17 |
| - protected $notificationStorage; |
| 26 | + private $notificationStorage; |
18 | 27 |
|
19 |
| - /** @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */ |
20 |
| - protected $customerRepository; |
| 28 | + /** @var CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 29 | + private $customerRepository; |
21 | 30 |
|
22 |
| - /** @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject */ |
23 |
| - protected $appState; |
| 31 | + /** @var State|\PHPUnit_Framework_MockObject_MockObject */ |
| 32 | + private $appState; |
24 | 33 |
|
25 |
| - /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */ |
26 |
| - protected $request; |
| 34 | + /** @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 35 | + private $request; |
27 | 36 |
|
28 |
| - /** @var \Magento\Backend\App\AbstractAction|\PHPUnit_Framework_MockObject_MockObject */ |
29 |
| - protected $abstractAction; |
| 37 | + /** @var AbstractAction|\PHPUnit_Framework_MockObject_MockObject */ |
| 38 | + private $abstractAction; |
30 | 39 |
|
31 | 40 | /** @var CustomerNotification */
|
32 |
| - protected $plugin; |
| 41 | + private $plugin; |
| 42 | + |
| 43 | + /** @var int */ |
| 44 | + private static $customerId = 1; |
33 | 45 |
|
34 | 46 | protected function setUp()
|
35 | 47 | {
|
36 |
| - $this->session = $this->getMockBuilder(\Magento\Customer\Model\Session::class) |
| 48 | + $this->session = $this->getMockBuilder(Session::class) |
37 | 49 | ->disableOriginalConstructor()
|
38 | 50 | ->getMock();
|
39 |
| - $this->notificationStorage = $this->getMockBuilder(\Magento\Customer\Model\Customer\NotificationStorage::class) |
| 51 | + $this->notificationStorage = $this->getMockBuilder(NotificationStorage::class) |
40 | 52 | ->disableOriginalConstructor()
|
41 | 53 | ->getMock();
|
42 |
| - $this->customerRepository = $this->getMockBuilder(\Magento\Customer\Api\CustomerRepositoryInterface::class) |
43 |
| - ->getMockForAbstractClass(); |
44 |
| - $this->abstractAction = $this->getMockBuilder(\Magento\Backend\App\AbstractAction::class) |
| 54 | + $this->customerRepository = $this->getMockForAbstractClass(CustomerRepositoryInterface::class); |
| 55 | + $this->abstractAction = $this->getMockBuilder(AbstractAction::class) |
45 | 56 | ->disableOriginalConstructor()
|
46 | 57 | ->getMockForAbstractClass();
|
47 |
| - $this->request = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class) |
| 58 | + $this->request = $this->getMockBuilder(RequestInterface::class) |
48 | 59 | ->setMethods(['isPost'])
|
49 | 60 | ->getMockForAbstractClass();
|
50 |
| - $this->appState = $this->getMockBuilder(\Magento\Framework\App\State::class) |
| 61 | + $this->appState = $this->getMockBuilder(State::class) |
51 | 62 | ->disableOriginalConstructor()->getMock();
|
| 63 | + $this->logger = $this->getMockForAbstractClass(LoggerInterface::class); |
| 64 | + |
| 65 | + $this->appState->method('getAreaCode')->willReturn(Area::AREA_FRONTEND); |
| 66 | + $this->request->method('isPost')->willReturn(true); |
| 67 | + $this->session->method('getCustomerId')->willReturn(self::$customerId); |
| 68 | + $this->notificationStorage->expects($this->any()) |
| 69 | + ->method('isExists') |
| 70 | + ->with(NotificationStorage::UPDATE_CUSTOMER_SESSION, self::$customerId) |
| 71 | + ->willReturn(true); |
| 72 | + |
52 | 73 | $this->plugin = new CustomerNotification(
|
53 | 74 | $this->session,
|
54 | 75 | $this->notificationStorage,
|
55 | 76 | $this->appState,
|
56 |
| - $this->customerRepository |
| 77 | + $this->customerRepository, |
| 78 | + $this->logger |
57 | 79 | );
|
58 | 80 | }
|
59 | 81 |
|
60 | 82 | public function testBeforeDispatch()
|
61 | 83 | {
|
62 |
| - $customerId = 1; |
63 | 84 | $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()) |
| 85 | + |
| 86 | + $customerMock = $this->getMockForAbstractClass(CustomerInterface::class); |
| 87 | + $customerMock->method('getGroupId')->willReturn($customerGroupId); |
| 88 | + $this->customerRepository->expects($this->once()) |
72 | 89 | ->method('getById')
|
73 |
| - ->with($customerId) |
| 90 | + ->with(self::$customerId) |
74 | 91 | ->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); |
| 92 | + $this->session->expects($this->once())->method('setCustomerData')->with($customerMock); |
| 93 | + $this->session->expects($this->once())->method('setCustomerGroupId')->with($customerGroupId); |
78 | 94 | $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); |
| 95 | + $this->notificationStorage->expects($this->once()) |
| 96 | + ->method('remove') |
| 97 | + ->with(NotificationStorage::UPDATE_CUSTOMER_SESSION, self::$customerId); |
| 98 | + |
| 99 | + $this->plugin->beforeDispatch($this->abstractAction, $this->request); |
| 100 | + } |
| 101 | + |
| 102 | + public function testBeforeDispatchWithNoCustomerFound() |
| 103 | + { |
| 104 | + $this->customerRepository->method('getById') |
| 105 | + ->with(self::$customerId) |
| 106 | + ->willThrowException(new NoSuchEntityException()); |
| 107 | + $this->logger->expects($this->once()) |
| 108 | + ->method('error'); |
83 | 109 |
|
84 | 110 | $this->plugin->beforeDispatch($this->abstractAction, $this->request);
|
85 | 111 | }
|
|
0 commit comments