Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CheckExpirePersistentQuoteObserverTest.php
Go to the documentation of this file.
1 <?php
9 
10 class CheckExpirePersistentQuoteObserverTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $model;
16 
20  protected $sessionMock;
21 
26 
31 
36 
40  protected $observerMock;
41 
45  protected $quoteManagerMock;
46 
50  protected $eventManagerMock;
51 
55  private $requestMock;
56 
60  protected function setUp()
61  {
62  $this->sessionMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
63  $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
64  $this->persistentHelperMock = $this->createMock(\Magento\Persistent\Helper\Data::class);
65  $this->observerMock = $this->createPartialMock(
66  \Magento\Framework\Event\Observer::class,
67  ['getControllerAction','__wakeUp']
68  );
69  $this->quoteManagerMock = $this->createMock(\Magento\Persistent\Model\QuoteManager::class);
70  $this->eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
71  $this->checkoutSessionMock = $this->createMock(\Magento\Checkout\Model\Session::class);
72  $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
73  ->disableOriginalConstructor()
74  ->setMethods(['getRequestUri', 'getServer'])
75  ->getMockForAbstractClass();
76 
77  $this->model = new \Magento\Persistent\Observer\CheckExpirePersistentQuoteObserver(
78  $this->sessionMock,
79  $this->persistentHelperMock,
80  $this->quoteManagerMock,
81  $this->eventManagerMock,
82  $this->customerSessionMock,
83  $this->checkoutSessionMock,
84  $this->requestMock
85  );
86  }
87 
89  {
90  $this->persistentHelperMock
91  ->expects($this->once())
92  ->method('canProcess')
93  ->with($this->observerMock)
94  ->willReturn(false);
95  $this->persistentHelperMock->expects($this->never())->method('isEnabled');
96  $this->model->execute($this->observerMock);
97  }
98 
100  {
101  $this->persistentHelperMock
102  ->expects($this->once())
103  ->method('canProcess')
104  ->with($this->observerMock)
105  ->willReturn(true);
106  $this->persistentHelperMock->expects($this->once())->method('isEnabled')->willReturn(false);
107  $this->eventManagerMock->expects($this->never())->method('dispatch');
108  $this->model->execute($this->observerMock);
109  }
110 
123  string $refererUri,
124  string $requestUri,
125  \PHPUnit\Framework\MockObject\Matcher\InvokedCount $expireCounter,
126  \PHPUnit\Framework\MockObject\Matcher\InvokedCount $dispatchCounter,
127  \PHPUnit\Framework\MockObject\Matcher\InvokedCount $setCustomerIdCounter
128  ): void {
129  $this->persistentHelperMock
130  ->expects($this->once())
131  ->method('canProcess')
132  ->with($this->observerMock)
133  ->willReturn(true);
134  $this->persistentHelperMock->expects($this->once())->method('isEnabled')->willReturn(true);
135  $this->sessionMock->expects($this->once())->method('isPersistent')->willReturn(false);
136  $this->customerSessionMock
137  ->expects($this->atLeastOnce())
138  ->method('isLoggedIn')
139  ->willReturn(false);
140  $this->checkoutSessionMock
141  ->expects($this->atLeastOnce())
142  ->method('getQuoteId')
143  ->willReturn(10);
144  $this->eventManagerMock->expects($dispatchCounter)->method('dispatch');
145  $this->quoteManagerMock->expects($expireCounter)->method('expire');
146  $this->customerSessionMock
147  ->expects($setCustomerIdCounter)
148  ->method('setCustomerId')
149  ->with(null)
150  ->willReturnSelf();
151  $this->requestMock->expects($this->atLeastOnce())->method('getRequestUri')->willReturn($refererUri);
152  $this->requestMock
153  ->expects($this->atLeastOnce())
154  ->method('getServer')
155  ->with('HTTP_REFERER')
156  ->willReturn($requestUri);
157  $this->model->execute($this->observerMock);
158  }
159 
165  public function requestDataProvider()
166  {
167  return [
168  [
169  'refererUri' => 'checkout',
170  'requestUri' => 'index',
171  'expireCounter' => $this->never(),
172  'dispatchCounter' => $this->never(),
173  'setCustomerIdCounter' => $this->never(),
174  ],
175  [
176  'refererUri' => 'checkout',
177  'requestUri' => 'checkout',
178  'expireCounter' => $this->never(),
179  'dispatchCounter' => $this->never(),
180  'setCustomerIdCounter' => $this->never(),
181  ],
182  [
183  'refererUri' => 'index',
184  'requestUri' => 'checkout',
185  'expireCounter' => $this->never(),
186  'dispatchCounter' => $this->never(),
187  'setCustomerIdCounter' => $this->never(),
188  ],
189  [
190  'refererUri' => 'index',
191  'requestUri' => 'index',
192  'expireCounter' => $this->once(),
193  'dispatchCounter' => $this->once(),
194  'setCustomerIdCounter' => $this->once(),
195  ],
196  ];
197  }
198 }
testExecuteWhenPersistentIsEnabled(string $refererUri, string $requestUri, \PHPUnit\Framework\MockObject\Matcher\InvokedCount $expireCounter, \PHPUnit\Framework\MockObject\Matcher\InvokedCount $dispatchCounter, \PHPUnit\Framework\MockObject\Matcher\InvokedCount $setCustomerIdCounter)