Skip to content

Commit

Permalink
Merge pull request #7 from TheM1984/feature/5-add-enum-v3-support
Browse files Browse the repository at this point in the history
Feature/5 add enum v3 support
  • Loading branch information
fulopattila122 authored Sep 8, 2019
2 parents 26c06cd + 9769ed2 commit b4289d0
Show file tree
Hide file tree
Showing 15 changed files with 229 additions and 47 deletions.
55 changes: 23 additions & 32 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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';
Expand Down Expand Up @@ -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).
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/" },
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/stubs/enum.boot.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/stubs/enum.labels.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/stubs/enum.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
61 changes: 60 additions & 1 deletion tests/EnumAccessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -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];
}
}
2 changes: 0 additions & 2 deletions tests/Models/AddressStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

class AddressStatus extends Enum
{
const __default = self::UNKNOWN;

const UNKNOWN = null;
const VALID = 'valid';
const INVALID = 'invalid';
Expand Down
2 changes: 0 additions & 2 deletions tests/Models/EloquentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

class EloquentType extends Enum
{
const __default = self::NADA;

const NADA = null;
const WHATEVER = 'whatever';
const NEVERMIND = 'nevermind';
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/OrderStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class OrderStatus extends Enum
{
const __default = self::SUBMITTED;
const __DEFAULT = self::SUBMITTED;

const SUBMITTED = 'submitted';
const PROCESSING = 'processing';
Expand Down
25 changes: 25 additions & 0 deletions tests/Models/OrderStatusV2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Contains the OrderStatus class with Enum version 2 default.
*
* @copyright Copyright (c) 2017 Attila Fulop
* @author Mark Boessenkool
* @license MIT
* @since 2019-09-03
*
*/


namespace Konekt\Enum\Eloquent\Tests\Models;

use Konekt\Enum\Enum;

class OrderStatusV2 extends Enum
{
const __default = self::SUBMITTED;

const SUBMITTED = 'submitted';
const PROCESSING = 'processing';
const SHIPPING = 'shipping';
const COMPLETED = 'completed';
}
26 changes: 26 additions & 0 deletions tests/Models/OrderStatusVX.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Contains the OrderStatus class with Enum version 2 and 3 default.
*
* @copyright Copyright (c) 2017 Attila Fulop
* @author Mark Boessenkool
* @license MIT
* @since 2019-09-03
*
*/


namespace Konekt\Enum\Eloquent\Tests\Models;

use Konekt\Enum\Enum;

class OrderStatusVX extends Enum
{
const __DEFAULT = self::SUBMITTED;
const __default = self::SUBMITTED; // v2 default for backwards compatibility

const SUBMITTED = 'submitted';
const PROCESSING = 'processing';
const SHIPPING = 'shipping';
const COMPLETED = 'completed';
}
39 changes: 39 additions & 0 deletions tests/Models/OrderV2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Contains the Order model class for Enum version 2 default.
*
* @copyright Copyright (c) 2017 Attila Fulop
* @author Mark Boessenkool
* @license MIT
* @since 2019-09-03
*
*/


namespace Konekt\Enum\Eloquent\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use Konekt\Enum\Eloquent\CastsEnums;

class OrderV2 extends Model
{
use CastsEnums;

protected $guarded = ['id'];

protected $casts = [
'is_active' => 'boolean'
];

protected $enums = [
'status' => OrderStatusV2::class
];

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function client()
{
return $this->belongsTo(Client::class);
}
}
Loading

0 comments on commit b4289d0

Please sign in to comment.