/
ImmutableRecordLogicTest.php
261 lines (213 loc) · 7.32 KB
/
ImmutableRecordLogicTest.php
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/**
* This file is part of event-engine/php-data.
* (c) 2018-2020 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace EventEngineTest\Data;
use EventEngineTest\Data\Stub\ImmutableItem;
use EventEngineTest\Data\Stub\ImmutableRecordWithNoTypes;
use EventEngineTest\Data\Stub\ImmutableRecordWithTypedGetters;
use EventEngineTest\Data\Stub\RecordWithStringList;
use EventEngineTest\Data\Stub\TypeHintedImmutableRecord;
use EventEngineTest\Data\Stub\TypeHintedImmutableRecordWithValueObjects;
use EventEngineTest\Data\Stub\ValueObject\Access;
use EventEngineTest\Data\Stub\ValueObject\ItemList;
use EventEngineTest\Data\Stub\ValueObject\Name;
use EventEngineTest\Data\Stub\ValueObject\Percentage;
use EventEngineTest\Data\Stub\ValueObject\Type;
use EventEngineTest\Data\Stub\ValueObject\Version;
use PHPUnit\Framework\TestCase;
use function sprintf;
final class ImmutableRecordLogicTest extends TestCase
{
private $data = [];
protected function setUp()
{
parent::setUp();
$this->data = [
'name' => 'test',
'version' => 1,
'itemList' => [['name' => 'one']],
'access' => true,
];
}
/**
* @test
*/
public function it_detects_type_hinted_properties()
{
$typeHinted = TypeHintedImmutableRecord::fromArray($this->data);
$this->data['type'] = null;
$this->data['percentage'] = 0.5;
$this->assertEquals(
$this->data,
$typeHinted->toArray()
);
}
/**
* @test
*/
public function it_detects_coupled_getters_for_properties()
{
$typedGetters = ImmutableRecordWithTypedGetters::fromArray($this->data);
$this->data['type'] = null;
$this->data['percentage'] = 0.5;
$this->assertEquals(
$this->data,
$typedGetters->toArray()
);
}
/**
* @test
*/
public function it_can_handle_value_objects()
{
$valueObjects = TypeHintedImmutableRecordWithValueObjects::fromArray($this->data);
$this->data['type'] = null;
$this->data['percentage'] = 0.5;
$this->assertEquals(
$this->data,
$valueObjects->toArray()
);
}
/**
* @test
*/
public function it_takes_value_object_as_initialization_params()
{
$valueObjects = TypeHintedImmutableRecordWithValueObjects::fromRecordData([
'name' => Name::fromString($this->data['name']),
'type' => Type::fromString('value_object'),
'version' => Version::fromInt($this->data['version']),
'access' => Access::fromBool($this->data['access']),
'percentage' => Percentage::fromFloat(0.9),
'itemList' => ItemList::fromItems(ImmutableItem::fromRecordData(['name' => 'one'])),
]);
$this->data['type'] = 'value_object';
$this->data['percentage'] = 0.9;
$this->assertEquals(
$this->data,
$valueObjects->toArray()
);
}
/**
* @test
*/
public function it_returns_new_record_with_changed_properties()
{
$valueObjects = TypeHintedImmutableRecordWithValueObjects::fromArray($this->data);
$changedValueObjects = $valueObjects->with([
'version' => Version::fromInt(2),
'percentage' => Percentage::fromFloat(0.9)
]);
$this->data['type'] = null;
$this->data['percentage'] = 0.5;
$this->assertEquals(
$this->data,
$valueObjects->toArray()
);
$this->data['percentage'] = 0.9;
$this->data['version'] = 2;
$this->assertEquals(
$this->data,
$changedValueObjects->toArray()
);
}
/**
* @test
*/
public function it_equals_other_record_with_same_values()
{
$valueObjects = TypeHintedImmutableRecordWithValueObjects::fromArray($this->data);
$other = TypeHintedImmutableRecordWithValueObjects::fromArray($this->data);
$this->assertTrue($valueObjects->equals($other));
}
/**
* @test
*/
public function it_excludes_keys_while_converting_back_to_array()
{
$valueObjects = TypeHintedImmutableRecord::fromArray($this->data);
$this->data['type'] = null;
$this->data['percentage'] = 0.5;
unset($this->data['version'], $this->data['name']);
$dataWithoutKeys = $valueObjects->toArray('version', 'name');
$this->assertArrayNotHasKey('version', $dataWithoutKeys);
$this->assertArrayNotHasKey('name', $dataWithoutKeys);
$this->assertEquals(
$this->data,
$dataWithoutKeys
);
}
/**
* @test
*/
public function it_throws_exception_if_unkown_property_provided()
{
$this->data['unknown'] = 'value';
$this->expectExceptionMessage('Invalid property passed to Record ' . TypeHintedImmutableRecordWithValueObjects::class . '. Got property with key unknown');
TypeHintedImmutableRecordWithValueObjects::fromArray($this->data);
}
/**
* @test
*/
public function it_throws_exception_if_non_nullable_prop_is_missing()
{
unset($this->data['version']);
$this->expectExceptionMessage('Missing record data for key version of record ' . TypeHintedImmutableRecord::class);
TypeHintedImmutableRecord::fromArray($this->data);
}
/**
* @test
*/
public function it_throws_exception_if_non_nullable_prop_should_be_set_to_null()
{
$this->data['version'] = null;
$this->expectExceptionMessage("Got null for non nullable property version of Record " . TypeHintedImmutableRecord::class);
TypeHintedImmutableRecord::fromArray($this->data);
}
/**
* @test
*/
public function it_throws_exception_if_property_value_has_wrong_type()
{
$this->data['version'] = 'v1';
$this->expectExceptionMessage(sprintf(
"Record %s data contains invalid value for property version. Expected type is int. Got type string.",
TypeHintedImmutableRecord::class
));
TypeHintedImmutableRecord::fromArray($this->data);
}
/**
* @test
*/
public function it_throws_exception_if_array_property_contains_invalid_value()
{
$stringList = ["abc", 123, "def"];
$this->expectExceptionMessage(sprintf(
"Record %s data contains invalid value for property stringList. Value should be an array of string, but at least one item of the array has the wrong type.",
RecordWithStringList::class
));
RecordWithStringList::fromArray(['stringList' => $stringList]);
}
/**
* @test
*/
public function it_throws_an_exception_if_no_type_hint_was_found()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage(
\sprintf(
'Could not find a type for property %s of record %s. ' .
'No type hint or getter method with a return type is given.',
'name',
ImmutableRecordWithNoTypes::class
)
);
ImmutableRecordWithNoTypes::fromArray($this->data);
}
}