From 13247e19a97935ec536c3b9bec30f7d14e082244 Mon Sep 17 00:00:00 2001 From: Louis-Gabriel Date: Fri, 29 Oct 2021 15:21:12 +0200 Subject: [PATCH] [8.x] Custom cast string into Stringable (#39410) * custom cast string to stringable * fix ci style * styleci --- .../Database/Eloquent/Casts/AsStringable.php | 32 +++++++++++++++++++ tests/Database/DatabaseEloquentModelTest.php | 22 +++++++++++++ ...stTest.php => DatabaseCustomCastsTest.php} | 19 ++++++++--- 3 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 src/Illuminate/Database/Eloquent/Casts/AsStringable.php rename tests/Integration/Database/{DatabaseArrayObjectAndCollectionCustomCastTest.php => DatabaseCustomCastsTest.php} (72%) diff --git a/src/Illuminate/Database/Eloquent/Casts/AsStringable.php b/src/Illuminate/Database/Eloquent/Casts/AsStringable.php new file mode 100644 index 000000000000..912659f38d54 --- /dev/null +++ b/src/Illuminate/Database/Eloquent/Casts/AsStringable.php @@ -0,0 +1,32 @@ +assertTrue($model->isDirty('ascollectionAttribute')); } + public function testDirtyOnCastedStringable() + { + $model = new EloquentModelCastingStub; + $model->setRawAttributes([ + 'asStringableAttribute' => 'foo bar', + ]); + $model->syncOriginal(); + + $this->assertInstanceOf(Stringable::class, $model->asStringableAttribute); + $this->assertFalse($model->isDirty('asStringableAttribute')); + + $model->asStringableAttribute = Str::of('foo bar'); + $this->assertFalse($model->isDirty('asStringableAttribute')); + + $model->asStringableAttribute = Str::of('foo baz'); + $this->assertTrue($model->isDirty('asStringableAttribute')); + } + public function testCleanAttributes() { $model = new EloquentModelStub(['foo' => '1', 'bar' => 2, 'baz' => 3]); @@ -2611,6 +2632,7 @@ class EloquentModelCastingStub extends Model 'timestampAttribute' => 'timestamp', 'asarrayobjectAttribute' => AsArrayObject::class, 'ascollectionAttribute' => AsCollection::class, + 'asStringableAttribute' => AsStringable::class, ]; public function jsonAttributeValue() diff --git a/tests/Integration/Database/DatabaseArrayObjectAndCollectionCustomCastTest.php b/tests/Integration/Database/DatabaseCustomCastsTest.php similarity index 72% rename from tests/Integration/Database/DatabaseArrayObjectAndCollectionCustomCastTest.php rename to tests/Integration/Database/DatabaseCustomCastsTest.php index 06bca3d65a1c..248e955dc277 100644 --- a/tests/Integration/Database/DatabaseArrayObjectAndCollectionCustomCastTest.php +++ b/tests/Integration/Database/DatabaseCustomCastsTest.php @@ -4,30 +4,37 @@ use Illuminate\Database\Eloquent\Casts\AsArrayObject; use Illuminate\Database\Eloquent\Casts\AsCollection; +use Illuminate\Database\Eloquent\Casts\AsStringable; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; +use Illuminate\Support\Str; -class DatabaseArrayObjectAndCollectionCustomCastTest extends DatabaseTestCase +/** + * @group integration + */ +class DatabaseCustomCastsTest extends DatabaseTestCase { protected function setUp(): void { parent::setUp(); - Schema::create('test_eloquent_model_with_custom_array_object_casts', function (Blueprint $table) { + Schema::create('test_eloquent_model_with_custom_casts', function (Blueprint $table) { $table->increments('id'); $table->text('array_object'); $table->text('collection'); + $table->string('stringable'); $table->timestamps(); }); } - public function test_array_object_and_collection_casting() + public function test_custom_casting() { - $model = new TestEloquentModelWithCustomArrayObjectCast; + $model = new TestEloquentModelWithCustomCasts; $model->array_object = ['name' => 'Taylor']; $model->collection = collect(['name' => 'Taylor']); + $model->stringable = Str::of('Taylor'); $model->save(); @@ -35,6 +42,7 @@ public function test_array_object_and_collection_casting() $this->assertEquals(['name' => 'Taylor'], $model->array_object->toArray()); $this->assertEquals(['name' => 'Taylor'], $model->collection->toArray()); + $this->assertEquals('Taylor', (string) $model->stringable); $model->array_object['age'] = 34; $model->array_object['meta']['title'] = 'Developer'; @@ -51,7 +59,7 @@ public function test_array_object_and_collection_casting() } } -class TestEloquentModelWithCustomArrayObjectCast extends Model +class TestEloquentModelWithCustomCasts extends Model { /** * The attributes that aren't mass assignable. @@ -68,5 +76,6 @@ class TestEloquentModelWithCustomArrayObjectCast extends Model protected $casts = [ 'array_object' => AsArrayObject::class, 'collection' => AsCollection::class, + 'stringable' => AsStringable::class, ]; }