Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Commit

Permalink
prefer eloquent mutators
Browse files Browse the repository at this point in the history
  • Loading branch information
avrahamappel committed Jul 16, 2019
1 parent 14c5024 commit c43ede0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
</testsuite>
</testsuites>
<logging>
<log type="testdox-text" target="./tests/testdox.txt"/>
<log type="testdox-text" target="testdox"/>
</logging>
</phpunit>
14 changes: 11 additions & 3 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function mutateForStorage($attribute)
*/
public function hasGetMutator($key)
{
return $this->isMoneyAttribute($key) ?: parent::hasGetMutator($key);
return parent::hasGetMutator($key) || $this->isMoneyAttribute($key);
}

/**
Expand All @@ -55,11 +55,15 @@ public function hasGetMutator($key)
*/
protected function mutateAttribute($key, $value)
{
if (parent::hasGetMutator($key)) {
return parent::mutateAttribute($key, $value);
}

if ($this->isMoneyAttribute($key)) {
return $value ? $this->mutateForDisplay($value) : $value;
}

return parent::mutateAttribute($key, $value);
return $value;
}

/**
Expand All @@ -70,7 +74,7 @@ protected function mutateAttribute($key, $value)
*/
public function hasSetMutator($key)
{
return $this->isMoneyAttribute($key) ? true : parent::hasSetMutator($key);
return parent::hasSetMutator($key) || $this->isMoneyAttribute($key);
}

/**
Expand All @@ -82,6 +86,10 @@ public function hasSetMutator($key)
*/
protected function setMutatedAttributeValue($key, $value)
{
if (parent::hasSetMutator($key)) {
return parent::setMutatedAttributeValue($key, $value);
}

if ($this->isMoneyAttribute($key)) {
$value = $this->mutateForStorage($value);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/testdox.txt → testdox
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ DisplayAttributes
[x] Displays monetary attributes correctly in array
[x] Stores display monetary attribute in base attribute
[x] Returns null if value is not set
[x] Prefers get mutator method if one exists
[x] Prefers set mutator method if one exists

30 changes: 30 additions & 0 deletions tests/DisplayAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ class Model extends Eloquent
protected $moneyAttributes = ['price'];
}

class ModelWithGetMutator extends Model
{
public function getPriceAttribute($price)
{
return $price;
}
}

class ModelWithSetMutator extends Model
{
public function setPriceAttribute($price)
{
$this->attributes['price'] = $price;
}
}

class DisplayAttributesTest extends TestCase
{
public function test_displays_monetary_attributes_correctly()
Expand Down Expand Up @@ -40,4 +56,18 @@ public function test_returns_null_if_value_is_not_set()

self::assertNull($model->price);
}

public function test_prefers_get_mutator_method_if_one_exists()
{
$model = new ModelWithGetMutator(['price' => 5.99]);

self::assertEquals(599, $model->price);
}

public function test_prefers_set_mutator_method_if_one_exists()
{
$model = new ModelWithSetMutator(['price' => 599]);

self::assertEquals(599, $model->getAttributes()['price']);
}
}

0 comments on commit c43ede0

Please sign in to comment.