Completed
Push — master ( 0978bf...663ac6 )
by Andrii
01:58
created

ActionTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 14.81 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 8
loc 54
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 8 8 1
A createAction() 0 4 1
A tearDown() 0 3 1
A testCalculateCharge() 0 11 1
A testCalculateChargeNull() 0 6 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
 * 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\action\Action;
14
use hiqdev\php\billing\charge\Charge;
15
use hiqdev\php\billing\price\SinglePrice;
16
use hiqdev\php\billing\target\Target;
17
use hiqdev\php\billing\type\Type;
18
use hiqdev\php\units\Quantity;
19
use Money\Money;
20
21
/**
22
 * @author Andrii Vasyliev <[email protected]>
23
 */
24
class ActionTest extends \PHPUnit\Framework\TestCase
25
{
26
    /**
27
     * @var SinglePrice
28
     */
29
    protected $price;
30
31
    /**
32
     * @var Action
33
     */
34
    protected $action;
35
36
    /**
37
     * @var Money
38
     */
39
    protected $money;
40
41 View Code Duplication
    protected function setUp()
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...
42
    {
43
        $this->type     = new Type('server_traf');
44
        $this->target   = new Target('server', 2);
45
        $this->prepaid  = Quantity::gigabyte(10);
46
        $this->money    = Money::USD(15);
47
        $this->price    = new SinglePrice(5, $this->type, $this->target, null, $this->prepaid, $this->money);
48
    }
49
50
    protected function createAction($quantity)
51
    {
52
        return new Action(null, $this->type, $this->target, $quantity);
53
    }
54
55
    protected function tearDown()
56
    {
57
    }
58
59
    public function testCalculateCharge()
60
    {
61
        $action = $this->createAction($this->prepaid->multiply(2));
62
        $charge = $action->calculateCharge($this->price);
63
        $this->assertInstanceOf(Charge::class, $charge);
64
        $this->assertSame($action, $charge->getAction());
65
        $this->assertSame($this->target, $charge->getTarget());
66
        $this->assertSame($this->type, $charge->getPrice()->getType());
67
        $this->assertEquals($this->prepaid, $charge->getUsage());
68
        $this->assertEquals($this->money->multiply($this->prepaid->getQuantity()), $charge->getSum());
69
    }
70
71
    public function testCalculateChargeNull()
72
    {
73
        $action = $this->createAction($this->prepaid);
74
        $charge = $action->calculateCharge($this->price);
75
        $this->assertNull($charge);
76
    }
77
}
78