UnleashedBulkLoaderUpdateRecordsTest::testUpdate()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 62
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 42
nc 1
nop 0
dl 0
loc 62
rs 9.248
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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

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