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, 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
|
|
|
/** |
22
|
|
|
* @var Quantity |
23
|
|
|
*/ |
24
|
|
|
protected $byte; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Quantity |
28
|
|
|
*/ |
29
|
|
|
protected $kilo; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Quantity |
33
|
|
|
*/ |
34
|
|
|
protected $mega; |
35
|
|
|
|
36
|
|
|
protected function setUp() |
37
|
|
|
{ |
38
|
|
|
$this->byte = Quantity::byte(1); |
39
|
|
|
$this->kilo = Quantity::kilobyte(1); |
40
|
|
|
$this->mega = Quantity::megabyte(1); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testGetUnit() |
44
|
|
|
{ |
45
|
|
|
$this->assertSame(Unit::byte(), $this->byte->getUnit()); |
46
|
|
|
$this->assertSame(Unit::kilobyte(), $this->kilo->getUnit()); |
47
|
|
|
$this->assertSame(Unit::megabyte(), $this->mega->getUnit()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testGetQuantity() |
51
|
|
|
{ |
52
|
|
|
$this->assertSame(1, $this->byte->getQuantity()); |
53
|
|
|
$this->assertSame(1, $this->kilo->getQuantity()); |
54
|
|
|
$this->assertSame(1, $this->mega->getQuantity()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testCompare() |
58
|
|
|
{ |
59
|
|
|
$this->assertSame(1, $this->kilo->compare($this->byte)); |
60
|
|
|
$this->assertSame(-1, $this->byte->compare($this->mega)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testEquals() |
64
|
|
|
{ |
65
|
|
|
$this->assertTrue($this->byte->equals(Quantity::byte(1))); |
66
|
|
|
$this->assertTrue($this->kilo->equals(Quantity::KB(1))); |
67
|
|
|
$this->assertTrue($this->kilo->equals(Quantity::byte(1000))); |
68
|
|
|
$this->assertTrue(Quantity::KB(1)->equals($this->kilo)); |
69
|
|
|
$this->assertFalse($this->byte->equals($this->kilo)); |
70
|
|
|
$this->assertFalse($this->byte->equals(Quantity::bit(1))); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testIsPositive() |
74
|
|
|
{ |
75
|
|
|
$this->assertTrue($this->kilo->isPositive()); |
76
|
|
|
$this->assertFalse(Quantity::byte(-1)->isPositive()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testIsNegative() |
80
|
|
|
{ |
81
|
|
|
$this->assertFalse($this->kilo->isNegative()); |
82
|
|
|
$this->assertTrue(Quantity::byte(-1)->isNegative()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testIsConvertible() |
86
|
|
|
{ |
87
|
|
|
$this->assertTrue($this->byte->isConvertible(Unit::KB())); |
88
|
|
|
$this->assertFalse($this->byte->isConvertible(Unit::meter())); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testConvert() |
92
|
|
|
{ |
93
|
|
|
$this->assertSame(1, $this->byte->convert(Unit::byte())->getQuantity()); |
94
|
|
|
$this->assertSame(1000, $this->kilo->convert(Unit::byte())->getQuantity()); |
95
|
|
|
$this->assertSame(1000000, $this->mega->convert(Unit::byte())->getQuantity()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testAdd() |
99
|
|
|
{ |
100
|
|
|
$this->assertSame(1001, $this->byte->add($this->kilo)->getQuantity()); |
101
|
|
|
$this->assertSame(1.001, $this->kilo->add($this->byte)->getQuantity()); |
102
|
|
|
$this->assertSame(1.000001, $this->mega->add($this->byte)->getQuantity()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testSubtract() |
106
|
|
|
{ |
107
|
|
|
$this->assertSame(-999, $this->byte->subtract($this->kilo)->getQuantity()); |
108
|
|
|
$this->assertSame(0.999, $this->kilo->subtract($this->byte)->getQuantity()); |
109
|
|
|
$this->assertSame(0.999999, $this->mega->subtract($this->byte)->getQuantity()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|