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

UnleashedCustomerTest::testMatchCustomerAddress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
3
namespace AntonyThorpe\SilverShopUnleashed\Tests;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverShop\Model\Order;
7
use SilverShop\Tests\ShopTest;
8
use AntonyThorpe\SilverShopUnleashed\Defaults;
9
10
class UnleashedCustomerTest extends SapphireTest
11
{
12
    protected static $fixture_file = [
13
        'vendor/silvershop/core/tests/php/Fixtures/ShopMembers.yml',
14
        'vendor/silvershop/core/tests/php/Fixtures/Orders.yml',
15
        'fixtures/models.yml'
16
    ];
17
18
    public function setUp()
19
    {
20
        Defaults::config()->send_sales_orders_to_unleashed = false;
21
        parent::setUp();
22
        ShopTest::setConfiguration(); //reset config
23
        $this->order = $this->objFromFixture(Order::class, "cart1");
0 ignored issues
show
Bug Best Practice introduced by
The property order does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
24
    }
25
26
    public function testGetAddressNameFromOrder()
27
    {
28
        $this->assertSame(
29
            '201-203 BROADWAY AVE U 235 WEST BEACH',
30
            $this->order->getAddressName($this->order->ShippingAddress()),
31
            'Result matches 201-203 Broadway Ave U 235 West Beach'
32
        );
33
    }
34
35
    public function testGetAddressNameItems()
36
    {
37
        $apidata_array = json_decode($this->jsondata, true);
38
        $apidata_array = reset($apidata_array);
39
        $apidata = $apidata_array['Items'];
40
41
        $this->assertSame(
42
            '31 Hurstmere Road RD1 Auckland',
43
            $this->order->getAddressName($apidata[0]['Addresses'][1]),
44
            'Result matches 31 Hurstmere Road RD1 Auckland'
45
        );
46
    }
47
48
    public function testMatchCustomerAddress()
49
    {
50
        $apidata_array = json_decode($this->jsondata, true);
51
        $apidata_array = reset($apidata_array);
52
        $apidata = $apidata_array['Items'];
53
54
        // Test a failed match
55
        $this->assertFalse(
56
            $this->order->matchCustomerAddress($apidata, $this->order->ShippingAddress()),
57
            "The address in the API data does not match the order's shipping address"
58
        );
59
60
        // Test a direct match
61
        $shipping_address = $this->order->ShippingAddress();
62
        $shipping_address->Address = '31 Hurstmere Road';
63
        $shipping_address->AddressLine2 = 'RD1';
64
        $shipping_address->City = 'Auckland';
65
66
        $this->assertTrue(
67
            $this->order->matchCustomerAddress($apidata, $shipping_address),
68
            "The address in the API data matches the order's shipping address"
69
        );
70
    }
71
72
    /**
73
     * JSON data for test
74
     *
75
     * @link (Unleashed Software API Documentation, https://apidocs.unleashedsoftware.com/Products)
76
     * @var string
77
     */
78
    protected $jsondata = '[
79
        {
80
          "Pagination": {
81
            "NumberOfItems": 11,
82
            "PageSize": 200,
83
            "PageNumber": 1,
84
            "NumberOfPages": 1
85
          },
86
          "Items": [
87
            {
88
              "Addresses": [
89
                  {
90
                      "AddressType": "Postal",
91
                      "AddressName": "1 Queen St",
92
                      "StreetAddress": "1 Queen St",
93
                      "StreetAddress2": "RD 2",
94
                      "Suburb": "",
95
                      "City": "Pukekohe",
96
                      "Region": "Auckland",
97
                      "Country": "New Zealand",
98
                      "PostalCode": "0622"
99
                  },
100
                  {
101
                      "AddressType": "Physical",
102
                      "AddressName": "Main Warehouse",
103
                      "StreetAddress": "31 Hurstmere Road",
104
                      "StreetAddress2": "RD1",
105
                      "Suburb": "Takapuna",
106
                      "City": "Auckland",
107
                      "Region": "North Shore",
108
                      "Country": "New Zealand",
109
                      "PostalCode": "0622"
110
                  }
111
              ],
112
              "CustomerCode": "FRANCK",
113
              "CustomerName": "Franck & Co.",
114
              "GSTVATNumber": null,
115
              "BankName": null,
116
              "BankBranch": null,
117
              "BankAccount": null,
118
              "Website": null,
119
              "PhoneNumber": null,
120
              "FaxNumber": null,
121
              "MobileNumber": null,
122
              "DDINumber": null,
123
              "TollFreeNumber": null,
124
              "Email": null,
125
              "EmailCC": null,
126
              "Currency": {
127
                  "CurrencyCode": "USD",
128
                  "Description": "United States of America, Dollars",
129
                  "Guid": "3088672d-2a24-4d23-bb1c-c91813ed4c76",
130
                  "LastModifiedOn": "/Date(1458675959843)/"
131
              },
132
              "Notes": null,
133
              "Taxable": true,
134
              "XeroContactId": null,
135
              "SalesPerson": {
136
                  "FullName": "John Smith",
137
                  "Email": "[email protected]",
138
                  "Obsolete": false,
139
                  "Guid": "224ae802-88d3-43c1-b9cc-208d5d6d0ccf",
140
                  "LastModifiedOn": "/Date(1459218946890)/"
141
              },
142
              "DiscountRate": 0,
143
              "PrintPackingSlipInsteadOfInvoice": false,
144
              "PrintInvoice": false,
145
              "StopCredit": false,
146
              "Obsolete": false,
147
              "XeroSalesAccount": null,
148
              "XeroCostOfGoodsAccount": null,
149
              "SellPriceTier": "",
150
              "SellPriceTierReference": null,
151
              "CustomerType": "Cash",
152
              "PaymentTerm": "20th Month following",
153
              "ContactFirstName": null,
154
              "ContactLastName": null,
155
              "SourceId": null,
156
              "CreatedBy": "admin",
157
              "CreatedOn": "/Date(1411781315923)/",
158
              "Guid": "d32d0ca7-fd8e-4a99-9da6-be546ac5252b",
159
              "LastModifiedOn": "/Date(1459218740823)/"
160
            }
161
          ]
162
        }]';
163
}
164