Completed
Push — master ( 9b22ab...6029c9 )
by Andrii
01:55
created

QuantityTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 115
Duplicated Lines 12.17 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 14
loc 115
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A testGetUnit() 0 6 1
A testGetQuantity() 0 6 1
A testCompare() 0 5 1
A testEquals() 0 10 1
A testIsPositive() 0 5 1
A testIsNegative() 0 5 1
A testIsConvertible() 0 6 1
A testConvert() 0 13 3
A testAdd() 7 7 1
A testSubtract() 7 7 1
A testMultiply() 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 Units of Measure Library
4
 *
5
 * @link      https://github.com/hiqdev/php-units
6
 * @package   php-units
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\units\tests;
12
13
use hiqdev\php\units\Quantity;
14
use hiqdev\php\units\Unit;
15
16
/**
17
 * @author Andrii Vasyliev <[email protected]>
18
 */
19
class QuantityTest extends \PHPUnit\Framework\TestCase
20
{
21
    /** @var Quantity */
22
    protected $some;
23
24
    /**
25
     * @var Quantity
26
     */
27
    protected $byte;
28
29
    /**
30
     * @var Quantity
31
     */
32
    protected $kilo;
33
34
    /**
35
     * @var Quantity
36
     */
37
    protected $mega;
38
39
    protected function setUp()
40
    {
41
        $this->some = Quantity::some(1);
42
        $this->byte = Quantity::byte(1);
43
        $this->kilo = Quantity::kilobyte(1);
44
        $this->mega = Quantity::megabyte(1);
45
    }
46
47
    public function testGetUnit()
48
    {
49
        $this->assertSame(Unit::byte(), $this->byte->getUnit());
50
        $this->assertSame(Unit::kilobyte(), $this->kilo->getUnit());
51
        $this->assertSame(Unit::megabyte(), $this->mega->getUnit());
52
    }
53
54
    public function testGetQuantity()
55
    {
56
        $this->assertSame(1, $this->byte->getQuantity());
57
        $this->assertSame(1, $this->kilo->getQuantity());
58
        $this->assertSame(1, $this->mega->getQuantity());
59
    }
60
61
    public function testCompare()
62
    {
63
        $this->assertSame(1, $this->kilo->compare($this->byte));
64
        $this->assertSame(-1, $this->byte->compare($this->mega));
65
    }
66
67
    public function testEquals()
68
    {
69
        $this->assertTrue($this->some->equals($this->some));
70
        $this->assertTrue($this->byte->equals(Quantity::byte(1)));
71
        $this->assertTrue($this->kilo->equals(Quantity::KB(1)));
72
        $this->assertTrue($this->kilo->equals(Quantity::byte(1000)));
73
        $this->assertTrue(Quantity::KB(1)->equals($this->kilo));
74
        $this->assertFalse($this->byte->equals($this->kilo));
75
        $this->assertFalse($this->byte->equals(Quantity::bit(1)));
76
    }
77
78
    public function testIsPositive()
79
    {
80
        $this->assertTrue($this->kilo->isPositive());
81
        $this->assertFalse(Quantity::byte(-1)->isPositive());
82
    }
83
84
    public function testIsNegative()
85
    {
86
        $this->assertFalse($this->kilo->isNegative());
87
        $this->assertTrue(Quantity::byte(-1)->isNegative());
88
    }
89
90
    public function testIsConvertible()
91
    {
92
        $this->assertTrue($this->some->isConvertible(Unit::some()));
93
        $this->assertTrue($this->byte->isConvertible(Unit::KB()));
94
        $this->assertFalse($this->byte->isConvertible(Unit::meter()));
95
    }
96
97
    public function testConvert()
98
    {
99
        $this->assertSame(1, $this->some->convert(Unit::some())->getQuantity());
100
        $this->assertSame(1, $this->byte->convert(Unit::byte())->getQuantity());
101
        $this->assertSame(1000, $this->kilo->convert(Unit::byte())->getQuantity());
102
        $this->assertSame(1000000, $this->mega->convert(Unit::byte())->getQuantity());
103
        foreach ([$this->byte, $this->kilo, $this->mega] as $value) {
104
            $this->assertTrue($value->convert(Unit::byte())->getUnit()->equals(Unit::byte()));
105
        }
106
        foreach ([Unit::byte(), Unit::kilobyte(), Unit::megabyte()] as $unit) {
107
            $this->assertTrue($this->byte->convert($unit)->getUnit()->equals($unit));
108
        }
109
    }
110
111 View Code Duplication
    public function testAdd()
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...
112
    {
113
        $this->assertSame(2, $this->some->add($this->some)->getQuantity());
114
        $this->assertSame(1001, $this->byte->add($this->kilo)->getQuantity());
115
        $this->assertSame(1.001, $this->kilo->add($this->byte)->getQuantity());
116
        $this->assertSame(1.000001, $this->mega->add($this->byte)->getQuantity());
117
    }
118
119 View Code Duplication
    public function testSubtract()
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...
120
    {
121
        $this->assertSame(0, $this->some->subtract($this->some)->getQuantity());
122
        $this->assertSame(-999, $this->byte->subtract($this->kilo)->getQuantity());
123
        $this->assertSame(0.999, $this->kilo->subtract($this->byte)->getQuantity());
124
        $this->assertSame(0.999999, $this->mega->subtract($this->byte)->getQuantity());
125
    }
126
127
    public function testMultiply()
128
    {
129
        $this->assertSame(123, $this->some->multiply(123)->getQuantity());
130
        $this->assertSame(-42, $this->byte->multiply(-42)->getQuantity());
131
        $this->assertSame(1.3, $this->mega->multiply(1.3)->getQuantity());
132
    }
133
}
134