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