Passed
Push — master ( 9ff768...b736eb )
by Laurens
02:33
created

DiscountCollectionTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 38
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 4
dl 38
loc 38
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B discountCollection() 25 25 1
A getDiscountWithUuid() 8 8 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
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\IzettleApi\Tests\Unit\Api\Prozduct;
6
7
use LauLamanApps\IzettleApi\API\ImageCollection;
8
use LauLamanApps\IzettleApi\API\Product\Discount;
9
use LauLamanApps\IzettleApi\API\Product\DiscountCollection;
10
use PHPUnit\Framework\TestCase;
11
12
/** * @small */
13 View Code Duplication
final class DiscountCollectionTest extends TestCase
14
{
15
    /** @test */
16
    public function discountCollection()
17
    {
18
        $discount1 = $this->getDiscountWithUuid();
19
        $discount2 = $this->getDiscountWithUuid();
20
        $discount3 = $this->getDiscountWithUuid();
21
22
        $discountCollection = new DiscountCollection([$discount1, $discount2, $discount3]);
23
        $discountCollection->add($discount3);// add discount 3 again it should only end up once in the collection
24
25
        //-- Check if collection contains all 3 discounts
26
        $collection = $discountCollection->getAll();
27
        self::assertEquals(3, count($collection));
28
        self::assertEquals($discount1, $collection[(string) $discount1->getUuid()]);
29
        self::assertEquals($discount2, $collection[(string) $discount2->getUuid()]);
30
        self::assertEquals($discount3, $collection[(string) $discount3->getUuid()]);
31
        
32
        $discountCollection->remove($discount2);
33
34
        //-- Check if collection does not contains discount 2 but does contain the others
35
        $collection = $discountCollection->getAll();
36
        self::assertEquals(2, count($collection));
37
        self::assertEquals($discount1, $collection[(string) $discount1->getUuid()]);
38
        self::assertEquals($discount3, $discountCollection->get($discount3->getUuid()));
39
        self::assertFalse(array_key_exists((string) $discount2->getUuid(), $collection));
40
    }
41
42
    private function getDiscountWithUuid(): Discount
43
    {
44
        return Discount::new(
45
            'name',
46
            'description',
47
            new ImageCollection()
48
        );
49
    }
50
}
51