Skip to content

Commit 49768c3

Browse files
committed
fix:
- Object casting of Arrayable/DateTime Objects
1 parent deff929 commit 49768c3

File tree

3 files changed

+37
-32
lines changed

3 files changed

+37
-32
lines changed

README.md

+1-7
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,9 @@ This Laravel package aims to store and manage user settings/preferences in a sim
4848
### Roadmap
4949

5050
- Additional inbuilt Custom Rules -> v2.x
51-
- Api Enum support -> v2.2
51+
- Api Enum support -> v2.1
5252
- Suggestions are welcome
5353

54-
### Known Issues
55-
`Cast::Object`: internally, laravel tries the toArray()
56-
if the object implements Arrayable, resulting in an array rather than an object as required by the validation
57-
58-
Consider sticking to Enums or Primitive casts for now
59-
6054
## Installation
6155

6256
You can install the package via composer:

src/Models/Preference.php

+14
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,18 @@ class Preference extends BaseModel
5555
'nullable' => 'boolean',
5656
];
5757

58+
public function attributesToArray()
59+
{
60+
61+
$attributes = parent::attributesToArray();
62+
return array_merge($attributes,
63+
[
64+
'default_value' => $this->default_value,
65+
'policy' => $this->policy,
66+
'rule' => $this->rule,
67+
'cast' => $this->cast,
68+
],
69+
);
70+
}
71+
5872
}

tests/ApiTest/WorkflowTest.php

+22-25
Original file line numberDiff line numberDiff line change
@@ -177,29 +177,26 @@ public function test_timestamp_workflow()
177177
$lastLogin->assertJson(['value' => $time]);
178178
}
179179

180-
// /** @test */
181-
// public function test_backed_enum_workflow()
182-
// {
183-
// PreferenceBuilder::init(General::CONFIG, Cast::BACKED_ENUM)
184-
// ->withDefaultValue(OtherPreferences::QUALITY)
185-
// ->create();
186-
//
187-
// $status = $this->patch(route('preferences.user.general.update', ['scope_id' => 1, 'preference' => 'config']), ['value' => OtherPreferences::CONFIG->value]);
188-
// $status->assertJson(['value' => OtherPreferences::CONFIG]);
189-
// }
190-
//
191-
//
192-
193-
//
194-
// /** @test */
195-
// public function test_object_workflow()
196-
// {
197-
// // issue: if ($attributes[$key] instanceof Arrayable) {
198-
// PreferenceBuilder::init(General::CONFIG, Cast::OBJECT)
199-
// ->withDefaultValue($this->adminUser)
200-
// ->create();
201-
//
202-
// $profile = $this->patch(route('preferences.user.profile.update', ['scope_id' => 1, 'preference' => 'config']), ['value' => ['name' => 'Jane Doe', 'email' => '[email protected]']]);
203-
// $profile->assertRedirect();
204-
// }
180+
/** @test */
181+
public function test_backed_enum_workflow()
182+
{
183+
PreferenceBuilder::init(General::CONFIG, Cast::BACKED_ENUM)
184+
->withDefaultValue(OtherPreferences::QUALITY)
185+
->create();
186+
187+
$config = $this->patch(route('preferences.user.general.update', ['scope_id' => 1, 'preference' => 'config']), ['value' => OtherPreferences::CONFIG->value]);
188+
$config->assertRedirect();
189+
}
190+
191+
192+
/** @test */
193+
public function test_object_workflow()
194+
{
195+
PreferenceBuilder::init(General::CONFIG, Cast::OBJECT)
196+
->withDefaultValue($this->adminUser)
197+
->create();
198+
199+
$profile = $this->patch(route('preferences.user.general.update', ['scope_id' => 1, 'preference' => 'config']), ['value' => ['name' => 'Jane Doe', 'email' => '[email protected]']]);
200+
$profile->assertRedirect();
201+
}
205202
}

0 commit comments

Comments
 (0)