Completed
Push — master ( d02bdd...0a5fc3 )
by Andrii
02:11
created

PriceFactoryTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 51.61 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 4
dl 32
loc 62
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 15 1
A testEnumPrice() 16 16 1
A testSinglePrice() 16 16 1
A createDto() 0 9 2

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
 * PHP Billing Library
4
 *
5
 * @link      https://github.com/hiqdev/php-billing
6
 * @package   php-billing
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\tests\unit;
12
13
use hiqdev\php\billing\price\PriceCreationDto;
14
use hiqdev\php\billing\price\PriceFactory;
15
use hiqdev\php\billing\price\EnumPrice;
16
use hiqdev\php\billing\price\SinglePrice;
17
use hiqdev\php\billing\target\Target;
18
use hiqdev\php\billing\type\Type;
19
use hiqdev\php\units\Quantity;
20
use hiqdev\php\units\Unit;
21
use Money\Money;
22
23
/**
24
 * @author Andrii Vasyliev <[email protected]>
25
 */
26
class PriceFactoryTest extends \PHPUnit\Framework\TestCase
27
{
28
    protected function setUp()
29
    {
30
        $this->id       = 'foo:bar';
31
        $this->single   = new Type('server_traf');
32
        $this->enum     = new Type('certificate_purchase');
33
        $this->target   = new Target('server', 1);
34
        $this->prepaid  = Quantity::gigabyte(10);
35
        $this->price    = Money::USD(15);
36
        $this->unit     = Unit::gigabyte();
37
        $this->prices   = [];
38
        $this->factory  = new PriceFactory([
39
            $this->single->getName()    => SinglePrice::class,
40
            $this->enum->getName()      => EnumPrice::class,
41
        ]);
42
    }
43
44 View Code Duplication
    public function testEnumPrice()
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...
45
    {
46
        $price = $this->factory->create($this->createDto([
47
            'id'        => $this->id,
48
            'type'      => $this->enum,
49
            'target'    => $this->target,
50
            'unit'      => $this->unit,
51
            'prices'    => $this->prices,
52
        ]));
53
        $this->assertInstanceOf(EnumPrice::class, $price);
54
        $this->assertSame($this->id,        $price->getId());
55
        $this->assertSame($this->enum,      $price->getType());
56
        $this->assertSame($this->target,    $price->getTarget());
57
        $this->assertSame($this->unit,      $price->getUnit());
58
        $this->assertSame($this->prices,    $price->getPrices());
59
    }
60
61 View Code Duplication
    public function testSinglePrice()
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...
62
    {
63
        $price = $this->factory->create($this->createDto([
64
            'id'        => $this->id,
65
            'type'      => $this->single,
66
            'target'    => $this->target,
67
            'prepaid'   => $this->prepaid,
68
            'price'     => $this->price,
69
        ]));
70
        $this->assertInstanceOf(SinglePrice::class, $price);
71
        $this->assertSame($this->id,        $price->getId());
72
        $this->assertSame($this->single,    $price->getType());
73
        $this->assertSame($this->target,    $price->getTarget());
74
        $this->assertSame($this->prepaid,   $price->getPrepaid());
75
        $this->assertSame($this->price,     $price->getPrice());
76
    }
77
78
    public function createDto(array $data)
79
    {
80
        $dto = new PriceCreationDto;
81
        foreach ($data as $key => $value) {
82
            $dto->{$key} = $value;
83
        }
84
85
        return $dto;
86
    }
87
}
88