|
| 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