DecimalCreateTest::testCreateFromDecimal()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
use Litipk\BigNumbers\Decimal as Decimal;
4
use Litipk\Exceptions\InvalidArgumentTypeException;
5
use PHPUnit\Framework\TestCase;
6
7
date_default_timezone_set('UTC');
8
9
class A {}  // Empty class used for testing
10
11
class DecimalCreateTest extends TestCase
12
{
13
    public function testCreateWithInvalidType()
14
    {
15
        $thrown = false;
16
        try {
17
            Decimal::create([25, 67]);
18
        } catch (\TypeError $e) {
19
            $thrown = true;
20
        }
21
        $this->assertTrue($thrown);
22
23
        $thrown = false;
24
        try {
25
            Decimal::create(new A());
26
        } catch (\TypeError $e) {
27
            $thrown = true;
28
        }
29
        $this->assertTrue($thrown);
30
    }
31
32
    public function testCreateFromInteger()
33
    {
34
        $this->assertTrue(Decimal::create(-35)->equals(Decimal::fromInteger(-35)));
35
        $this->assertTrue(Decimal::create(0)->equals(Decimal::fromInteger(0)));
36
        $this->assertTrue(Decimal::create(35)->equals(Decimal::fromInteger(35)));
37
    }
38
39
    public function testCreateFromFloat()
40
    {
41
        $this->assertTrue(Decimal::create(-35.125)->equals(Decimal::fromFloat(-35.125)));
42
        $this->assertTrue(Decimal::create(0.0)->equals(Decimal::fromFloat(0.0)));
43
        $this->assertTrue(Decimal::create(35.125)->equals(Decimal::fromFloat(35.125)));
44
    }
45
46
    public function testCreateFromString()
47
    {
48
        $this->assertTrue(Decimal::create('-35.125')->equals(Decimal::fromString('-35.125')));
49
        $this->assertTrue(Decimal::create('0.0')->equals(Decimal::fromString('0.0')));
50
        $this->assertTrue(Decimal::create('35.125')->equals(Decimal::fromString('35.125')));
51
    }
52
53
    public function testCreateFromDecimal()
54
    {
55
        $this->assertTrue(Decimal::create(Decimal::fromString('345.76'), 1)->equals(Decimal::fromString('345.8')));
56
        $this->assertTrue(Decimal::create(Decimal::fromString('345.76'), 2)->equals(Decimal::fromString('345.76')));
57
    }
58
}
59