Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/database/src/Builder/ModelInspector.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function getPropertyValues(): array
continue;
}

if (! $property->isInitialized($this->instance)) {
if ($property->getGetHook() === null && ! $property->isInitialized($this->instance)) {
continue;
}

Expand Down
8 changes: 2 additions & 6 deletions packages/support/tests/Arr/ToArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions packages/support/tests/Arr/WrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions packages/view/src/Parser/TokenCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
105 changes: 105 additions & 0 deletions tests/Integration/Database/HookedPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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);
}

Expand All @@ -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')]
Expand Down Expand Up @@ -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');
}
}
Loading