|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Customer\Test\Unit\Controller\Account; |
| 7 | + |
| 8 | +use Magento\Customer\Controller\Account\UpdateSession; |
| 9 | +use Magento\Customer\Model\Customer\NotificationStorage; |
| 10 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 11 | +use Magento\Framework\App\Action\Context; |
| 12 | +use Magento\Customer\Model\Session; |
| 13 | +use Magento\Framework\Json\Helper\Data; |
| 14 | + |
| 15 | +class UpdateSessionTest extends \PHPUnit_Framework_TestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var UpdateSession |
| 19 | + */ |
| 20 | + protected $model; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var Context|\PHPUnit_Framework_MockObject_MockObject |
| 24 | + */ |
| 25 | + protected $context; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var NotificationStorage|\PHPUnit_Framework_MockObject_MockObject |
| 29 | + */ |
| 30 | + protected $notificationStorage; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject |
| 34 | + */ |
| 35 | + protected $customerRepository; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var Session|\PHPUnit_Framework_MockObject_MockObject |
| 39 | + */ |
| 40 | + protected $customerSession; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var Data|\PHPUnit_Framework_MockObject_MockObject |
| 44 | + */ |
| 45 | + protected $jsonHelper; |
| 46 | + |
| 47 | + |
| 48 | + protected function setUp() |
| 49 | + { |
| 50 | + $this->context = $this->getMockBuilder('Magento\Framework\App\Action\Context') |
| 51 | + ->disableOriginalConstructor() |
| 52 | + ->getMock(); |
| 53 | + |
| 54 | + $this->notificationStorage = $this->getMockBuilder('Magento\Customer\Model\Customer\NotificationStorage') |
| 55 | + ->disableOriginalConstructor() |
| 56 | + ->getMock(); |
| 57 | + |
| 58 | + $this->customerSession = $this->getMockBuilder('Magento\Customer\Model\Session') |
| 59 | + ->disableOriginalConstructor() |
| 60 | + ->getMock(); |
| 61 | + |
| 62 | + $this->customerRepository = $this->getMockBuilder('Magento\Customer\Api\CustomerRepositoryInterface') |
| 63 | + ->getMockForAbstractClass(); |
| 64 | + |
| 65 | + $this->jsonHelper = $this->getMockBuilder('Magento\Framework\Json\Helper\Data') |
| 66 | + ->disableOriginalConstructor() |
| 67 | + ->getMock(); |
| 68 | + |
| 69 | + $this->model = new UpdateSession( |
| 70 | + $this->context, |
| 71 | + $this->notificationStorage, |
| 72 | + $this->customerRepository, |
| 73 | + $this->customerSession, |
| 74 | + $this->jsonHelper |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + public function testExecute() |
| 79 | + { |
| 80 | + $customerData = ['customer_id' => 1]; |
| 81 | + $customerGroup = 1; |
| 82 | + |
| 83 | + $requestMock = $this->getMockBuilder('Magento\Framework\App\RequestInterface') |
| 84 | + ->setMethods(['getContent']) |
| 85 | + ->getMockForAbstractClass(); |
| 86 | + $requestMock->expects($this->any())->method('getContent')->willReturn(json_encode($customerData)); |
| 87 | + |
| 88 | + $reflection = new \ReflectionClass(get_class($this->model)); |
| 89 | + $reflectionProperty = $reflection->getProperty('_request'); |
| 90 | + $reflectionProperty->setAccessible(true); |
| 91 | + $reflectionProperty->setValue($this->model, $requestMock); |
| 92 | + |
| 93 | + $this->jsonHelper->expects($this->any())->method('jsonDecode')->willReturn($customerData); |
| 94 | + |
| 95 | + $customer = $this->getMockBuilder('Magento\Customer\Api\Data\CustomerInterface')->getMockForAbstractClass(); |
| 96 | + $customer->expects($this->once())->method('getId')->willReturn($customerData['customer_id']); |
| 97 | + $customer->expects($this->once())->method('getGroupId')->willReturn($customerGroup); |
| 98 | + |
| 99 | + $this->customerRepository->expects($this->once()) |
| 100 | + ->method('getById') |
| 101 | + ->with($customerData['customer_id']) |
| 102 | + ->willReturn($customer); |
| 103 | + |
| 104 | + $this->customerSession->expects($this->once())->method('setCustomerData')->with($customer); |
| 105 | + $this->customerSession->expects($this->once())->method('setCustomerGroupId')->with($customerGroup); |
| 106 | + $this->customerSession->expects($this->once())->method('regenerateId'); |
| 107 | + |
| 108 | + $this->notificationStorage->expects($this->once()) |
| 109 | + ->method('isExists') |
| 110 | + ->with(NotificationStorage::UPDATE_CUSTOMER_SESSION, $customerData['customer_id']) |
| 111 | + ->willReturn(true); |
| 112 | + $this->notificationStorage->expects($this->once()) |
| 113 | + ->method('remove') |
| 114 | + ->with(NotificationStorage::UPDATE_CUSTOMER_SESSION, $customerData['customer_id']); |
| 115 | + |
| 116 | + $this->model->execute(); |
| 117 | + } |
| 118 | +} |
0 commit comments