forked from GwendolenLynch/msgphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDomainIdentityHelperTest.php
More file actions
225 lines (184 loc) · 10.6 KB
/
DomainIdentityHelperTest.php
File metadata and controls
225 lines (184 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
declare(strict_types=1);
namespace MsgPhp\Domain\Tests;
use MsgPhp\Domain\{DomainIdInterface, DomainIdentityHelper, DomainIdentityMappingInterface};
use MsgPhp\Domain\Exception\InvalidClassException;
use MsgPhp\Domain\Tests\Fixtures\Entities;
use PHPUnit\Framework\TestCase;
final class DomainIdentityHelperTest extends TestCase
{
private $mapping;
protected function setUp(): void
{
$this->mapping = $this->createMock(DomainIdentityMappingInterface::class);
$this->mapping->expects(self::any())
->method('getIdentifierFieldNames')
->willReturnCallback(function ($class): array {
if (is_subclass_of($class, Entities\BaseTestEntity::class)) {
return $class::getIdFields();
}
throw InvalidClassException::create($class);
});
$this->mapping->expects(self::any())
->method('getIdentity')
->willReturnCallback(function ($object): array {
if ($object instanceof Entities\BaseTestEntity) {
return Entities\BaseTestEntity::getPrimaryIds($object);
}
throw InvalidClassException::create(\get_class($object));
});
}
public function testIsIdentifier(): void
{
$helper = new DomainIdentityHelper($this->mapping);
self::assertTrue($helper->isIdentifier($this->createMock(DomainIdInterface::class)));
self::assertTrue($helper->isIdentifier(Entities\TestEntity::create()));
self::assertTrue($helper->isIdentifier(Entities\TestCompositeEntity::create()));
self::assertFalse($helper->isIdentifier(new \stdClass()));
self::assertFalse($helper->isIdentifier(null));
self::assertFalse($helper->isIdentifier([]));
self::assertFalse($helper->isIdentifier(1));
self::assertFalse($helper->isIdentifier('foo'));
}
public function testIsEmptyIdentifier(): void
{
$helper = new DomainIdentityHelper($this->mapping);
$emptyId = $this->createMock(DomainIdInterface::class);
$emptyId->expects(self::any())
->method('isEmpty')
->willReturn(true);
$id = $this->createMock(DomainIdInterface::class);
$id->expects(self::any())
->method('isEmpty')
->willReturn(false);
self::assertTrue($helper->isEmptyIdentifier(null));
self::assertTrue($helper->isEmptyIdentifier($emptyId));
self::assertTrue($helper->isEmptyIdentifier(Entities\TestEntity::create()));
self::assertTrue($helper->isEmptyIdentifier(Entities\TestEntity::create(['id' => $emptyId, 'strField' => 'foo'])));
self::assertTrue($helper->isEmptyIdentifier(Entities\TestCompositeEntity::create(['idB' => 'foo'])));
self::assertFalse($helper->isEmptyIdentifier($id));
self::assertFalse($helper->isEmptyIdentifier(Entities\TestEntity::create(['id' => $id, 'strField' => 'foo'])));
self::assertFalse($helper->isEmptyIdentifier([]));
self::assertFalse($helper->isEmptyIdentifier(1));
self::assertFalse($helper->isEmptyIdentifier('foo'));
self::assertFalse($helper->isEmptyIdentifier(new \stdClass()));
}
public function testNormalizeIdentifier(): void
{
$helper = new DomainIdentityHelper($this->mapping);
$emptyId = $this->createMock(DomainIdInterface::class);
$emptyId->expects(self::any())
->method('isEmpty')
->willReturn(true);
$id = $this->createMock(DomainIdInterface::class);
$id->expects(self::any())
->method('isEmpty')
->willReturn(false);
$id->expects(self::any())
->method('toString')
->willReturn('id');
self::assertNull($helper->normalizeIdentifier(null));
self::assertNull($helper->normalizeIdentifier($emptyId));
self::assertNull($helper->normalizeIdentifier(Entities\TestEntity::create()));
self::assertNull($helper->normalizeIdentifier($entity = Entities\TestEntity::create(['id' => $emptyId, 'strField' => 'foo'])));
self::assertNull($helper->normalizeIdentifier(Entities\TestDerivedEntity::create(['entity' => $entity])));
self::assertSame('id', $helper->normalizeIdentifier($id));
self::assertSame('id', $helper->normalizeIdentifier(Entities\TestEntity::create(['id' => $id, 'strField' => 'foo'])));
self::assertSame(['idA' => 'id', 'idB' => null], $helper->normalizeIdentifier(Entities\TestCompositeEntity::create(['idA' => $id])));
self::assertSame(['idA' => 'id', 'idB' => 'id-b'], $helper->normalizeIdentifier(Entities\TestCompositeEntity::create(['idA' => $id, 'idB' => 'id-b'])));
self::assertSame(['entity' => null, 'id' => 0], $helper->normalizeIdentifier(Entities\TestDerivedCompositeEntity::create(['entity' => Entities\TestPrimitiveEntity::create([]), 'id' => 0])));
self::assertSame([], $helper->normalizeIdentifier([]));
self::assertSame(['id' => 1], $helper->normalizeIdentifier(['id' => 1]));
self::assertSame(1, $helper->normalizeIdentifier(1));
self::assertSame('foo', $helper->normalizeIdentifier('foo'));
self::assertSame($object = new \stdClass(), $helper->normalizeIdentifier($object));
}
public function testGetIdentifiers(): void
{
$helper = new DomainIdentityHelper($this->mapping);
self::assertSame([$id = $this->createMock(DomainIdInterface::class)], $helper->getIdentifiers($entity = Entities\TestEntity::create(['id' => $id])));
self::assertSame([$entity], $helper->getIdentifiers(Entities\TestDerivedEntity::create(['entity' => $entity])));
self::assertSame([null, 'bar'], $helper->getIdentifiers(Entities\TestCompositeEntity::create(['idB' => 'bar'])));
self::assertSame([null], $helper->getIdentifiers(Entities\TestPrimitiveEntity::create()));
}
public function testGetIdentifiersWithInvalidEntity(): void
{
$helper = new DomainIdentityHelper($this->mapping);
$this->expectException(InvalidClassException::class);
$helper->getIdentifiers(new \stdClass());
}
public function testGetIdentifierFieldNames(): void
{
$helper = new DomainIdentityHelper($this->mapping);
self::assertSame(['id'], $helper->getIdentifierFieldNames(Entities\TestPrimitiveEntity::class));
self::assertSame(['idA', 'idB'], $helper->getIdentifierFieldNames(Entities\TestCompositeEntity::class));
}
public function testGetIdentifierFieldNamesWithInvalidEntity(): void
{
$helper = new DomainIdentityHelper($this->mapping);
$this->expectException(InvalidClassException::class);
$helper->getIdentifierFieldNames(\stdClass::class);
}
public function testIsIdentity(): void
{
$helper = new DomainIdentityHelper($this->mapping);
$emptyId = $this->createMock(DomainIdInterface::class);
$emptyId->expects(self::any())
->method('isEmpty')
->willReturn(true);
$id = $this->createMock(DomainIdInterface::class);
$id->expects(self::any())
->method('isEmpty')
->willReturn(false);
self::assertTrue($helper->isIdentity(Entities\TestEntity::class, ['id' => $id]));
self::assertTrue($helper->isIdentity(Entities\TestEntity::class, $id));
self::assertTrue($helper->isIdentity(Entities\TestEntity::class, 'foo'));
self::assertTrue($helper->isIdentity(Entities\TestCompositeEntity::class, ['idA' => $id, 'idB' => 'b']));
self::assertTrue($helper->isIdentity(Entities\TestDerivedEntity::class, ['entity' => Entities\TestEntity::create(['id' => $id])]));
self::assertFalse($helper->isIdentity(Entities\TestEntity::class, null));
self::assertFalse($helper->isIdentity(Entities\TestEntity::class, []));
self::assertFalse($helper->isIdentity(Entities\TestEntity::class, ['id' => $emptyId]));
self::assertFalse($helper->isIdentity(Entities\TestEntity::class, $emptyId));
self::assertFalse($helper->isIdentity(Entities\TestCompositeEntity::class, ['idA' => $id]));
self::assertFalse($helper->isIdentity(Entities\TestCompositeEntity::class, ['idA' => $id, 'idB' => null]));
self::assertFalse($helper->isIdentity(Entities\TestCompositeEntity::class, ['idA' => $emptyId, 'idB' => 'foo']));
self::assertFalse($helper->isIdentity(Entities\TestCompositeEntity::class, ['idA' => $id, 'idB' => 'b', 'foo' => 'bar']));
self::assertFalse($helper->isIdentity(Entities\TestDerivedEntity::class, ['entity' => Entities\TestEntity::create(['id' => $emptyId])]));
}
public function testIsIdentityWithInvalidClass(): void
{
$helper = new DomainIdentityHelper($this->mapping);
$this->expectException(InvalidClassException::class);
$helper->isIdentity(\stdClass::class, ['id' => 1]);
}
public function testToIdentity(): void
{
$helper = new DomainIdentityHelper($this->mapping);
self::assertSame([], $helper->toIdentity(Entities\TestEntity::class, []));
self::assertSame([null], $helper->toIdentity(Entities\TestEntity::class, [null]));
self::assertSame([1], $helper->toIdentity(Entities\TestEntity::class, [1]));
self::assertSame(['foo' => 'bar', 'bar' => null], $helper->toIdentity(Entities\TestEntity::class, ['foo' => 'bar', 'bar' => null]));
self::assertSame(['id' => null], $helper->toIdentity(Entities\TestEntity::class, null));
self::assertSame(['id' => 'foo'], $helper->toIdentity(Entities\TestEntity::class, 'foo'));
self::assertSame(['idA' => 'foo'], $helper->toIdentity(Entities\TestCompositeEntity::class, 'foo'));
}
public function testToIdentityWithInvalidClass(): void
{
$helper = new DomainIdentityHelper($this->mapping);
$this->expectException(InvalidClassException::class);
$helper->toIdentity(\stdClass::class, 1);
}
public function testGetIdentity(): void
{
$helper = new DomainIdentityHelper($this->mapping);
self::assertSame(['id' => $id = $this->createMock(DomainIdInterface::class)], $helper->getIdentity(Entities\TestEntity::create(['id' => $id])));
self::assertSame(['idA' => null, 'idB' => 'bar'], $helper->getIdentity(Entities\TestCompositeEntity::create(['idB' => 'bar'])));
self::assertSame(['id' => null], $helper->getIdentity(Entities\TestPrimitiveEntity::create()));
}
public function testGetIdentityWithInvalidClass(): void
{
$helper = new DomainIdentityHelper($this->mapping);
$this->expectException(InvalidClassException::class);
$helper->getIdentity(new \stdClass());
}
}