Completed
Push — master ( b9f967...7a222d )
by Andreu
9s
created

DecimalCreateTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 5
Bugs 2 Features 1
Metric Value
wmc 7
c 5
b 2
f 1
lcom 1
cbo 3
dl 0
loc 51
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testCreateWithInvalidType() 0 18 3
A testCreateFromInteger() 0 6 1
A testCreateFromFloat() 0 9 1
A testCreateFromString() 0 6 1
A testCreateFromDecimal() 0 5 1
1
<?php
2
3
use Litipk\BigNumbers\Decimal as Decimal;
4
use Litipk\Exceptions\InvalidArgumentTypeException;
5
6
7
date_default_timezone_set('UTC');
8
9
10
class A {}  // Empty class used for testing
11
12
13
class DecimalCreateTest extends PHPUnit_Framework_TestCase
14
{
15
    public function testCreateWithInvalidType()
16
    {
17
        $thrown = false;
18
        try {
19
            Decimal::create([25, 67]);
20
        } catch (InvalidArgumentTypeException $e) {
21
            $thrown = true;
22
        }
23
        $this->assertTrue($thrown);
24
25
        $thrown = false;
26
        try {
27
            Decimal::create(new A());
28
        } catch (InvalidArgumentTypeException $e) {
29
            $thrown = true;
30
        }
31
        $this->assertTrue($thrown);
32
    }
33
34
    public function testCreateFromInteger()
35
    {
36
        $this->assertTrue(Decimal::create(-35)->equals(Decimal::fromInteger(-35)));
37
        $this->assertTrue(Decimal::create(0)->equals(Decimal::fromInteger(0)));
38
        $this->assertTrue(Decimal::create(35)->equals(Decimal::fromInteger(35)));
39
    }
40
41
    public function testCreateFromFloat()
42
    {
43
        $this->assertTrue(Decimal::create(-35.125)->equals(Decimal::fromFloat(-35.125)));
44
        $this->assertTrue(Decimal::create(0.0)->equals(Decimal::fromFloat(0.0)));
45
        $this->assertTrue(Decimal::create(35.125)->equals(Decimal::fromFloat(35.125)));
46
47
        $this->assertTrue(Decimal::create(INF)->equals(Decimal::getPositiveInfinite()));
48
        $this->assertTrue(Decimal::create(-INF)->equals(Decimal::getNegativeInfinite()));
49
    }
50
51
    public function testCreateFromString()
52
    {
53
        $this->assertTrue(Decimal::create('-35.125')->equals(Decimal::fromString('-35.125')));
54
        $this->assertTrue(Decimal::create('0.0')->equals(Decimal::fromString('0.0')));
55
        $this->assertTrue(Decimal::create('35.125')->equals(Decimal::fromString('35.125')));
56
    }
57
58
    public function testCreateFromDecimal()
59
    {
60
        $this->assertTrue(Decimal::create(Decimal::fromString('345.76'), 1)->equals(Decimal::fromString('345.8')));
61
        $this->assertTrue(Decimal::create(Decimal::fromString('345.76'), 2)->equals(Decimal::fromString('345.76')));
62
    }
63
}
64