Skip to content

Commit

Permalink
Enum values no longer get force-added to the result array on toArray …
Browse files Browse the repository at this point in the history
…conversion if the field is absent in the attribute list
  • Loading branch information
fulopattila122 committed Nov 3, 2020
1 parent f17db16 commit 0ec0982
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/vendor/
composer.lock
.idea/



/.phpunit.result.cache
20 changes: 10 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ env:
- ENUM=2 LARAVEL=5.6
- ENUM=2 LARAVEL=5.7
- ENUM=2 LARAVEL=5.8
- ENUM=2 LARAVEL=6.18
- ENUM=2 LARAVEL=6.19
- ENUM=2 LARAVEL=7.0
- ENUM=2 LARAVEL=7.28
- ENUM=2 LARAVEL=8.0
- ENUM=2 LARAVEL=8.12
- ENUM=3 LARAVEL=5.4
- ENUM=3 LARAVEL=5.5
- ENUM=3 LARAVEL=5.6
- ENUM=3 LARAVEL=5.7
- ENUM=3 LARAVEL=5.8
- ENUM=3 LARAVEL=6.18
- ENUM=3 LARAVEL=6.19
- ENUM=3 LARAVEL=7.0
- ENUM=3 LARAVEL=7.28
- ENUM=3 LARAVEL=8.0
- ENUM=3 LARAVEL=8.12

matrix:
exclude:
- php: '7.1'
env: 'ENUM=2 LARAVEL=6.18'
env: 'ENUM=2 LARAVEL=6.19'
- php: '7.1'
env: 'ENUM=3 LARAVEL=6.18'
env: 'ENUM=3 LARAVEL=6.19'
- php: '7.1'
env: 'ENUM=2 LARAVEL=7.0'
- php: '7.1'
Expand All @@ -45,13 +45,13 @@ matrix:
- php: '7.1'
env: 'ENUM=3 LARAVEL=7.28'
- php: '7.1'
env: 'ENUM=2 LARAVEL=8.0'
env: 'ENUM=2 LARAVEL=8.12'
- php: '7.1'
env: 'ENUM=3 LARAVEL=8.0'
env: 'ENUM=3 LARAVEL=8.12'
- php: '7.2'
env: 'ENUM=2 LARAVEL=8.0'
env: 'ENUM=2 LARAVEL=8.12'
- php: '7.2'
env: 'ENUM=3 LARAVEL=8.0'
env: 'ENUM=3 LARAVEL=8.12'

script:
- vendor/bin/phpunit -c phpunit.xml tests/
Expand Down
5 changes: 4 additions & 1 deletion src/CastsEnums.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ protected function isEnumAttribute($key)
protected function addEnumAttributesToArray(array $attributes): array
{
foreach ($this->enums as $key => $value) {
$attributes[$key] = $this->getAttributeValue($key)->value();
// Don't set if the field is not present (pluck or not selecting them in the SQL can cause it)
if (isset($this->attributes[$key])) {
$attributes[$key] = $this->getAttributeValue($key)->value();
}
}

return $attributes;
Expand Down
14 changes: 13 additions & 1 deletion tests/EnumToArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public function returns_enum_default_string_value_when_attribute_is_null()
}

$order = new Order([
'number' => 'abc123'
'number' => 'abc123',
'status' => null
]);

$array = $order->attributesToArray();
Expand All @@ -88,6 +89,17 @@ public function returns_enum_default_string_value_when_attribute_is_null()
$this->assertEquals($array['status'], OrderStatus::__DEFAULT);
}

/** @test */
public function it_does_not_set_the_attribute_key_if_the_attribute_is_absent_in_the_model()
{
$order = new Order([
'number' => 'abc123'
]);

$array = $order->attributesToArray();
$this->assertArrayNotHasKey('status', $array);
}

/**
* @test
*/
Expand Down

0 comments on commit 0ec0982

Please sign in to comment.