diff --git a/README.md b/README.md index cf9db5f..f904294 100644 --- a/README.md +++ b/README.md @@ -48,15 +48,9 @@ This Laravel package aims to store and manage user settings/preferences in a sim ### Roadmap - Additional inbuilt Custom Rules -> v2.x -- Api Enum support -> v2.2 +- Api Enum support -> v2.1 - Suggestions are welcome -### Known Issues -`Cast::Object`: internally, laravel tries the toArray() -if the object implements Arrayable, resulting in an array rather than an object as required by the validation - -Consider sticking to Enums or Primitive casts for now - ## Installation You can install the package via composer: diff --git a/src/Models/Preference.php b/src/Models/Preference.php index 0449911..37b7bf1 100644 --- a/src/Models/Preference.php +++ b/src/Models/Preference.php @@ -55,4 +55,18 @@ class Preference extends BaseModel 'nullable' => 'boolean', ]; + public function attributesToArray() + { + + $attributes = parent::attributesToArray(); + return array_merge($attributes, + [ + 'default_value' => $this->default_value, + 'policy' => $this->policy, + 'rule' => $this->rule, + 'cast' => $this->cast, + ], + ); + } + } \ No newline at end of file diff --git a/tests/ApiTest/WorkflowTest.php b/tests/ApiTest/WorkflowTest.php index b3055b1..53157a4 100644 --- a/tests/ApiTest/WorkflowTest.php +++ b/tests/ApiTest/WorkflowTest.php @@ -177,29 +177,26 @@ public function test_timestamp_workflow() $lastLogin->assertJson(['value' => $time]); } -// /** @test */ -// public function test_backed_enum_workflow() -// { -// PreferenceBuilder::init(General::CONFIG, Cast::BACKED_ENUM) -// ->withDefaultValue(OtherPreferences::QUALITY) -// ->create(); -// -// $status = $this->patch(route('preferences.user.general.update', ['scope_id' => 1, 'preference' => 'config']), ['value' => OtherPreferences::CONFIG->value]); -// $status->assertJson(['value' => OtherPreferences::CONFIG]); -// } -// -// - -// -// /** @test */ -// public function test_object_workflow() -// { -// // issue: if ($attributes[$key] instanceof Arrayable) { -// PreferenceBuilder::init(General::CONFIG, Cast::OBJECT) -// ->withDefaultValue($this->adminUser) -// ->create(); -// -// $profile = $this->patch(route('preferences.user.profile.update', ['scope_id' => 1, 'preference' => 'config']), ['value' => ['name' => 'Jane Doe', 'email' => 'janedoe@example.com']]); -// $profile->assertRedirect(); -// } + /** @test */ + public function test_backed_enum_workflow() + { + PreferenceBuilder::init(General::CONFIG, Cast::BACKED_ENUM) + ->withDefaultValue(OtherPreferences::QUALITY) + ->create(); + + $config = $this->patch(route('preferences.user.general.update', ['scope_id' => 1, 'preference' => 'config']), ['value' => OtherPreferences::CONFIG->value]); + $config->assertRedirect(); + } + + + /** @test */ + public function test_object_workflow() + { + PreferenceBuilder::init(General::CONFIG, Cast::OBJECT) + ->withDefaultValue($this->adminUser) + ->create(); + + $profile = $this->patch(route('preferences.user.general.update', ['scope_id' => 1, 'preference' => 'config']), ['value' => ['name' => 'Jane Doe', 'email' => 'janedoe@example.com']]); + $profile->assertRedirect(); + } } \ No newline at end of file