diff --git a/.travis.yml b/.travis.yml index f39fe1e..c7f6f75 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,50 +4,41 @@ cache: directories: - "$HOME/.composer/cache" +php: + - '7.1' + - '7.2' + - '7.3' + +env: + - ENUM=2 LARAVEL=5.4 + - ENUM=2 LARAVEL=5.5 + - ENUM=2 LARAVEL=5.6 + - ENUM=2 LARAVEL=5.7 + - ENUM=2 LARAVEL=5.8 + - 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 + matrix: include: - php: 7.0 - env: LARAVEL=5.0 - - php: 7.0 - env: LARAVEL=5.1 + env: ENUM=2 LARAVEL=5.0 - php: 7.0 - env: LARAVEL=5.2 + env: ENUM=2 LARAVEL=5.1 - php: 7.0 - env: LARAVEL=5.3 + env: ENUM=2 LARAVEL=5.2 - php: 7.0 - env: LARAVEL=5.4 + env: ENUM=2 LARAVEL=5.3 - php: 7.0 - env: LARAVEL=5.5 - - php: 7.1 - env: LARAVEL=5.4 - - php: 7.1 - env: LARAVEL=5.5 - - php: 7.1 - env: LARAVEL=5.6 - - php: 7.1 - env: LARAVEL=5.7 - - php: 7.2 - env: LARAVEL=5.4 - - php: 7.2 - env: LARAVEL=5.5 - - php: 7.2 - env: LARAVEL=5.6 - - php: 7.2 - env: LARAVEL=5.7 - - php: 7.3 - env: LARAVEL=5.4 - - php: 7.3 - env: LARAVEL=5.5 - - php: 7.3 - env: LARAVEL=5.6 - - php: 7.3 - env: LARAVEL=5.7 - + env: ENUM=2 LARAVEL=5.4 script: - vendor/bin/phpunit -c phpunit.xml tests/ before_install: +- composer require "konekt/enum:${ENUM}.*" --no-update -v - composer require "illuminate/database:${LARAVEL}.*" --no-update -v - composer require "illuminate/events:${LARAVEL}.*" --no-update -v diff --git a/Changelog.md b/Changelog.md index 4348f94..42d0d5e 100644 --- a/Changelog.md +++ b/Changelog.md @@ -7,6 +7,8 @@ ### Unreleased - Added make:enum commands (when using in a Laravel application) +- Enum 3.0.0 is supported +- Updated references to https://konekt.dev/enum website ## 1.2 diff --git a/README.md b/README.md index 903f30b..8d727c1 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,9 @@ [![StyleCI](https://styleci.io/repos/105900484/shield?branch=master)](https://styleci.io/repos/105900484) [![MIT Software License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE.md) -This package provides support for auto casting [konekt enum](https://github.com/artkonekt/enum) fields in [Eloquent models](https://laravel.com/docs/5.4/eloquent-mutators). +This package provides support for auto casting [konekt enum](https://konekt.dev/enum) fields in [Eloquent models](https://laravel.com/docs/5.4/eloquent-mutators). -> Supported Konekt Enum versions are 2.0+ and Eloquent 5.0+ +> Supported Konekt Enum versions are 2.0+ or 3.0+ and Eloquent 5.0+ [Changelog](Changelog.md) @@ -33,7 +33,8 @@ use Konekt\Enum\Enum; class OrderStatus extends Enum { - const __default = self::PENDING; + const __DEFAULT = self::PENDING; + // const __default = self::PENDING; // usage of default in v2.x const PENDING = 'pending'; const CANCELLED = 'cancelled'; @@ -170,4 +171,4 @@ used by the forms library to obtain form field value. Enjoy! -For detailed usage of konekt enums refer to the [Konekt Enum Documentation](https://artkonekt.github.io/enum). +For detailed usage of konekt enums refer to the [Konekt Enum Documentation](https://konekt.dev/enum). diff --git a/composer.json b/composer.json index e85f80e..2f3e568 100644 --- a/composer.json +++ b/composer.json @@ -15,12 +15,13 @@ ], "require": { "php": ">=7.0.0", - "konekt/enum": "^2.0.2", + "konekt/enum": "^2.0.2 || ^3.0.0", "illuminate/database": "5.*" }, "require-dev": { "phpunit/phpunit": "6.3 - 7.4", - "illuminate/events": "5.*" + "illuminate/events": "5.*", + "ocramius/package-versions": "^1.2" }, "autoload": { "psr-4": { "Konekt\\Enum\\Eloquent\\": "src/" }, diff --git a/src/Commands/stubs/enum.boot.stub b/src/Commands/stubs/enum.boot.stub index 8de7267..a88eecb 100644 --- a/src/Commands/stubs/enum.boot.stub +++ b/src/Commands/stubs/enum.boot.stub @@ -6,7 +6,8 @@ use Konekt\Enum\Enum; class DummyClass extends Enum { - const __default = self::OPTION_ONE; + const __DEFAULT = self::OPTION_ONE; + // const __default = self::OPTION_ONE; // default for v2 const OPTION_ONE = 'option_one'; const OPTION_TWO = 'option_two'; diff --git a/src/Commands/stubs/enum.labels.stub b/src/Commands/stubs/enum.labels.stub index 75930bd..6d2cebb 100644 --- a/src/Commands/stubs/enum.labels.stub +++ b/src/Commands/stubs/enum.labels.stub @@ -6,7 +6,8 @@ use Konekt\Enum\Enum; class DummyClass extends Enum { - const __default = self::OPTION_ONE; + const __DEFAULT = self::OPTION_ONE; + // const __default = self::OPTION_ONE; // default for v2 const OPTION_ONE = 'option_one'; const OPTION_TWO = 'option_two'; diff --git a/src/Commands/stubs/enum.stub b/src/Commands/stubs/enum.stub index 12f1b66..8a0528e 100644 --- a/src/Commands/stubs/enum.stub +++ b/src/Commands/stubs/enum.stub @@ -6,7 +6,8 @@ use Konekt\Enum\Enum; class DummyClass extends Enum { - const __default = self::OPTION_ONE; + const __DEFAULT = self::OPTION_ONE; + // const __default = self::OPTION_ONE; // default for v2 const OPTION_ONE = 'option_one'; const OPTION_TWO = 'option_two'; diff --git a/tests/EnumAccessorTest.php b/tests/EnumAccessorTest.php index 0037c33..33cf54f 100644 --- a/tests/EnumAccessorTest.php +++ b/tests/EnumAccessorTest.php @@ -15,6 +15,10 @@ use Konekt\Enum\Eloquent\Tests\Models\Client; use Konekt\Enum\Eloquent\Tests\Models\Order; use Konekt\Enum\Eloquent\Tests\Models\OrderStatus; +use Konekt\Enum\Eloquent\Tests\Models\OrderV2; +use Konekt\Enum\Eloquent\Tests\Models\OrderStatusV2; +use Konekt\Enum\Eloquent\Tests\Models\OrderVX; +use Konekt\Enum\Eloquent\Tests\Models\OrderStatusVX; class EnumAccessorTest extends TestCase { @@ -38,12 +42,51 @@ public function it_casts_marked_attributes_to_their_proper_enum_class() */ public function it_returns_the_enum_default_when_attribute_is_null() { + // don't test if mayor version is lower than 3 + if ($this->getEnumVersionMajor() < 3) { + $this->assertTrue(true); + return; + } + $order = new Order([ 'number' => 'PLGU7S5' ]); $this->assertInstanceOf(OrderStatus::class, $order->status); - $this->assertEquals(OrderStatus::__default, $order->status->value()); + $this->assertEquals(OrderStatus::__DEFAULT, $order->status->value()); + } + + /** + * @test + */ + public function it_returns_the_enum_v2_default_when_attribute_is_null() + { + // don't test if mayor version is 3 or higher + if ($this->getEnumVersionMajor() >= 3) { + $this->assertTrue(true); + return; + } + + $order = new OrderV2([ + 'number' => 'PLGU7S5' + ]); + + $this->assertInstanceOf(OrderStatusV2::class, $order->status); + $this->assertEquals(OrderStatusV2::__default, $order->status->value()); + } + + /** + * @test + */ + public function it_returns_the_enum_backwards_compatible_default_when_attribute_is_null() + { + $order = new OrderVX([ + 'number' => 'PLGU7S5' + ]); + + $this->assertInstanceOf(OrderStatusVX::class, $order->status); + $this->assertEquals(OrderStatusVX::__DEFAULT, $order->status->value()); + $this->assertEquals(OrderStatusVX::__default, $order->status->value()); } /** @@ -92,4 +135,20 @@ public function it_doesnt_break_related_properties() $this->assertInstanceOf(Client::class, $order->client); $this->assertEquals($client->id, $order->client->id); } + + private function getEnumVersion() + { + $raw_version = \PackageVersions\Versions::getVersion('konekt/enum'); + + $parts = explode('@', $raw_version); + + return $parts[0]; + } + + private function getEnumVersionMajor() + { + $parts = explode('.', $this->getEnumVersion()); + + return $parts[0]; + } } diff --git a/tests/Models/AddressStatus.php b/tests/Models/AddressStatus.php index 68b6ba8..2de6654 100644 --- a/tests/Models/AddressStatus.php +++ b/tests/Models/AddressStatus.php @@ -16,8 +16,6 @@ class AddressStatus extends Enum { - const __default = self::UNKNOWN; - const UNKNOWN = null; const VALID = 'valid'; const INVALID = 'invalid'; diff --git a/tests/Models/EloquentType.php b/tests/Models/EloquentType.php index 432b89b..e2d35b6 100644 --- a/tests/Models/EloquentType.php +++ b/tests/Models/EloquentType.php @@ -16,8 +16,6 @@ class EloquentType extends Enum { - const __default = self::NADA; - const NADA = null; const WHATEVER = 'whatever'; const NEVERMIND = 'nevermind'; diff --git a/tests/Models/OrderStatus.php b/tests/Models/OrderStatus.php index 1748e54..9d1be0a 100644 --- a/tests/Models/OrderStatus.php +++ b/tests/Models/OrderStatus.php @@ -16,7 +16,7 @@ class OrderStatus extends Enum { - const __default = self::SUBMITTED; + const __DEFAULT = self::SUBMITTED; const SUBMITTED = 'submitted'; const PROCESSING = 'processing'; diff --git a/tests/Models/OrderStatusV2.php b/tests/Models/OrderStatusV2.php new file mode 100644 index 0000000..de65a6c --- /dev/null +++ b/tests/Models/OrderStatusV2.php @@ -0,0 +1,25 @@ + 'boolean' + ]; + + protected $enums = [ + 'status' => OrderStatusV2::class + ]; + + /** + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function client() + { + return $this->belongsTo(Client::class); + } +} diff --git a/tests/Models/OrderVX.php b/tests/Models/OrderVX.php new file mode 100644 index 0000000..9ef2a73 --- /dev/null +++ b/tests/Models/OrderVX.php @@ -0,0 +1,39 @@ + 'boolean' + ]; + + protected $enums = [ + 'status' => OrderStatusVX::class + ]; + + /** + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function client() + { + return $this->belongsTo(Client::class); + } +}