From fb609c05299fa72017f2cede6d1dc706ece636a1 Mon Sep 17 00:00:00 2001 From: Edwin Daniels Date: Fri, 3 May 2024 15:20:50 -0700 Subject: [PATCH 1/3] Bug fix for fetching set of defaults, to avoid always adding a null to fields that have no default defined. --- src/Models/Model.php | 25 +++++++++++++++++++++---- tests/wpunit/ModelTest.php | 12 ++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/Models/Model.php b/src/Models/Model.php index 1eb9a3f..e983546 100644 --- a/src/Models/Model.php +++ b/src/Models/Model.php @@ -124,6 +124,19 @@ public function getOriginal( string $key = null ) { return $key ? $this->original[ $key ] : $this->original; } + /** + * Check if there is a default value for a property. + * + * @since TBD + * + * @param string $key Property name. + * + * @return bool + */ + public function hasDefault( string $key ): bool { + return is_array( $this->properties[ $key ] ) && array_key_exists( 1, $this->properties[ $key ] ); + } + /** * Returns the default value for a property if one is provided, otherwise null. * @@ -134,9 +147,11 @@ public function getOriginal( string $key = null ) { * @return mixed|null */ protected function getPropertyDefault( string $key ) { - return is_array( $this->properties[ $key ] ) && isset( $this->properties[ $key ][1] ) - ? $this->properties[ $key ][1] - : null; + if ( $this->hasDefault( $key ) ) { + return $this->properties[ $key ][1]; + } + + return null; } /** @@ -149,7 +164,9 @@ protected function getPropertyDefault( string $key ) { protected function getPropertyDefaults() : array { $defaults = []; foreach ( array_keys( $this->properties ) as $property ) { - $defaults[ $property ] = $this->getPropertyDefault( $property ); + if ( $this->hasDefault( $property ) ) { + $defaults[ $property ] = $this->getPropertyDefault( $property ); + } } return $defaults; diff --git a/tests/wpunit/ModelTest.php b/tests/wpunit/ModelTest.php index b8e05c8..0648736 100644 --- a/tests/wpunit/ModelTest.php +++ b/tests/wpunit/ModelTest.php @@ -277,6 +277,18 @@ public function testShouldSetMultipleAttributes() { $this->assertEquals( 'Skywalker', $model->lastName ); } + /** + * @since TBD + * + * @return void + */ + public function testHasDefaultValue() { + $model = new MockModel(); + $this->assertTrue( $model->hasDefault( 'firstName' ) ); + $this->assertTrue( $model->hasDefault( 'emails' ) ); + $this->assertFalse( $model->hasDefault( 'lastName' ) ); + } + /** * @since 1.0.0 * From 5361486139993167f350e955434e9d17c8ca2241 Mon Sep 17 00:00:00 2001 From: Edwin Daniels Date: Fri, 10 May 2024 14:14:29 -0700 Subject: [PATCH 2/3] Adding isSet, changing scope of hasDefault to protected. --- src/Models/Model.php | 14 +++++++++++++- tests/wpunit/ModelTest.php | 15 +++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/Models/Model.php b/src/Models/Model.php index e983546..55947a9 100644 --- a/src/Models/Model.php +++ b/src/Models/Model.php @@ -124,6 +124,18 @@ public function getOriginal( string $key = null ) { return $key ? $this->original[ $key ] : $this->original; } + /** + * Whether the property is set or not. This is different from isset() because this considers a `null` value as + * being set. Defaults are considered set as well. + * + * @since TBD + * + * @return boolean + */ + public function isSet( string $key ): bool { + return array_key_exists( $key, $this->attributes ) || $this->hasDefault( $key ); + } + /** * Check if there is a default value for a property. * @@ -133,7 +145,7 @@ public function getOriginal( string $key = null ) { * * @return bool */ - public function hasDefault( string $key ): bool { + protected function hasDefault( string $key ): bool { return is_array( $this->properties[ $key ] ) && array_key_exists( 1, $this->properties[ $key ] ); } diff --git a/tests/wpunit/ModelTest.php b/tests/wpunit/ModelTest.php index 0648736..8627616 100644 --- a/tests/wpunit/ModelTest.php +++ b/tests/wpunit/ModelTest.php @@ -282,11 +282,18 @@ public function testShouldSetMultipleAttributes() { * * @return void */ - public function testHasDefaultValue() { + public function testIsSet() { $model = new MockModel(); - $this->assertTrue( $model->hasDefault( 'firstName' ) ); - $this->assertTrue( $model->hasDefault( 'emails' ) ); - $this->assertFalse( $model->hasDefault( 'lastName' ) ); + + // This has a default so we should see as set. + $this->assertTrue( $model->isSet( 'firstName' ) ); + + // No default, and hasn't been set so show false. + $this->assertFalse( $model->isSet( 'lastName' ) ); + + // Now we set it, so it should be true - even though we set it to null. + $model->lastName = null; + $this->assertTrue( $model->isSet( 'lastName' ) ); } /** From 4f43a8d1830fc52964b4246cd62277a0aee41c76 Mon Sep 17 00:00:00 2001 From: Edwin Daniels Date: Fri, 10 May 2024 14:18:59 -0700 Subject: [PATCH 3/3] Change @since tag. --- src/Models/Model.php | 4 ++-- tests/wpunit/ModelTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Models/Model.php b/src/Models/Model.php index 55947a9..5ddc042 100644 --- a/src/Models/Model.php +++ b/src/Models/Model.php @@ -128,7 +128,7 @@ public function getOriginal( string $key = null ) { * Whether the property is set or not. This is different from isset() because this considers a `null` value as * being set. Defaults are considered set as well. * - * @since TBD + * @unreleased * * @return boolean */ @@ -139,7 +139,7 @@ public function isSet( string $key ): bool { /** * Check if there is a default value for a property. * - * @since TBD + * @unreleased * * @param string $key Property name. * diff --git a/tests/wpunit/ModelTest.php b/tests/wpunit/ModelTest.php index 8627616..1adf2e4 100644 --- a/tests/wpunit/ModelTest.php +++ b/tests/wpunit/ModelTest.php @@ -278,7 +278,7 @@ public function testShouldSetMultipleAttributes() { } /** - * @since TBD + * @unreleased * * @return void */