|
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
|
|
|
|