Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Fix casts + withAttributes #54422

Merged
merged 6 commits into from
Feb 5, 2025
Merged
Changes from 1 commit
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
Next Next commit
Test casts for pending attributes
  • Loading branch information
tontonsb authored Jan 30, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit b54aff8868a4f51098af75623b3ce44afbcaad7f
99 changes: 99 additions & 0 deletions tests/Database/DatabaseEloquentWithAttributesTest.php
Original file line number Diff line number Diff line change
@@ -3,7 +3,9 @@
namespace Illuminate\Tests\Database;

use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Builder;
use PHPUnit\Framework\TestCase;

class DatabaseEloquentWithAttributesTest extends TestCase
@@ -20,6 +22,11 @@ protected function setUp(): void
$db->setAsGlobal();
}

protected function tearDown(): void
{
$this->schema()->dropIfExists((new WithAttributesModel)->getTable());
}

public function testAddsAttributes(): void
{
$key = 'a key';
@@ -51,9 +58,101 @@ public function testAddsWheres(): void
'boolean' => 'and',
], $wheres);
}

public function testAddsWithCasts(): void
{
$query = WithAttributesModel::query()
->withAttributes([
'is_admin' => 1,
'first_name' => 'FIRST',
'last_name' => 'LAST',
'type' => WithAttributesEnum::internal,
]);

$model = $query->make();

$this->assertSame(true, $model->is_admin);
$this->assertSame('First', $model->first_name);
$this->assertSame('Last', $model->last_name);
$this->assertSame(WithAttributesEnum::internal, $model->type);

$this->assertEqualsCanonicalizing([
'id_admin' => 1,
'first_name' => 'first',
'last_name' => 'last',
'type' => 'int',
], $model->getAttributes());
}

public function testAddsWithCastsViaDb(): void
{
$this->bootTable();

$query = WithAttributesModel::query()
->withAttributes([
'is_admin' => 1,
'first_name' => 'FIRST',
'last_name' => 'LAST',
'type' => WithAttributesEnum::internal,
]);

$query->create();

$model = WithAttributesModel::first();

$this->assertSame(true, $model->is_admin);
$this->assertSame('First', $model->first_name);
$this->assertSame('Last', $model->last_name);
$this->assertSame(WithAttributesEnum::internal, $model->type);
}

protected function bootTable(): void
{
$this->schema()->create((new WithAttributesModel)->getTable(), function ($table) {
$table->id();
$table->boolean('is_admin');
$table->string('first_name');
$table->string('last_name');
$table->string('type');
$table->timestamps();
});
}

protected function schema(): Builder
{
return WithAttributesModel::getConnectionResolver()->connection()->getSchemaBuilder();
}
}

class WithAttributesModel extends Model
{
protected $guarded = [];

protected $casts = [
'is_admin' => 'boolean',
'type' => WithAttributesEnum::class,
];

public function setFirstNameAttribute(string $value): void
{
$this->attributes['first_name'] = strtolower($value);
}

public function getFirstNameAttribute(?string $value): string
{
return ucfirst($value);
}

protected function lastName(): Attribute
{
return Attribute::make(
get: fn (string $value) => ucfirst($value),
set: fn (string $value) => strtolower($value),
);
}
}

enum WithAttributesEnum: string
{
case internal = 'int';
}