|
13 | 13 | use EventEngineTest\Data\Stub\ImmutableItem; |
14 | 14 | use EventEngineTest\Data\Stub\ImmutableRecordWithNoTypes; |
15 | 15 | use EventEngineTest\Data\Stub\ImmutableRecordWithTypedGetters; |
| 16 | +use EventEngineTest\Data\Stub\RecordWithStringList; |
16 | 17 | use EventEngineTest\Data\Stub\TypeHintedImmutableRecord; |
17 | 18 | use EventEngineTest\Data\Stub\TypeHintedImmutableRecordWithValueObjects; |
18 | 19 | use EventEngineTest\Data\Stub\ValueObject\Access; |
|
22 | 23 | use EventEngineTest\Data\Stub\ValueObject\Type; |
23 | 24 | use EventEngineTest\Data\Stub\ValueObject\Version; |
24 | 25 | use PHPUnit\Framework\TestCase; |
| 26 | +use function sprintf; |
25 | 27 |
|
26 | 28 | final class ImmutableRecordLogicTest extends TestCase |
27 | 29 | { |
@@ -110,6 +112,112 @@ public function it_takes_value_object_as_initialization_params() |
110 | 112 | ); |
111 | 113 | } |
112 | 114 |
|
| 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 | + |
113 | 221 | /** |
114 | 222 | * @test |
115 | 223 | */ |
|
0 commit comments