Completed
Branch master (e562f1)
by Antony
02:11
created

testSetBodyCustomerCodeAndName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 20
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace AntonyThorpe\SilverShopUnleashed\Tests;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\Core\Config\Config;
7
use SilverShop\Page\Product;
8
use SilverShop\Model\Product\OrderItem;
9
use SilverShop\Model\Modifiers\OrderModifier;
10
use SilverShop\Model\Order;
11
use SilverShop\Checkout\OrderProcessor;
12
use SilverShop\Cart\ShoppingCart;
13
use SilverShop\Tests\ShopTest;
14
use SilverShop\Model\Modifiers\Shipping\Simple;
15
use SilverShop\Model\Modifiers\Tax\FlatTax;
16
use SilverShop\Shipping\Model\DistanceShippingMethod;
17
use AntonyThorpe\SilverShopUnleashed\OrderBulkLoader;
18
use AntonyThorpe\SilverShopUnleashed\Defaults;
19
20
class UnleashedOrderTest extends SapphireTest
21
{
22
    protected static $fixture_file = [
23
        'vendor/silvershop/core/tests/php/Fixtures/ShopMembers.yml',
24
        'vendor/silvershop/core/tests/php/Fixtures/shop.yml',
25
        'vendor/silvershop/shipping/tests/DistanceShippingMethod.yml',
26
        'vendor/silvershop/shipping/tests/Warehouses.yml',
27
        'fixtures/models.yml'
28
    ];
29
30
    public function setUp()
31
    {
32
        Defaults::config()->send_sales_orders_to_unleashed = false;
33
        Defaults::config()->tax_modifier_class_name = 'SilverShop\Model\Modifiers\Tax\FlatTax';
34
        Defaults::config()->shipping_modifier_class_name = 'SilverShop\Model\Modifiers\Shipping\Simple';
35
        parent::setUp();
36
        ShoppingCart::singleton()->clear();
37
        ShopTest::setConfiguration(); //reset config
38
        Config::modify()
39
            ->set(FlatTax::class, 'rate', 0.15)
40
            ->set(FlatTax::class, 'exclusive', true)
41
            ->set(FlatTax::class, 'name', 'GST')
42
            ->set(FlatTax::class, 'tax_code', 'OUTPUT2')
43
            ->set(Simple::class, 'default_charge', 8.95)
44
            ->set(Simple::class, 'product_code', 'Freight')
45
            ->merge(Order::class, 'modifiers', [FlatTax::class, Simple::class]);
46
    }
47
48
    protected $order_status_map = [
49
        'Open' => 'Unpaid',
50
        'Parked' => 'Paid',
51
        'Backordered' => 'Processing',
52
        'Placed' => 'Processing',
53
        'Picking' => 'Processing',
54
        'Picked' => 'Processing',
55
        'Packed' => 'Processing',
56
        'Dispatched' => 'Sent',
57
        'Complete' => 'Complete',
58
        'Deleted' => 'MemberCancelled'
59
    ];
60
61
    public function testChangeOrderStatus()
62
    {
63
        $apidata_array = json_decode($this->jsondata, true);
64
        $apidata_array = reset($apidata_array);
65
        $apidata = $apidata_array['Items'];
66
67
        $loader = OrderBulkLoader::create('SilverShop\Model\Order');
68
        $loader->transforms = [
69
            'Status' => [
70
                'callback' => function ($value, &$placeholder) {
0 ignored issues
show
Unused Code introduced by
The parameter $placeholder is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

70
                'callback' => function ($value, /** @scrutinizer ignore-unused */ &$placeholder) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
                    // convert from Unleashed Sales Order status to Silvershop
72
                    return $this->order_status_map[$value];
73
                }
74
            ]
75
        ];
76
        $results = $loader->updateRecords($apidata);
77
78
        // Check Results
79
        $this->assertEquals($results->CreatedCount(), 0);
80
        $this->assertEquals($results->UpdatedCount(), 2);
81
        $this->assertEquals($results->DeletedCount(), 0);
82
        $this->assertEquals($results->SkippedCount(), 0);
83
        $this->assertEquals($results->Count(), 2);
84
85
        // Check Dataobjects
86
        $order1 = Order::get()->find('Reference', 'O1');
87
        //print_r($order1);
88
        $this->assertEquals(
89
            'Processing',
90
            $order1->Status,
91
            'OrderStatus of reference O1 is "Processing"'
92
        );
93
94
        $order2 = Order::get()->find('Reference', 'O2');
95
        $this->assertEquals(
96
            'Sent',
97
            $order2->Status,
98
            'OrderStatus of reference O1 is "Sent"'
99
        );
100
    }
101
102
    public function testSetBodyAddress()
103
    {
104
        $body = [
105
            'Addresses' => []
106
        ];
107
        $order = $this->objFromFixture(Order::class, "payablecart");
108
        OrderProcessor::create($order)->placeOrder();
109
        $body = $order->setBodyAddress($body, $order, 'Postal');
110
        $result = implode('|', array_values($body['Addresses'][0]));
111
        $this->assertSame(
112
            $result,
113
            '12 Foo Street Bar Farmville|Postal|Farmville|United States||New Sandwich|12 Foo Street|Bar',
114
            'Postal Address added to $body["Address"]'
115
        );
116
117
        $body = $order->setBodyAddress($body, $order, 'Physical');
118
        $result = implode('|', array_values($body['Addresses'][1]));
119
        $this->assertSame(
120
            $result,
121
            '12 Foo Street Bar Farmville|Physical|Farmville|United States||New Sandwich|12 Foo Street|Bar',
122
            'Physical Address added to $body["Address"]'
123
        );
124
        $result = implode('|', array_values($body['Addresses'][2]));
125
        $this->assertSame(
126
            $result,
127
            '12 Foo Street Bar Farmville|Shipping|Farmville|United States||New Sandwich|12 Foo Street|Bar',
128
            'Shipping Address added to $body["Address"]'
129
        );
130
131
        $result = $body['DeliveryCity'] . '|' . $body['DeliveryCountry'] . '|' . $body['DeliveryPostCode'] . '|' . $body['DeliveryRegion'] . '|' . $body['DeliveryStreetAddress'] . '|' . $body['DeliveryStreetAddress2'];
132
        $this->assertSame(
133
            $result,
134
            'Farmville|United States||New Sandwich|12 Foo Street|Bar',
135
            'Delivery Address added to $body'
136
        );
137
    }
138
139
    public function testSetBodyCurrencyCode()
140
    {
141
        $body = [
142
            'Currency' => []
143
        ];
144
        $order = $this->objFromFixture(Order::class, "payablecart");
145
        OrderProcessor::create($order)->placeOrder();
146
        $body = $order->setBodyCurrencyCode($body, $order);
147
148
        $this->assertSame(
149
            'NZD',
150
            $body['Currency']['CurrencyCode'],
151
            'Currency Code added to $body'
152
        );
153
    }
154
155
    public function testSetBodyCustomerCodeAndName()
156
    {
157
        $body = [];
158
        $order = $this->objFromFixture(Order::class, "payablecart");
159
        OrderProcessor::create($order)->placeOrder();
160
        $body = $order->setBodyCustomerCodeAndName($body, $order);
161
        $result = implode('|', array_values($body));
162
        $this->assertSame(
163
            $result,
164
            'Payable Smith|Payable Smith',
165
            'Set BodyCustomerCodeAndName'
166
        );
167
168
        $order->BillingAddress()->Company = 'Test Company';
169
        $body = $order->setBodyCustomerCodeAndName($body, $order);
170
        $result = implode('|', array_values($body));
171
        $this->assertSame(
172
            $result,
173
            'Test Company|Test Company',
174
            'Set BodyCustomerCodeAndName with Company name'
175
        );
176
    }
177
178
    public function testSetBodySalesOrderLines()
179
    {
180
        $body = [
181
            'Tax' => [
182
                'TaxCode' => 'OUTPUT2'
183
            ]
184
        ];
185
        $order = $this->objFromFixture(Order::class, "paid1");
186
        OrderProcessor::create($order)->placeOrder();
187
        $order->write();
188
        $order->calculate();
189
        $body = $order->setBodySalesOrderLines($body, $order, 'SilverShop\Model\Modifiers\Tax\FlatTax', 2);
190
        $result = json_encode($body);
191
        $this->assertContains(
192
            'SalesOrderLines',
193
            $result,
194
            'Contains SalesOrderLines'
195
        );
196
        $this->assertContains(
197
            '"LineType":null,"LineTotal":8,"OrderQuantity":1,"Product":{"Guid":"G11"},"UnitPrice":8,"LineTax":1.2,"LineTaxCode":"OUTPUT2"}',
198
            $result,
199
            'Set SetBodySalesOrderLines line 1',
200
        );
201
202
        $this->assertContains(
203
            '"LineType":null,"LineTotal":400,"OrderQuantity":2,"Product":{"Guid":"G15"},"UnitPrice":200,"LineTax":60,"LineTaxCode":"OUTPUT2"}',
204
            $result,
205
            'Set SetBodySalesOrderLines line 2'
206
        );
207
    }
208
209
    public function testSetBodySalesOrderLinesWithModifiers()
210
    {
211
        $body = [
212
            'Tax' => [
213
                'TaxCode' => 'OUTPUT2'
214
            ]
215
        ];
216
        $order = Order::create();
217
        $urntap = $this->objFromFixture(Product::class, 'urntap');
218
        $urntap->publishSingle();
219
        $order->Items()->add($urntap->createItem(1));
220
        $order->write();
221
        OrderProcessor::create($order)->placeOrder();
222
        $total = $order->calculate();
223
224
        $this->assertEquals(1, $order->Items()->count());
225
        $this->assertEquals(2, $order->Modifiers()->count()); // Shipping & GST
226
        $this->assertEquals(84.45, $total, 'Total equals $84.45');
227
        $body = $order->setBodySalesOrderLines($body, $order, 'SilverShop\Model\Modifiers\Tax\FlatTax', 2);
228
        $freight_modifier = $body['SalesOrderLines'][1];
229
        $result = $freight_modifier['DiscountRate'] . '|' . $freight_modifier['LineNumber'] . '|' . $freight_modifier['LineTotal'] . '|' . $freight_modifier['LineType'] . '|' . $freight_modifier['OrderQuantity'] . '|' . $freight_modifier['UnitPrice'] . '|' . $freight_modifier['LineTax'] . '|' . $freight_modifier['LineTaxCode'];
230
        $this->assertSame(
231
            $result,
232
            '0|2|8.95||1|8.95|1.34|OUTPUT2',
233
            'Modifiers in the SalesOrderLines added to $body'
234
        );
235
        $this->assertSame(
236
            $freight_modifier['Product']['ProductCode'],
237
            'Freight',
238
            'ProductCode of the Freight Modifier in $body is "Freight"'
239
        );
240
    }
241
242
    public function testSetBodySubTotalAndTax()
243
    {
244
        $order = Order::create();
245
        $urntap = $this->objFromFixture(Product::class, 'urntap');
246
        $urntap->publishSingle();
247
        $order->Items()->add($urntap->createItem(1));
248
        $order->write();
249
        OrderProcessor::create($order)->placeOrder();
250
        $total = $order->calculate();
0 ignored issues
show
Unused Code introduced by
The assignment to $total is dead and can be removed.
Loading history...
251
252
        $body = $order->setBodySubTotalAndTax([], $order, 'SilverShop\Model\Modifiers\Tax\FlatTax', 2);
253
        $result = $body['Taxable'] . '|' . $body['TaxTotal'] . '|' . $body['SubTotal'];
254
255
        $this->assertSame(
256
            $result,
257
            '1|9.85|74.60',
258
            'Taxable, TaxTotal & SubTotal added to $body'
259
        );
260
        $this->assertSame(
261
            $body['Tax']['TaxCode'],
262
            'OUTPUT2',
263
            'TaxCode set in $body'
264
        );
265
    }
266
267
    public function testSetBodyDeliveryMethodAndDeliveryName()
268
    {
269
        Defaults::config()->shipping_modifier_class_name = 'SilverShop\Shipping\ShippingFrameworkModifier';
270
        Config::modify()->set('SilverShop\Shipping\ShippingFrameworkModifier', 'product_code', 'Freight');
271
        $body = [];
272
        $defaults = Defaults::config();
273
        $order = $this->objFromFixture(Order::class, "payablecart");
274
        $body = $order->setBodyDeliveryMethodAndDeliveryName($body, $order, $defaults->shipping_modifier_class_name);
275
        $result = implode('|', array_values($body));
276
        $this->assertSame(
277
            $result,
278
            'Freight|Freight',
279
            'Set BodyDeliveryMethodAndDeliveryName'
280
        );
281
    }
282
283
    /**
284
     * JSON data for test
285
     *
286
     * @link (Unleashed Software API Documentation, https://apidocs.unleashedsoftware.com/Products)
287
     * @var string
288
     */
289
    protected $jsondata = '[
290
        {
291
          "Pagination": {
292
            "NumberOfItems": 2,
293
            "PageSize": 200,
294
            "PageNumber": 1,
295
            "NumberOfPages": 1
296
          },
297
          "Items": [
298
            {
299
              "SalesOrderLines": [
300
                {
301
                  "LineNumber": 1,
302
                  "LineType": null,
303
                  "Product": {
304
                    "Guid": "G11",
305
                    "ProductCode": "IIID1",
306
                    "ProductDescription": "Socks"
307
                  },
308
                  "DueDate": "/Date(1473099415000)/",
309
                  "OrderQuantity": 1,
310
                  "UnitPrice": 8,
311
                  "DiscountRate": 0,
312
                  "LineTotal": 8,
313
                  "Volume": null,
314
                  "Weight": null,
315
                  "Comments": null,
316
                  "AverageLandedPriceAtTimeOfSale": 8,
317
                  "TaxRate": 0,
318
                  "LineTax": 0,
319
                  "XeroTaxCode": "G.S.T.",
320
                  "BCUnitPrice": 8,
321
                  "BCLineTotal": 8,
322
                  "BCLineTax": 0,
323
                  "LineTaxCode": "G.S.T.",
324
                  "XeroSalesAccount": null,
325
                  "SerialNumbers": null,
326
                  "BatchNumbers": null,
327
                  "Guid": "G401",
328
                  "LastModifiedOn": "/Date(1473149768263)/"
329
                },
330
                {
331
                  "LineNumber": 2,
332
                  "LineType": null,
333
                  "Product": {
334
                    "Guid": "G15",
335
                    "ProductCode": "IIID5",
336
                    "ProductDescription": "Mp3 Player"
337
                  },
338
                  "DueDate": "/Date(1473099415000)/",
339
                  "OrderQuantity": 2,
340
                  "UnitPrice": 200,
341
                  "DiscountRate": 0,
342
                  "LineTotal": 400,
343
                  "Volume": null,
344
                  "Weight": null,
345
                  "Comments": null,
346
                  "AverageLandedPriceAtTimeOfSale": 200,
347
                  "TaxRate": 0,
348
                  "LineTax": 0,
349
                  "XeroTaxCode": "G.S.T.",
350
                  "BCUnitPrice": 200,
351
                  "BCLineTotal": 400,
352
                  "BCLineTax": 0,
353
                  "LineTaxCode": "G.S.T.",
354
                  "XeroSalesAccount": null,
355
                  "SerialNumbers": null,
356
                  "BatchNumbers": null,
357
                  "Guid": "G402",
358
                  "LastModifiedOn": "/Date(1473149768279)/"
359
                }
360
              ],
361
              "OrderNumber": "O1",
362
              "OrderDate": "/Date(1473116400000)/",
363
              "RequiredDate": "/Date(1473548400000)/",
364
              "OrderStatus": "Placed",
365
              "Customer": {
366
                "CustomerCode": "Jeremy Peremy",
367
                "CustomerName": "Jeremy Peremy",
368
                "CurrencyId": 110,
369
                "Guid": "test",
370
                "LastModifiedOn": "/Date(1472624588017)/"
371
              },
372
              "CustomerRef": null,
373
              "Comments": "Test",
374
              "Warehouse": {
375
                "WarehouseCode": "test",
376
                "WarehouseName": "Queen St",
377
                "IsDefault": true,
378
                "StreetNo": "1",
379
                "AddressLine1": "Queen St",
380
                "AddressLine2": null,
381
                "City": "Invercargill",
382
                "Region": "Southland",
383
                "Country": "New Zealand",
384
                "PostCode": "9999",
385
                "PhoneNumber": "1234 567",
386
                "FaxNumber": null,
387
                "MobileNumber": null,
388
                "DDINumber": null,
389
                "ContactName": "Ed Hillary",
390
                "Obsolete": false,
391
                "Guid": "test",
392
                "LastModifiedOn": "/Date(1471582972964)/"
393
              },
394
              "ReceivedDate": "/Date(1473099415000)/",
395
              "DeliveryName": null,
396
              "DeliveryStreetAddress": "15 Ray St",
397
              "DeliverySuburb": "",
398
              "DeliveryCity": "Kaitaia",
399
              "DeliveryRegion": "Northland",
400
              "DeliveryCountry": "New Zealand",
401
              "DeliveryPostCode": "1111",
402
              "Currency": {
403
                "CurrencyCode": "NZD",
404
                "Description": "New Zealand, Dollars",
405
                "Guid": "test",
406
                "LastModifiedOn": "/Date(1415058050647)/"
407
              },
408
              "ExchangeRate": 1,
409
              "DiscountRate": 0,
410
              "Tax": {
411
                "TaxCode": "G.S.T.",
412
                "Description": null,
413
                "TaxRate": 0,
414
                "CanApplyToExpenses": false,
415
                "CanApplyToRevenue": false,
416
                "Obsolete": false,
417
                "Guid": "00000000-0000-0000-0000-000000000000",
418
                "LastModifiedOn": null
419
              },
420
              "TaxRate": 0,
421
              "XeroTaxCode": "G.S.T.",
422
              "SubTotal": 408,
423
              "TaxTotal": 0,
424
              "Total": 408,
425
              "TotalVolume": 0,
426
              "TotalWeight": 0,
427
              "BCSubTotal": 408,
428
              "BCTaxTotal": 0,
429
              "BCTotal": 408,
430
              "PaymentDueDate": "/Date(1473106568169)/",
431
              "SalesOrderGroup": null,
432
              "DeliveryMethod": null,
433
              "SalesPerson": null,
434
              "SendAccountingJournalOnly": false,
435
              "SourceId": "web",
436
              "CreatedBy": "[email protected]",
437
              "Guid": "G201",
438
              "LastModifiedOn": "/Date(1473149768310)/"
439
            },
440
            {
441
              "SalesOrderLines": [
442
                {
443
                  "LineNumber": 1,
444
                  "LineType": null,
445
                  "Product": {
446
                    "Guid": "G11",
447
                    "ProductCode": "IIID1",
448
                    "ProductDescription": "Socks"
449
                  },
450
                  "DueDate": "/Date(1473099415000)/",
451
                  "OrderQuantity": 1,
452
                  "UnitPrice": 8,
453
                  "DiscountRate": 0,
454
                  "LineTotal": 8,
455
                  "Volume": null,
456
                  "Weight": null,
457
                  "Comments": null,
458
                  "AverageLandedPriceAtTimeOfSale": 8,
459
                  "TaxRate": 0,
460
                  "LineTax": 0,
461
                  "XeroTaxCode": "G.S.T.",
462
                  "BCUnitPrice": 8,
463
                  "BCLineTotal": 8,
464
                  "BCLineTax": 0,
465
                  "LineTaxCode": "G.S.T.",
466
                  "XeroSalesAccount": null,
467
                  "SerialNumbers": null,
468
                  "BatchNumbers": null,
469
                  "Guid": "G403",
470
                  "LastModifiedOn": "/Date(1473149768263)/"
471
                },
472
                {
473
                  "LineNumber": 2,
474
                  "LineType": null,
475
                  "Product": {
476
                    "Guid": "G15",
477
                    "ProductCode": "IIID5",
478
                    "ProductDescription": "Mp3 Player"
479
                  },
480
                  "DueDate": "/Date(1473099415000)/",
481
                  "OrderQuantity": 2,
482
                  "UnitPrice": 200,
483
                  "DiscountRate": 0,
484
                  "LineTotal": 400,
485
                  "Volume": null,
486
                  "Weight": null,
487
                  "Comments": null,
488
                  "AverageLandedPriceAtTimeOfSale": 200,
489
                  "TaxRate": 0,
490
                  "LineTax": 0,
491
                  "XeroTaxCode": "G.S.T.",
492
                  "BCUnitPrice": 200,
493
                  "BCLineTotal": 400,
494
                  "BCLineTax": 0,
495
                  "LineTaxCode": "G.S.T.",
496
                  "XeroSalesAccount": null,
497
                  "SerialNumbers": null,
498
                  "BatchNumbers": null,
499
                  "Guid": "G404",
500
                  "LastModifiedOn": "/Date(1473149768279)/"
501
                }
502
              ],
503
              "OrderNumber": "O2",
504
              "OrderDate": "/Date(1473116400000)/",
505
              "RequiredDate": "/Date(1473548400000)/",
506
              "OrderStatus": "Dispatched",
507
              "Customer": {
508
                "CustomerCode": "Jeremy Peremy",
509
                "CustomerName": "Jeremy Peremy",
510
                "CurrencyId": 110,
511
                "Guid": "test",
512
                "LastModifiedOn": "/Date(1472624588017)/"
513
              },
514
              "CustomerRef": null,
515
              "Comments": "Test",
516
              "Warehouse": {
517
                "WarehouseCode": "test",
518
                "WarehouseName": "Queen St",
519
                "IsDefault": true,
520
                "StreetNo": "1",
521
                "AddressLine1": "Queen St",
522
                "AddressLine2": null,
523
                "City": "Invercargill",
524
                "Region": "Southland",
525
                "Country": "New Zealand",
526
                "PostCode": "9999",
527
                "PhoneNumber": "1234 567",
528
                "FaxNumber": null,
529
                "MobileNumber": null,
530
                "DDINumber": null,
531
                "ContactName": "Ed Hillary",
532
                "Obsolete": false,
533
                "Guid": "test",
534
                "LastModifiedOn": "/Date(1471582972964)/"
535
              },
536
              "ReceivedDate": "/Date(1473099415000)/",
537
              "DeliveryName": null,
538
              "DeliveryStreetAddress": "15 Ray St",
539
              "DeliverySuburb": "",
540
              "DeliveryCity": "Kaitaia",
541
              "DeliveryRegion": "Northland",
542
              "DeliveryCountry": "New Zealand",
543
              "DeliveryPostCode": "1111",
544
              "Currency": {
545
                "CurrencyCode": "NZD",
546
                "Description": "New Zealand, Dollars",
547
                "Guid": "test",
548
                "LastModifiedOn": "/Date(1415058050647)/"
549
              },
550
              "ExchangeRate": 1,
551
              "DiscountRate": 0,
552
              "Tax": {
553
                "TaxCode": "G.S.T.",
554
                "Description": null,
555
                "TaxRate": 0,
556
                "CanApplyToExpenses": false,
557
                "CanApplyToRevenue": false,
558
                "Obsolete": false,
559
                "Guid": "00000000-0000-0000-0000-000000000000",
560
                "LastModifiedOn": null
561
              },
562
              "TaxRate": 0,
563
              "XeroTaxCode": "G.S.T.",
564
              "SubTotal": 408,
565
              "TaxTotal": 0,
566
              "Total": 408,
567
              "TotalVolume": 0,
568
              "TotalWeight": 0,
569
              "BCSubTotal": 408,
570
              "BCTaxTotal": 0,
571
              "BCTotal": 408,
572
              "PaymentDueDate": "/Date(1473106568169)/",
573
              "SalesOrderGroup": null,
574
              "DeliveryMethod": null,
575
              "SalesPerson": null,
576
              "SendAccountingJournalOnly": false,
577
              "SourceId": "web",
578
              "CreatedBy": "[email protected]",
579
              "Guid": "G202",
580
              "LastModifiedOn": "/Date(1473149768310)/"
581
            }
582
          ]
583
        }
584
      ]';
585
}
586