From d470daf9575b00ca61abc3d0adee5a623af62acf Mon Sep 17 00:00:00 2001 From: Mark Boessenkool Date: Wed, 2 Aug 2023 14:40:34 +0300 Subject: [PATCH 1/4] added tests to check if the hidden property is used --- tests/EnumToArrayTest.php | 62 +++++++++++++++++++++++++++++++++ tests/Models/Visibility.php | 38 ++++++++++++++++++++ tests/Models/VisibilityTalk.php | 25 +++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 tests/Models/Visibility.php create mode 100644 tests/Models/VisibilityTalk.php diff --git a/tests/EnumToArrayTest.php b/tests/EnumToArrayTest.php index 624cfc0..b8daaa9 100644 --- a/tests/EnumToArrayTest.php +++ b/tests/EnumToArrayTest.php @@ -18,6 +18,8 @@ use Konekt\Enum\Eloquent\Tests\Models\OrderStatus; use Konekt\Enum\Eloquent\Tests\Models\OrderStatusV2; use Konekt\Enum\Eloquent\Tests\Models\OrderV2; +use Konekt\Enum\Eloquent\Tests\Models\Visibility; +use Konekt\Enum\Eloquent\Tests\Models\VisibilityTalk; class EnumToArrayTest extends TestCase { @@ -129,4 +131,64 @@ public function returns_enum_v2_default_string_value_when_attribute_is_null() $this->assertIsString($array['status']); $this->assertEquals($array['status'], OrderStatusV2::__default); } + + /** @test */ + public function returns_no_enum_if_hidden() + { + $normal = new Visibility([ + 'number' => 'na321', + 'talk1' => VisibilityTalk::BLABLA, + 'talk2' => VisibilityTalk::YADDA, + ]); + + $array = $normal->attributesToArray(); + + $this->assertArrayHasKey('number', $array); + $this->assertArrayNotHasKey('talk1', $array); + $this->assertArrayHasKey('talk2', $array); + $this->assertIsString($array['talk2']); + $this->assertEquals($array['talk2'], VisibilityTalk::YADDA); + } + + /** @test */ + public function returns_no_enum_if_hidden_dynamic() + { + $dynamic_hidden = new Visibility([ + 'number' => 'na654', + 'talk1' => VisibilityTalk::BLABLA, + 'talk2' => VisibilityTalk::YADDA, + ]); + + $dynamic_hidden->makeHidden('talk2'); + + $array = $dynamic_hidden->attributesToArray(); + + $this->assertArrayHasKey('number', $array); + $this->assertArrayNotHasKey('talk1', $array); + $this->assertArrayNotHasKey('talk2', $array); + } + + /** @test */ + public function returns_enum_if_hidden_made_visible() + { + $made_visible = new Visibility([ + 'number' => 'na987', + 'talk1' => VisibilityTalk::BLABLA, + 'talk2' => VisibilityTalk::YADDA, + ]); + + $made_visible->makeVisible('talk1'); + + $array = $made_visible->attributesToArray(); + + $this->assertArrayHasKey('number', $array); + + $this->assertArrayHasKey('talk1', $array); + $this->assertIsString($array['talk1']); + $this->assertEquals($array['talk1'], VisibilityTalk::BLABLA); + + $this->assertArrayHasKey('talk2', $array); + $this->assertIsString($array['talk2']); + $this->assertEquals($array['talk2'], VisibilityTalk::YADDA); + } } diff --git a/tests/Models/Visibility.php b/tests/Models/Visibility.php new file mode 100644 index 0000000..7ecdfd6 --- /dev/null +++ b/tests/Models/Visibility.php @@ -0,0 +1,38 @@ + 'boolean' + ]; + + protected $enums = [ + 'talk1' => VisibilityTalk::class, + 'talk2' => VisibilityTalk::class, + ]; + + protected $hidden = [ + 'talk1', + ]; +} diff --git a/tests/Models/VisibilityTalk.php b/tests/Models/VisibilityTalk.php new file mode 100644 index 0000000..2c81f0b --- /dev/null +++ b/tests/Models/VisibilityTalk.php @@ -0,0 +1,25 @@ + Date: Wed, 2 Aug 2023 14:42:11 +0300 Subject: [PATCH 2/4] use Eloquent getArrayableItems() to return only the attributes that are visible --- src/CastsEnums.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/CastsEnums.php b/src/CastsEnums.php index 3eb6250..c00ab64 100644 --- a/src/CastsEnums.php +++ b/src/CastsEnums.php @@ -81,7 +81,11 @@ public function setAttribute($key, $value) */ public function attributesToArray() { - return $this->addEnumAttributesToArray(parent::attributesToArray()); + return $this->getArrayableItems( + $this->addEnumAttributesToArray( + parent::attributesToArray() + ) + ); } /** From 734213457c17a87e3c4d605fe3de9a2e5cd67253 Mon Sep 17 00:00:00 2001 From: Mark Boessenkool Date: Wed, 2 Aug 2023 14:43:47 +0300 Subject: [PATCH 3/4] added entry into changelog + made it a new version --- Changelog.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Changelog.md b/Changelog.md index 0e5f491..8731720 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,6 +1,11 @@ # Changelog ### Konekt Enum Eloquent +## 1.10.0 +##### 2023-08-03 + +- Fixed issue where CastEnum did not take in account hidden or visible properties from Eloquent + ## 1.9.0 ##### 2023-02-16 From d844e27414fc953536160f12bbc2e9bd8eba6ba2 Mon Sep 17 00:00:00 2001 From: Attila Fulop <1162360+fulopattila122@users.noreply.github.com> Date: Mon, 25 Sep 2023 12:44:03 +0200 Subject: [PATCH 4/4] Move the changes to an "Unreleased" section --- Changelog.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Changelog.md b/Changelog.md index 8731720..416d2ab 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,8 +1,8 @@ # Changelog ### Konekt Enum Eloquent -## 1.10.0 -##### 2023-08-03 +## Unreleased +##### 2023-XX-YY - Fixed issue where CastEnum did not take in account hidden or visible properties from Eloquent