Skip to content

Commit

Permalink
Added tests for Enum v4
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Mar 10, 2022
1 parent d6305a0 commit 6922f17
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/DetectsEnumVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ trait DetectsEnumVersion
{
private function getEnumVersion(): string
{
if (method_exists(Enum::class, 'hasNot')) {
return '4.0.0';
}

return defined(Enum::class . '::__DEFAULT') ? '3.0.0' : '2.3.0';
}

Expand Down
19 changes: 19 additions & 0 deletions tests/EnumAccessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace Konekt\Enum\Eloquent\Tests;

use Konekt\Enum\Eloquent\Tests\Models\BillingRule;
use Konekt\Enum\Eloquent\Tests\Models\Client;
use Konekt\Enum\Eloquent\Tests\Models\Order;
use Konekt\Enum\Eloquent\Tests\Models\OrderStatus;
Expand Down Expand Up @@ -140,4 +141,22 @@ public function it_doesnt_break_related_properties()
$this->assertInstanceOf(Client::class, $order->client);
$this->assertEquals($client->id, $order->client->id);
}

/** @test */
public function it_works_with_integer_database_fields()
{
$clientAny = Client::create(['name' => 'Pawel Jedrzejewsky']);
$clientInvoiceOnly = Client::create(['name' => 'Pawel Jedrzejewsky', 'billing_rule' => 1]);
$clientNoInvoice = Client::create(['name' => 'Pawel Jedrzejewsky', 'billing_rule' => 0]);

$this->assertInstanceOf(BillingRule::class, $clientAny->billing_rule);
$this->assertNull($clientAny->billing_rule->value());

$this->assertInstanceOf(BillingRule::class, $clientInvoiceOnly->billing_rule);
$this->assertEquals(1, $clientInvoiceOnly->billing_rule->value());

$this->assertInstanceOf(BillingRule::class, $clientNoInvoice->billing_rule);
$this->assertEquals(0, $clientNoInvoice->billing_rule->value());

}
}
25 changes: 25 additions & 0 deletions tests/Models/BillingRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/**
* Contains the BillingRule class.
*
* @copyright Copyright (c) 2022 Attila Fulop
* @author Attila Fulop
* @license MIT
* @since 2022-03-10
*
*/

namespace Konekt\Enum\Eloquent\Tests\Models;

use Konekt\Enum\Enum;

class BillingRule extends Enum
{
public const __DEFAULT = self::ANY;
public const ANY = null;
public const INVOICE_ONLY = 1;
public const NO_INVOICE = 0;
}
13 changes: 13 additions & 0 deletions tests/Models/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,21 @@
namespace Konekt\Enum\Eloquent\Tests\Models;

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

/**
* @property int $id
* @property BillingRule $billing_rule
*
* @method static Client create(array $attributes)
*/
class Client extends Model
{
use CastsEnums;

protected $guarded = ['id'];

protected array $enums = [
'billing_rule' => BillingRule::class,
];
}
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ protected function setUpDatabase()
$this->capsule->schema()->create('clients', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('billing_rule')->nullable();
$table->timestamps();
});

Expand Down

0 comments on commit 6922f17

Please sign in to comment.