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