Skip to content

Commit

Permalink
refs #2923 Allow mutation elocument model on getContent
Browse files Browse the repository at this point in the history
  • Loading branch information
tabuna committed Nov 19, 2024
1 parent 0b14b8b commit 19dbc19
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Screen/AsSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public function getContent(string $field): mixed

return $this->getAttribute($field) // Try to get the field value from the object's attributes.
?? Arr::get($this->getRelations(), $field) // Try to get the field value from the object's relations.
?? Arr::get($this, $field); // Try to get the field value from the object's array representation.
?? Arr::get($this->attributesToArray(), $field); // Try to get the field value from the object's array representation.
}
}
19 changes: 18 additions & 1 deletion tests/Unit/Screen/SourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,20 @@ protected function setUp(): void
{
use AsSource;

protected $attributes = [
'preferences' => '{"theme":"dark","notifications":{"email":true,"sms":false}}',
];

protected $fillable = [
'id',
'name',
'options',
'preferences',
];

protected $casts = [
'options' => 'array',
'options' => 'array',
'preferences' => 'json',
];

public function getGreetingAttribute()
Expand Down Expand Up @@ -81,6 +87,17 @@ public function testGetArrayAttribute(): void
$this->assertTrue($this->model->getContent('options.skills.php'));
}

public function testGetJsonAttribute(): void
{
$this->assertIsArray($this->model->getContent('preferences.notifications'));
$this->assertArrayHasKey('email', $this->model->getContent('preferences.notifications'));
$this->assertTrue($this->model->getContent('preferences.notifications.email'));
$this->assertFalse($this->model->getContent('preferences.notifications.sms'));

$this->assertIsString($this->model->getContent('preferences.theme'));
$this->assertSame('dark', $this->model->getContent('preferences.theme'));
}

public function testGetRelation(): void
{
$this->assertIsInt($this->model->getContent('many.three'));
Expand Down

0 comments on commit 19dbc19

Please sign in to comment.