ShipmentWriterTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 307
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 307
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 26 1
A testExceptionIsThrownIfNoOrderId() 0 5 1
A testExceptionIsThrownIfOrderCannotBeFound() 0 18 1
B testShipmentCanBeCreatedWithTracking() 0 96 1
B testShipmentCanBeCreatedWithoutTracking() 0 91 1
A testMagentoSaveExceptionIsThrownIfSaveFails() 0 56 1
1
<?php
2
3
namespace Jh\DataImportMagentoTest\Writer;
4
5
use Jh\DataImportMagento\Writer\ShipmentWriter;
6
use Jh\DataImportMagento\Writer\ProductWriter;
7
8
/**
9
 * Class ShipmentWriterTest
10
 * @package Jh\DataImportMagentoTest\Writer
11
 * @author Aydin Hassan <[email protected]>
12
 */
13
class ShipmentWriterTest extends \PHPUnit_Framework_TestCase
14
{
15
16
    protected $orderModel;
17
    protected $transactionResourceModel;
18
    protected $shipmentWriter;
19
    protected $trackingModel;
20
    protected $options;
21
22
    public function setUp()
23
    {
24
        $this->orderModel = $this->getMockBuilder('Mage_Sales_Model_Order')
25
            ->disableOriginalConstructor()
26
            ->setMethods([])
27
            ->getMock();
28
29
        $this->transactionResourceModel = $this->getMockBuilder('Mage_Core_Model_Resource_Transaction')
30
            ->setMethods([])
31
            ->getMock();
32
33
        $this->trackingModel = $this->getMockBuilder('Mage_Sales_Model_Order_Shipment_Track')
34
            ->setMethods([])
35
            ->getMock();
36
37
        $this->options = [
38
            'send_shipment_email' => 1
39
        ];
40
41
        $this->shipmentWriter = new ShipmentWriter(
42
            $this->orderModel,
43
            $this->transactionResourceModel,
44
            $this->trackingModel,
45
            $this->options
46
        );
47
    }
48
49
    public function testExceptionIsThrownIfNoOrderId()
50
    {
51
        $this->setExpectedException('Ddeboer\DataImport\Exception\WriterException', 'order_id must be set');
52
        $this->shipmentWriter->writeItem([]);
53
    }
54
55
    public function testExceptionIsThrownIfOrderCannotBeFound()
56
    {
57
        $this->orderModel
58
            ->expects($this->once())
59
            ->method('loadByIncrementId')
60
            ->with(5);
61
62
        $this->orderModel
63
            ->expects($this->once())
64
            ->method('getId')
65
            ->will($this->returnValue(null));
66
67
        $this->setExpectedException(
68
            'Ddeboer\DataImport\Exception\WriterException',
69
            'Order with ID: "5" cannot be found'
70
        );
71
        $this->shipmentWriter->writeItem(['order_id' => 5]);
72
    }
73
74
    public function testShipmentCanBeCreatedWithTracking()
75
    {
76
        $this->orderModel
77
            ->expects($this->once())
78
            ->method('loadByIncrementId')
79
            ->with(5);
80
81
        $this->orderModel
82
            ->expects($this->any())
83
            ->method('getId')
84
            ->will($this->returnValue(5));
85
86
        $shipment = $this->getMock('Mage_Sales_Model_Order_Shipment');
87
88
        $this->orderModel
89
            ->expects($this->once())
90
            ->method('prepareShipment')
91
            ->will($this->returnValue($shipment));
92
93
        $shipment
94
            ->expects($this->once())
95
            ->method('register');
96
97
        $shipment
98
            ->expects($this->any())
99
            ->method('getOrder')
100
            ->will($this->returnValue($this->orderModel));
101
102
        $this->orderModel
103
            ->expects($this->once())
104
            ->method('setData')
105
            ->with('is_in_process', true);
106
107
        $this->transactionResourceModel
108
            ->expects($this->at(0))
109
            ->method('addObject')
110
            ->with($shipment)
111
            ->will($this->returnSelf());
112
113
        $this->transactionResourceModel
114
            ->expects($this->at(1))
115
            ->method('addObject')
116
            ->with($this->orderModel)
117
            ->will($this->returnSelf());
118
119
        $this->transactionResourceModel
120
            ->expects($this->once())
121
            ->method('save');
122
123
        $this->trackingModel
124
            ->expects($this->once())
125
            ->method('setShipment')
126
            ->with($shipment);
127
128
        $this->trackingModel
129
            ->expects($this->once())
130
            ->method('setData')
131
            ->with(
132
                [
133
                    'title'         => 'Test Carrier',
134
                    'number'        => '782773742',
135
                    'carrier_code'  => 'custom',
136
                    'order_id'      => 5
137
                ]
138
            );
139
140
        $this->trackingModel
141
            ->expects($this->once())
142
            ->method('save');
143
144
        $this->shipmentWriter->writeItem(
145
            [
146
                'tracks' => [
147
                    0 => [
148
                        'carrier'           => 'Test Carrier',
149
                        'tracking_number'   => '782773742'
150
                    ]
151
                ],
152
                'items' => [
153
                    'items' => [
154
                        0 => [
155
                            'LineNo'    => 70000,
156
                            'SKU'       => 'FILAM317RL1',
157
                            'Qty'       => 1
158
                        ],
159
                        1 => [
160
                            'LineNo'    => 70001,
161
                            'SKU'       => 'FILAM317RL2',
162
                            'Qty'       => 1
163
                        ]
164
                    ]
165
                ],
166
                'order_id' => 5
167
            ]
168
        );
169
    }
170
171
    public function testShipmentCanBeCreatedWithoutTracking()
172
    {
173
        $this->orderModel
174
            ->expects($this->once())
175
            ->method('loadByIncrementId')
176
            ->with(5);
177
178
        $this->orderModel
179
            ->expects($this->any())
180
            ->method('getId')
181
            ->will($this->returnValue(5));
182
183
        $shipment = $this->getMock('Mage_Sales_Model_Order_Shipment');
184
185
        $this->orderModel
186
            ->expects($this->once())
187
            ->method('prepareShipment')
188
            ->will($this->returnValue($shipment));
189
190
        $shipment
191
            ->expects($this->once())
192
            ->method('register');
193
194
        $shipment
195
            ->expects($this->any())
196
            ->method('getOrder')
197
            ->will($this->returnValue($this->orderModel));
198
199
        $this->orderModel
200
            ->expects($this->once())
201
            ->method('setData')
202
            ->with('is_in_process', true);
203
204
        $this->transactionResourceModel
205
            ->expects($this->at(0))
206
            ->method('addObject')
207
            ->with($shipment)
208
            ->will($this->returnSelf());
209
210
211
        $this->transactionResourceModel
212
            ->expects($this->at(1))
213
            ->method('addObject')
214
            ->with($this->orderModel)
215
            ->will($this->returnSelf());
216
217
        $this->transactionResourceModel
218
            ->expects($this->once())
219
            ->method('save');
220
221
        $this->trackingModel
222
            ->expects($this->never())
223
            ->method('setShipment')
224
            ->with($shipment);
225
226
        $this->trackingModel
227
            ->expects($this->never())
228
            ->method('setData')
229
            ->with(
230
                [
231
                    'title'         => 'Test Carrier',
232
                    'number'        => '782773742',
233
                    'carrier_code'  => 'custom',
234
                    'order_id'      => 5
235
                ]
236
            );
237
238
        $this->trackingModel
239
            ->expects($this->never())
240
            ->method('save');
241
242
        $this->shipmentWriter->writeItem(
243
            [
244
                'items' => [
245
                    'items' => [
246
                        0 => [
247
                            'LineNo'    => 70000,
248
                            'SKU'       => 'FILAM317RL1',
249
                            'Qty'       => 1
250
                        ],
251
                        1 => [
252
                            'LineNo'    => 70001,
253
                            'SKU'       => 'FILAM317RL2',
254
                            'Qty'       => 1
255
                        ]
256
                    ]
257
                ],
258
                'order_id' => 5
259
            ]
260
        );
261
    }
262
263
    public function testMagentoSaveExceptionIsThrownIfSaveFails()
264
    {
265
266
        $this->orderModel
267
            ->expects($this->once())
268
            ->method('loadByIncrementId')
269
            ->with(5);
270
271
        $this->orderModel
272
            ->expects($this->any())
273
            ->method('getId')
274
            ->will($this->returnValue(5));
275
276
        $shipment = $this->getMock('Mage_Sales_Model_Order_Shipment');
277
278
        $this->orderModel
279
            ->expects($this->once())
280
            ->method('prepareShipment')
281
            ->will($this->returnValue($shipment));
282
283
        $shipment
284
            ->expects($this->once())
285
            ->method('register');
286
287
        $shipment
288
            ->expects($this->any())
289
            ->method('getOrder')
290
            ->will($this->returnValue($this->orderModel));
291
292
        $this->orderModel
293
            ->expects($this->once())
294
            ->method('setData')
295
            ->with('is_in_process', true);
296
297
        $this->transactionResourceModel
298
            ->expects($this->at(0))
299
            ->method('addObject')
300
            ->with($shipment)
301
            ->will($this->returnSelf());
302
303
304
        $this->transactionResourceModel
305
            ->expects($this->at(1))
306
            ->method('addObject')
307
            ->with($this->orderModel)
308
            ->will($this->returnSelf());
309
310
        $e = new \Mage_Core_Exception("Save Failed");
311
        $this->transactionResourceModel
312
            ->expects($this->once())
313
            ->method('save')
314
            ->will($this->throwException($e));
315
316
        $this->setExpectedException('Jh\DataImportMagento\Exception\MagentoSaveException', 'Save Failed');
317
        $this->shipmentWriter->writeItem(['order_id' => 5]);
318
    }
319
}
320