27 $this->objectManagerHelper =
new ObjectManagerHelper($this);
28 $this->salesOrderBeforeSaveObserver = $this->objectManagerHelper->getObject(
29 \
Magento\Payment\Observer\SalesOrderBeforeSaveObserver::class,
33 $this->observerMock = $this->getMockBuilder(
34 \
Magento\Framework\Event\Observer::class
35 )->disableOriginalConstructor()->setMethods([])->getMock();
40 $this->_prepareEventMockWithMethods([
'getOrder']);
41 $neverInvokedMethods = [
'canUnhold',
'isCanceled',
'getState',
'hasForcedCanCreditMemo'];
42 $order = $this->_getPreparedOrderMethod(
46 $this->_prepareNeverInvokedOrderMethods(
$order, $neverInvokedMethods);
47 $this->eventMock->expects($this->once())->method(
'getOrder')->will(
51 $this->salesOrderBeforeSaveObserver->execute($this->observerMock);
56 $this->_prepareEventMockWithMethods([
'getOrder']);
57 $neverInvokedMethods = [
'isCanceled',
'getState',
'hasForcedCanCreditMemo'];
58 $order = $this->getMockBuilder(\
Magento\Sales\Model\Order::class)->disableOriginalConstructor()->setMethods(
59 array_merge([
'__wakeup',
'getPayment',
'canUnhold'], $neverInvokedMethods)
61 $paymentMock = $this->getMockBuilder(
62 \
Magento\Sales\Model\Order\Payment::class
63 )->disableOriginalConstructor()->setMethods([])->getMock();
64 $order->expects($this->once())->method(
'getPayment')->will($this->returnValue($paymentMock));
65 $methodInstance = $this->getMockBuilder(
66 \
Magento\Payment\Model\MethodInterface::class
67 )->getMockForAbstractClass();
68 $paymentMock->expects($this->once())->method(
'getMethodInstance')->will($this->returnValue($methodInstance));
69 $methodInstance->expects($this->once())->method(
'getCode')->will($this->returnValue(
'free'));
70 $this->_prepareNeverInvokedOrderMethods(
$order, $neverInvokedMethods);
71 $this->eventMock->expects($this->once())->method(
'getOrder')->will(
74 $order->expects($this->once())->method(
'canUnhold')->will($this->returnValue(
true));
75 $this->salesOrderBeforeSaveObserver->execute($this->observerMock);
81 $this->_prepareEventMockWithMethods([
'getOrder']);
82 $neverInvokedMethods = [
'getState',
'hasForcedCanCreditMemo'];
83 $order = $this->getMockBuilder(\
Magento\Sales\Model\Order::class)->disableOriginalConstructor()->setMethods(
84 array_merge([
'__wakeup',
'getPayment',
'canUnhold',
'isCanceled'], $neverInvokedMethods)
86 $paymentMock = $this->getMockBuilder(
87 \
Magento\Sales\Model\Order\Payment::class
88 )->disableOriginalConstructor()->setMethods([])->getMock();
89 $order->expects($this->once())->method(
'getPayment')->will($this->returnValue($paymentMock));
90 $methodInstance = $this->getMockBuilder(
91 \
Magento\Payment\Model\MethodInterface::class
92 )->getMockForAbstractClass();
93 $paymentMock->expects($this->once())->method(
'getMethodInstance')->will($this->returnValue($methodInstance));
94 $methodInstance->expects($this->once())->method(
'getCode')->will($this->returnValue(
'free'));
95 $this->_prepareNeverInvokedOrderMethods(
$order, $neverInvokedMethods);
96 $this->eventMock->expects($this->once())->method(
'getOrder')->will(
99 $order->expects($this->once())->method(
'canUnhold')->will($this->returnValue(
false));
101 $order->expects($this->once())->method(
'isCanceled')->will($this->returnValue(
true));
103 $this->salesOrderBeforeSaveObserver->execute($this->observerMock);
109 $this->_prepareEventMockWithMethods([
'getOrder']);
110 $neverInvokedMethods = [
'hasForcedCanCreditMemo'];
111 $order = $this->getMockBuilder(\
Magento\Sales\Model\Order::class)->disableOriginalConstructor()->setMethods(
112 array_merge([
'__wakeup',
'getPayment',
'isCanceled',
'canUnhold',
'getState'], $neverInvokedMethods)
114 $paymentMock = $this->getMockBuilder(
115 \
Magento\Sales\Model\Order\Payment::class
116 )->disableOriginalConstructor()->setMethods([])->getMock();
117 $order->expects($this->once())->method(
'getPayment')->will($this->returnValue($paymentMock));
118 $methodInstance = $this->getMockBuilder(
119 \
Magento\Payment\Model\MethodInterface::class
120 )->getMockForAbstractClass();
121 $paymentMock->expects($this->once())->method(
'getMethodInstance')->will($this->returnValue($methodInstance));
122 $methodInstance->expects($this->once())->method(
'getCode')->will($this->returnValue(
'free'));
123 $this->_prepareNeverInvokedOrderMethods(
$order, $neverInvokedMethods);
124 $this->eventMock->expects($this->once())->method(
'getOrder')->will(
125 $this->returnValue(
$order)
127 $order->expects($this->once())->method(
'canUnhold')->will($this->returnValue(
false));
129 $order->expects($this->once())->method(
'isCanceled')->will($this->returnValue(
false));
130 $order->expects($this->once())->method(
'getState')->will(
131 $this->returnValue(\
Magento\Sales\Model\Order::STATE_CLOSED)
133 $this->salesOrderBeforeSaveObserver->execute($this->observerMock);
139 $this->_prepareEventMockWithMethods([
'getOrder']);
140 $order = $this->_getPreparedOrderMethod(
142 [
'canUnhold',
'isCanceled',
'getState',
'setForcedCanCreditmemo',
'hasForcedCanCreditmemo']
144 $this->eventMock->expects($this->once())->method(
'getOrder')->will(
145 $this->returnValue(
$order)
147 $order->expects($this->once())->method(
'canUnhold')->will($this->returnValue(
false));
149 $order->expects($this->once())->method(
'isCanceled')->will($this->returnValue(
false));
150 $order->expects($this->once())->method(
'getState')->will(
151 $this->returnValue(
'not_closed_state')
153 $order->expects($this->once())->method(
'hasForcedCanCreditmemo')->will($this->returnValue(
false));
154 $order->expects($this->once())->method(
'setForcedCanCreditmemo')->will($this->returnValue(
true));
156 $this->salesOrderBeforeSaveObserver->execute($this->observerMock);
164 private function _prepareEventMockWithMethods($methodsList)
166 $this->eventMock = $this->getMockBuilder(
167 \
Magento\Framework\Event::class
168 )->disableOriginalConstructor()->setMethods($methodsList)->getMock();
169 $this->observerMock->expects($this->any())->method(
'getEvent')->will($this->returnValue($this->eventMock));
179 private function _getPreparedOrderMethod(
$methodCode, $orderMethods = [])
181 $order = $this->getMockBuilder(\
Magento\Sales\Model\Order::class)->disableOriginalConstructor()->setMethods(
182 array_merge([
'__wakeup',
'getPayment'], $orderMethods)
184 $paymentMock = $this->getMockBuilder(
185 \
Magento\Sales\Model\Order\Payment::class
186 )->disableOriginalConstructor()->setMethods([])->getMock();
187 $order->expects($this->once())->method(
'getPayment')->will($this->returnValue($paymentMock));
188 $methodInstance = $this->getMockBuilder(
189 \
Magento\Payment\Model\MethodInterface::class
190 )->getMockForAbstractClass();
191 $paymentMock->expects($this->once())->method(
'getMethodInstance')->will($this->returnValue($methodInstance));
192 $methodInstance->expects($this->once())->method(
'getCode')->will($this->returnValue(
$methodCode));
202 private function _prepareNeverInvokedOrderMethods(\PHPUnit_Framework_MockObject_MockObject
$order,
$methods = [])
testSalesOrderBeforeSaveCantUnhold()
$salesOrderBeforeSaveObserver
testSalesOrderBeforeSaveSetForced()
testSalesOrderBeforeSaveIsClosed()
testSalesOrderBeforeSaveIsCanceled()
testSalesOrderBeforeSaveMethodNotFree()