Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdateCustomerCookiesObserverTest.php
Go to the documentation of this file.
1 <?php
8 
12 class UpdateCustomerCookiesObserverTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $model;
18 
22  protected $sessionHelperMock;
23 
28 
32  protected $eventManagerMock;
33 
37  protected $observerMock;
38 
42  protected $sessionMock;
43 
47  protected $customerMock;
48 
49  protected function setUp()
50  {
51  $eventMethods = ['getCustomerCookies', '__wakeUp'];
52  $sessionMethods = ['getId', 'getGroupId', 'getCustomerId', '__wakeUp'];
53  $this->sessionHelperMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
54  $this->customerRepository = $this->getMockForAbstractClass(
55  \Magento\Customer\Api\CustomerRepositoryInterface::class,
56  [],
57  '',
58  false
59  );
60  $this->observerMock = $this->createMock(\Magento\Framework\Event\Observer::class);
61  $this->eventManagerMock = $this->createPartialMock(\Magento\Framework\Event::class, $eventMethods);
62  $this->sessionMock = $this->createPartialMock(\Magento\Persistent\Model\Session::class, $sessionMethods);
63  $this->customerMock = $this->getMockForAbstractClass(
64  \Magento\Customer\Api\Data\CustomerInterface::class,
65  [],
66  '',
67  false
68  );
69  $this->model = new \Magento\Persistent\Observer\UpdateCustomerCookiesObserver(
70  $this->sessionHelperMock,
71  $this->customerRepository
72  );
73  }
74 
76  {
77  $this->sessionHelperMock->expects($this->once())->method('isPersistent')->will($this->returnValue(false));
78  $this->observerMock->expects($this->never())->method('getEvent');
79  $this->model->execute($this->observerMock);
80  }
81 
83  {
84  $customerId = 1;
85  $customerGroupId = 2;
86  $cookieMock = $this->createPartialMock(
87  \Magento\Framework\DataObject::class,
88  ['setCustomerId', 'setCustomerGroupId', '__wakeUp']
89  );
90  $this->sessionHelperMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
91  $this->observerMock
92  ->expects($this->once())
93  ->method('getEvent')
94  ->will($this->returnValue($this->eventManagerMock));
95  $this->eventManagerMock
96  ->expects($this->once())
97  ->method('getCustomerCookies')
98  ->will($this->returnValue($cookieMock));
99  $this->sessionHelperMock
100  ->expects($this->once())
101  ->method('getSession')
102  ->will($this->returnValue($this->sessionMock));
103  $this->sessionMock->expects($this->once())->method('getCustomerId')->will($this->returnValue($customerId));
104  $this->customerRepository
105  ->expects($this->once())
106  ->method('getById')
107  ->will($this->returnValue($this->customerMock));
108  $this->customerMock->expects($this->once())->method('getId')->will($this->returnValue($customerId));
109  $this->customerMock->expects($this->once())->method('getGroupId')->will($this->returnValue($customerGroupId));
110  $cookieMock->expects($this->once())->method('setCustomerId')->with($customerId)->will($this->returnSelf());
111  $cookieMock
112  ->expects($this->once())
113  ->method('setCustomerGroupId')
114  ->with($customerGroupId)
115  ->will($this->returnSelf());
116  $this->model->execute($this->observerMock);
117  }
118 }