From 05e862b6a823a58de8218153c29a987fff263b34 Mon Sep 17 00:00:00 2001 From: bfiessinger Date: Fri, 30 Jun 2023 19:03:26 +0200 Subject: [PATCH 01/10] fix getMeta returns empty Collection if Attribute is casted in some cases --- src/Kodeine/Metable/Metable.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Kodeine/Metable/Metable.php b/src/Kodeine/Metable/Metable.php index 04a7564..01913f9 100644 --- a/src/Kodeine/Metable/Metable.php +++ b/src/Kodeine/Metable/Metable.php @@ -437,6 +437,11 @@ public function getAttribute($key) { return $attr; } + // Check if the key is a casted attribute. + if ( $this->hasCast( $key ) ) { + return $this->castAttribute( $key, $attr ); + } + // Don't get meta data if fluent access is disabled. if ( property_exists( $this, 'disableFluentMeta' ) && $this->disableFluentMeta ) { return $attr; From a47115edb12ba3e3ffdb06d230b4e06916ab233c Mon Sep 17 00:00:00 2001 From: bfiessinger Date: Fri, 30 Jun 2023 21:03:25 +0200 Subject: [PATCH 02/10] add tests --- tests/Casts/UserCastedObject.php | 26 ++++++++++++++++++++++++ tests/MetableTest.php | 5 +++++ tests/Models/UserTest.php | 7 +++++++ tests/Traits/HasUserCasts.php | 35 ++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 tests/Casts/UserCastedObject.php create mode 100644 tests/Traits/HasUserCasts.php diff --git a/tests/Casts/UserCastedObject.php b/tests/Casts/UserCastedObject.php new file mode 100644 index 0000000..d284218 --- /dev/null +++ b/tests/Casts/UserCastedObject.php @@ -0,0 +1,26 @@ + json_encode($value)]; + } + + public static function castUsing(array $arguments) + { + return new self(...$arguments); + } +} \ No newline at end of file diff --git a/tests/MetableTest.php b/tests/MetableTest.php index dd36069..7875304 100644 --- a/tests/MetableTest.php +++ b/tests/MetableTest.php @@ -31,6 +31,7 @@ public static function setUpBeforeClass(): void { $table->string( 'name' )->default( 'john' ); $table->string( 'email' )->default( 'john@doe.com' ); $table->string( 'password' )->nullable(); + $table->string( 'state' )->nullable(); $table->integer( 'user_test_id' )->unsigned()->nullable(); $table->foreign( 'user_test_id' )->references( 'id' )->on( 'user_tests' ); $table->timestamps(); @@ -227,6 +228,10 @@ public function testMetaMethods() { $user->save(); + // $meta = $user->getMeta(); + // $this->assertInstanceOf( 'Illuminate\Support\Collection', $meta, 'Meta method getMeta is not typeof Collection' ); + // $this->assertNotEmpty( $meta, 'Meta method getMeta did return empty collection' ); + // re retrieve user to make sure meta is saved $user = UserTest::with( ['metas'] )->find( $user->getKey() ); diff --git a/tests/Models/UserTest.php b/tests/Models/UserTest.php index 0bcfdb6..582bd2e 100644 --- a/tests/Models/UserTest.php +++ b/tests/Models/UserTest.php @@ -6,10 +6,13 @@ use Illuminate\Events\Dispatcher; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasOne; +use Kodeine\Metable\Tests\Casts\UserCastedObject; +use Kodeine\Metable\Tests\Traits\HasUserCasts; class UserTest extends Model { use Metable; + use HasUserCasts; public $defaultMetaValues = [ 'default_meta_key' => 'default_meta_value', @@ -18,6 +21,10 @@ class UserTest extends Model public $hideMeta = false; public $disableFluentMeta = false; + + protected $casts = [ + 'state' => UserCastedObject::class, + ]; /** * This is dummy relation to itself. diff --git a/tests/Traits/HasUserCasts.php b/tests/Traits/HasUserCasts.php new file mode 100644 index 0000000..777b526 --- /dev/null +++ b/tests/Traits/HasUserCasts.php @@ -0,0 +1,35 @@ + UserCastedObject::class, + ]; + + public static function bootHasUserCasts() + { + self::creating( function ( $model ) { + $model->setDefaultCastedProperties(); + } ); + } + + public function initializeHasUserCasts() + { + $this->setDefaultCastedProperties(); + } + + private function setDefaultCastedProperties() + { + foreach ( $this->casted as $key => $class ) { + if ($this->{$key} === null) { + continue; + } + + $this->{$key} = new $class( $key ); + } + } +} \ No newline at end of file From 42347c1b69f0dfa375082991712cd60d97e71d06 Mon Sep 17 00:00:00 2001 From: bfiessinger Date: Fri, 30 Jun 2023 21:06:28 +0200 Subject: [PATCH 03/10] fix missing tests --- tests/MetableTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/MetableTest.php b/tests/MetableTest.php index 7875304..71869ca 100644 --- a/tests/MetableTest.php +++ b/tests/MetableTest.php @@ -220,7 +220,7 @@ public function testMetaMethods() { $user->setMeta( 'foo', 'bar' ); $this->assertEquals( 'bar', $user->getMeta( 'foo' ), 'Meta method getMeta did not return correct value' ); - + $user->setMeta( [ 'foo' => 'baz', 'bas' => 'bar', @@ -228,9 +228,9 @@ public function testMetaMethods() { $user->save(); - // $meta = $user->getMeta(); - // $this->assertInstanceOf( 'Illuminate\Support\Collection', $meta, 'Meta method getMeta is not typeof Collection' ); - // $this->assertNotEmpty( $meta, 'Meta method getMeta did return empty collection' ); + $meta = $user->getMeta(); + $this->assertInstanceOf( 'Illuminate\Support\Collection', $meta, 'Meta method getMeta is not typeof Collection' ); + $this->assertNotEmpty( $meta, 'Meta method getMeta did return empty collection' ); // re retrieve user to make sure meta is saved $user = UserTest::with( ['metas'] )->find( $user->getKey() ); From 2826b9d4b6f9d4d12ea97615a660280c519625c8 Mon Sep 17 00:00:00 2001 From: Siamak Salimi Date: Sat, 1 Jul 2023 00:03:18 +0330 Subject: [PATCH 04/10] Fixed some errors in UserCastedObject.php --- tests/Casts/UserCastedObject.php | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/tests/Casts/UserCastedObject.php b/tests/Casts/UserCastedObject.php index d284218..0b9e0aa 100644 --- a/tests/Casts/UserCastedObject.php +++ b/tests/Casts/UserCastedObject.php @@ -7,20 +7,18 @@ class UserCastedObject implements Castable, CastsAttributes { - public string $description; - - public function get($model, $key, $value, $attributes) - { - return json_decode($value, true); - } - - public function set($model, $key, $value, $attributes) - { - return [$key => json_encode($value)]; - } - - public static function castUsing(array $arguments) - { - return new self(...$arguments); + public $description; + + public function get($model, $key, $value, $attributes) { + if ( is_null( $value ) ) return null; + return json_decode( $value, true ); + } + + public function set($model, $key, $value, $attributes) { + return [$key => json_encode( $value )]; + } + + public static function castUsing(array $arguments) { + return new self(); } } \ No newline at end of file From 213d254f97b705bfda7093ecaaf352ec7cb9bfad Mon Sep 17 00:00:00 2001 From: Siamak Salimi Date: Sat, 1 Jul 2023 00:40:15 +0330 Subject: [PATCH 05/10] Fixed a wrong assertion in MetableTest --- tests/MetableTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/MetableTest.php b/tests/MetableTest.php index 71869ca..2bb1712 100644 --- a/tests/MetableTest.php +++ b/tests/MetableTest.php @@ -220,7 +220,7 @@ public function testMetaMethods() { $user->setMeta( 'foo', 'bar' ); $this->assertEquals( 'bar', $user->getMeta( 'foo' ), 'Meta method getMeta did not return correct value' ); - + $user->setMeta( [ 'foo' => 'baz', 'bas' => 'bar', @@ -230,8 +230,8 @@ public function testMetaMethods() { $meta = $user->getMeta(); $this->assertInstanceOf( 'Illuminate\Support\Collection', $meta, 'Meta method getMeta is not typeof Collection' ); - $this->assertNotEmpty( $meta, 'Meta method getMeta did return empty collection' ); - + $this->assertTrue( $meta->isNotEmpty(), 'Meta method getMeta did return empty collection' ); + // re retrieve user to make sure meta is saved $user = UserTest::with( ['metas'] )->find( $user->getKey() ); From edf1d83eaa1e5d0a1cceb070b10dc3cd81d615da Mon Sep 17 00:00:00 2001 From: Siamak Salimi Date: Sat, 1 Jul 2023 00:41:30 +0330 Subject: [PATCH 06/10] Check for null attribute or null cast in getAttribute method --- src/Kodeine/Metable/Metable.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Kodeine/Metable/Metable.php b/src/Kodeine/Metable/Metable.php index 01913f9..46e5c79 100644 --- a/src/Kodeine/Metable/Metable.php +++ b/src/Kodeine/Metable/Metable.php @@ -437,16 +437,20 @@ public function getAttribute($key) { return $attr; } - // Check if the key is a casted attribute. - if ( $this->hasCast( $key ) ) { - return $this->castAttribute( $key, $attr ); - } - // Don't get meta data if fluent access is disabled. if ( property_exists( $this, 'disableFluentMeta' ) && $this->disableFluentMeta ) { return $attr; } + // It is possible that attribute exists, or it has a cast, but it's null, so we check for that + if ( array_key_exists( $key, $this->attributes ) || + array_key_exists( $key, $this->casts ) || + $this->hasGetMutator( $key ) || + $this->hasAttributeMutator( $key ) || + $this->isClassCastable( $key ) ) { + return $attr; + } + // If key is a relation name, then return parent value. // The reason for this is that it's possible that the relation does not exist and parent call returns null for that. if ( $this->isRelation( $key ) && $this->relationLoaded( $key ) ) { From 58bec9684d26de567f23e6a89b688d0ec5268ae5 Mon Sep 17 00:00:00 2001 From: Siamak Salimi Date: Tue, 4 Jul 2023 22:16:44 +0330 Subject: [PATCH 07/10] Added more tests for when property or mutator is null --- tests/MetableTest.php | 16 ++++++++++++++++ tests/Models/UserTest.php | 6 +++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/MetableTest.php b/tests/MetableTest.php index 2bb1712..cb8f87a 100644 --- a/tests/MetableTest.php +++ b/tests/MetableTest.php @@ -32,6 +32,7 @@ public static function setUpBeforeClass(): void { $table->string( 'email' )->default( 'john@doe.com' ); $table->string( 'password' )->nullable(); $table->string( 'state' )->nullable(); + $table->string( 'null_value' )->nullable(); $table->integer( 'user_test_id' )->unsigned()->nullable(); $table->foreign( 'user_test_id' )->references( 'id' )->on( 'user_tests' ); $table->timestamps(); @@ -113,6 +114,21 @@ public function testFluentMeta() { $this->assertTrue( $user->isMetaDirty( 'foo', 'bar' ), 'isMetaDirty should return true even if one of metas has changed' ); $this->assertTrue( $user->isMetaDirty( 'foo,bar' ), 'isMetaDirty should return true even if one of metas has changed' ); + //re retrieve user from database + /** @var UserTest $user */ + $user = UserTest::find( $user->id ); + + $this->assertNull( $user->null_value, 'null_value property should be null' ); + $this->assertNull( $user->null_cast, 'null_cast property should be null' ); + + $user->setMeta( 'null_value', true ); + $user->setMeta( 'null_cast', true ); + + $this->assertTrue( $user->getMeta( 'null_value' ), 'Meta should be set' ); + $this->assertTrue( $user->getMeta( 'null_cast' ), 'Meta should be set' ); + $this->assertNull( $user->null_value, 'null_value property should be null' ); + $this->assertNull( $user->null_cast, 'null_cast property should be null' ); + $user->delete(); $this->assertEquals( 0, $metaData->count(), 'Meta should be deleted from database after deleting user.' ); diff --git a/tests/Models/UserTest.php b/tests/Models/UserTest.php index 582bd2e..8815062 100644 --- a/tests/Models/UserTest.php +++ b/tests/Models/UserTest.php @@ -21,11 +21,15 @@ class UserTest extends Model public $hideMeta = false; public $disableFluentMeta = false; - + protected $casts = [ 'state' => UserCastedObject::class, ]; + public function getNullCastAttribute() { + return null; + } + /** * This is dummy relation to itself. * From acc55379d4d1bc8745f8638263dd85880a087348 Mon Sep 17 00:00:00 2001 From: bfiessinger Date: Wed, 5 Jul 2023 15:49:45 +0200 Subject: [PATCH 08/10] Update user test state casting --- tests/Casts/StateCaster.php | 45 +++++++++++++++++++++ tests/Casts/UserCastedObject.php | 43 +++++++++++++++----- tests/Casts/UserState/DefaultState.php | 16 ++++++++ tests/Casts/UserState/State.php | 17 ++++++++ tests/MetableTest.php | 11 +++++ tests/Models/UserTest.php | 10 ++--- tests/Traits/HasUserCasts.php | 35 ---------------- tests/Traits/HasUserStates.php | 56 ++++++++++++++++++++++++++ 8 files changed, 182 insertions(+), 51 deletions(-) create mode 100644 tests/Casts/StateCaster.php create mode 100644 tests/Casts/UserState/DefaultState.php create mode 100644 tests/Casts/UserState/State.php delete mode 100644 tests/Traits/HasUserCasts.php create mode 100644 tests/Traits/HasUserStates.php diff --git a/tests/Casts/StateCaster.php b/tests/Casts/StateCaster.php new file mode 100644 index 0000000..7097f87 --- /dev/null +++ b/tests/Casts/StateCaster.php @@ -0,0 +1,45 @@ +baseStateClass = $baseStateClass; + } + + public function get($model, $key, $value, $attributes) { + if ( is_null( $value ) ) return null; + + if (! is_subclass_of($value, $this->baseStateClass)) { + return null; + } + + $stateClassName = $value::config()['default']; + + $state = new $stateClassName($model); + + return $state; + } + + public function set($model, $key, $value, $attributes) { + if ( is_null( $value ) ) return null; + + if (! is_subclass_of($value, $this->baseStateClass)) { + throw new \Exception('Invalid state class.'); + } + + $value = new $value($model); + + if ($value instanceof $this->baseStateClass) { + $value->setField($key); + } + + return $value->getMorphClass(); + } +} \ No newline at end of file diff --git a/tests/Casts/UserCastedObject.php b/tests/Casts/UserCastedObject.php index 0b9e0aa..8694511 100644 --- a/tests/Casts/UserCastedObject.php +++ b/tests/Casts/UserCastedObject.php @@ -3,22 +3,43 @@ namespace Kodeine\Metable\Tests\Casts; use Illuminate\Contracts\Database\Eloquent\Castable; -use Illuminate\Contracts\Database\Eloquent\CastsAttributes; -class UserCastedObject implements Castable, CastsAttributes +abstract class UserCastedObject implements Castable { - public $description; - - public function get($model, $key, $value, $attributes) { - if ( is_null( $value ) ) return null; - return json_decode( $value, true ); + public function __construct($model) { + $this->model = $model; + $this->stateConfig = static::config(); } - - public function set($model, $key, $value, $attributes) { - return [$key => json_encode( $value )]; + + public static function config() + { + return [ + 'default' => null, + ]; } public static function castUsing(array $arguments) { - return new self(); + return new StateCaster(static::class); + } + + public function setField(string $field): self + { + $this->field = $field; + + return $this; + } + + public static function getMorphClass(): string + { + return static::$name ?? static::class; + } + + public function make(string $name, $model) + { + if (is_null($name)) { + return null; + } + + return new $name($model); } } \ No newline at end of file diff --git a/tests/Casts/UserState/DefaultState.php b/tests/Casts/UserState/DefaultState.php new file mode 100644 index 0000000..600898e --- /dev/null +++ b/tests/Casts/UserState/DefaultState.php @@ -0,0 +1,16 @@ +description = $this->description(); + } + + public function description(): string { + return 'This is a default description.'; + } +} \ No newline at end of file diff --git a/tests/Casts/UserState/State.php b/tests/Casts/UserState/State.php new file mode 100644 index 0000000..9a91a1e --- /dev/null +++ b/tests/Casts/UserState/State.php @@ -0,0 +1,17 @@ + DefaultState::class, + ]; + } +} \ No newline at end of file diff --git a/tests/MetableTest.php b/tests/MetableTest.php index cb8f87a..866d4da 100644 --- a/tests/MetableTest.php +++ b/tests/MetableTest.php @@ -10,6 +10,7 @@ use Kodeine\Metable\Tests\Models\UserTest; use Illuminate\Database\Capsule\Manager as Capsule; use Illuminate\Database\Eloquent\ModelNotFoundException; +use Kodeine\Metable\Tests\Casts\UserState\DefaultState; class MetableTest extends TestCase { @@ -49,6 +50,16 @@ public static function setUpBeforeClass(): void { } ); } + public function testCast() { + $user = new UserTest; + + $this->assertNull( $user->state, 'Casted object should be null by default' ); + + $user->state = DefaultState::class; + + $this->assertTrue( $user->state instanceof DefaultState, 'Casted object should be instanceof DefaultState' ); + } + public function testFluentMeta() { $user = new UserTest; diff --git a/tests/Models/UserTest.php b/tests/Models/UserTest.php index 8815062..dfd0841 100644 --- a/tests/Models/UserTest.php +++ b/tests/Models/UserTest.php @@ -6,13 +6,13 @@ use Illuminate\Events\Dispatcher; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasOne; -use Kodeine\Metable\Tests\Casts\UserCastedObject; -use Kodeine\Metable\Tests\Traits\HasUserCasts; +use Kodeine\Metable\Tests\Casts\UserState\State; +use Kodeine\Metable\Tests\Traits\HasUserStates; class UserTest extends Model { use Metable; - use HasUserCasts; + use HasUserStates; public $defaultMetaValues = [ 'default_meta_key' => 'default_meta_value', @@ -21,9 +21,9 @@ class UserTest extends Model public $hideMeta = false; public $disableFluentMeta = false; - + protected $casts = [ - 'state' => UserCastedObject::class, + 'state' => State::class, ]; public function getNullCastAttribute() { diff --git a/tests/Traits/HasUserCasts.php b/tests/Traits/HasUserCasts.php deleted file mode 100644 index 777b526..0000000 --- a/tests/Traits/HasUserCasts.php +++ /dev/null @@ -1,35 +0,0 @@ - UserCastedObject::class, - ]; - - public static function bootHasUserCasts() - { - self::creating( function ( $model ) { - $model->setDefaultCastedProperties(); - } ); - } - - public function initializeHasUserCasts() - { - $this->setDefaultCastedProperties(); - } - - private function setDefaultCastedProperties() - { - foreach ( $this->casted as $key => $class ) { - if ($this->{$key} === null) { - continue; - } - - $this->{$key} = new $class( $key ); - } - } -} \ No newline at end of file diff --git a/tests/Traits/HasUserStates.php b/tests/Traits/HasUserStates.php new file mode 100644 index 0000000..816e92b --- /dev/null +++ b/tests/Traits/HasUserStates.php @@ -0,0 +1,56 @@ + State::class, + ]; + + public static function bootHasUserCasts() + { + self::creating( function ( $model ) { + $model->setDefaultCastedProperties(); + } ); + } + + public function initializeHasUserCasts() + { + $this->setDefaultCastedProperties(); + } + + private function getStateConfigs() + { + $casts = $this->getCasts(); + + $states = []; + + foreach ($casts as $prop => $state) { + if (! is_subclass_of($state, UserCastedObject::class)) { + continue; + } + + $states[$prop] = $state::config(); + } + + return $states; + } + + private function setDefaultCastedProperties() + { + foreach ( $this->getStateConfigs() as $prop => $config ) { + if ($this->{$prop} === null) { + continue; + } + + if ( ! isset( $config['default'] ) ) { + continue; + } + + $this->{$prop} = $config['default']; + } + } +} \ No newline at end of file From 59b61e8a29ecbf8b7ef31e37f8e4a2fa51a5c0aa Mon Sep 17 00:00:00 2001 From: Siamak Salimi Date: Wed, 5 Jul 2023 23:51:37 +0330 Subject: [PATCH 09/10] Fixed some IDE warnings and reformat codes --- tests/Casts/StateCaster.php | 47 +++++++++++++------------- tests/Casts/UserCastedObject.php | 45 ++++++++++++------------ tests/Casts/UserState/DefaultState.php | 5 +-- tests/MetableTest.php | 10 +++--- tests/Models/UserTest.php | 2 +- tests/Traits/HasUserStates.php | 39 ++++++++++----------- 6 files changed, 74 insertions(+), 74 deletions(-) diff --git a/tests/Casts/StateCaster.php b/tests/Casts/StateCaster.php index 7097f87..4ec4a69 100644 --- a/tests/Casts/StateCaster.php +++ b/tests/Casts/StateCaster.php @@ -2,44 +2,45 @@ namespace Kodeine\Metable\Tests\Casts; +use Exception; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; class StateCaster implements CastsAttributes { - private string $baseStateClass; - - public function __construct(string $baseStateClass) - { - $this->baseStateClass = $baseStateClass; - } - + private $baseStateClass; + + public function __construct(string $baseStateClass) { + $this->baseStateClass = $baseStateClass; + } + public function get($model, $key, $value, $attributes) { if ( is_null( $value ) ) return null; - - if (! is_subclass_of($value, $this->baseStateClass)) { + + if ( ! is_subclass_of( $value, $this->baseStateClass ) ) { return null; } - + $stateClassName = $value::config()['default']; - - $state = new $stateClassName($model); - - return $state; + + return new $stateClassName( $model ); } + /** + * @throws Exception + */ public function set($model, $key, $value, $attributes) { if ( is_null( $value ) ) return null; - - if (! is_subclass_of($value, $this->baseStateClass)) { - throw new \Exception('Invalid state class.'); + + if ( ! is_subclass_of( $value, $this->baseStateClass ) ) { + throw new Exception( 'Invalid state class.' ); } - - $value = new $value($model); - - if ($value instanceof $this->baseStateClass) { - $value->setField($key); + + $value = new $value( $model ); + + if ( $value instanceof $this->baseStateClass ) { + $value->setField( $key ); } - + return $value->getMorphClass(); } } \ No newline at end of file diff --git a/tests/Casts/UserCastedObject.php b/tests/Casts/UserCastedObject.php index 8694511..5d8e668 100644 --- a/tests/Casts/UserCastedObject.php +++ b/tests/Casts/UserCastedObject.php @@ -6,40 +6,41 @@ abstract class UserCastedObject implements Castable { + public static $name; + public $model; + public $stateConfig; + public $field; + public function __construct($model) { $this->model = $model; $this->stateConfig = static::config(); } - - public static function config() - { + + public static function config() { return [ 'default' => null, ]; } public static function castUsing(array $arguments) { - return new StateCaster(static::class); + return new StateCaster( static::class ); } - - public function setField(string $field): self - { - $this->field = $field; - - return $this; - } - - public static function getMorphClass(): string - { - return static::$name ?? static::class; - } - - public function make(string $name, $model) - { - if (is_null($name)) { + + public function setField(string $field): self { + $this->field = $field; + + return $this; + } + + public static function getMorphClass(): string { + return static::$name ?? static::class; + } + + public function make(?string $name, $model) { + if ( is_null( $name ) ) { return null; } - - return new $name($model); + + return new $name( $model ); } } \ No newline at end of file diff --git a/tests/Casts/UserState/DefaultState.php b/tests/Casts/UserState/DefaultState.php index 600898e..d669192 100644 --- a/tests/Casts/UserState/DefaultState.php +++ b/tests/Casts/UserState/DefaultState.php @@ -4,12 +4,13 @@ class DefaultState extends State { - public string $description; + public $description; + /** @noinspection PhpMissingParentConstructorInspection */ public function __construct() { $this->description = $this->description(); } - + public function description(): string { return 'This is a default description.'; } diff --git a/tests/MetableTest.php b/tests/MetableTest.php index 866d4da..97e5eca 100644 --- a/tests/MetableTest.php +++ b/tests/MetableTest.php @@ -52,14 +52,14 @@ public static function setUpBeforeClass(): void { public function testCast() { $user = new UserTest; - + $this->assertNull( $user->state, 'Casted object should be null by default' ); - + $user->state = DefaultState::class; - - $this->assertTrue( $user->state instanceof DefaultState, 'Casted object should be instanceof DefaultState' ); + + $this->assertInstanceOf( DefaultState::class, $user->state, 'Casted object should be instanceof DefaultState' ); } - + public function testFluentMeta() { $user = new UserTest; diff --git a/tests/Models/UserTest.php b/tests/Models/UserTest.php index dfd0841..347bf45 100644 --- a/tests/Models/UserTest.php +++ b/tests/Models/UserTest.php @@ -21,7 +21,7 @@ class UserTest extends Model public $hideMeta = false; public $disableFluentMeta = false; - + protected $casts = [ 'state' => State::class, ]; diff --git a/tests/Traits/HasUserStates.php b/tests/Traits/HasUserStates.php index 816e92b..89c620b 100644 --- a/tests/Traits/HasUserStates.php +++ b/tests/Traits/HasUserStates.php @@ -3,53 +3,50 @@ namespace Kodeine\Metable\Tests\Traits; use Kodeine\Metable\Tests\Casts\UserState\State; +use Kodeine\Metable\Tests\Casts\UserCastedObject; trait HasUserStates { public $casted = [ 'state' => State::class, ]; - - public static function bootHasUserCasts() - { - self::creating( function ( $model ) { + + public static function bootHasUserCasts() { + self::creating( function ($model) { $model->setDefaultCastedProperties(); } ); } - - public function initializeHasUserCasts() - { + + public function initializeHasUserCasts() { $this->setDefaultCastedProperties(); } - - private function getStateConfigs() - { + + private function getStateConfigs() { $casts = $this->getCasts(); $states = []; - + foreach ($casts as $prop => $state) { - if (! is_subclass_of($state, UserCastedObject::class)) { + if ( ! is_subclass_of( $state, UserCastedObject::class ) ) { continue; } - + $states[$prop] = $state::config(); } - + return $states; } - - private function setDefaultCastedProperties() - { - foreach ( $this->getStateConfigs() as $prop => $config ) { - if ($this->{$prop} === null) { + + private function setDefaultCastedProperties() { + foreach ($this->getStateConfigs() as $prop => $config) { + if ( $this->{$prop} === null ) { continue; } - + if ( ! isset( $config['default'] ) ) { continue; } - + $this->{$prop} = $config['default']; } } From 4b475337dc381b0cff5b50852f82c777a6785fc6 Mon Sep 17 00:00:00 2001 From: Siamak Salimi Date: Wed, 5 Jul 2023 23:57:39 +0330 Subject: [PATCH 10/10] Updated CHANGELOG.md --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d61cb1f..b50f096 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Release Notes +## v2.2.1 + +### Fixed + +* Fixed a bug that was caused by a null value or a null cast. [PR #104](https://github.com/kodeine/laravel-meta/pull/104) + ## v2.2.0 ### Added