|
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 testIsConvertible() |
|
74
|
|
|
{ |
|
75
|
|
|
$this->assertTrue($this->byte->isConvertible(Unit::KB())); |
|
76
|
|
|
$this->assertFalse($this->byte->isConvertible(Unit::meter())); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testConvert() |
|
80
|
|
|
{ |
|
81
|
|
|
$this->assertSame(1, $this->byte->convert(Unit::byte())->getQuantity()); |
|
82
|
|
|
$this->assertSame(1000, $this->kilo->convert(Unit::byte())->getQuantity()); |
|
83
|
|
|
$this->assertSame(1000000, $this->mega->convert(Unit::byte())->getQuantity()); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|