diff --git a/src/Data/ImmutableRecordLogic.php b/src/Data/ImmutableRecordLogic.php index 4b1e235..0cb8108 100644 --- a/src/Data/ImmutableRecordLogic.php +++ b/src/Data/ImmutableRecordLogic.php @@ -60,9 +60,19 @@ private function __construct(array $recordData = null, array $nativeData = null) $this->setNativeData($nativeData); } + $this->init(); + $this->assertAllNotNull(); } + /** + * Called in constructor after setting props but before not null assertion + * + * Override to set default props after construction + */ + private function init(): void {} + + /** * @param array $recordData * @return self diff --git a/tests/Data/ImmutableRecordTest.php b/tests/Data/ImmutableRecordTest.php index fec5495..df26a26 100644 --- a/tests/Data/ImmutableRecordTest.php +++ b/tests/Data/ImmutableRecordTest.php @@ -8,6 +8,7 @@ use Prooph\EventMachine\JsonSchema\JsonSchema; use Prooph\EventMachineTest\Data\Stubs\TestBuildingVO; use Prooph\EventMachineTest\Data\Stubs\TestCommentVO; +use Prooph\EventMachineTest\Data\Stubs\TestDefaultPrice; use Prooph\EventMachineTest\Data\Stubs\TestIdentityVO; use Prooph\EventMachineTest\Data\Stubs\TestProduct; use Prooph\EventMachineTest\Data\Stubs\TestProductVO; @@ -171,4 +172,14 @@ public function it_uses_default_value_if_val_is_not_passed_to_constructor() $this->assertEquals(['name' => 'My House', 'type' => 'house'], $testBuilding->toArray()); } + + /** + * @test + */ + public function it_calls_init_to_give_immutable_record_the_chance_to_set_defaults_before_not_null_assertion() + { + $defaultPrice = TestDefaultPrice::fromArray([]); + + $this->assertEquals(['amount' => 9.99, 'currency' => 'EUR'], $defaultPrice->toArray()); + } } diff --git a/tests/Data/Stubs/TestDefaultPrice.php b/tests/Data/Stubs/TestDefaultPrice.php new file mode 100644 index 0000000..059efa0 --- /dev/null +++ b/tests/Data/Stubs/TestDefaultPrice.php @@ -0,0 +1,46 @@ +amount = TestAmountVO::fromFloat(9.99); + $this->currency = TestCurrencyVO::fromString('EUR'); + } + + /** + * @return TestAmountVO + */ + public function amount(): TestAmountVO + { + return $this->amount; + } + + /** + * @return null|TestCurrencyVO + */ + public function currency(): TestCurrencyVO + { + return $this->currency; + } + + +}