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() |
|
|
|
|
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() |
|
|
|
|
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
|
|
|
|
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.