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..1ae6e75ab 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,15 @@ public function test_hooked_property_with_dto(): void $this->database->migrate(CreateMigrationsTable::class, HookedModelWithKey::class); $model = HookedModelWithKey::create(); + $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); } @@ -86,6 +97,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')] @@ -153,3 +223,38 @@ public function up(): QueryStatement ->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; + } + } +} + +#[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'); + } +}