Completed
Push — master ( 719ee7...6f1833 )
by Andrii
02:23
created

UnitTest::assertConvert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 3
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
28
        $this->bps  = Unit::bps();
29
        $this->kbps = Unit::kbps();
30
        $this->mbps = Unit::mbps();
31
        $this->gbps = Unit::gbps();
32
33
        $this->minute = Unit::minute();
34
        $this->min  = Unit::min();
35
        $this->hour = Unit::hour();
36
        $this->day  = Unit::day();
37
        $this->week = Unit::week();
38
    }
39
40
    public function testSame()
41
    {
42
        $this->assertSame($this->byte, Unit::byte());
43
    }
44
45
    public function testEquals()
46
    {
47
        $this->assertTrue($this->byte->equals(Unit::byte()));
48
        $this->assertTrue($this->kilo->equals(Unit::KB()));
49
        $this->assertTrue(Unit::KB()->equals($this->kilo));
50
        $this->assertFalse($this->byte->equals($this->kilo));
51
        $this->assertFalse($this->byte->equals(Unit::bit()));
52
    }
53
54
    public function testGetMeasure()
55
    {
56
        $this->assertSame('bit', $this->byte->getMeasure());
57
    }
58
59
    public function testIsConvertible()
60
    {
61
        $this->assertTrue($this->byte->isConvertible($this->kilo));
62
        $this->assertFalse($this->byte->isConvertible(Unit::meter()));
63
    }
64
65
    public function testConvert()
66
    {
67
        $this->assertConvert(1, $this->min, $this->minute);
68
        $this->assertConvert(60, $this->min, $this->hour);
69
        $this->assertConvert(24, $this->hour, $this->day);
70
        $this->assertConvert(7, $this->day, $this->week);
71
72
        $this->assertConvert(8, $this->bit, $this->byte);
73
        $this->assertConvert(1000, $this->byte, $this->kilo);
74
        $this->assertConvert(1000000, $this->byte, $this->mega);
75
        $this->assertConvert(1000000000, $this->byte, $this->giga);
76
77
        $this->assertConvert(1000, $this->bps, $this->kbps);
78
        $this->assertConvert(1000000, $this->bps, $this->mbps);
79
        $this->assertConvert(1000000000, $this->bps, $this->gbps);
80
    }
81
82
    protected function assertConvert($factor, $lesser, $bigger)
83
    {
84
        $this->assertSame(1, $lesser->convert($lesser, 1));
85
        $this->assertSame(1, $bigger->convert($bigger, 1));
86
87
        $this->assertSame(1, $lesser->convert($bigger, $factor));
88
        $this->assertSame($factor, $bigger->convert($lesser, 1));
89
    }
90
}
91