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\Unit; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Andrii Vasyliev <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class UnitTest extends \PHPUnit\Framework\TestCase |
19
|
|
|
{ |
20
|
|
|
protected function setUp() |
21
|
|
|
{ |
22
|
|
|
$this->bit = Unit::bit(); |
23
|
|
|
$this->byte = Unit::byte(); |
24
|
|
|
$this->kilo = Unit::kilobyte(); |
25
|
|
|
$this->mega = Unit::megabyte(); |
26
|
|
|
$this->giga = Unit::gigabyte(); |
27
|
|
|
$this->kb = Unit::kb(); |
28
|
|
|
$this->mb = Unit::mb(); |
29
|
|
|
$this->gb = Unit::gb(); |
30
|
|
|
|
31
|
|
|
$this->bps = Unit::bps(); |
32
|
|
|
$this->kbps = Unit::kbps(); |
33
|
|
|
$this->mbps = Unit::mbps(); |
34
|
|
|
$this->gbps = Unit::gbps(); |
35
|
|
|
|
36
|
|
|
$this->minute = Unit::minute(); |
37
|
|
|
$this->min = Unit::min(); |
38
|
|
|
$this->hour = Unit::hour(); |
39
|
|
|
$this->day = Unit::day(); |
40
|
|
|
$this->week = Unit::week(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testSame() |
44
|
|
|
{ |
45
|
|
|
$this->assertSame($this->byte, Unit::byte()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testEquals() |
49
|
|
|
{ |
50
|
|
|
$this->assertTrue($this->byte->equals(Unit::byte())); |
51
|
|
|
$this->assertTrue($this->kilo->equals(Unit::KB())); |
52
|
|
|
$this->assertTrue(Unit::KB()->equals($this->kilo)); |
53
|
|
|
$this->assertFalse($this->byte->equals($this->kilo)); |
54
|
|
|
$this->assertFalse($this->byte->equals(Unit::bit())); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testGetMeasure() |
58
|
|
|
{ |
59
|
|
|
$this->assertSame('bit', $this->byte->getMeasure()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testIsConvertible() |
63
|
|
|
{ |
64
|
|
|
$this->assertTrue($this->byte->isConvertible($this->kilo)); |
65
|
|
|
$this->assertFalse($this->byte->isConvertible(Unit::meter())); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testConvert() |
69
|
|
|
{ |
70
|
|
|
$this->assertConvert(1, $this->min, $this->minute); |
71
|
|
|
$this->assertConvert(60, $this->min, $this->hour); |
72
|
|
|
$this->assertConvert(24, $this->hour, $this->day); |
73
|
|
|
$this->assertConvert(7, $this->day, $this->week); |
74
|
|
|
|
75
|
|
|
$this->assertConvert(8, $this->bit, $this->byte); |
76
|
|
|
$this->assertConvert(1000, $this->byte, $this->kilo); |
77
|
|
|
$this->assertConvert(1000000, $this->byte, $this->mega); |
78
|
|
|
$this->assertConvert(1000000000, $this->byte, $this->giga); |
79
|
|
|
|
80
|
|
|
$this->assertConvert(1000, $this->byte, $this->kb); |
81
|
|
|
$this->assertConvert(1000000, $this->byte, $this->mb); |
82
|
|
|
$this->assertConvert(1000000000, $this->byte, $this->gb); |
83
|
|
|
|
84
|
|
|
$this->assertConvert(1000, $this->bps, $this->kbps); |
85
|
|
|
$this->assertConvert(1000000, $this->bps, $this->mbps); |
86
|
|
|
$this->assertConvert(1000000000, $this->bps, $this->gbps); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function assertConvert($factor, Unit $lesser, Unit $bigger) |
90
|
|
|
{ |
91
|
|
|
$this->assertSame(1, $lesser->convert($lesser, 1)); |
92
|
|
|
$this->assertSame(1, $bigger->convert($bigger, 1)); |
93
|
|
|
|
94
|
|
|
$this->assertSame(1, $lesser->convert($bigger, $factor)); |
95
|
|
|
$this->assertSame($factor, $bigger->convert($lesser, 1)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testConvertThroughRoot() |
99
|
|
|
{ |
100
|
|
|
$this->assertConvert(1440, $this->minute, $this->day); |
101
|
|
|
$this->assertConvert(1000, $this->mb, $this->gb); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testUnknownUnit() |
105
|
|
|
{ |
106
|
|
|
$unknown = Unit::create('unknown'); |
107
|
|
|
$this->assertInstanceOf(Unit::class, $unknown); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|