Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomerQuoteObserverTest.php
Go to the documentation of this file.
1 <?php
8 
9 class CustomerQuoteObserverTest extends \PHPUnit\Framework\TestCase
10 {
14  protected $customerQuote;
15 
19  protected $storeManagerMock;
20 
24  protected $configMock;
25 
30 
34  protected $observerMock;
35 
39  protected $eventMock;
40 
41  protected function setUp()
42  {
43  $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
44  ->disableOriginalConstructor()
45  ->getMock();
46  $this->configMock = $this->getMockBuilder(\Magento\Customer\Model\Config\Share::class)
47  ->disableOriginalConstructor()
48  ->getMock();
49  $this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
50  $this->observerMock = $this->getMockBuilder(\Magento\Framework\Event\Observer::class)
51  ->disableOriginalConstructor()
52  ->getMock();
53  $this->eventMock = $this->getMockBuilder(\Magento\Framework\Event::class)
54  ->disableOriginalConstructor()
55  ->setMethods(['getCustomerDataObject', 'getOrigCustomerDataObject'])
56  ->getMock();
57  $this->observerMock->expects($this->any())->method('getEvent')->will($this->returnValue($this->eventMock));
58  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
59  $this->customerQuote = $objectManager->getObject(
60  \Magento\Quote\Observer\Backend\CustomerQuoteObserver::class,
61  [
62  'storeManager' => $this->storeManagerMock,
63  'config' => $this->configMock,
64  'quoteRepository' => $this->quoteRepositoryMock,
65  ]
66  );
67  }
68 
70  {
71  $customerDataObjectMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
72  ->disableOriginalConstructor()
73  ->getMock();
74  $customerDataObjectMock->expects($this->any())
75  ->method('getGroupId')
76  ->will($this->returnValue(1));
77  $origCustomerDataObjectMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
78  ->disableOriginalConstructor()
79  ->getMock();
80  $origCustomerDataObjectMock->expects($this->any())
81  ->method('getGroupId')
82  ->will($this->returnValue(1));
83  $this->eventMock->expects($this->any())
84  ->method('getCustomerDataObject')
85  ->will($this->returnValue($customerDataObjectMock));
86  $this->eventMock->expects($this->any())
87  ->method('getOrigCustomerDataObject')
88  ->will($this->returnValue($origCustomerDataObjectMock));
89  $this->quoteRepositoryMock->expects($this->once())
90  ->method('getForCustomer')
91  ->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException());
92 
93  $this->customerQuote->execute($this->observerMock);
94  }
95 
101  public function testDispatch($isWebsiteScope, $websites)
102  {
103  $this->configMock->expects($this->once())
104  ->method('isWebsiteScope')
105  ->will($this->returnValue($isWebsiteScope));
106  $customerDataObjectMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
107  ->disableOriginalConstructor()
108  ->getMock();
109  $customerDataObjectMock->expects($this->any())
110  ->method('getGroupId')
111  ->will($this->returnValue(1));
112  $customerDataObjectMock->expects($this->any())
113  ->method('getWebsiteId')
114  ->will($this->returnValue(2));
115  if ($isWebsiteScope) {
116  $websites = $websites[0];
117  $this->storeManagerMock->expects($this->once())
118  ->method('getWebsite')
119  ->with(2)
120  ->will($this->returnValue($websites));
121  } else {
122  $this->storeManagerMock->expects($this->once())
123  ->method('getWebsites')
124  ->will($this->returnValue($websites));
125  }
126  $this->eventMock->expects($this->any())
127  ->method('getCustomerDataObject')
128  ->will($this->returnValue($customerDataObjectMock));
130  $quoteMock = $this->getMockBuilder(
131  \Magento\Quote\Model\Quote::class
132  )->setMethods(
133  [
134  'setWebsite',
135  'setCustomerGroupId',
136  'getCustomerGroupId',
137  'collectTotals',
138  '__wakeup',
139  ]
140  )->disableOriginalConstructor()->getMock();
141  $websiteCount = count($websites);
142  $this->quoteRepositoryMock->expects($this->once())
143  ->method('getForCustomer')
144  ->will($this->returnValue($quoteMock));
145  $quoteMock->expects($this->exactly($websiteCount))
146  ->method('setWebsite');
147  $quoteMock->expects($this->exactly($websiteCount))
148  ->method('setCustomerGroupId');
149  $quoteMock->expects($this->exactly($websiteCount))
150  ->method('collectTotals');
151  $this->quoteRepositoryMock->expects($this->exactly($websiteCount))
152  ->method('save')
153  ->with($quoteMock);
154  $quoteMock->expects($this->once())
155  ->method('getCustomerGroupId')
156  ->willReturn(2);
157  $this->customerQuote->execute($this->observerMock);
158  }
159 
163  public function dispatchDataProvider()
164  {
165  return [
166  [true, [['website1']]],
167  [true, [['website1'], ['website2']]],
168  [false, ['website1']],
169  [false, ['website1', 'website2']],
170  ];
171  }
172 }
$objectManager
Definition: bootstrap.php:17