1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/entity 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\Entity; |
10
|
|
|
|
11
|
|
|
use Daikon\Entity\EntityDiff; |
12
|
|
|
use Daikon\Interop\InvalidArgumentException; |
13
|
|
|
use Daikon\Tests\Entity\Fixture\Article; |
14
|
|
|
use Daikon\Tests\Entity\Fixture\Location; |
15
|
|
|
use Daikon\Tests\Entity\Fixture\Paragraph; |
16
|
|
|
use Daikon\ValueObject\Text; |
17
|
|
|
use PHPUnit\Framework\TestCase; |
18
|
|
|
|
19
|
|
|
class EntityTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
private const FIXED_UUID = '941b4e51-e524-4e5d-8c17-1ef96585abc3'; |
22
|
|
|
|
23
|
|
|
private const FIXTURE = [ |
24
|
|
|
'@type' => Article::class, |
25
|
|
|
'id' => '525b4e51-e524-4e5d-8c17-1ef96585cbd3', |
26
|
|
|
'created' => '2017-04-02T23:42:05.000000+00:00', |
27
|
|
|
'title' => 'hello world!', |
28
|
|
|
'url' => 'http://www.example.com/', |
29
|
|
|
'feedbackMail' => '[email protected]', |
30
|
|
|
'averageVoting' => 23.42, |
31
|
|
|
'workshopDate' => '2017-05-23', |
32
|
|
|
'workshopCancelled' => true, |
33
|
|
|
'workshopLocation' => [ |
34
|
|
|
'@type' => Location::class, |
35
|
|
|
'id' => 42, |
36
|
|
|
'coords' => [ 'lat' => 52.5119, 'lon' => 13.3084 ] |
37
|
|
|
], |
38
|
|
|
'paragraphs' => [[ |
39
|
|
|
'@type' => Paragraph::class, |
40
|
|
|
'id' => 23, |
41
|
|
|
'kicker' => 'this is the kicker baby!', |
42
|
|
|
'content' => 'hell yeah!' |
43
|
|
|
]] |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
private Article $entity; |
47
|
|
|
|
48
|
1 |
|
public function testGet(): void |
49
|
|
|
{ |
50
|
1 |
|
$this->assertEquals(self::FIXTURE['id'], $this->entity->getIdentity()->toNative()); |
51
|
1 |
|
$this->assertEquals(self::FIXTURE['title'], $this->entity->getTitle()->toNative()); |
52
|
1 |
|
} |
53
|
|
|
|
54
|
1 |
|
public function testHas(): void |
55
|
|
|
{ |
56
|
1 |
|
$this->assertTrue($this->entity->has('id')); |
57
|
1 |
|
$this->assertTrue($this->entity->has('title')); |
58
|
1 |
|
$this->assertTrue($this->entity->has('paragraphs')); |
59
|
1 |
|
$article = $this->entity::fromNative(['id' => '941b4e51-e524-4e5d-8c17-1ef96585abc3']); |
60
|
1 |
|
$this->assertFalse($article->has('title')); |
61
|
1 |
|
} |
62
|
|
|
|
63
|
1 |
|
public function testWith(): void |
64
|
|
|
{ |
65
|
1 |
|
$article = $this->entity->withValue('id', self::FIXED_UUID); |
66
|
1 |
|
$this->assertEquals(self::FIXTURE['id'], $this->entity->getId()->toNative()); |
67
|
1 |
|
$this->assertEquals(self::FIXED_UUID, $article->getId()->toNative()); |
68
|
1 |
|
} |
69
|
|
|
|
70
|
1 |
|
public function testDiff(): void |
71
|
|
|
{ |
72
|
1 |
|
$article = Article::fromNative([ |
73
|
1 |
|
'id' => self::FIXED_UUID, |
74
|
1 |
|
'title' => 'Hello world!', |
75
|
1 |
|
'url' => 'http://metallica.com', |
76
|
|
|
]); |
77
|
|
|
$diffData = [ |
78
|
1 |
|
'title' => 'This is different', |
79
|
|
|
'url' => 'http://tv.lol', |
80
|
|
|
]; |
81
|
1 |
|
$calculatedDiff = (new EntityDiff)($article->withValues($diffData), $article); |
82
|
1 |
|
$this->assertEquals($diffData, $calculatedDiff->toNative()); |
83
|
1 |
|
} |
84
|
|
|
|
85
|
1 |
|
public function testIsSameAs(): void |
86
|
|
|
{ |
87
|
1 |
|
$articleTwo = Article::fromNative(['id' => self::FIXTURE['id'], 'title' => 'Hello world!']); |
88
|
|
|
// considered same, due to identifier |
89
|
1 |
|
$this->assertTrue($this->entity->isSameAs($articleTwo)); |
90
|
|
|
|
91
|
1 |
|
$this->expectException(InvalidArgumentException::class); |
92
|
|
|
/** @psalm-suppress InvalidArgument */ |
93
|
1 |
|
$this->entity->isSameAs(Location::fromNative([])); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
public function testGetPath(): void |
97
|
|
|
{ |
98
|
|
|
/** @var Text $value */ |
99
|
1 |
|
$value = $this->entity->get('paragraphs.0-kicker'); |
100
|
1 |
|
$this->assertEquals(self::FIXTURE['paragraphs'][0]['kicker'], $value->toNative()); |
101
|
1 |
|
} |
102
|
|
|
|
103
|
1 |
|
public function testToNative(): void |
104
|
|
|
{ |
105
|
1 |
|
$this->assertEquals(self::FIXTURE, $this->entity->toNative()); |
106
|
1 |
|
} |
107
|
|
|
|
108
|
1 |
|
public function testInvalidValue(): void |
109
|
|
|
{ |
110
|
1 |
|
$this->expectException(InvalidArgumentException::class); |
111
|
1 |
|
Article::fromNative(['id' => self::FIXED_UUID, 'title' => [123]]); |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
public function testInvalidHas(): void |
115
|
|
|
{ |
116
|
1 |
|
$this->expectException(InvalidArgumentException::class); |
117
|
1 |
|
$article = Article::fromNative(['id' => self::FIXED_UUID]); |
118
|
1 |
|
$article->has('foobar'); |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
public function testInvalidPath(): void |
122
|
|
|
{ |
123
|
1 |
|
$this->expectException(InvalidArgumentException::class); |
124
|
1 |
|
$article = Article::fromNative(['id' => self::FIXED_UUID]); |
125
|
1 |
|
$article->get('foo.0'); |
126
|
|
|
} |
127
|
|
|
|
128
|
10 |
|
protected function setUp(): void |
129
|
|
|
{ |
130
|
10 |
|
$this->entity = Article::fromNative(self::FIXTURE); |
131
|
10 |
|
} |
132
|
|
|
} |
133
|
|
|
|