From e79dcb050eaf2836dd5c52a3c3a0a1d5c2d89a7c Mon Sep 17 00:00:00 2001 From: brendt Date: Fri, 17 Jul 2026 08:23:25 +0200 Subject: [PATCH 1/3] wip --- .../database/src/Builder/ModelInspector.php | 2 +- packages/support/tests/Arr/ToArrayTest.php | 8 +-- packages/support/tests/Arr/WrapTest.php | 4 +- packages/view/src/Parser/TokenCollection.php | 4 +- .../Database/HookedPropertyTest.php | 63 ++++++++++++++++++- 5 files changed, 67 insertions(+), 14 deletions(-) diff --git a/packages/database/src/Builder/ModelInspector.php b/packages/database/src/Builder/ModelInspector.php index 3a24a044c..c9a09ac53 100644 --- a/packages/database/src/Builder/ModelInspector.php +++ b/packages/database/src/Builder/ModelInspector.php @@ -152,7 +152,7 @@ public function getPropertyValues(): array continue; } - if (! $property->isInitialized($this->instance)) { + if ($property->getGetHook() === null && ! $property->isInitialized($this->instance)) { continue; } diff --git a/packages/support/tests/Arr/ToArrayTest.php b/packages/support/tests/Arr/ToArrayTest.php index 8c25b0a39..88d9af62a 100644 --- a/packages/support/tests/Arr/ToArrayTest.php +++ b/packages/support/tests/Arr/ToArrayTest.php @@ -62,9 +62,7 @@ public function offsetExists($offset): bool public function offsetGet($offset): mixed { - return array_key_exists($offset, $this->data) - ? $this->data[$offset] - : null; + return $this->data[$offset] ?? null; } public function offsetSet($offset, $value): void @@ -104,9 +102,7 @@ public function offsetExists($offset): bool public function offsetGet($offset): mixed { - return array_key_exists($offset, $this->data) - ? $this->data[$offset] - : null; + return $this->data[$offset] ?? null; } public function offsetSet($offset, $value): void diff --git a/packages/support/tests/Arr/WrapTest.php b/packages/support/tests/Arr/WrapTest.php index df52502b8..3893ad3b6 100644 --- a/packages/support/tests/Arr/WrapTest.php +++ b/packages/support/tests/Arr/WrapTest.php @@ -63,9 +63,7 @@ public function offsetExists($offset): bool public function offsetGet($offset): mixed { - return array_key_exists($offset, $this->data) - ? $this->data[$offset] - : null; + return $this->data[$offset] ?? null; } public function offsetSet($offset, $value): void diff --git a/packages/view/src/Parser/TokenCollection.php b/packages/view/src/Parser/TokenCollection.php index c9bc0ae5c..c25679810 100644 --- a/packages/view/src/Parser/TokenCollection.php +++ b/packages/view/src/Parser/TokenCollection.php @@ -45,9 +45,7 @@ public function offsetExists(mixed $offset): bool public function offsetGet(mixed $offset): mixed { - return array_key_exists($offset, $this->tokens) - ? $this->tokens[$offset] - : null; + return $this->tokens[$offset] ?? null; } public function offsetSet(mixed $offset, mixed $value): void diff --git a/tests/Integration/Database/HookedPropertyTest.php b/tests/Integration/Database/HookedPropertyTest.php index e80b7028a..30f140f5c 100644 --- a/tests/Integration/Database/HookedPropertyTest.php +++ b/tests/Integration/Database/HookedPropertyTest.php @@ -13,6 +13,9 @@ use Tempest\Mapper\SerializeAs; use Tests\Tempest\Integration\FrameworkIntegrationTestCase; +use function Tempest\Database\inspect; +use function Tempest\Database\query; + final class HookedPropertyTest extends FrameworkIntegrationTestCase { #[Test] @@ -59,7 +62,6 @@ public function test_hooked_property_with_dto(): void $this->database->migrate(CreateMigrationsTable::class, HookedModelWithKey::class); $model = HookedModelWithKey::create(); - $this->assertSame('default', $model->key->value); } @@ -86,6 +88,65 @@ public function test_hooked_property_with_set_hook(): void $this->assertSame('other', $model->key->value); } + + #[Test] + public function test_with_inspect(): void + { + $values = inspect(new HookedModel())->getPropertyValues(); + + $this->assertSame(['hooked' => 'default'], $values); + } + + #[Test] + public function test_with_create_query_builder(): void + { + $this->database->migrate(CreateMigrationsTable::class, HookedModel::class); + + $model = query(HookedModel::class)->create( + hooked: 'other', + ); + + $this->assertSame('other', $model->hooked); + + $this->database->assertTableHasRow( + 'hooked_model', + hooked: 'other', + ); + } + + #[Test] + public function test_default_with_create_query_builder(): void + { + $this->database->migrate(CreateMigrationsTable::class, HookedModel::class); + + $model = query(HookedModel::class)->create(); + + $this->assertSame('default', $model->hooked); + + $this->database->assertTableHasRow( + 'hooked_model', + hooked: 'default', + ); + } + + #[Test] + public function test_with_update_query_builder(): void + { + $this->database->migrate(CreateMigrationsTable::class, HookedModel::class); + + $model = query(HookedModel::class)->create(); + + $model->update( + hooked: 'other', + ); + + $this->assertSame('other', $model->hooked); + + $this->database->assertTableHasRow( + 'hooked_model', + hooked: 'other', + ); + } } #[Table('hooked_model')] From 7fbcf39e832d8cc330cde7791aca7189d5ce1db8 Mon Sep 17 00:00:00 2001 From: brendt Date: Fri, 17 Jul 2026 08:25:33 +0200 Subject: [PATCH 2/3] wip --- .../Database/HookedPropertyTest.php | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/Integration/Database/HookedPropertyTest.php b/tests/Integration/Database/HookedPropertyTest.php index 30f140f5c..d141e4d0f 100644 --- a/tests/Integration/Database/HookedPropertyTest.php +++ b/tests/Integration/Database/HookedPropertyTest.php @@ -65,6 +65,15 @@ public function test_hooked_property_with_dto(): void $this->assertSame('default', $model->key->value); } + #[Test] + public function test_hooked_property_with_dto_via_trait(): void + { + $this->database->migrate(CreateMigrationsTable::class, HookedModelWithTrait::class); + + $model = HookedModelWithTrait::create(); + $this->assertSame('default', $model->key->value); + } + #[Test] public function test_hooked_property_with_dto_and_provided_value(): void { @@ -214,3 +223,38 @@ public function up(): QueryStatement ->dto('key'); } } + +#[Table('hooked_model_with_trait')] +class HookedModelWithTrait implements MigratesUp +{ + use IsDatabaseModel; + use HasKey; + + #[Virtual] + public string $name = 'hooked_model_with_trait'; + + public function up(): QueryStatement + { + return new CreateTableStatement('hooked_model_with_trait') + ->primary() + ->dto('key'); + } +} + +trait HasKey +{ + public Key $key { + get { + $this->key ??= new Key('default'); + + return $this->key; + } + set(string|Key $value) { + if (! $value instanceof Key) { + $value = new Key($value); + } + + $this->key = $value; + } + } +} From 6545b207fb283308bf958d1da8cf2823e8336cb7 Mon Sep 17 00:00:00 2001 From: brendt Date: Fri, 17 Jul 2026 08:34:20 +0200 Subject: [PATCH 3/3] wip --- .../Database/HookedPropertyTest.php | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/tests/Integration/Database/HookedPropertyTest.php b/tests/Integration/Database/HookedPropertyTest.php index d141e4d0f..1ae6e75ab 100644 --- a/tests/Integration/Database/HookedPropertyTest.php +++ b/tests/Integration/Database/HookedPropertyTest.php @@ -224,23 +224,6 @@ public function up(): QueryStatement } } -#[Table('hooked_model_with_trait')] -class HookedModelWithTrait implements MigratesUp -{ - use IsDatabaseModel; - use HasKey; - - #[Virtual] - public string $name = 'hooked_model_with_trait'; - - public function up(): QueryStatement - { - return new CreateTableStatement('hooked_model_with_trait') - ->primary() - ->dto('key'); - } -} - trait HasKey { public Key $key { @@ -258,3 +241,20 @@ trait HasKey } } } + +#[Table('hooked_model_with_trait')] +class HookedModelWithTrait implements MigratesUp +{ + use IsDatabaseModel; + use HasKey; + + #[Virtual] + public string $name = 'hooked_model_with_trait'; + + public function up(): QueryStatement + { + return new CreateTableStatement('hooked_model_with_trait') + ->primary() + ->dto('key'); + } +}