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
|
1 |
|
$float = FloatValue::fromNative('0'); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
public function testIsEmpty(): void |
58
|
|
|
{ |
59
|
1 |
|
$float = FloatValue::makeEmpty(); |
60
|
1 |
|
$this->assertTrue($float->isEmpty()); |
61
|
1 |
|
$float = FloatValue::fromNative(null); |
62
|
1 |
|
$this->assertTrue($float->isEmpty()); |
63
|
1 |
|
$float = FloatValue::fromNative(0); |
64
|
1 |
|
$this->assertFalse($float->isEmpty()); |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
public function testToString(): void |
68
|
|
|
{ |
69
|
|
|
$float = FloatValue::fromNative(self::FIXED_DEC); |
70
|
|
|
$this->assertEquals((string)self::FIXED_DEC, (string)$float); |
71
|
|
|
$float = FloatValue::fromNative(null); |
72
|
|
|
$this->assertEquals('null', (string)$float); |
73
|
|
|
$float = FloatValue::makeEmpty(); |
74
|
|
|
$this->assertEquals('null', (string)$float); |
75
|
|
|
$float = FloatValue::zero(); |
76
|
|
|
$this->assertEquals('0', (string)$float); |
77
|
|
|
$this->markTestIncomplete('This handling needs to be fixed.'); |
78
|
|
|
$float = FloatValue::fromNative(10.0); |
79
|
|
|
$this->assertEquals('10.0', (string)$float); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|