From 69565a115175eaede5501e7c04174ee1ddde986f Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Tue, 14 Dec 2021 09:31:27 +0100 Subject: [PATCH] Fix tests --- ...abaseEloquentModelAttributeCastingTest.php | 75 ++----------------- .../TestEloquentModelWithAttributeCast.php | 72 ++++++++++++++++++ 2 files changed, 80 insertions(+), 67 deletions(-) create mode 100644 tests/Integration/Database/Fixtures/TestEloquentModelWithAttributeCast.php diff --git a/tests/Integration/Database/DatabaseEloquentModelAttributeCastingTest.php b/tests/Integration/Database/DatabaseEloquentModelAttributeCastingTest.php index 8ec5316592db..e61c94a83121 100644 --- a/tests/Integration/Database/DatabaseEloquentModelAttributeCastingTest.php +++ b/tests/Integration/Database/DatabaseEloquentModelAttributeCastingTest.php @@ -2,12 +2,17 @@ namespace Illuminate\Tests\Integration\Database; -use Illuminate\Database\Eloquent\Casts\Attribute; -use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Schema\Blueprint; -use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Schema; +use Illuminate\Tests\Integration\Database\Fixtures\TestEloquentModelWithAttributeCast; +if (PHP_VERSION_ID >= 80000) { + include 'Fixtures/TestEloquentModelWithAttributeCast.php'; +} + +/** + * @requires PHP 8.0 + */ class DatabaseEloquentModelAttributeCastingTest extends DatabaseTestCase { protected function defineDatabaseMigrationsAfterDatabaseRefreshed() @@ -177,70 +182,6 @@ public function testSettingRawAttributesClearsTheCastCache() } } -class TestEloquentModelWithAttributeCast extends Model -{ - /** - * The attributes that aren't mass assignable. - * - * @var string[] - */ - protected $guarded = []; - - public function uppercase(): Attribute - { - return new Attribute( - get: fn ($value) => strtoupper($value), - set: fn ($value) => strtoupper($value), - ); - } - - public function address(): Attribute - { - return new Attribute( - get: function ($value, $attributes) { - if (is_null($attributes['address_line_one'])) { - return; - } - - return new AttributeCastAddress($attributes['address_line_one'], $attributes['address_line_two']); - }, - set: function ($value) { - if (is_null($value)) { - return [ - 'address_line_one' => null, - 'address_line_two' => null, - ]; - } - - return ['address_line_one' => $value->lineOne, 'address_line_two' => $value->lineTwo]; - }, - ); - } - - public function options(): Attribute - { - return new Attribute( - get: fn ($value) => json_decode($value, true), - set: fn ($value) => json_encode($value), - ); - } - - public function birthdayAt(): Attribute - { - return new Attribute( - get: fn ($value) => Carbon::parse($value), - set: fn ($value) => $value->format('Y-m-d'), - ); - } - - public function password(): Attribute - { - return new Attribute( - set: fn ($value) => hash('sha256', $value) - ); - } -} - class AttributeCastAddress { public $lineOne; diff --git a/tests/Integration/Database/Fixtures/TestEloquentModelWithAttributeCast.php b/tests/Integration/Database/Fixtures/TestEloquentModelWithAttributeCast.php new file mode 100644 index 000000000000..edff3a1f57f3 --- /dev/null +++ b/tests/Integration/Database/Fixtures/TestEloquentModelWithAttributeCast.php @@ -0,0 +1,72 @@ + strtoupper($value), + set: fn ($value) => strtoupper($value), + ); + } + + public function address(): Attribute + { + return new Attribute( + get: function ($value, $attributes) { + if (is_null($attributes['address_line_one'])) { + return; + } + + return new AttributeCastAddress($attributes['address_line_one'], $attributes['address_line_two']); + }, + set: function ($value) { + if (is_null($value)) { + return [ + 'address_line_one' => null, + 'address_line_two' => null, + ]; + } + + return ['address_line_one' => $value->lineOne, 'address_line_two' => $value->lineTwo]; + }, + ); + } + + public function options(): Attribute + { + return new Attribute( + get: fn ($value) => json_decode($value, true), + set: fn ($value) => json_encode($value), + ); + } + + public function birthdayAt(): Attribute + { + return new Attribute( + get: fn ($value) => Carbon::parse($value), + set: fn ($value) => $value->format('Y-m-d'), + ); + } + + public function password(): Attribute + { + return new Attribute( + set: fn ($value) => hash('sha256', $value) + ); + } +}