diff --git a/.gitignore b/.gitignore index 3d1741c..b104b34 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,4 @@ /vendor/ composer.lock .idea/ - - - +/.phpunit.result.cache diff --git a/.travis.yml b/.travis.yml index fc5cab0..fbe9c65 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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' @@ -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/ diff --git a/src/CastsEnums.php b/src/CastsEnums.php index afd6f8c..6003e2b 100644 --- a/src/CastsEnums.php +++ b/src/CastsEnums.php @@ -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; diff --git a/tests/EnumToArrayTest.php b/tests/EnumToArrayTest.php index 0d00513..f3a9396 100644 --- a/tests/EnumToArrayTest.php +++ b/tests/EnumToArrayTest.php @@ -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(); @@ -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 */