Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OrderPlaceTest.php
Go to the documentation of this file.
1 <?php
7 
17 
25 class OrderPlaceTest extends \PHPUnit\Framework\TestCase
26 {
28 
32  private $cartManagementMock;
33 
37  private $agreementsValidatorMock;
38 
42  private $customerSessionMock;
43 
47  private $checkoutHelperMock;
48 
52  private $billingAddressMock;
53 
57  private $orderPlace;
58 
59  protected function setUp()
60  {
61  $this->cartManagementMock = $this->getMockBuilder(CartManagementInterface::class)
62  ->getMockForAbstractClass();
63  $this->agreementsValidatorMock = $this->getMockBuilder(AgreementsValidatorInterface::class)
64  ->getMockForAbstractClass();
65  $this->customerSessionMock = $this->getMockBuilder(Session::class)
66  ->disableOriginalConstructor()
67  ->getMock();
68  $this->checkoutHelperMock = $this->getMockBuilder(Data::class)
69  ->disableOriginalConstructor()
70  ->getMock();
71 
72  $this->orderPlace = new OrderPlace(
73  $this->cartManagementMock,
74  $this->agreementsValidatorMock,
75  $this->customerSessionMock,
76  $this->checkoutHelperMock
77  );
78  }
79 
80  public function testExecuteGuest()
81  {
82  $agreement = ['test', 'test'];
83  $quoteMock = $this->getQuoteMock();
84 
85  $this->agreementsValidatorMock->expects(self::once())
86  ->method('isValid')
87  ->willReturn(true);
88 
89  $this->getCheckoutMethodStep($quoteMock);
90  $this->prepareGuestQuoteStep($quoteMock);
91  $this->disabledQuoteAddressValidationStep($quoteMock);
92 
93  $quoteMock->expects(self::once())
94  ->method('collectTotals');
95 
96  $quoteMock->expects(self::once())
97  ->method('getId')
98  ->willReturn(10);
99 
100  $this->cartManagementMock->expects(self::once())
101  ->method('placeOrder')
102  ->with(10);
103 
104  $this->orderPlace->execute($quoteMock, $agreement);
105  }
106 
110  private function disabledQuoteAddressValidationStep(\PHPUnit_Framework_MockObject_MockObject $quoteMock)
111  {
112  $billingAddressMock = $this->getBillingAddressMock($quoteMock);
113  $shippingAddressMock = $this->getMockBuilder(Address::class)
114  ->setMethods(['setShouldIgnoreValidation'])
115  ->disableOriginalConstructor()
116  ->getMock();
117 
118  $quoteMock->expects(self::once())
119  ->method('getShippingAddress')
120  ->willReturn($shippingAddressMock);
121 
122  $billingAddressMock->expects(self::once())
123  ->method('setShouldIgnoreValidation')
124  ->with(true)
125  ->willReturnSelf();
126 
127  $quoteMock->expects(self::once())
128  ->method('getIsVirtual')
129  ->willReturn(false);
130 
131  $shippingAddressMock->expects(self::once())
132  ->method('setShouldIgnoreValidation')
133  ->with(true)
134  ->willReturnSelf();
135 
136  $billingAddressMock->expects(self::any())
137  ->method('getEmail')
138  ->willReturn(self::TEST_EMAIL);
139 
140  $billingAddressMock->expects(self::never())
141  ->method('setSameAsBilling');
142  }
143 
147  private function getCheckoutMethodStep(\PHPUnit_Framework_MockObject_MockObject $quoteMock)
148  {
149  $this->customerSessionMock->expects(self::once())
150  ->method('isLoggedIn')
151  ->willReturn(false);
152 
153  $quoteMock->expects(self::at(1))
154  ->method('getCheckoutMethod')
155  ->willReturn(null);
156 
157  $this->checkoutHelperMock->expects(self::once())
158  ->method('isAllowedGuestCheckout')
159  ->with($quoteMock)
160  ->willReturn(true);
161 
162  $quoteMock->expects(self::once())
163  ->method('setCheckoutMethod')
164  ->with(Onepage::METHOD_GUEST);
165 
166  $quoteMock->expects(self::at(2))
167  ->method('getCheckoutMethod')
168  ->willReturn(Onepage::METHOD_GUEST);
169  }
170 
174  private function prepareGuestQuoteStep(\PHPUnit_Framework_MockObject_MockObject $quoteMock)
175  {
176  $billingAddressMock = $this->getBillingAddressMock($quoteMock);
177 
178  $quoteMock->expects(self::once())
179  ->method('setCustomerId')
180  ->with(null)
181  ->willReturnSelf();
182 
183  $billingAddressMock->expects(self::at(0))
184  ->method('getEmail')
185  ->willReturn(self::TEST_EMAIL);
186 
187  $quoteMock->expects(self::once())
188  ->method('setCustomerEmail')
189  ->with(self::TEST_EMAIL)
190  ->willReturnSelf();
191 
192  $quoteMock->expects(self::once())
193  ->method('setCustomerIsGuest')
194  ->with(true)
195  ->willReturnSelf();
196 
197  $quoteMock->expects(self::once())
198  ->method('setCustomerGroupId')
200  ->willReturnSelf();
201  }
202 
207  private function getBillingAddressMock(\PHPUnit_Framework_MockObject_MockObject $quoteMock)
208  {
209  if (!isset($this->billingAddressMock)) {
210  $this->billingAddressMock = $this->getMockBuilder(Address::class)
211  ->setMethods(['setShouldIgnoreValidation', 'getEmail', 'setSameAsBilling'])
212  ->disableOriginalConstructor()
213  ->getMock();
214  }
215 
216  $quoteMock->expects(self::any())
217  ->method('getBillingAddress')
218  ->willReturn($this->billingAddressMock);
219 
220  return $this->billingAddressMock;
221  }
222 
226  private function getQuoteMock()
227  {
228  return $this->getMockBuilder(Quote::class)
229  ->setMethods(
230  [
231  'setCustomerId',
232  'setCustomerEmail',
233  'setCustomerIsGuest',
234  'setCustomerGroupId',
235  'getCheckoutMethod',
236  'setCheckoutMethod',
237  'collectTotals',
238  'getId',
239  'getBillingAddress',
240  'getShippingAddress',
241  'getIsVirtual'
242  ]
243  )->disableOriginalConstructor()
244  ->getMock();
245  }
246 }