UnitTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 1
dl 0
loc 97
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 25 1
A testSame() 0 6 1
A testEquals() 0 8 1
A testGetMeasure() 0 4 1
A testIsConvertible() 0 5 1
A testConvert() 0 20 1
A assertConvert() 0 8 1
A testConvertThroughRoot() 0 5 1
A testUnknownUnit() 0 5 1
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
        $this->usd  = Unit::usd();
43
        $this->eur  = Unit::eur();
44
    }
45
46
    public function testSame()
47
    {
48
        $this->assertSame($this->byte, Unit::byte());
49
        $this->assertSame($this->usd, Unit::usd());
50
        $this->assertSame($this->eur, Unit::eur());
51
    }
52
53
    public function testEquals()
54
    {
55
        $this->assertTrue($this->byte->equals(Unit::byte()));
56
        $this->assertTrue($this->kilo->equals(Unit::KB()));
57
        $this->assertTrue(Unit::KB()->equals($this->kilo));
58
        $this->assertFalse($this->byte->equals($this->kilo));
59
        $this->assertFalse($this->byte->equals(Unit::bit()));
60
    }
61
62
    public function testGetMeasure()
63
    {
64
        $this->assertSame('bit', $this->byte->getMeasure());
65
    }
66
67
    public function testIsConvertible()
68
    {
69
        $this->assertTrue($this->byte->isConvertible($this->kilo));
70
        $this->assertFalse($this->byte->isConvertible(Unit::meter()));
71
    }
72
73
    public function testConvert()
74
    {
75
        $this->assertConvert(1, $this->min, $this->minute);
76
        $this->assertConvert(60, $this->min, $this->hour);
77
        $this->assertConvert(24, $this->hour, $this->day);
78
        $this->assertConvert(7, $this->day, $this->week);
79
80
        $this->assertConvert(8, $this->bit, $this->byte);
81
        $this->assertConvert(1000, $this->byte, $this->kilo);
82
        $this->assertConvert(1000000, $this->byte, $this->mega);
83
        $this->assertConvert(1000000000, $this->byte, $this->giga);
84
85
        $this->assertConvert(1000, $this->byte, $this->kb);
86
        $this->assertConvert(1000000, $this->byte, $this->mb);
87
        $this->assertConvert(1000000000, $this->byte, $this->gb);
88
89
        $this->assertConvert(1000, $this->bps, $this->kbps);
90
        $this->assertConvert(1000000, $this->bps, $this->mbps);
91
        $this->assertConvert(1000000000, $this->bps, $this->gbps);
92
    }
93
94
    protected function assertConvert($factor, Unit $lesser, Unit $bigger)
95
    {
96
        $this->assertSame(1, $lesser->convert($lesser, 1));
97
        $this->assertSame(1, $bigger->convert($bigger, 1));
98
99
        $this->assertSame(1, $lesser->convert($bigger, $factor));
100
        $this->assertSame($factor, $bigger->convert($lesser, 1));
101
    }
102
103
    public function testConvertThroughRoot()
104
    {
105
        $this->assertConvert(1440, $this->minute, $this->day);
106
        $this->assertConvert(1000, $this->mb, $this->gb);
107
    }
108
109
    public function testUnknownUnit()
110
    {
111
        $unknown = Unit::create('unknown');
112
        $this->assertInstanceOf(Unit::class, $unknown);
113
    }
114
}
115