|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Stockbase\Integration\Test\Unit\Model\Observer; |
|
5
|
|
|
|
|
6
|
|
|
use Magento\Framework\ObjectManagerInterface; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Stockbase\Integration\Model\Config\StockbaseConfiguration; |
|
9
|
|
|
use Stockbase\Integration\Model\Inventory\StockbaseStockManagement; |
|
10
|
|
|
use Stockbase\Integration\Model\Observer\OrderPaymentPayObserver; |
|
11
|
|
|
use Stockbase\Integration\Model\StockItemReserve; |
|
12
|
|
|
use Stockbase\Integration\StockbaseApi\Client\StockbaseClient; |
|
13
|
|
|
use Stockbase\Integration\StockbaseApi\Client\StockbaseClientFactory; |
|
14
|
|
|
use Stockbase\Integration\Model\OrderedItem as StockbaseOrderedItem; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class OrderPaymentPayObserverTest |
|
18
|
|
|
*/ |
|
19
|
|
|
class OrderPaymentPayObserverTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var StockbaseConfiguration|\PHPUnit_Framework_MockObject_MockObject */ |
|
22
|
|
|
private $stockbaseConfiguration; |
|
23
|
|
|
|
|
24
|
|
|
/** @var StockbaseStockManagement|\PHPUnit_Framework_MockObject_MockObject */ |
|
25
|
|
|
private $stockbaseStockManagement; |
|
26
|
|
|
|
|
27
|
|
|
/** @var StockbaseClientFactory|\PHPUnit_Framework_MockObject_MockObject */ |
|
28
|
|
|
private $stockbaseClientFactory; |
|
29
|
|
|
|
|
30
|
|
|
/** @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject */ |
|
31
|
|
|
private $objectManager; |
|
32
|
|
|
|
|
33
|
|
|
/** @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject */ |
|
34
|
|
|
private $observer; |
|
35
|
|
|
|
|
36
|
|
|
/** @var \Magento\Sales\Model\Order|\PHPUnit_Framework_MockObject_MockObject */ |
|
37
|
|
|
private $order; |
|
38
|
|
|
|
|
39
|
|
|
/** @var \Magento\Sales\Api\Data\InvoiceInterface|\PHPUnit_Framework_MockObject_MockObject */ |
|
40
|
|
|
private $invoice; |
|
41
|
|
|
|
|
42
|
|
|
/** @var StockbaseClient|\PHPUnit_Framework_MockObject_MockObject */ |
|
43
|
|
|
private $stockbaseClient; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
3 |
|
public function setUp() |
|
49
|
|
|
{ |
|
50
|
3 |
|
$this->stockbaseConfiguration = $this->createMock(StockbaseConfiguration::class); |
|
51
|
|
|
|
|
52
|
3 |
|
$this->stockbaseStockManagement = $this->createMock(StockbaseStockManagement::class); |
|
53
|
|
|
|
|
54
|
3 |
|
$this->stockbaseClientFactory = $this->createMock(StockbaseClientFactory::class); |
|
55
|
|
|
|
|
56
|
3 |
|
$this->stockbaseClient = $this->createMock(StockbaseClient::class); |
|
57
|
|
|
|
|
58
|
3 |
|
$this->objectManager = $this->createMock(ObjectManagerInterface::class); |
|
59
|
|
|
|
|
60
|
3 |
|
$this->order = $this->getMockBuilder(\Magento\Sales\Model\Order::class) |
|
61
|
3 |
|
->disableOriginalConstructor() |
|
62
|
3 |
|
->setMethods(['getAllItems', 'getId', 'save', 'addStatusHistoryComment']) |
|
63
|
3 |
|
->getMock(); |
|
64
|
|
|
|
|
65
|
3 |
|
$this->invoice = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class) |
|
66
|
3 |
|
->disableOriginalConstructor() |
|
67
|
3 |
|
->setMethods(['getOrder', 'getOrderId']) |
|
68
|
3 |
|
->getMock(); |
|
69
|
|
|
|
|
70
|
3 |
|
$this->observer = $this->getMockBuilder(\Magento\Framework\Event\Observer::class) |
|
71
|
3 |
|
->setMethods(['getData', 'getInvoice']) |
|
72
|
3 |
|
->getMock(); |
|
73
|
3 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* testDisabled |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function testDisabled() |
|
79
|
|
|
{ |
|
80
|
1 |
|
$this->stockbaseConfiguration->expects($this->once()) |
|
|
|
|
|
|
81
|
1 |
|
->method('isModuleEnabled') |
|
82
|
1 |
|
->willReturn(false); |
|
83
|
|
|
|
|
84
|
1 |
|
$this->observer->expects($this->never())->method('getData'); |
|
85
|
1 |
|
$this->observer->expects($this->never())->method('getInvoice'); |
|
86
|
|
|
|
|
87
|
1 |
|
$handler = $this->createHandler(); |
|
88
|
1 |
|
$this->assertEquals($handler, $handler->execute($this->observer)); |
|
89
|
1 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* testNotReserved |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function testNotReserved() |
|
95
|
|
|
{ |
|
96
|
1 |
|
$this->stockbaseConfiguration->expects($this->once()) |
|
|
|
|
|
|
97
|
1 |
|
->method('isModuleEnabled') |
|
98
|
1 |
|
->willReturn(true); |
|
99
|
|
|
|
|
100
|
1 |
|
$this->observer->method('getData')->with('invoice')->willReturn($this->invoice); |
|
101
|
1 |
|
$this->invoice->method('getOrder')->willReturn($this->order); |
|
102
|
|
|
|
|
103
|
1 |
|
$this->order->method('getAllItems')->willReturn($this->createOrderItems([101, 102])); |
|
104
|
|
|
|
|
105
|
1 |
|
$this->stockbaseStockManagement->expects($this->once()) |
|
|
|
|
|
|
106
|
1 |
|
->method('getReserveForQuoteItem') |
|
107
|
1 |
|
->with([101, 102]) |
|
108
|
1 |
|
->willReturn([]); |
|
109
|
|
|
|
|
110
|
1 |
|
$this->stockbaseClientFactory->expects($this->never())->method('create'); |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
1 |
|
$handler = $this->createHandler(); |
|
113
|
1 |
|
$this->assertEquals($handler, $handler->execute($this->observer)); |
|
114
|
1 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* testStockbaseOrderCreation |
|
118
|
|
|
*/ |
|
119
|
1 |
|
public function testStockbaseOrderCreation() |
|
120
|
|
|
{ |
|
121
|
1 |
|
$this->stockbaseConfiguration->expects($this->once()) |
|
|
|
|
|
|
122
|
1 |
|
->method('isModuleEnabled') |
|
123
|
1 |
|
->willReturn(true); |
|
124
|
|
|
|
|
125
|
1 |
|
$this->observer->method('getData')->with('invoice')->willReturn($this->invoice); |
|
126
|
1 |
|
$this->invoice->method('getOrder')->willReturn($this->order); |
|
127
|
|
|
|
|
128
|
1 |
|
$this->order->method('getAllItems')->willReturn($this->createOrderItems([101])); |
|
129
|
|
|
|
|
130
|
1 |
|
$reservedStockbaseItem = $this->createMock(StockItemReserve::class); |
|
131
|
1 |
|
$reservedStockbaseItem->method('getEan')->willReturn('12345'); |
|
132
|
1 |
|
$reservedStockbaseItem->method('getAmount')->willReturn(5.0); |
|
133
|
1 |
|
$reservedStockbaseItem->method('getOrderItemId')->willReturn(12345); |
|
134
|
1 |
|
$reservedStockbaseItem->method('getProductId')->willReturn(1234); |
|
135
|
|
|
|
|
136
|
1 |
|
$reservedStockbaseItems = [$reservedStockbaseItem]; |
|
137
|
|
|
|
|
138
|
1 |
|
$this->stockbaseStockManagement->expects($this->once()) |
|
|
|
|
|
|
139
|
1 |
|
->method('getReserveForQuoteItem') |
|
140
|
1 |
|
->with([101]) |
|
141
|
1 |
|
->willReturn($reservedStockbaseItems); |
|
142
|
|
|
|
|
143
|
1 |
|
$this->stockbaseClientFactory->method('create')->willReturn($this->stockbaseClient); |
|
144
|
|
|
|
|
145
|
1 |
|
$this->stockbaseClient->expects($this->once())->method('createOrder') |
|
|
|
|
|
|
146
|
1 |
|
->with($this->order, $reservedStockbaseItems); |
|
147
|
|
|
|
|
148
|
1 |
|
$stockbaseOrderedItem = $this->createMock(StockbaseOrderedItem::class); |
|
149
|
1 |
|
$stockbaseOrderedItem->expects($this->any())->method('setOrderItemId')->with(12345); |
|
150
|
1 |
|
$stockbaseOrderedItem->expects($this->any())->method('setEan')->with('12345'); |
|
151
|
1 |
|
$stockbaseOrderedItem->expects($this->any())->method('setAmount')->with(5.0); |
|
152
|
1 |
|
$stockbaseOrderedItem->expects($this->once())->method('save'); |
|
153
|
|
|
|
|
154
|
1 |
|
$this->objectManager->expects($this->once())->method('create')->with(StockbaseOrderedItem::class) |
|
155
|
1 |
|
->willReturn($stockbaseOrderedItem); |
|
156
|
|
|
|
|
157
|
1 |
|
$this->stockbaseStockManagement->expects($this->once())->method('updateStockAmount') |
|
158
|
1 |
|
->with('12345', 5.0, '-'); |
|
159
|
|
|
|
|
160
|
1 |
|
$this->stockbaseStockManagement->expects($this->once())->method('releaseReserve') |
|
161
|
1 |
|
->with($reservedStockbaseItem); |
|
162
|
|
|
|
|
163
|
1 |
|
$this->order->expects($this->exactly(2))->method('addStatusHistoryComment') |
|
164
|
1 |
|
->withConsecutive( |
|
165
|
1 |
|
[$this->matchesRegularExpression('/has been ordered from Stockbase/'), $this->anything()], |
|
166
|
1 |
|
[$this->matchesRegularExpression('/Local Stockbase stock index for item with/'), $this->anything()] |
|
167
|
|
|
); |
|
168
|
|
|
|
|
169
|
1 |
|
$handler = $this->createHandler(); |
|
170
|
1 |
|
$this->assertEquals($handler, $handler->execute($this->observer)); |
|
171
|
1 |
|
} |
|
172
|
|
|
|
|
173
|
3 |
|
protected function createHandler() |
|
174
|
|
|
{ |
|
175
|
3 |
|
return new OrderPaymentPayObserver( |
|
176
|
3 |
|
$this->stockbaseConfiguration, |
|
177
|
3 |
|
$this->stockbaseStockManagement, |
|
178
|
3 |
|
$this->stockbaseClientFactory, |
|
179
|
3 |
|
$this->objectManager |
|
180
|
|
|
); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
2 |
|
protected function createOrderItems(array $quoteItemIds) |
|
184
|
|
|
{ |
|
185
|
2 |
|
$items = []; |
|
186
|
2 |
|
foreach ($quoteItemIds as $quoteItemId) { |
|
187
|
2 |
|
$item = $this->createMock(\Magento\Sales\Api\Data\OrderItemInterface::class); |
|
188
|
2 |
|
$item->method('getQuoteItemId')->willReturn($quoteItemId); |
|
189
|
2 |
|
$items[] = $item; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
2 |
|
return $items; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: