UnleashedBulkLoaderUpdateRecordsTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 72
dl 0
loc 114
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 17 1
A testUpdate() 0 74 2
1
<?php
2
3
namespace AntonyThorpe\SilverShopUnleashed\Tests;
4
5
use AntonyThorpe\SilverShopUnleashed\BulkLoader\ProductBulkLoader;
6
use AntonyThorpe\SilverShopUnleashed\Defaults;
7
use SilverShop\Model\Order;
0 ignored issues
show
Bug introduced by
The type SilverShop\Model\Order was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use SilverShop\Page\Product;
0 ignored issues
show
Bug introduced by
The type SilverShop\Page\Product was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use SilverShop\Page\ProductCategory;
0 ignored issues
show
Bug introduced by
The type SilverShop\Page\ProductCategory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use SilverShop\Tests\ShopTest;
0 ignored issues
show
Bug introduced by
The type SilverShop\Tests\ShopTest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use SilverStripe\Dev\SapphireTest;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Dev\SapphireTest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
class UnleashedBulkLoaderUpdateRecordsTest extends SapphireTest
14
{
15
    protected static $fixture_file = [
16
        'vendor/silvershop/core/tests/php/Fixtures/ShopMembers.yml',
17
        'fixtures/models.yml'
18
    ];
19
20
    private object $mp3player;
21
    private object $socks;
22
23
    public function setUp(): void
24
    {
25
        Defaults::config()->send_sales_orders_to_unleashed = false;
26
        parent::setUp();
27
        ShopTest::setConfiguration(); //reset config
28
29
        $this->mp3player = $this->objFromFixture(Product::class, 'mp3player');
30
        $this->socks = $this->objFromFixture(Product::class, 'socks');
31
32
        //publish some product categories and products
33
        $this->objFromFixture(ProductCategory::class, 'products')->copyVersionToStage('Stage', 'Live');
34
        $this->objFromFixture(ProductCategory::class, 'clothing')->copyVersionToStage('Stage', 'Live');
35
        $this->objFromFixture(ProductCategory::class, 'clearance')->copyVersionToStage('Stage', 'Live');
36
        $this->objFromFixture(ProductCategory::class, 'musicplayers')->copyVersionToStage('Stage', 'Live');
37
        $this->objFromFixture(ProductCategory::class, 'electronics')->copyVersionToStage('Stage', 'Live');
38
        $this->mp3player->copyVersionToStage('Stage', 'Live');
39
        $this->socks->copyVersionToStage('Stage', 'Live');
40
    }
41
42
    public function testUpdate(): void
43
    {
44
        $apidata = (array) json_decode($this->jsondata, true);
45
        $apidata = reset($apidata);
46
        $loader = ProductBulkLoader::create(Product::class);
47
        $loader->transforms = [
48
            'Parent' => [
49
                '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

49
                '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...
50
                    $obj = ProductCategory::get()->find('Guid', $value);
51
                    if ($obj) {
52
                        return $obj;
53
                    } else {
54
                        return ProductCategory::get()->find('Title', $value);
55
                    }
56
                }
57
            ]
58
        ];
59
        $results = $loader->updateRecords($apidata['Items']);
60
61
        // Check Results
62
        $this->assertEquals($results->CreatedCount(), 0, 'zero created');
63
        $this->assertEquals($results->UpdatedCount(), 2, 'two updated');
64
        $this->assertEquals($results->DeletedCount(), 0, 'zero deleted');
65
        $this->assertEquals($results->SkippedCount(), 0, 'zero skipped');
66
        $this->assertEquals($results->Count(), 2);
67
68
        // Check Dataobjects
69
        $product_category_clothing = ProductCategory::get()->find('Title', 'Clothing');
70
        $product_category_electronics = ProductCategory::get()->find('Title', 'Electronics');
71
        $socks = Product::get()->find('InternalItemID', 'IIID1');
72
        $mp3player = Product::get()->find('InternalItemID', 'IIID5');
73
74
        // Check Parent
75
        $this->assertSame(
76
            $product_category_clothing->Title,
77
            $socks->Parent()->Title,
78
            'Socks should have a parent page with title of Clothing'
79
        );
80
        $this->assertSame(
81
            $product_category_electronics->Title,
82
            $mp3player->Parent()->Title,
83
            'MP3 Player should have a parent page with title of Electronics as it was changed from Music Players'
84
        );
85
86
        $this->assertEquals(
87
            false,
88
            $mp3player->AllowPurchase,
89
            'AllowPurchase of the MP3 player set to false'
90
        );
91
92
        $this->assertSame(
93
            'Socks Updated',
94
            $socks->Title,
95
            'Title is set to Socks Updated'
96
        );
97
        $this->assertSame(
98
            1.0,
99
            $socks->Width,
100
            'Width of socks updated to 1'
101
        );
102
        $this->assertSame(
103
            2.0,
104
            $socks->Height,
105
            'Height of socks updated to 2'
106
        );
107
        $this->assertSame(
108
            3.0,
109
            $socks->Depth,
110
            'Depth of socks updated to 3'
111
        );
112
        $this->assertSame(
113
            8.50,
114
            $socks->getPrice(),
115
            'Socks BasePrice updated to 8.50'
116
        );
117
    }
118
119
120
    /**
121
     * JSON data for test
122
     *
123
     * @link (Unleashed Software API Documentation, https://apidocs.unleashedsoftware.com/Products)
124
     * @var string
125
     */
126
    protected $jsondata = '[
127
      {
128
          "Pagination": {
129
              "NumberOfItems": 14,
130
              "PageSize": 200,
131
              "PageNumber": 1,
132
              "NumberOfPages": 1
133
          },
134
          "Items": [
135
              {
136
                  "ProductCode": "IIID1",
137
                  "ProductDescription": "Socks Updated",
138
                  "Barcode": null,
139
                  "PackSize": null,
140
                  "Width": 1,
141
                  "Height": 2,
142
                  "Depth": 3,
143
                  "Weight": 0.1,
144
                  "MinStockAlertLevel": null,
145
                  "MaxStockAlertLevel": null,
146
                  "ReOrderPoint": null,
147
                  "UnitOfMeasure": {
148
                      "Guid": "2b9e913b-f815-440f-83ac-3639e3f04d64",
149
                      "Name": "EA"
150
                  },
151
                  "NeverDiminishing": false,
152
                  "LastCost": 47.5,
153
                  "DefaultPurchasePrice": 47.5,
154
                  "DefaultSellPrice": 8.50,
155
                  "AverageLandPrice": 40.4294,
156
                  "Obsolete": false,
157
                  "Notes": null,
158
                  "SellPriceTier1": {
159
                      "Name": "Sell Price Tier 1",
160
                      "Value": null
161
                  },
162
                  "SellPriceTier2": {
163
                      "Name": "Sell Price Tier 2",
164
                      "Value": null
165
                  },
166
                  "SellPriceTier3": {
167
                      "Name": "Sell Price Tier 3",
168
                      "Value": null
169
                  },
170
                  "SellPriceTier4": {
171
                      "Name": "Sell Price Tier 4",
172
                      "Value": null
173
                  },
174
                  "SellPriceTier5": {
175
                      "Name": "Sell Price Tier 5",
176
                      "Value": null
177
                  },
178
                  "SellPriceTier6": {
179
                      "Name": "Sell Price Tier 6",
180
                      "Value": null
181
                  },
182
                  "SellPriceTier7": {
183
                      "Name": "Sell Price Tier 7",
184
                      "Value": null
185
                  },
186
                  "SellPriceTier8": {
187
                      "Name": "Sell Price Tier 8",
188
                      "Value": null
189
                  },
190
                  "SellPriceTier9": {
191
                      "Name": "Sell Price Tier 9",
192
                      "Value": null
193
                  },
194
                  "SellPriceTier10": {
195
                      "Name": "Sell Price Tier 10",
196
                      "Value": null
197
                  },
198
                  "XeroTaxCode": null,
199
                  "XeroTaxRate": null,
200
                  "TaxablePurchase": true,
201
                  "TaxableSales": true,
202
                  "XeroSalesTaxCode": null,
203
                  "XeroSalesTaxRate": null,
204
                  "IsComponent": false,
205
                  "IsAssembledProduct": false,
206
                  "CanAutoAssemble": false,
207
                  "ProductGroup": {
208
                      "GroupName": "Clothing",
209
                      "Guid": "G112",
210
                      "LastModifiedOn": "2015-11-21T08:07:20"
211
                  },
212
                  "XeroSalesAccount": null,
213
                  "XeroCostOfGoodsAccount": null,
214
                  "BinLocation": null,
215
                  "Supplier": null,
216
                  "SourceId": null,
217
                  "CreatedBy": "admin",
218
                  "SourceVariantParentId": null,
219
                  "Guid": "G11",
220
                  "LastModifiedOn": "2015-11-21T08:07:20"
221
              },
222
              {
223
                  "ProductCode": "IIID5",
224
                  "ProductDescription": "Mp3 Player Updated",
225
                  "Barcode": null,
226
                  "PackSize": null,
227
                  "Width": 0.4,
228
                  "Height": 1,
229
                  "Depth": 1,
230
                  "Weight": 1,
231
                  "MinStockAlertLevel": 5,
232
                  "MaxStockAlertLevel": 100,
233
                  "ReOrderPoint": null,
234
                  "UnitOfMeasure": {
235
                      "Guid": "2b9e913b-f815-440f-83ac-3639e3f04d64",
236
                      "Name": "EA"
237
                  },
238
                  "NeverDiminishing": false,
239
                  "LastCost": 12.8529,
240
                  "DefaultPurchasePrice": 200,
241
                  "DefaultSellPrice": 24.95,
242
                  "AverageLandPrice": 12.4539,
243
                  "Obsolete": true,
244
                  "Notes": null,
245
                  "SellPriceTier1": {
246
                      "Name": "Sell Price Tier 1",
247
                      "Value": null
248
                  },
249
                  "SellPriceTier2": {
250
                      "Name": "Sell Price Tier 2",
251
                      "Value": null
252
                  },
253
                  "SellPriceTier3": {
254
                      "Name": "Sell Price Tier 3",
255
                      "Value": null
256
                  },
257
                  "SellPriceTier4": {
258
                      "Name": "Sell Price Tier 4",
259
                      "Value": null
260
                  },
261
                  "SellPriceTier5": {
262
                      "Name": "Sell Price Tier 5",
263
                      "Value": null
264
                  },
265
                  "SellPriceTier6": {
266
                      "Name": "Sell Price Tier 6",
267
                      "Value": null
268
                  },
269
                  "SellPriceTier7": {
270
                      "Name": "Sell Price Tier 7",
271
                      "Value": null
272
                  },
273
                  "SellPriceTier8": {
274
                      "Name": "Sell Price Tier 8",
275
                      "Value": null
276
                  },
277
                  "SellPriceTier9": {
278
                      "Name": "Sell Price Tier 9",
279
                      "Value": null
280
                  },
281
                  "SellPriceTier10": {
282
                      "Name": "Sell Price Tier 10",
283
                      "Value": null
284
                  },
285
                  "XeroTaxCode": null,
286
                  "XeroTaxRate": null,
287
                  "TaxablePurchase": true,
288
                  "TaxableSales": true,
289
                  "XeroSalesTaxCode": null,
290
                  "XeroSalesTaxRate": null,
291
                  "IsComponent": false,
292
                  "IsAssembledProduct": false,
293
                  "CanAutoAssemble": false,
294
                  "ProductGroup": {
295
                      "GroupName": "Electronics",
296
                      "Guid": "G113",
297
                      "LastModifiedOn": "2015-11-21T08:07:20"
298
                  },
299
                  "XeroSalesAccount": null,
300
                  "XeroCostOfGoodsAccount": null,
301
                  "BinLocation": null,
302
                  "Supplier": null,
303
                  "SourceId": null,
304
                  "CreatedBy": "admin",
305
                  "SourceVariantParentId": null,
306
                  "Guid": "G15",
307
                  "LastModifiedOn": "2015-11-21T08:07:20"
308
              }
309
          ]
310
      }
311
    ]';
312
}
313