diff --git a/src/core/src/Database/Eloquent/Concerns/HasAttributes.php b/src/core/src/Database/Eloquent/Concerns/HasAttributes.php index 0c673d83..ebe428fc 100644 --- a/src/core/src/Database/Eloquent/Concerns/HasAttributes.php +++ b/src/core/src/Database/Eloquent/Concerns/HasAttributes.php @@ -64,7 +64,7 @@ public function getCasts(): array return static::$castsCache[static::class] = array_merge([$this->getKeyName() => $this->getKeyType()], $this->casts, $this->casts()); } - return static::$castsCache[static::class] = $this->casts; + return static::$castsCache[static::class] = array_merge($this->casts, $this->casts()); } /** diff --git a/tests/Core/Database/Eloquent/Concerns/HasAttributesTest.php b/tests/Core/Database/Eloquent/Concerns/HasAttributesTest.php new file mode 100644 index 00000000..8a3b8b8c --- /dev/null +++ b/tests/Core/Database/Eloquent/Concerns/HasAttributesTest.php @@ -0,0 +1,96 @@ +getCasts(); + + $this->assertArrayHasKey('id', $casts); + $this->assertArrayHasKey('data', $casts); + $this->assertSame('array', $casts['data']); + } + + public function testGetCastsIncludesCastsMethodForNonIncrementingModels(): void + { + $model = new HasAttributesUuidModel(); + + $casts = $model->getCasts(); + + $this->assertArrayHasKey('data', $casts); + $this->assertSame('array', $casts['data']); + } + + public function testGetCastsMergesPropertyAndMethodForNonIncrementingModels(): void + { + $model = new HasAttributesMixedCastsModel(); + + $casts = $model->getCasts(); + + // From $casts property + $this->assertArrayHasKey('config', $casts); + $this->assertSame('array', $casts['config']); + + // From casts() method + $this->assertArrayHasKey('data', $casts); + $this->assertSame('array', $casts['data']); + } +} + +class HasAttributesIncrementingModel extends Model +{ + protected ?string $table = 'test_models'; + + protected function casts(): array + { + return [ + 'data' => 'array', + ]; + } +} + +class HasAttributesUuidModel extends Model +{ + use HasUuids; + + protected ?string $table = 'test_models'; + + protected function casts(): array + { + return [ + 'data' => 'array', + ]; + } +} + +class HasAttributesMixedCastsModel extends Model +{ + use HasUuids; + + protected ?string $table = 'test_models'; + + protected array $casts = [ + 'config' => 'array', + ]; + + protected function casts(): array + { + return [ + 'data' => 'array', + ]; + } +}