Skip to content

Commit

Permalink
fix:
Browse files Browse the repository at this point in the history
 - Object casting of Arrayable/DateTime Objects
  • Loading branch information
matteoc99 committed Apr 23, 2024
1 parent deff929 commit 49768c3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 14 additions & 0 deletions src/Models/Preference.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
);
}

}
47 changes: 22 additions & 25 deletions tests/ApiTest/WorkflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '[email protected]']]);
// $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' => '[email protected]']]);
$profile->assertRedirect();
}
}

0 comments on commit 49768c3

Please sign in to comment.