Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RenewCookieObserverTest.php
Go to the documentation of this file.
1 <?php
9 
10 class RenewCookieObserverTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $model;
16 
20  protected $helperMock;
21 
25  protected $sessionHelperMock;
26 
31 
36 
40  protected $observerMock;
41 
45  protected $eventManagerMock;
46 
50  protected $sessionMock;
51 
55  protected $requestMock;
56 
57  protected function setUp()
58  {
59  $this->requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
60  $this->helperMock = $this->createMock(\Magento\Persistent\Helper\Data::class);
61  $this->sessionHelperMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
62  $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
63  $this->sessionFactoryMock =
64  $this->createPartialMock(\Magento\Persistent\Model\SessionFactory::class, ['create']);
65  $this->observerMock = $this->createMock(\Magento\Framework\Event\Observer::class);
66  $eventMethods = ['getRequest', '__wakeUp'];
67  $this->eventManagerMock = $this->createPartialMock(\Magento\Framework\Event::class, $eventMethods);
68  $this->sessionMock = $this->createMock(\Magento\Persistent\Model\Session::class);
69  $this->model = new \Magento\Persistent\Observer\RenewCookieObserver(
70  $this->helperMock,
71  $this->sessionHelperMock,
72  $this->customerSessionMock,
73  $this->sessionFactoryMock
74  );
75  }
76 
77  public function testRenewCookie()
78  {
79  $this->helperMock
80  ->expects($this->once())
81  ->method('canProcess')
82  ->with($this->observerMock)
83  ->will($this->returnValue(true));
84  $this->helperMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true));
85  $this->sessionHelperMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
86 
87  $this->observerMock
88  ->expects($this->once())
89  ->method('getEvent')
90  ->will($this->returnValue($this->eventManagerMock));
91  $this->eventManagerMock
92  ->expects($this->once())
93  ->method('getRequest')
94  ->will($this->returnValue($this->requestMock));
95  $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
96  $this->requestMock
97  ->expects($this->once())
98  ->method('getFullActionName')
99  ->will($this->returnValue('customer_account_logout'));
100  $this->helperMock->expects($this->once())->method('getLifeTime')->will($this->returnValue(60));
101  $this->customerSessionMock
102  ->expects($this->once())->method('getCookiePath')->will($this->returnValue('path/cookie'));
103  $this->sessionFactoryMock
104  ->expects($this->once())
105  ->method('create')
106  ->will($this->returnValue($this->sessionMock));
107  $this->sessionMock->expects($this->once())->method('renewPersistentCookie')->with(60, 'path/cookie');
108  $this->model->execute($this->observerMock);
109  }
110 
112  {
113  $this->helperMock
114  ->expects($this->once())
115  ->method('canProcess')
116  ->with($this->observerMock)
117  ->will($this->returnValue(false));
118  $this->helperMock->expects($this->never())->method('isEnabled');
119 
120  $this->observerMock
121  ->expects($this->never())
122  ->method('getEvent');
123 
124  $this->model->execute($this->observerMock);
125  }
126 }