Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MakePersistentQuoteGuestObserverTest.php
Go to the documentation of this file.
1 <?php
9 
10 class MakePersistentQuoteGuestObserverTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $model;
16 
21 
25  protected $sessionHelperMock;
26 
30  protected $helperMock;
31 
35  protected $observerMock;
36 
40  protected $quoteManagerMock;
41 
45  protected $eventManagerMock;
46 
50  protected $actionMock;
51 
52  protected function setUp()
53  {
54  $this->actionMock = $this->createMock(\Magento\Persistent\Controller\Index::class);
55  $this->observerMock = $this->createMock(\Magento\Framework\Event\Observer::class);
56  $this->sessionHelperMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
57  $this->helperMock = $this->createMock(\Magento\Persistent\Helper\Data::class);
58  $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
59  $this->quoteManagerMock = $this->createMock(\Magento\Persistent\Model\QuoteManager::class);
60  $this->eventManagerMock =
61  $this->createPartialMock(\Magento\Framework\Event::class, ['getControllerAction', '__wakeUp']);
62  $this->observerMock
63  ->expects($this->once())
64  ->method('getEvent')
65  ->will($this->returnValue($this->eventManagerMock));
66  $this->model = new \Magento\Persistent\Observer\MakePersistentQuoteGuestObserver(
67  $this->sessionHelperMock,
68  $this->helperMock,
69  $this->customerSessionMock,
70  $this->quoteManagerMock
71  );
72  }
73 
74  public function testExecute()
75  {
76  $this->eventManagerMock
77  ->expects($this->once())
78  ->method('getControllerAction')
79  ->will($this->returnValue($this->actionMock));
80  $this->sessionHelperMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
81  $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
82  $this->helperMock->expects($this->never())->method('isShoppingCartPersist');
83  $this->quoteManagerMock->expects($this->once())->method('setGuest')->with(true);
84  $this->model->execute($this->observerMock);
85  }
86 
88  {
89  $this->eventManagerMock
90  ->expects($this->once())
91  ->method('getControllerAction')
92  ->will($this->returnValue($this->actionMock));
93  $this->sessionHelperMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
94  $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(true));
95  $this->helperMock->expects($this->once())->method('isShoppingCartPersist')->will($this->returnValue(true));
96  $this->quoteManagerMock->expects($this->once())->method('setGuest')->with(true);
97  $this->model->execute($this->observerMock);
98  }
99 
101  {
102  $this->eventManagerMock
103  ->expects($this->once())
104  ->method('getControllerAction')
105  ->will($this->returnValue($this->actionMock));
106  $this->sessionHelperMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
107  $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(true));
108  $this->helperMock->expects($this->once())->method('isShoppingCartPersist')->will($this->returnValue(false));
109  $this->quoteManagerMock->expects($this->never())->method('setGuest');
110  $this->model->execute($this->observerMock);
111  }
112 }