-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Improve testing (including database). * Test on PHP 7.3 and 7.4. * Add cakephp/chronos minimum dependency to fix tests.
- Loading branch information
Showing
11 changed files
with
223 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
8 changes: 6 additions & 2 deletions
8
tests/ExampleEnum.php → tests/Examples/ExampleIntegerEnum.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,19 @@ | ||
<?php | ||
|
||
namespace SimpleSquid\Nova\Fields\Enum\Tests; | ||
namespace SimpleSquid\Nova\Fields\Enum\Tests\Examples; | ||
|
||
use BenSampo\Enum\Enum; | ||
|
||
/** | ||
* @method static Administrator() | ||
* @method static Moderator() | ||
* @method static Subscriber() | ||
*/ | ||
class ExampleEnum extends Enum | ||
class ExampleIntegerEnum extends Enum | ||
{ | ||
const Administrator = 0; | ||
|
||
const Moderator = 1; | ||
|
||
const Subscriber = 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace SimpleSquid\Nova\Fields\Enum\Tests\Examples; | ||
|
||
use BenSampo\Enum\Traits\CastsEnums; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class ExampleModel extends Model | ||
{ | ||
use CastsEnums; | ||
|
||
public $table = 'example_models'; | ||
|
||
protected $guarded = []; | ||
|
||
public $timestamps = false; | ||
|
||
protected $casts = [ | ||
'enum' => ExampleIntegerEnum::class, | ||
]; | ||
} |
6 changes: 5 additions & 1 deletion
6
tests/ExampleStringEnum.php → tests/Examples/ExampleStringEnum.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,19 @@ | ||
<?php | ||
|
||
namespace SimpleSquid\Nova\Fields\Enum\Tests; | ||
namespace SimpleSquid\Nova\Fields\Enum\Tests\Examples; | ||
|
||
use BenSampo\Enum\Enum; | ||
|
||
/** | ||
* @method static Administrator() | ||
* @method static Moderator() | ||
* @method static Subscriber() | ||
*/ | ||
class ExampleStringEnum extends Enum | ||
{ | ||
const Administrator = 'administrator'; | ||
|
||
const Moderator = 'moderator'; | ||
|
||
const Subscriber = 'subscriber'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace SimpleSquid\Nova\Fields\Enum\Tests; | ||
|
||
use BenSampo\Enum\Rules\EnumValue; | ||
use PHPUnit\Framework\TestCase; | ||
use SimpleSquid\Nova\Fields\Enum\Enum; | ||
use SimpleSquid\Nova\Fields\Enum\Tests\Examples\ExampleIntegerEnum; | ||
|
||
class IntegerEnumTest extends TestCase | ||
{ | ||
/** @var \SimpleSquid\Nova\Fields\Enum\Enum */ | ||
private $field; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->field = Enum::make('Enum'); | ||
|
||
$this->field->attachEnum(ExampleIntegerEnum::class); | ||
} | ||
|
||
/** @test */ | ||
public function field_starts_with_zero_config() | ||
{ | ||
$field = Enum::make('Enum'); | ||
|
||
$this->assertEmpty($field->meta); | ||
$this->assertEmpty($field->rules); | ||
$this->assertNull($field->resolveCallback); | ||
$this->assertNull($field->displayCallback); | ||
} | ||
|
||
/** @test */ | ||
public function an_enum_can_be_attached_to_the_field() | ||
{ | ||
$this->assertArrayHasKey('options', $this->field->meta); | ||
|
||
$this->assertEquals([ | ||
[ | ||
'label' => 'Administrator', | ||
'value' => 0, | ||
], | ||
[ | ||
'label' => 'Moderator', | ||
'value' => 1, | ||
], | ||
[ | ||
'label' => 'Subscriber', | ||
'value' => 2, | ||
], | ||
], $this->field->meta['options']); | ||
} | ||
|
||
/** @test */ | ||
public function attaching_an_enum_adds_correct_rules() | ||
{ | ||
$this->assertContains('required', $this->field->rules); | ||
|
||
$this->assertContainsEquals(new EnumValue(ExampleIntegerEnum::class, false), $this->field->rules); | ||
} | ||
|
||
/** @test */ | ||
public function field_resolves_correct_value() | ||
{ | ||
$this->field->resolve(['enum' => ExampleIntegerEnum::Moderator()]); | ||
|
||
$this->assertSame(1, $this->field->value); | ||
} | ||
|
||
/** @test */ | ||
public function field_displays_correct_description() | ||
{ | ||
$this->field->resolveForDisplay(['enum' => ExampleIntegerEnum::Moderator()]); | ||
|
||
$this->assertSame('Moderator', $this->field->value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace SimpleSquid\Nova\Fields\Enum\Tests; | ||
|
||
use Laravel\Nova\Http\Requests\NovaRequest; | ||
use SimpleSquid\Nova\Fields\Enum\Enum; | ||
use SimpleSquid\Nova\Fields\Enum\Tests\Examples\ExampleIntegerEnum; | ||
use SimpleSquid\Nova\Fields\Enum\Tests\Examples\ExampleModel; | ||
|
||
class ModelTest extends TestCase | ||
{ | ||
/** @var \SimpleSquid\Nova\Fields\Enum\Tests\Examples\ExampleModel */ | ||
private $model; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->model = ExampleModel::create(['enum' => ExampleIntegerEnum::Moderator()]); | ||
} | ||
|
||
/** @test */ | ||
public function field_resolves_correct_value() | ||
{ | ||
$field = Enum::make('Enum')->attachEnum(ExampleIntegerEnum::class); | ||
|
||
$field->resolve($this->model); | ||
|
||
$this->assertSame(1, $field->value); | ||
} | ||
|
||
/** @test */ | ||
public function field_displays_correct_description() | ||
{ | ||
$field = Enum::make('Enum')->attachEnum(ExampleIntegerEnum::class); | ||
|
||
$field->resolveForDisplay($this->model); | ||
|
||
$this->assertSame('Moderator', $field->value); | ||
} | ||
|
||
/** @test */ | ||
public function field_fills_database_with_enum_value() | ||
{ | ||
$field = Enum::make('Enum')->attachEnum(ExampleIntegerEnum::class); | ||
|
||
$request = new NovaRequest(); | ||
$request->query->add(['enum' => ExampleIntegerEnum::Subscriber()]); | ||
|
||
$field->fill($request, $this->model); | ||
|
||
$this->model->save(); | ||
|
||
$this->assertDatabaseHas('example_models', ['enum' => 2]); | ||
$this->assertDatabaseMissing('example_models', ['enum' => 1]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.