Skip to content

Commit d0fd049

Browse files
authored
Merge pull request #336 from binaryfire/fix/has-attributes-casts-method
fix: getCasts() to include casts() method for non-incrementing models
2 parents bc05b17 + 053344e commit d0fd049

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

src/core/src/Database/Eloquent/Concerns/HasAttributes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function getCasts(): array
6464
return static::$castsCache[static::class] = array_merge([$this->getKeyName() => $this->getKeyType()], $this->casts, $this->casts());
6565
}
6666

67-
return static::$castsCache[static::class] = $this->casts;
67+
return static::$castsCache[static::class] = array_merge($this->casts, $this->casts());
6868
}
6969

7070
/**
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypervel\Tests\Core\Database\Eloquent\Concerns;
6+
7+
use Hypervel\Database\Eloquent\Concerns\HasUuids;
8+
use Hypervel\Database\Eloquent\Model;
9+
use Hypervel\Testbench\TestCase;
10+
11+
/**
12+
* @internal
13+
* @coversNothing
14+
*/
15+
class HasAttributesTest extends TestCase
16+
{
17+
public function testGetCastsIncludesCastsMethodForIncrementingModels(): void
18+
{
19+
$model = new HasAttributesIncrementingModel();
20+
21+
$casts = $model->getCasts();
22+
23+
$this->assertArrayHasKey('id', $casts);
24+
$this->assertArrayHasKey('data', $casts);
25+
$this->assertSame('array', $casts['data']);
26+
}
27+
28+
public function testGetCastsIncludesCastsMethodForNonIncrementingModels(): void
29+
{
30+
$model = new HasAttributesUuidModel();
31+
32+
$casts = $model->getCasts();
33+
34+
$this->assertArrayHasKey('data', $casts);
35+
$this->assertSame('array', $casts['data']);
36+
}
37+
38+
public function testGetCastsMergesPropertyAndMethodForNonIncrementingModels(): void
39+
{
40+
$model = new HasAttributesMixedCastsModel();
41+
42+
$casts = $model->getCasts();
43+
44+
// From $casts property
45+
$this->assertArrayHasKey('config', $casts);
46+
$this->assertSame('array', $casts['config']);
47+
48+
// From casts() method
49+
$this->assertArrayHasKey('data', $casts);
50+
$this->assertSame('array', $casts['data']);
51+
}
52+
}
53+
54+
class HasAttributesIncrementingModel extends Model
55+
{
56+
protected ?string $table = 'test_models';
57+
58+
protected function casts(): array
59+
{
60+
return [
61+
'data' => 'array',
62+
];
63+
}
64+
}
65+
66+
class HasAttributesUuidModel extends Model
67+
{
68+
use HasUuids;
69+
70+
protected ?string $table = 'test_models';
71+
72+
protected function casts(): array
73+
{
74+
return [
75+
'data' => 'array',
76+
];
77+
}
78+
}
79+
80+
class HasAttributesMixedCastsModel extends Model
81+
{
82+
use HasUuids;
83+
84+
protected ?string $table = 'test_models';
85+
86+
protected array $casts = [
87+
'config' => 'array',
88+
];
89+
90+
protected function casts(): array
91+
{
92+
return [
93+
'data' => 'array',
94+
];
95+
}
96+
}

0 commit comments

Comments
 (0)