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\Natural; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
|
15
|
|
|
final class NaturalTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
private const FIXED_NUM = 23; |
18
|
|
|
|
19
|
|
|
private Natural $natural; |
20
|
|
|
|
21
|
1 |
|
public function testToNative(): void |
22
|
|
|
{ |
23
|
1 |
|
$this->assertEquals(self::FIXED_NUM, $this->natural->toNative()); |
24
|
1 |
|
$this->assertNull(Natural::fromNative(null)->toNative()); |
25
|
1 |
|
} |
26
|
|
|
|
27
|
1 |
|
public function testFromNative(): void |
28
|
|
|
{ |
29
|
1 |
|
$this->assertNull(Natural::fromNative(null)->toNative()); |
30
|
1 |
|
$this->assertNull(Natural::fromNative('')->toNative()); |
31
|
1 |
|
$this->assertSame(0, Natural::fromNative('0')->toNative()); |
32
|
1 |
|
$this->expectException(InvalidArgumentException::class); |
33
|
1 |
|
Natural::fromNative(-1); |
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
public function testMakeEmpty(): void |
37
|
|
|
{ |
38
|
1 |
|
$empty = Natural::makeEmpty(); |
39
|
1 |
|
$this->assertNull($empty->toNative()); |
40
|
1 |
|
$this->assertSame('', (string)$empty); |
41
|
1 |
|
$this->assertTrue($empty->isEmpty()); |
42
|
1 |
|
} |
43
|
|
|
|
44
|
1 |
|
public function testZero(): void |
45
|
|
|
{ |
46
|
1 |
|
$zero = Natural::zero(); |
47
|
1 |
|
$this->assertSame(0, $zero->toNative()); |
48
|
1 |
|
$this->assertSame('0', (string)$zero); |
49
|
1 |
|
$this->assertTrue($zero->isZero()); |
50
|
1 |
|
} |
51
|
|
|
|
52
|
1 |
|
public function testEquals(): void |
53
|
|
|
{ |
54
|
1 |
|
$sameNumber = Natural::fromNative(self::FIXED_NUM); |
55
|
1 |
|
$this->assertTrue($this->natural->equals($sameNumber)); |
56
|
1 |
|
$differentNumber = Natural::fromNative(42); |
57
|
1 |
|
$this->assertFalse($this->natural->equals($differentNumber)); |
58
|
1 |
|
} |
59
|
|
|
|
60
|
1 |
|
public function testAdd(): void |
61
|
|
|
{ |
62
|
1 |
|
$amount = Natural::fromNative(10); |
63
|
1 |
|
$this->assertEquals(33, $this->natural->add($amount)->toNative()); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
1 |
|
public function testAddWithEmpty(): void |
67
|
|
|
{ |
68
|
1 |
|
$amount = Natural::fromNative(10); |
69
|
1 |
|
$this->expectException(InvalidArgumentException::class); |
70
|
1 |
|
$amount->add(Natural::makeEmpty()); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
public function testAddOnEmpty(): void |
74
|
|
|
{ |
75
|
1 |
|
$amount = Natural::fromNative(10); |
76
|
1 |
|
$this->expectException(InvalidArgumentException::class); |
77
|
1 |
|
Natural::makeEmpty()->add($amount); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
public function testSubtract(): void |
81
|
|
|
{ |
82
|
1 |
|
$amount = Natural::fromNative(10); |
83
|
1 |
|
$this->assertEquals(13, $this->natural->subtract($amount)->toNative()); |
84
|
1 |
|
} |
85
|
|
|
|
86
|
1 |
|
public function testSubtractWithEmpty(): void |
87
|
|
|
{ |
88
|
1 |
|
$amount = Natural::fromNative(10); |
89
|
1 |
|
$this->expectException(InvalidArgumentException::class); |
90
|
1 |
|
$amount->subtract(Natural::makeEmpty()); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
public function testSubtractOnEmpty(): void |
94
|
|
|
{ |
95
|
1 |
|
$amount = Natural::fromNative(10); |
96
|
1 |
|
$this->expectException(InvalidArgumentException::class); |
97
|
1 |
|
Natural::makeEmpty()->subtract($amount); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
public function testSubtractToInvalid(): void |
101
|
|
|
{ |
102
|
1 |
|
$this->expectException(InvalidArgumentException::class); |
103
|
1 |
|
Natural::zero()->subtract(Natural::fromNative(1)); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
public function testToString(): void |
107
|
|
|
{ |
108
|
1 |
|
$this->assertEquals((string)self::FIXED_NUM, (string)$this->natural); |
109
|
1 |
|
$this->assertSame('', (string)Natural::makeEmpty()); |
110
|
1 |
|
} |
111
|
|
|
|
112
|
13 |
|
protected function setUp(): void |
113
|
|
|
{ |
114
|
13 |
|
$this->natural = Natural::fromNative(self::FIXED_NUM); |
115
|
13 |
|
} |
116
|
|
|
} |
117
|
|
|
|