From d470daf9575b00ca61abc3d0adee5a623af62acf Mon Sep 17 00:00:00 2001 From: Mark Boessenkool Date: Wed, 2 Aug 2023 14:40:34 +0300 Subject: [PATCH] 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 @@ +