Skip to content

Commit 614d408

Browse files
author
Alexander Miertsch
authored
Merge pull request #10 from event-engine/feature/code_coverage
More tests
2 parents ad3710a + c48b0ec commit 614d408

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed

tests/ImmutableRecordDataConverterTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace EventEngineTest\Data;
1212

1313
use EventEngine\Data\ImmutableRecordDataConverter;
14+
use EventEngineTest\Data\Stub\ImmutableItem;
1415
use EventEngineTest\Data\Stub\TypeHintedImmutableRecordWithValueObjects;
1516
use PHPUnit\Framework\TestCase;
1617
use stdClass;
@@ -53,6 +54,30 @@ public function it_converts_immutable_record_to_array()
5354
);
5455
}
5556

57+
/**
58+
* @test
59+
*/
60+
public function it_returns_array_if_passed_as_input()
61+
{
62+
$input = ["a" => "test"];
63+
64+
$dataConverter = new ImmutableRecordDataConverter();
65+
66+
$output = $dataConverter->convertDataToArray('data', $input);
67+
68+
$this->assertEquals($input, $output);
69+
}
70+
71+
/**
72+
* @test
73+
*/
74+
public function it_returns_false_if_unknown_class_is_passed()
75+
{
76+
$dataConverter = new ImmutableRecordDataConverter();
77+
78+
$this->assertFalse($dataConverter->canConvertTypeToData(ImmutableItem::class . 'Unknown'));
79+
}
80+
5681
/**
5782
* @test
5883
*/

tests/ImmutableRecordLogicTest.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use EventEngineTest\Data\Stub\ImmutableItem;
1414
use EventEngineTest\Data\Stub\ImmutableRecordWithNoTypes;
1515
use EventEngineTest\Data\Stub\ImmutableRecordWithTypedGetters;
16+
use EventEngineTest\Data\Stub\RecordWithStringList;
1617
use EventEngineTest\Data\Stub\TypeHintedImmutableRecord;
1718
use EventEngineTest\Data\Stub\TypeHintedImmutableRecordWithValueObjects;
1819
use EventEngineTest\Data\Stub\ValueObject\Access;
@@ -22,6 +23,7 @@
2223
use EventEngineTest\Data\Stub\ValueObject\Type;
2324
use EventEngineTest\Data\Stub\ValueObject\Version;
2425
use PHPUnit\Framework\TestCase;
26+
use function sprintf;
2527

2628
final class ImmutableRecordLogicTest extends TestCase
2729
{
@@ -110,6 +112,112 @@ public function it_takes_value_object_as_initialization_params()
110112
);
111113
}
112114

115+
/**
116+
* @test
117+
*/
118+
public function it_returns_new_record_with_changed_properties()
119+
{
120+
$valueObjects = TypeHintedImmutableRecordWithValueObjects::fromArray($this->data);
121+
122+
$changedValueObjects = $valueObjects->with([
123+
'version' => Version::fromInt(2),
124+
'percentage' => Percentage::fromFloat(0.9)
125+
]);
126+
127+
$this->data['type'] = null;
128+
$this->data['percentage'] = 0.5;
129+
130+
$this->assertEquals(
131+
$this->data,
132+
$valueObjects->toArray()
133+
);
134+
135+
$this->data['percentage'] = 0.9;
136+
$this->data['version'] = 2;
137+
138+
$this->assertEquals(
139+
$this->data,
140+
$changedValueObjects->toArray()
141+
);
142+
}
143+
144+
/**
145+
* @test
146+
*/
147+
public function it_equals_other_record_with_same_values()
148+
{
149+
$valueObjects = TypeHintedImmutableRecordWithValueObjects::fromArray($this->data);
150+
$other = TypeHintedImmutableRecordWithValueObjects::fromArray($this->data);
151+
152+
$this->assertTrue($valueObjects->equals($other));
153+
}
154+
155+
/**
156+
* @test
157+
*/
158+
public function it_throws_exception_if_unkown_property_provided()
159+
{
160+
$this->data['unknown'] = 'value';
161+
162+
$this->expectExceptionMessage('Invalid property passed to Record ' . TypeHintedImmutableRecordWithValueObjects::class . '. Got property with key unknown');
163+
164+
TypeHintedImmutableRecordWithValueObjects::fromArray($this->data);
165+
}
166+
167+
/**
168+
* @test
169+
*/
170+
public function it_throws_exception_if_non_nullable_prop_is_missing()
171+
{
172+
unset($this->data['version']);
173+
174+
$this->expectExceptionMessage('Missing record data for key version of record ' . TypeHintedImmutableRecord::class);
175+
176+
TypeHintedImmutableRecord::fromArray($this->data);
177+
}
178+
179+
/**
180+
* @test
181+
*/
182+
public function it_throws_exception_if_non_nullable_prop_should_be_set_to_null()
183+
{
184+
$this->data['version'] = null;
185+
186+
$this->expectExceptionMessage("Got null for non nullable property version of Record " . TypeHintedImmutableRecord::class);
187+
188+
TypeHintedImmutableRecord::fromArray($this->data);
189+
}
190+
191+
/**
192+
* @test
193+
*/
194+
public function it_throws_exception_if_property_value_has_wrong_type()
195+
{
196+
$this->data['version'] = 'v1';
197+
198+
$this->expectExceptionMessage(sprintf(
199+
"Record %s data contains invalid value for property version. Expected type is int. Got type string.",
200+
TypeHintedImmutableRecord::class
201+
));
202+
203+
TypeHintedImmutableRecord::fromArray($this->data);
204+
}
205+
206+
/**
207+
* @test
208+
*/
209+
public function it_throws_exception_if_array_property_contains_invalid_value()
210+
{
211+
$stringList = ["abc", 123, "def"];
212+
213+
$this->expectExceptionMessage(sprintf(
214+
"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.",
215+
RecordWithStringList::class
216+
));
217+
218+
RecordWithStringList::fromArray(['stringList' => $stringList]);
219+
}
220+
113221
/**
114222
* @test
115223
*/
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* This file is part of event-engine/php-data.
4+
* (c) 2018-2020 prooph software GmbH <contact@prooph.de>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
declare(strict_types=1);
10+
11+
namespace EventEngineTest\Data\Stub;
12+
13+
use EventEngine\Data\ImmutableRecord;
14+
use EventEngine\Data\ImmutableRecordLogic;
15+
16+
final class RecordWithStringList implements ImmutableRecord
17+
{
18+
use ImmutableRecordLogic;
19+
20+
private array $stringList;
21+
22+
private static function arrayPropItemTypeMap(): array
23+
{
24+
return ['stringList' => ImmutableRecord::PHP_TYPE_STRING];
25+
}
26+
27+
/**
28+
* @return array
29+
*/
30+
public function stringList(): array
31+
{
32+
return $this->stringList;
33+
}
34+
}

0 commit comments

Comments
 (0)