PackIntoManyTest   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 426
Duplicated Lines 20.42 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 13
dl 87
loc 426
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testPackIntoMany() 0 70 1
A testGetClient() 0 15 1
A testGetRegion() 0 14 1
A testCacheTtl() 0 8 1
A testSetCacheDriver() 0 10 1
A testGetEndpoint() 0 6 1
B testCorrectCachePackIntoManyRequest() 0 102 1
B testCorrectPackIntoManyRequest() 0 91 3
B testIncorrectPackIntoManyRequest() 29 29 1
B testWarningPackIntoManyRequest() 29 29 1
B testErrorPackIntoManyRequest() 29 29 1
A testQuery() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace BinPacking3d\Tests;
2
3
use BinPacking3d\PackIntoMany;
4
use BinPacking3d\Query;
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Handler\MockHandler;
7
use GuzzleHttp\HandlerStack;
8
use GuzzleHttp\Psr7\Response;
9
10
class PackIntoManyTest extends BinPackingTestBase
11
{
12
13
    public function testPackIntoMany()
14
    {
15
        // Build packing request
16
        $request = new \BinPacking3d\Entity\Request();
17
18
        $bin = new \BinPacking3d\Entity\Bin();
19
        $bin->setWidth(100);
20
        $bin->setHeight(120);
21
        $bin->setDepth(130);
22
        $bin->setMaxWeight(10);
23
        $bin->setOuterWidth(110);
24
        $bin->setOuterHeight(130);
25
        $bin->setOuterDepth(140);
26
        $bin->setWeight(0.1);
27
        $bin->setIdentifier('Test');
28
        $bin->setInternalIdentifier(1);
29
        $request->addBin($bin);
30
31
        // Item
32
        $item = new \BinPacking3d\Entity\Item();
33
        $item->setWidth(50);
34
        $item->setHeight(60);
35
        $item->setDepth(70);
36
        $item->setWeight(5);
37
        $item->setItemIdentifier('Test');
38
        $item->setProduct(['product_id' => 1]);
39
        $request->addItem($item);
40
41
        // Set extra info
42
        $request->setApiKey('API KEY');
43
        $request->setUsername('USERNAME');
44
45
        // Test request
46
        $this->assertInstanceOf('\BinPacking3d\Entity\Request', $request);
47
48
        // Build object
49
        $packIntoMany = new PackIntoMany($request);
50
        $this->assertInstanceOf('\BinPacking3d\PackIntoMany', $packIntoMany);
51
52
        // Test params
53
        $this->assertEquals(
54
            [
55
                'images_background_color' => '255,255,255',
56
                'images_bin_border_color' => '59,59,59',
57
                'images_bin_fill_color' => '230,230,230',
58
                'images_item_border_color' => '214,79,79',
59
                'images_item_fill_color' => '177,14,14',
60
                'images_item_back_border_color' => '215,103,103',
61
                'images_sbs_last_item_fill_color' => '99,93,93',
62
                'images_sbs_last_item_border_color' => '145,133,133',
63
                'images_width' => 250,
64
                'images_height' => 250,
65
                'images_source' => 'base64',
66
                'images_sbs' => 1,
67
                'stats' => 1,
68
                'item_coordinates' => 1,
69
                'images_complete' => 1,
70
                'images_separated' => 1,
71
            ], $packIntoMany->getParams()
72
        );
73
74
        // Test get logger without logger
75
        $this->assertInstanceOf('\Psr\Log\NullLogger', $packIntoMany->getLogger());
76
77
        // Test set logger with logger & result
78
        $result = $packIntoMany->setLogger(new \Psr\Log\NullLogger());
79
        $this->assertInstanceOf('\BinPacking3d\PackIntoMany', $result);
80
        $this->assertInstanceOf('\Psr\\Log\LoggerInterface', $packIntoMany->getLogger());
81
        $this->assertInstanceOf('\Psr\Log\NullLogger', $packIntoMany->getLogger());
82
    }
83
84
    public function testGetClient()
85
    {
86
        $request = new \BinPacking3d\Entity\Request();
87
        $packIntoMany = new PackIntoMany($request);
88
        $this->assertInstanceOf('\GuzzleHttp\Client', $packIntoMany->getClient());
89
90
        $client = new \GuzzleHttp\Client(
91
            [
92
                'base_uri' => 'http://test',
93
                'timeout' => 2.0,
94
            ]
95
        );
96
        $packIntoMany->setClient($client);
97
        $this->assertInstanceOf('\GuzzleHttp\Client', $packIntoMany->getClient());
98
    }
99
100
    public function testGetRegion()
101
    {
102
        $request = new \BinPacking3d\Entity\Request();
103
        $packIntoMany = new PackIntoMany($request, [], Query::REGION_EU);
104
        $this->assertEquals('EU', $packIntoMany->getRegion());
105
        $packIntoMany->setRegion(Query::REGION_US);
106
        $this->assertEquals('US', $packIntoMany->getRegion());
107
        $packIntoMany->setRegion(null);
108
        $this->assertNull($packIntoMany->getRegion());
109
110
        $url = $packIntoMany->getUrl(null);
111
        $this->assertEquals($url, $packIntoMany->getUrl(Query::REGION_GLOBAL));
112
        $this->assertEquals('https://us-east.api.3dbinpacking.com/packer/', $packIntoMany->getUrl(Query::REGION_US));
113
    }
114
115
    public function testCacheTtl()
116
    {
117
        $request = new \BinPacking3d\Entity\Request();
118
        $packIntoMany = new PackIntoMany($request, [], Query::REGION_US);
119
        $this->assertEquals(3600, $packIntoMany->getCacheTtl());
120
        $packIntoMany->setCacheTtl(8400);
121
        $this->assertEquals(8400, $packIntoMany->getCacheTtl());
122
    }
123
124
    public function testSetCacheDriver()
125
    {
126
        $cacheDriver = new \Doctrine\Common\Cache\ArrayCache();
127
        $request = new \BinPacking3d\Entity\Request();
128
        $packIntoMany = new PackIntoMany($request, [], Query::REGION_EU);
129
        $this->assertNull($packIntoMany->getCache());
130
        $packIntoMany->setCache($cacheDriver);
131
        $this->assertInstanceOf('\Doctrine\Common\Cache\Cache', $packIntoMany->getCache());
132
        $this->assertInstanceOf('\Doctrine\Common\Cache\ArrayCache', $packIntoMany->getCache());
133
    }
134
135
    public function testGetEndpoint()
136
    {
137
        $request = new \BinPacking3d\Entity\Request();
138
        $packIntoMany = new PackIntoMany($request, [], Query::REGION_US);
139
        $this->assertEquals('packIntoMany', $packIntoMany->getEndPoint());
140
    }
141
142
    public function testCorrectCachePackIntoManyRequest()
143
    {
144
        // Build a fake request
145
        // Build packing request
146
        $request = new \BinPacking3d\Entity\Request();
147
148
        $bin = new \BinPacking3d\Entity\Bin();
149
        $bin->setWidth(100);
150
        $bin->setHeight(120);
151
        $bin->setDepth(130);
152
        $bin->setMaxWeight(10);
153
        $bin->setOuterWidth(110);
154
        $bin->setOuterHeight(130);
155
        $bin->setOuterDepth(140);
156
        $bin->setWeight(0.1);
157
        $bin->setIdentifier('Test');
158
        $bin->setInternalIdentifier(1);
159
        $request->addBin($bin);
160
161
        // Item
162
        $item = new \BinPacking3d\Entity\Item();
163
        $item->setWidth(50);
164
        $item->setHeight(60);
165
        $item->setDepth(70);
166
        $item->setWeight(5);
167
        $item->setItemIdentifier('Test');
168
        $item->setProduct(['product_id' => 1]);
169
        $request->addItem($item);
170
171
        // Set extra info
172
        $request->setApiKey('API KEY');
173
        $request->setUsername('USERNAME');
174
175
        $packIntoMany = new PackIntoMany($request);
176
        $cacheDriver = new \Doctrine\Common\Cache\ArrayCache();
177
        $packIntoMany->setCache($cacheDriver);
178
        $this->assertInstanceOf('\BinPacking3d\PackIntoMany', $packIntoMany);
179
        $this->assertInstanceOf('\BinPacking3d\Query', $packIntoMany);
180
181
        // Build response mock stack
182
        $mock = new MockHandler(
183
            [
184
                new Response(
185
                    200, ['Content-Type' => 'application/json'],
186
                    file_get_contents($this->getFilePath('responses/PackIntoMany/correct.json'))
187
                ),
188
            ]
189
        );
190
        $handler = HandlerStack::create($mock);
191
        $client = new Client(['handler' => $handler]);
192
193
        // Get response
194
        $response = $client->get('/PackIntoMany');
195
196
        // Tests
197
        $this->assertEquals(200, $response->getStatusCode());
198
199
        // Get parsed JSON request
200
        $this->assertJsonStringEqualsJsonFile(
201
            $this->getFilePath('requests/PackIntoMany/request.json'),
202
            $packIntoMany->renderRequestJson()
203
        );
204
205
        // Get response and test it
206
        $response = $packIntoMany->handleResponse($response);
207
        $this->assertInstanceOf('\BinPacking3d\Entity\Packed', $response);
208
        $this->assertEquals(1, $response->count());
209
        $this->assertCount(1, $response->getBins());
210
        $this->assertInstanceOf('\BinPacking3d\Entity\Bin', $response->getBins()[0]);
211
212
        // Run it again to use cache
213
        // Build response mock stack
214
        $mock = new MockHandler(
215
            [
216
                new Response(
217
                    200, ['Content-Type' => 'application/json'],
218
                    file_get_contents($this->getFilePath('responses/PackIntoMany/correct.json'))
219
                ),
220
            ]
221
        );
222
        $handler = HandlerStack::create($mock);
223
        $client = new Client(['handler' => $handler]);
224
225
        // Get response
226
        $response = $client->get('/PackIntoMany');
227
228
        // Tests
229
        $this->assertEquals(200, $response->getStatusCode());
230
231
        // Get parsed JSON request
232
        $this->assertJsonStringEqualsJsonFile(
233
            $this->getFilePath('requests/PackIntoMany/request.json'),
234
            $packIntoMany->renderRequestJson()
235
        );
236
237
        // Get response and test it
238
        $response = $packIntoMany->handleResponse($response);
239
        $this->assertInstanceOf('\BinPacking3d\Entity\Packed', $response);
240
        $this->assertEquals(1, $response->count());
241
        $this->assertCount(1, $response->getBins());
242
        $this->assertInstanceOf('\BinPacking3d\Entity\Bin', $response->getBins()[0]);
243
    }
244
245
    public function testCorrectPackIntoManyRequest()
246
    {
247
        // Build a fake request
248
        // Build packing request
249
        $request = new \BinPacking3d\Entity\Request();
250
251
        $bin = new \BinPacking3d\Entity\Bin();
252
        $bin->setWidth(100);
253
        $bin->setHeight(120);
254
        $bin->setDepth(130);
255
        $bin->setMaxWeight(10);
256
        $bin->setOuterWidth(110);
257
        $bin->setOuterHeight(130);
258
        $bin->setOuterDepth(140);
259
        $bin->setWeight(0.1);
260
        $bin->setIdentifier('Test');
261
        $bin->setInternalIdentifier(1);
262
        $request->addBin($bin);
263
264
        // Item
265
        $item = new \BinPacking3d\Entity\Item();
266
        $item->setWidth(50);
267
        $item->setHeight(60);
268
        $item->setDepth(70);
269
        $item->setWeight(5);
270
        $item->setItemIdentifier('Test');
271
        $item->setProduct(['product_id' => 1]);
272
        $request->addItem($item);
273
274
        // Set extra info
275
        $request->setApiKey('API KEY');
276
        $request->setUsername('USERNAME');
277
278
        $packIntoMany = new PackIntoMany($request);
279
        $this->assertInstanceOf('\BinPacking3d\PackIntoMany', $packIntoMany);
280
        $this->assertInstanceOf('\BinPacking3d\Query', $packIntoMany);
281
282
        // Build response mock stack
283
        $mock = new MockHandler(
284
            [
285
                new Response(
286
                    200, ['Content-Type' => 'application/json'],
287
                    file_get_contents($this->getFilePath('responses/PackIntoMany/correct.json'))
288
                ),
289
            ]
290
        );
291
        $handler = HandlerStack::create($mock);
292
        $client = new Client(['handler' => $handler]);
293
294
        // Get response
295
        $response = $client->get('/PackIntoMany');
296
297
        // Tests
298
        $this->assertEquals(200, $response->getStatusCode());
299
300
        // Get parsed JSON request
301
        $this->assertJsonStringEqualsJsonFile(
302
            $this->getFilePath('requests/PackIntoMany/request.json'),
303
            $packIntoMany->renderRequestJson()
304
        );
305
306
        // Get response and test it
307
        $response = $packIntoMany->handleResponse($response);
308
        $this->assertInstanceOf('\BinPacking3d\Entity\Packed', $response);
309
        $this->assertEquals(1, $response->count());
310
        $this->assertCount(1, $response->getBins());
311
        $this->assertInstanceOf('\BinPacking3d\Entity\Bin', $response->getBins()[0]);
312
313
        $count = 0;
314
        foreach ($response->yieldBins() as $bin) {
315
            $count++;
316
        }
317
        $this->assertEquals(1, $count);
318
319
        // Test bin to get items etc
320
        foreach ($response->yieldBins() as $bin) {
321
            $items = $bin->getItems();
322
            $this->assertCount(1, $items);
323
            $this->assertInstanceOf('\BinPacking3d\Entity\Item', $items[0]);
324
            $this->assertEquals(
325
                file_get_contents($this->getFilePath('responses/PackIntoMany/img_content.txt')), $bin->getImage()
326
            );
327
            $this->assertEquals(25.974, $bin->getUsedSpace());
328
            $this->assertEquals(0.4, $bin->getUsedWeight());
329
330
            // Try to save image
331
            $tempFile = tempnam(sys_get_temp_dir(), 'img');
332
            $this->assertTrue($bin->saveImage($tempFile));
333
            unlink($tempFile);
334
        }
335
    }
336
337 View Code Duplication
    public function testIncorrectPackIntoManyRequest()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
338
    {
339
        // Build a fake request
340
        // Build packing request
341
        $request = new \BinPacking3d\Entity\Request();
342
343
        $packIntoMany = new PackIntoMany($request);
344
        $this->assertInstanceOf('\BinPacking3d\PackIntoMany', $packIntoMany);
345
        $this->assertInstanceOf('\BinPacking3d\Query', $packIntoMany);
346
347
        // Set expected exception
348
        $this->setExpectedException('\BinPacking3d\Exception\CriticalException');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
349
350
        // Build response mock stack
351
        $mock = new MockHandler(
352
            [
353
                new Response(
354
                    200, ['Content-Type' => 'application/json'],
355
                    file_get_contents($this->getFilePath('responses/PackIntoMany/error_critical.json'))
356
                ),
357
            ]
358
        );
359
        $handler = HandlerStack::create($mock);
360
        $client = new Client(['handler' => $handler]);
361
362
        // Get response
363
        $response = $client->get('/PackIntoMany');
364
        $packIntoMany->handleResponse($response);
365
    }
366
367 View Code Duplication
    public function testWarningPackIntoManyRequest()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
368
    {
369
        // Build a fake request
370
        // Build packing request
371
        $request = new \BinPacking3d\Entity\Request();
372
373
        $packIntoMany = new PackIntoMany($request);
374
        $this->assertInstanceOf('\BinPacking3d\PackIntoMany', $packIntoMany);
375
        $this->assertInstanceOf('\BinPacking3d\Query', $packIntoMany);
376
377
        // Set expected exception
378
        $this->setExpectedException('\BinPacking3d\Exception\WarningException');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
379
380
        // Build response mock stack
381
        $mock = new MockHandler(
382
            [
383
                new Response(
384
                    200, ['Content-Type' => 'application/json'],
385
                    file_get_contents($this->getFilePath('responses/PackIntoMany/error_warning.json'))
386
                ),
387
            ]
388
        );
389
        $handler = HandlerStack::create($mock);
390
        $client = new Client(['handler' => $handler]);
391
392
        // Get response
393
        $response = $client->get('/PackIntoMany');
394
        $packIntoMany->handleResponse($response);
395
    }
396
397 View Code Duplication
    public function testErrorPackIntoManyRequest()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
398
    {
399
        // Build a fake request
400
        // Build packing request
401
        $request = new \BinPacking3d\Entity\Request();
402
403
        $packIntoMany = new PackIntoMany($request);
404
        $this->assertInstanceOf('\BinPacking3d\PackIntoMany', $packIntoMany);
405
        $this->assertInstanceOf('\BinPacking3d\Query', $packIntoMany);
406
407
        // Set expected exception
408
        $this->setExpectedException('\Exception');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
409
410
        // Build response mock stack
411
        $mock = new MockHandler(
412
            [
413
                new Response(
414
                    200, ['Content-Type' => 'application/json'],
415
                    file_get_contents($this->getFilePath('responses/PackIntoMany/error_other.json'))
416
                ),
417
            ]
418
        );
419
        $handler = HandlerStack::create($mock);
420
        $client = new Client(['handler' => $handler]);
421
422
        // Get response
423
        $response = $client->get('/PackIntoMany');
424
        $packIntoMany->handleResponse($response);
425
    }
426
427
    public function testQuery()
428
    {
429
        $request = new \BinPacking3d\Entity\Request();
430
        $obj = new PackIntoMany($request);
431
        $this->assertInstanceOf('\BinPacking3d\PackIntoMany', $obj);
432
        $this->assertInstanceOf('\BinPacking3d\Query', $obj);
433
    }
434
435
}
436