Passed
Pull Request — master (#42)
by Viacheslav
02:53 queued 10s
created

RearrangeArrayTest::testReplacementChanges()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 94
Code Lines 85

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 85
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 94
rs 8.3272

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 Swaggest\JsonDiff\Tests;
4
5
6
use Swaggest\JsonDiff\JsonDiff;
7
8
class RearrangeArrayTest extends \PHPUnit_Framework_TestCase
9
{
10
    /**
11
     * @throws \Swaggest\JsonDiff\Exception
12
     */
13
    public function testRearrangeArray()
14
    {
15
        $oldJson = <<<'JSON'
16
[
17
    {
18
        "name": "warehouse_code",
19
        "in": "query",
20
        "type": "string",
21
        "required": false
22
    },
23
    {
24
        "name": "simple_skus",
25
        "in": "query",
26
        "type": "array",
27
        "items": {
28
            "type": "string"
29
        },
30
        "collectionFormat": "multi",
31
        "required": true
32
    },
33
    {
34
        "name": "seller_id",
35
        "in": "query",
36
        "type": "integer",
37
        "format": "int64",
38
        "required": false
39
    },
40
    {
41
        "name": "oms_code",
42
        "in": "query",
43
        "type": "string",
44
        "required": false
45
    }
46
]
47
JSON;
48
49
        $newJson = <<<'JSON'
50
[
51
    {
52
        "name": "warehouse_code",
53
        "in": "query",
54
        "type": "string64",
55
        "x-go-name": "WarehouseCode",
56
        "x-go-type": "string"
57
    },
58
    {
59
        "name": "oms_code",
60
        "in": "query",
61
        "type": "string",
62
        "x-go-name": "OmsCode",
63
        "x-go-type": "string"
64
    },
65
    {
66
        "name": "simple_skus",
67
        "in": "query",
68
        "type": "array",
69
        "required": true,
70
        "items": {
71
            "type": "string"
72
        },
73
        "collectionFormat": "multi",
74
        "x-go-name": "SimpleSKUs",
75
        "x-go-type": "[]string"
76
    },
77
    {
78
        "name": "seller_id",
79
        "in": "query",
80
        "type": "integer",
81
        "format": "int64",
82
        "x-go-name": "SellerID",
83
        "x-go-type": "uint64"
84
    }
85
]
86
JSON;
87
88
        $expectedJson = <<<'JSON'
89
[
90
    {
91
        "name": "warehouse_code",
92
        "in": "query",
93
        "type": "string64",
94
        "x-go-name": "WarehouseCode",
95
        "x-go-type": "string"
96
    },
97
    {
98
        "name": "simple_skus",
99
        "in": "query",
100
        "type": "array",
101
        "items": {
102
            "type": "string"
103
        },
104
        "collectionFormat": "multi",
105
        "required": true,
106
        "x-go-name": "SimpleSKUs",
107
        "x-go-type": "[]string"
108
    },
109
    {
110
        "name": "seller_id",
111
        "in": "query",
112
        "type": "integer",
113
        "format": "int64",
114
        "x-go-name": "SellerID",
115
        "x-go-type": "uint64"
116
    },
117
    {
118
        "name": "oms_code",
119
        "in": "query",
120
        "type": "string",
121
        "x-go-name": "OmsCode",
122
        "x-go-type": "string"
123
    }
124
]
125
JSON;
126
127
        $m = new JsonDiff(json_decode($oldJson), json_decode($newJson), JsonDiff::REARRANGE_ARRAYS);
128
        $this->assertSame($expectedJson, json_encode($m->getRearranged(), JSON_PRETTY_PRINT));
129
    }
130
131
    function testRearrangeKeepOriginal()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
132
    {
133
        $old = json_decode('[
134
          {
135
            "type": "string",
136
            "name": "qp1",
137
            "in": "query"
138
          },
139
          {
140
            "type": "string",
141
            "name": "qp",
142
            "in": "query"
143
          },
144
          {
145
            "name": "body",
146
            "in": "body",
147
            "schema": {
148
              "$ref": "#/definitions/UsecaseSampleInput"
149
            },
150
            "required": true
151
          }
152
        ]');
153
154
        $new = json_decode('[
155
          {
156
            "type": "string",
157
            "name": "qp1",
158
            "in": "query"
159
          },
160
          {
161
            "type": "string",
162
            "name": "qp2",
163
            "in": "query"
164
          },
165
          {
166
            "name": "body",
167
            "in": "body",
168
            "schema": {
169
              "$ref": "#/definitions/UsecaseSampleInput"
170
            },
171
            "required": true
172
          }
173
        ]');
174
175
        $m = new JsonDiff($old, $new, JsonDiff::REARRANGE_ARRAYS);
176
        $this->assertSame(
177
            json_encode($new, JSON_PRETTY_PRINT),
178
            json_encode($m->getRearranged(), JSON_PRETTY_PRINT)
179
        );
180
    }
181
182
    public function testEqualItems()
183
    {
184
        $diff = new \Swaggest\JsonDiff\JsonDiff(
185
            json_decode('{"data": [{"A": 1, "C": [1,2,3]},{"B": 2}]}'),
186
            json_decode('{"data": [{"B": 2},{"A": 1, "C": [3,2,1]}]}'),
187
            JsonDiff::REARRANGE_ARRAYS);
188
189
        $this->assertEmpty($diff->getDiffCnt());
190
    }
191
192
    public function testEqualItemsDiff()
193
    {
194
        $diff = new \Swaggest\JsonDiff\JsonDiff(
195
            json_decode('{"data": [{"A": 1, "C": [1,2,3,4]},{"B": 2}]}'),
196
            json_decode('{"data": [{"B": 2},{"A": 1, "C": [5,3,2,1]}]}'),
197
            JsonDiff::REARRANGE_ARRAYS);
198
199
        $this->assertJsonStringEqualsJsonString('[{"value":4,"op":"test","path":"/data/0/C/3"},{"value":5,"op":"replace","path":"/data/0/C/3"}]',
200
            json_encode($diff->getPatch(), JSON_UNESCAPED_SLASHES));
201
    }
202
203
    public function testExample()
204
    {
205
        $diff = new \Swaggest\JsonDiff\JsonDiff(
206
            json_decode('[{"name": "Alex", "height": 180},{"name": "Joe", "height": 179},{"name": "Jane", "height": 165}]'),
207
            json_decode('[{"name": "Joe", "height": 179},{"name": "Jane", "height": 168},{"name": "Alex", "height": 180}]'),
208
            JsonDiff::REARRANGE_ARRAYS);
209
210
        $this->assertJsonStringEqualsJsonString('[{"value":165,"op":"test","path":"/2/height"},{"value":168,"op":"replace","path":"/2/height"}]',
211
            json_encode($diff->getPatch(), JSON_UNESCAPED_SLASHES));
212
    }
213
214
    public function testReplacement()
215
    {
216
        $ex1 = json_decode(<<<'JSON'
217
{
218
  "attribute": {
219
    "name": ".UpwardPropagation - Prescriptions - Log-Ranges",
220
    "attribute": ".UpwardPropagation - Prescriptions - Log-Ranges",
221
    "dimension": ".UpwardPropagation - Prescriptions",
222
    "object": "Patients"
223
  },
224
  "selectedStates": [
225
    "]200,500]",
226
    "]500,1000]",
227
    "]20,50]",
228
    "]100,200]",
229
    "]5000,10000]",
230
    "]5,10]",
231
    "]1,2]",
232
    "]10,20]",
233
    "null",
234
    "]10000,oo[",
235
    "]2,5]",
236
    "]0,1]",
237
    "]1000,2000]",
238
    "]50,100]",
239
    "]2000,5000]"
240
  ]
241
}
242
JSON
243
        );
244
245
        $ex2 = json_decode(<<<'JSON'
246
{
247
  "attribute": {
248
    "name": ".UpwardPropagation - Prescriptions - Log-Ranges",
249
    "attribute": ".UpwardPropagation - Prescriptions - Log-Ranges",
250
    "dimension": ".UpwardPropagation - Prescriptions",
251
    "object": "Patients"
252
  },
253
  "selectedStates": [
254
    "]2000,5000]",
255
    "]2,5]",
256
    "]20,50]",
257
    "]1,2]",
258
    "]10000,oo[",
259
    "]200,500]",
260
    "]50,100]",
261
    "]500,1000]",
262
    "]5,10]",
263
    "]10,20]",
264
    "null",
265
    "]0,1]",
266
    "]1000,2000]",
267
    "]5000,10000]",
268
    "]100,200]"
269
  ]
270
}
271
JSON
272
        );
273
274
        $diff = new JsonDiff($ex1, $ex2, JsonDiff::REARRANGE_ARRAYS);
275
        $ex2r = $diff->getRearranged();
276
        $missingItems = [];
277
        foreach ($ex2->selectedStates as $i => $item) {
278
            if (!in_array($item, $ex2r->selectedStates)) {
279
                $missingItems[$i] = $item;
280
            }
281
        }
282
283
        $this->assertEmpty($missingItems, json_encode($ex2r, JSON_UNESCAPED_SLASHES));
284
        $this->assertEquals(
285
            json_encode($ex1, JSON_UNESCAPED_SLASHES+JSON_PRETTY_PRINT),
286
            json_encode($ex2r, JSON_UNESCAPED_SLASHES+JSON_PRETTY_PRINT)
287
        );
288
    }
289
290
    public function testReplacementChanges()
291
    {
292
        $ex1 = json_decode(<<<'JSON'
293
{
294
  "attribute": {
295
    "name": ".UpwardPropagation - Prescriptions - Log-Ranges",
296
    "attribute": ".UpwardPropagation - Prescriptions - Log-Ranges",
297
    "dimension": ".UpwardPropagation - Prescriptions",
298
    "object": "Patients"
299
  },
300
  "selectedStates": [
301
    "]200,500]",
302
    "]500,1000]",
303
    "]100,200]",
304
    "]5000,10000]",
305
    "]5,10]",
306
    "]1,2]",
307
    "]10,20]",
308
    "null",
309
    "]10000,oo[",
310
    "]2,5]",
311
    "]0,1]",
312
    "]1000,2000]",
313
    "]50,100]",
314
    "]2000,5000]"
315
  ]
316
}
317
JSON
318
        );
319
320
        $ex2 = json_decode(<<<'JSON'
321
{
322
  "attribute": {
323
    "name": ".UpwardPropagation - Prescriptions - Log-Ranges",
324
    "attribute": ".UpwardPropagation - Prescriptions - Log-Ranges",
325
    "dimension": ".UpwardPropagation - Prescriptions",
326
    "object": "Patients"
327
  },
328
  "selectedStates": [
329
    "]2000,5000]",
330
    "]2,5]",
331
    "]20,50]",
332
    "]1,2]",
333
    "]10000,oo[",
334
    "]200,500]",
335
    "]50,100]",
336
    "]500,1000]",
337
    "]5,10]",
338
    "]10,20]",
339
    "]0,1]",
340
    "]1000,2000]",
341
    "]5000,10000]",
342
    "]100,200]"
343
  ]
344
}
345
JSON
346
        );
347
348
        $diff = new JsonDiff($ex1, $ex2, JsonDiff::REARRANGE_ARRAYS);
349
        $ex2r = $diff->getRearranged();
350
        $missingItems = [];
351
        foreach ($ex2->selectedStates as $i => $item) {
352
            if (!in_array($item, $ex2r->selectedStates)) {
353
                $missingItems[$i] = $item;
354
            }
355
        }
356
357
        $this->assertEmpty($missingItems, json_encode($ex2r, JSON_UNESCAPED_SLASHES));
358
        $this->assertEquals(
359
            '{
360
    "attribute": {
361
        "name": ".UpwardPropagation - Prescriptions - Log-Ranges",
362
        "attribute": ".UpwardPropagation - Prescriptions - Log-Ranges",
363
        "dimension": ".UpwardPropagation - Prescriptions",
364
        "object": "Patients"
365
    },
366
    "selectedStates": [
367
        "]200,500]",
368
        "]500,1000]",
369
        "]100,200]",
370
        "]5000,10000]",
371
        "]5,10]",
372
        "]1,2]",
373
        "]10,20]",
374
        "]20,50]",
375
        "]10000,oo[",
376
        "]2,5]",
377
        "]0,1]",
378
        "]1000,2000]",
379
        "]50,100]",
380
        "]2000,5000]"
381
    ]
382
}',
383
            json_encode($ex2r, JSON_UNESCAPED_SLASHES+JSON_PRETTY_PRINT)
384
        );
385
    }
386
387
}
388