|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the daikon-cqrs/value-object project. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Daikon\Tests\ValueObject; |
|
10
|
|
|
|
|
11
|
|
|
use Daikon\Interop\InvalidArgumentException; |
|
12
|
|
|
use Daikon\ValueObject\FloatValue; |
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
|
14
|
|
|
|
|
15
|
|
|
final class FloatValueTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
private const FIXED_DEC = 2.3; |
|
18
|
|
|
|
|
19
|
1 |
|
public function testToNative(): void |
|
20
|
|
|
{ |
|
21
|
1 |
|
$float = FloatValue::fromNative(self::FIXED_DEC); |
|
22
|
1 |
|
$this->assertSame(self::FIXED_DEC, $float->toNative()); |
|
23
|
1 |
|
$float = FloatValue::fromNative(null); |
|
24
|
1 |
|
$this->assertNull($float->toNative()); |
|
25
|
1 |
|
$float = FloatValue::makeEmpty(); |
|
26
|
1 |
|
$this->assertNull($float->toNative()); |
|
27
|
1 |
|
$float = FloatValue::zero(); |
|
28
|
1 |
|
$this->assertSame(0.0, $float->toNative()); |
|
29
|
1 |
|
$float = FloatValue::fromNative(1); |
|
30
|
1 |
|
$this->assertSame(1.0, $float->toNative()); |
|
31
|
1 |
|
} |
|
32
|
|
|
|
|
33
|
1 |
|
public function testEquals(): void |
|
34
|
|
|
{ |
|
35
|
1 |
|
$float = FloatValue::fromNative(self::FIXED_DEC); |
|
36
|
1 |
|
$sameNumber = FloatValue::fromNative(self::FIXED_DEC); |
|
37
|
1 |
|
$differentNumber = FloatValue::fromNative(4.2); |
|
38
|
1 |
|
$this->assertTrue($float->equals($sameNumber)); |
|
39
|
1 |
|
$this->assertFalse($float->equals($differentNumber)); |
|
40
|
1 |
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
public function testIsZero(): void |
|
43
|
|
|
{ |
|
44
|
1 |
|
$float = FloatValue::fromNative(self::FIXED_DEC); |
|
45
|
1 |
|
$this->assertFalse($float->isZero()); |
|
46
|
1 |
|
$float = FloatValue::zero(); |
|
47
|
1 |
|
$this->assertTrue($float->isZero()); |
|
48
|
1 |
|
$float = FloatValue::fromNative(0); |
|
49
|
1 |
|
$this->assertTrue($float->isZero()); |
|
50
|
1 |
|
$this->assertTrue($float->isZero()); |
|
51
|
1 |
|
$float = FloatValue::fromNative(0.0); |
|
52
|
1 |
|
$this->assertTrue($float->isZero()); |
|
53
|
1 |
|
$this->expectException(InvalidArgumentException::class); |
|
54
|
|
|
/** @psalm-suppress InvalidScalarArgument */ |
|
55
|
1 |
|
$float = FloatValue::fromNative('0'); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
public function testIsEmpty(): void |
|
59
|
|
|
{ |
|
60
|
1 |
|
$float = FloatValue::makeEmpty(); |
|
61
|
1 |
|
$this->assertTrue($float->isEmpty()); |
|
62
|
1 |
|
$float = FloatValue::fromNative(null); |
|
63
|
1 |
|
$this->assertTrue($float->isEmpty()); |
|
64
|
1 |
|
$float = FloatValue::fromNative(0); |
|
65
|
1 |
|
$this->assertFalse($float->isEmpty()); |
|
66
|
1 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testToString(): void |
|
69
|
|
|
{ |
|
70
|
|
|
$float = FloatValue::fromNative(self::FIXED_DEC); |
|
71
|
|
|
$this->assertEquals((string)self::FIXED_DEC, (string)$float); |
|
72
|
|
|
$float = FloatValue::fromNative(null); |
|
73
|
|
|
$this->assertSame('', (string)$float); |
|
74
|
|
|
$float = FloatValue::makeEmpty(); |
|
75
|
|
|
$this->assertSame('', (string)$float); |
|
76
|
|
|
$float = FloatValue::zero(); |
|
77
|
|
|
$this->assertSame('0', (string)$float); |
|
78
|
|
|
|
|
79
|
|
|
$this->markTestIncomplete('This handling needs to be fixed.'); |
|
80
|
|
|
$float = FloatValue::fromNative(10.0); |
|
81
|
|
|
$this->assertEquals('10.0', (string)$float); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
public function testFormat(): void |
|
85
|
|
|
{ |
|
86
|
1 |
|
$float = FloatValue::fromNative(self::FIXED_DEC); |
|
87
|
1 |
|
$this->assertSame('2.3', $float->format(1)); |
|
88
|
1 |
|
$this->assertSame('2.300', $float->format(3)); |
|
89
|
1 |
|
$this->assertSame('2,30', $float->format(2, ',')); |
|
90
|
1 |
|
$this->assertSame('2', $float->format(0, ',')); |
|
91
|
1 |
|
$largeFloat = FloatValue::fromNative(11111.00123); |
|
92
|
1 |
|
$this->assertSame('11,111.0', $largeFloat->format(1)); |
|
93
|
1 |
|
$this->assertSame('11,111.001', $largeFloat->format(3)); |
|
94
|
1 |
|
$this->assertSame('11,111,00', $largeFloat->format(2, ',')); |
|
95
|
1 |
|
$this->assertSame('11.111,0', $largeFloat->format(1, ',', '.')); |
|
96
|
1 |
|
$this->assertSame('11111001', $largeFloat->format(3, '', '')); |
|
97
|
1 |
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|