OrderWriterTest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 508
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 508
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 16 1
A getMockModel() 0 11 2
A testExceptionIsThrownIfMappingAttributeOrPaymentMethodIsNotString() 0 16 1
B testCalculateSubTotal() 0 24 1
B testCalculateGrandTotal() 0 30 1
B testAddProductsToQuote() 0 44 1
A testAddProductsToQuoteThrowsExceptionIfProductNotFound() 0 18 1
A testAddCustomerToQuoteWithNoDefaultAddress() 0 68 1
A testAddCustomerToQuoteWithDefaultAddress() 0 66 1
B testAddDetailsToQuote() 0 42 1
B testGetCustomerByAttribute() 0 33 1
B testQuoteToOrder() 0 118 1
1
<?php
2
3
namespace Jh\DataImportMagentoTest\Writer;
4
5
use Jh\DataImportMagento\Writer\OrderWriter;
6
7
/**
8
 * Class OrderWriterTest
9
 * @package Jh\DataImportMagentoTest\Writer
10
 * @author Aydin Hassan <[email protected]>
11
 */
12
class OrderWriterTest extends \PHPUnit_Framework_TestCase
13
{
14
15
    protected $quote;
16
    protected $convertQuote;
17
    protected $customer;
18
    protected $product;
19
    protected $quoteItem;
20
    protected $writer;
21
22
    public function setUp()
23
    {
24
        $this->quote        = $this->getMockModel('\Mage_Sales_Model_Quote');
25
        $this->convertQuote = $this->getMockModel('\Mage_Sales_Model_Convert_Quote');
26
        $this->customer     = $this->getMockModel('\Mage_Customer_Model_Customer');
27
        $this->product      = $this->getMockModel('\Mage_Catalog_Model_Product');
28
        $this->quoteItem    = $this->getMockModel('\Mage_Sales_Model_Quote_Item', true);
29
30
        $this->writer = new OrderWriter(
31
            $this->quote,
32
            $this->convertQuote,
33
            $this->customer,
34
            $this->product,
35
            $this->quoteItem
36
        );
37
    }
38
39
    protected function getMockModel($class, $disableClone = false)
40
    {
41
        $mockBuilder = $this->getMockBuilder($class)
42
            ->disableOriginalConstructor();
43
44
        if ($disableClone) {
45
            $mockBuilder->disableOriginalClone();
46
        }
47
48
        return $mockBuilder->getMock();
49
    }
50
51
    public function testExceptionIsThrownIfMappingAttributeOrPaymentMethodIsNotString()
52
    {
53
        $this->setExpectedException(
54
            'InvalidArgumentException',
55
            'Customer Mapping Attribute and Payment Method Code should be strings'
56
        );
57
58
        new OrderWriter(
59
            $this->quote,
60
            $this->convertQuote,
61
            $this->customer,
62
            $this->product,
63
            $this->quoteItem,
64
            new \stdClass
0 ignored issues
show
Documentation introduced by
new \stdClass() is of type object<stdClass>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
65
        );
66
    }
67
68
    public function testCalculateSubTotal()
69
    {
70
        $orderMock = $this->getMockModel('\Mage_Sales_Model_Order');
71
        $item1 = new \Mage_Sales_Model_Order_Item();
72
        $item1->setData([
73
           'price'          => 10,
74
            'tax_amount'    => 20,
75
        ]);
76
        $item2 = new \Mage_Sales_Model_Order_Item();
77
        $item2->setData([
78
            'price'         => 220,
79
            'tax_amount'    => 20,
80
        ]);
81
82
        $orderMock
83
            ->expects($this->any())
84
            ->method('getAllItems')
85
            ->will($this->returnValue([
86
                $item1,
87
                $item2
88
            ]));
89
90
        $this->assertEquals(270, $this->writer->calculateSubTotal($orderMock));
91
    }
92
93
    public function testCalculateGrandTotal()
94
    {
95
        $data = [
96
            'shipping_amount'   => 20,
97
            'gw_price'          => 15,
98
            'discount_amount'   => 10,
99
        ];
100
101
        $orderMock = $this->getMockModel('\Mage_Sales_Model_Order');
102
        $item1 = new \Mage_Sales_Model_Order_Item();
103
        $item1->setData([
104
            'price'          => 10,
105
            'tax_amount'    => 20,
106
        ]);
107
        $item2 = new \Mage_Sales_Model_Order_Item();
108
        $item2->setData([
109
            'price'         => 220,
110
            'tax_amount'    => 20,
111
        ]);
112
113
        $orderMock
114
            ->expects($this->once())
115
            ->method('getAllItems')
116
            ->will($this->returnValue([
117
                $item1,
118
                $item2
119
            ]));
120
121
        $this->assertEquals(295, $this->writer->calculateGrandTotal($orderMock, $data));
122
    }
123
124
    public function testAddProductsToQuote()
125
    {
126
        $items = [
127
            ['sku' => 'SKU1', 'price' => 5, 'qty' => 2],
128
        ];
129
130
        $this->product
131
            ->expects($this->once())
132
            ->method('loadByAttribute')
133
            ->will($this->returnSelf());
134
135
        $this->quoteItem
136
            ->expects($this->once())
137
            ->method('setProduct')
138
            ->with($this->product);
139
140
        $this->quoteItem
141
            ->expects($this->once())
142
            ->method('setQuote')
143
            ->with($this->quote);
144
145
        $this->quoteItem
146
            ->expects($this->once())
147
            ->method('setQty')
148
            ->with(2);
149
150
        $this->quoteItem
151
            ->expects($this->once())
152
            ->method('addData')
153
            ->with([
154
                'price'                 => 5,
155
                'base_price'            => 5,
156
                'original_price'        => 5,
157
                'custom_price'          => 5,
158
                'original_custom_price' => 5
159
            ]);
160
161
        $this->quote
162
            ->expects($this->once())
163
            ->method('addItem')
164
            ->with($this->quoteItem);
165
166
        $this->writer->addProductsToQuote($this->quote, $items);
167
    }
168
169
    public function testAddProductsToQuoteThrowsExceptionIfProductNotFound()
170
    {
171
        $items = [
172
            ['sku' => 'SKU1', 'price' => 5, 'qty' => 2],
173
        ];
174
175
        $this->product
176
            ->expects($this->once())
177
            ->method('loadByAttribute')
178
            ->will($this->returnValue(null));
179
180
        $this->setExpectedException(
181
            'Ddeboer\DataImport\Exception\WriterException',
182
            'Product with SKU: SKU1 does not exist in Magento'
183
        );
184
185
        $this->writer->addProductsToQuote($this->quote, $items);
186
    }
187
188
    public function testAddCustomerToQuoteWithNoDefaultAddress()
189
    {
190
        $firstName  = 'Aydin';
191
        $lastName   = 'Hassan';
192
        $email      = '[email protected]';
193
194
        $this->customer
195
            ->method('getData')
196
            ->will($this->returnValueMap([
197
                ['firstname', null, $firstName],
198
                ['lastname', null, $lastName],
199
                ['email', null, $email],
200
            ]));
201
202
        $this->quote
203
            ->expects($this->once())
204
            ->method('addData')
205
            ->with([
206
                'customer_firstname'    => $firstName,
207
                'customer_lastname'     => $lastName,
208
                'customer_email'        => $email,
209
            ]);
210
211
        $addressData = [
212
            'street'    => 'Some Street',
213
            'city'      => 'Some City',
214
        ];
215
        $address = $this->getMock('Mage_Customer_Model_Address');
216
        $address
217
            ->expects($this->any())
218
            ->method('getData')
219
            ->will($this->returnValue($addressData));
220
221
222
        $addressCollectionResource = $this->getMockModel('Mage_Customer_Model_Resource_Address_Collection');
223
        $addressCollectionResource
224
            ->expects($this->once())
225
            ->method('getFirstItem')
226
            ->will($this->returnValue($address));
227
        
228
        $this->customer
229
            ->expects($this->once())
230
            ->method('getAddressesCollection')
231
            ->will($this->returnValue($addressCollectionResource));
232
233
        $quoteAddress = $this->getMockModel('Mage_Sales_Model_Quote_Address');
234
        $quoteAddress
235
            ->expects($this->any())
236
            ->method('addData')
237
            ->with($addressData);
238
239
        $this->quote
240
            ->expects($this->once())
241
            ->method('getBillingAddress')
242
            ->will($this->returnValue($quoteAddress));
243
244
        $this->quote
245
            ->expects($this->once())
246
            ->method('getShippingAddress')
247
            ->will($this->returnValue($quoteAddress));
248
249
        $this->quote
250
            ->expects($this->once())
251
            ->method('assignCustomer')
252
            ->with($this->customer);
253
254
        $this->writer->addCustomerToQuote($this->quote, $this->customer);
255
    }
256
257
    public function testAddCustomerToQuoteWithDefaultAddress()
258
    {
259
        $firstName  = 'Aydin';
260
        $lastName   = 'Hassan';
261
        $email      = '[email protected]';
262
263
        $this->customer
264
            ->method('getData')
265
            ->will($this->returnValueMap([
266
                ['firstname', null, $firstName],
267
                ['lastname', null, $lastName],
268
                ['email', null, $email],
269
            ]));
270
271
        $this->quote
272
            ->expects($this->once())
273
            ->method('addData')
274
            ->with([
275
                'customer_firstname'    => $firstName,
276
                'customer_lastname'     => $lastName,
277
                'customer_email'        => $email,
278
            ]);
279
280
281
        $addressData = [
282
            'street'    => 'Some Street',
283
            'city'      => 'Some City',
284
        ];
285
        $address = $this->getMock('Mage_Customer_Model_Address');
286
        $address
287
            ->expects($this->any())
288
            ->method('getData')
289
            ->will($this->returnValue($addressData));
290
291
        $this->customer
292
            ->expects($this->once())
293
            ->method('getDefaultBillingAddress')
294
            ->will($this->returnValue($address));
295
296
        $this->customer
297
            ->expects($this->once())
298
            ->method('getDefaultShippingAddress')
299
            ->will($this->returnValue($address));
300
301
        $quoteAddress = $this->getMockModel('Mage_Sales_Model_Quote_Address');
302
        $quoteAddress
303
            ->expects($this->any())
304
            ->method('addData')
305
            ->with($addressData);
306
307
        $this->quote
308
            ->expects($this->once())
309
            ->method('getBillingAddress')
310
            ->will($this->returnValue($quoteAddress));
311
312
        $this->quote
313
            ->expects($this->once())
314
            ->method('getShippingAddress')
315
            ->will($this->returnValue($quoteAddress));
316
317
        $this->quote
318
            ->expects($this->once())
319
            ->method('assignCustomer')
320
            ->with($this->customer);
321
        $this->writer->addCustomerToQuote($this->quote, $this->customer);
322
    }
323
324
    public function testAddDetailsToQuote()
325
    {
326
        $data = [
327
            'created_at'    => '12-04-1988',
328
            'increment_id'  => '000001',
329
        ];
330
331
        $payment = $this->getMockModel('Mage_Payment_Model_Method_Abstract');
332
333
        $payment
334
            ->expects($this->once())
335
            ->method('addData')
336
            ->with(['method' => 'checkmo']);
337
338
        $this->quote
339
            ->expects($this->once())
340
            ->method('getPayment')
341
            ->will($this->returnValue($payment));
342
343
344
        $this->quote
345
            ->expects($this->once())
346
            ->method('addData')
347
            ->with([
348
                'created_at'          => $data['created_at'],
349
                'reserved_order_id'   => $data['increment_id'],
350
            ]);
351
352
        $address = $this->getMock('Mage_Customer_Model_Address');
353
        
354
        $address
355
            ->expects($this->once())
356
            ->method('addData')
357
            ->with(['payment_method' => 'checkmo']);
358
359
        $this->quote
360
            ->expects($this->once())
361
            ->method('getShippingAddress')
362
            ->will($this->returnValue($address));
363
364
        $this->writer->addDetailsToQuote($this->quote, $data);
365
    }
366
367
    public function testGetCustomerByAttribute()
368
    {
369
        $collection = $this->getMockModel('Mage_Customer_Model_Resource_Customer_Collection');
370
371
        $this->customer
372
            ->expects($this->once())
373
            ->method('getCollection')
374
            ->will($this->returnValue($collection));
375
376
        $attribute  = 'email';
377
        $value      = '[email protected]';
378
379
        $collection
380
            ->expects($this->once())
381
            ->method('addAttributeToFilter')
382
            ->with($attribute, $value)
383
            ->will($this->returnSelf());
384
        
385
        $collection
386
            ->expects($this->once())
387
            ->method('addAttributeToSelect')
388
            ->with('*')
389
            ->will($this->returnSelf());
390
391
        $customer = $this->getMockModel('Mage_Customer_Model_Customer');
392
393
        $collection
394
            ->expects($this->once())
395
            ->method('getFirstItem')
396
            ->will($this->returnValue($customer));
397
398
        $this->assertSame($customer, $this->writer->getCustomerByAttribute($attribute, $value));
399
    }
400
401
    public function testQuoteToOrder()
402
    {
403
        $order = $this->getMockModel('Mage_Sales_Model_Order');
404
405
        $this->convertQuote
406
            ->expects($this->once())
407
            ->method('toOrder')
408
            ->with($this->quote)
409
            ->will($this->returnValue($order));
410
411
        $quoteAddress1 = $this->getMockModel('Mage_Sales_Model_Quote_Address');
412
        $quoteAddress2 = $this->getMockModel('Mage_Sales_Model_Quote_Address');
413
        $orderAddress1 = $this->getMockModel('Mage_Sales_Model_Order_Address');
414
        $orderAddress2 = $this->getMockModel('Mage_Sales_Model_Order_Address');
415
416
        $this->quote
417
            ->expects($this->once())
418
            ->method('getBillingAddress')
419
            ->will($this->returnValue($quoteAddress1));
420
421
        $this->quote
422
            ->expects($this->once())
423
            ->method('getShippingAddress')
424
            ->will($this->returnValue($quoteAddress2));
425
426
        $this->convertQuote
427
            ->method('addressToOrderAddress')
428
            ->will($this->returnValueMap([
429
                [$quoteAddress1, $orderAddress1],
430
                [$quoteAddress2, $orderAddress2],
431
            ]));
432
433
        $order
434
            ->expects($this->once())
435
            ->method('setBillingAddress')
436
            ->with($orderAddress1);
437
438
        $order
439
            ->expects($this->once())
440
            ->method('setBillingAddress')
441
            ->with($orderAddress2);
442
443
        $item1 = new \Mage_Sales_Model_Order_Item();
444
        $item1->setData([
445
            'price'          => 10,
446
            'tax_amount'    => 20,
447
        ]);
448
        $item2 = new \Mage_Sales_Model_Order_Item();
449
        $item2->setData([
450
            'price'         => 220,
451
            'tax_amount'    => 20,
452
        ]);
453
454
        $order
455
            ->expects($this->any())
456
            ->method('getAllItems')
457
            ->will($this->returnValue([
458
                $item1,
459
                $item2
460
            ]));
461
462
        $quoteItem = new \Mage_Sales_Model_Quote_Item();
463
        $quoteItem->addData(['sku' => 'SKU1']);
464
        $orderItem = $this->getMockModel('Mage_Sales_Model_Order_Item');
465
466
        $this->quote
467
            ->expects($this->once())
468
            ->method('getAllItems')
469
            ->will($this->returnValue([$quoteItem]));
470
471
        $payment = $this->getMock('Mage_Sales_Model_Quote_Payment');
472
        $this->quote
473
            ->expects($this->once())
474
            ->method('getPayment')
475
            ->will($this->returnValue($payment));
476
477
        $orderPayment = $this->getMock('Mage_Sales_Model_Order_Payment');
478
        $this->convertQuote
479
            ->expects($this->once())
480
            ->method('paymentToOrderPayment')
481
            ->will($this->returnValue($orderPayment));
482
483
        $this->convertQuote
484
            ->expects($this->once())
485
            ->method('itemToOrderItem')
486
            ->with($quoteItem)
487
            ->will($this->returnValue($orderItem));
488
489
        $orderData = [
490
            'items' => [
491
                [
492
                    'sku' => 'SKU1',
493
                    'discount_amount' => 6,
494
                    'tax_amount' => 20,
495
                    'gw_price' => 8,
496
                    'price' => 100,
497
                ],
498
            ],
499
            'shipping_amount' => 30,
500
            'gw_price'        => 20,
501
            'discount_amount' => 20,
502
        ];
503
504
        $orderItem
505
            ->expects($this->once())
506
            ->method('addData')
507
            ->with([
508
                'discount_amount'       => 6,
509
                'base_discount_amount'  => 6,
510
                'tax_amount'            => 20,
511
                'base_tax_amount'       => 20,
512
                'gw_price'              => 8,
513
                'base_gw_price'         => 8,
514
                'tax_percent'           => 20,
515
            ]);
516
517
        $this->writer->quoteToOrder($this->quote, $orderData);
518
    }
519
}
520