-
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.
- Loading branch information
Showing
4 changed files
with
228 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace SimpleSquid\Nova\Fields\Enum\Tests\Examples; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class NoCastsModel extends Model | ||
{ | ||
public $table = 'example_models'; | ||
|
||
protected $guarded = []; | ||
|
||
public $timestamps = false; | ||
} |
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,60 @@ | ||
<?php | ||
|
||
namespace SimpleSquid\Nova\Fields\Enum\Tests\Fields; | ||
|
||
use Laravel\Nova\Http\Requests\NovaRequest; | ||
use SimpleSquid\Nova\Fields\Enum\Enum; | ||
use SimpleSquid\Nova\Fields\Enum\Tests\Examples\NoCastsModel; | ||
use SimpleSquid\Nova\Fields\Enum\Tests\Examples\StringEnum; | ||
use SimpleSquid\Nova\Fields\Enum\Tests\TestCase; | ||
|
||
class NoCastsFieldTest extends TestCase | ||
{ | ||
private $field; | ||
|
||
private $model; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->setUpDatabase($this->app, 'string'); | ||
|
||
$this->field = Enum::make('Enum')->attach(StringEnum::class); | ||
|
||
$this->model = NoCastsModel::create(['enum' => StringEnum::Moderator]); | ||
} | ||
|
||
/** @test */ | ||
public function it_resolves_enum_value() | ||
{ | ||
$this->field->resolve($this->model); | ||
|
||
$this->assertSame(StringEnum::Moderator, $this->field->value); | ||
} | ||
|
||
/** @test */ | ||
public function it_displays_enum_description() | ||
{ | ||
$this->field->resolveForDisplay($this->model); | ||
|
||
$this->assertSame(StringEnum::Moderator, $this->field->value); | ||
} | ||
|
||
/** @test */ | ||
public function it_fills_database_with_enum_value() | ||
{ | ||
$request = new NovaRequest(); | ||
$request->query->add(['enum' => StringEnum::Subscriber]); | ||
|
||
$this->field->fill($request, $this->model); | ||
|
||
$this->assertDatabaseHas('example_models', ['enum' => StringEnum::Moderator]); | ||
|
||
$this->model->save(); | ||
|
||
$this->assertDatabaseHas('example_models', ['enum' => StringEnum::Subscriber]); | ||
|
||
$this->assertDatabaseMissing('example_models', ['enum' => StringEnum::Moderator]); | ||
} | ||
} |
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,89 @@ | ||
<?php | ||
|
||
namespace SimpleSquid\Nova\Fields\Enum\Tests\Filters; | ||
|
||
use JoshGaber\NovaUnit\Filters\MockFilter; | ||
use SimpleSquid\Nova\Fields\Enum\EnumBooleanFilter; | ||
use SimpleSquid\Nova\Fields\Enum\Tests\Examples\NoCastsModel; | ||
use SimpleSquid\Nova\Fields\Enum\Tests\Examples\StringEnum; | ||
use SimpleSquid\Nova\Fields\Enum\Tests\TestCase; | ||
|
||
class NoCastsBooleanFilterTest extends TestCase | ||
{ | ||
private $filter; | ||
|
||
private $models = []; | ||
|
||
private $results = []; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->setUpDatabase($this->app, 'string'); | ||
|
||
$this->filter = new MockFilter(new EnumBooleanFilter('enum', StringEnum::class)); | ||
|
||
$this->models[0] = NoCastsModel::create(['enum' => StringEnum::Moderator]); | ||
|
||
$this->models[1] = NoCastsModel::create(['enum' => StringEnum::Moderator]); | ||
|
||
$this->models[2] = NoCastsModel::create(['enum' => StringEnum::Administrator]); | ||
|
||
$this->results = [ | ||
StringEnum::Moderator => [0, 1], | ||
StringEnum::Administrator => [2], | ||
StringEnum::Subscriber => [], | ||
]; | ||
} | ||
|
||
private function getOptions(array $keys, array $options = [[]]): array | ||
{ | ||
if (empty($keys)) { | ||
return $options; | ||
} | ||
|
||
$current = array_shift($keys); | ||
$newOptions = []; | ||
|
||
foreach ($options as $option) { | ||
$newOptions[] = $option + [$current => true]; | ||
$newOptions[] = $option + [$current => false]; | ||
} | ||
|
||
return $this->getOptions($keys, $newOptions); | ||
} | ||
|
||
/** @test */ | ||
public function it_contains_all_the_filter_values() | ||
{ | ||
foreach (StringEnum::getValues() as $enum) { | ||
$this->filter->assertHasOption($enum); | ||
} | ||
} | ||
|
||
/** @test */ | ||
public function it_returns_the_correct_results() | ||
{ | ||
foreach ($options = $this->getOptions(StringEnum::getValues()) as $option) { | ||
$response = $this->filter->apply(NoCastsModel::class, $option); | ||
|
||
// None selected should show all models | ||
if (count(array_filter($option)) === 0) { | ||
$models = array_keys($this->models); | ||
} else { | ||
$models = array_unique(array_merge(...array_values(array_intersect_key($this->results, array_filter($option))))); | ||
} | ||
|
||
$response->assertCount(count($models)); | ||
|
||
foreach ($models as $contain) { | ||
$response->assertContains($this->models[$contain]); | ||
} | ||
|
||
foreach (array_diff(array_keys($this->models), $models) as $missing) { | ||
$response->assertMissing($this->models[$missing]); | ||
} | ||
} | ||
} | ||
} |
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,65 @@ | ||
<?php | ||
|
||
namespace SimpleSquid\Nova\Fields\Enum\Tests\Filters; | ||
|
||
use JoshGaber\NovaUnit\Filters\MockFilter; | ||
use SimpleSquid\Nova\Fields\Enum\EnumFilter; | ||
use SimpleSquid\Nova\Fields\Enum\Tests\Examples\NoCastsModel; | ||
use SimpleSquid\Nova\Fields\Enum\Tests\Examples\StringEnum; | ||
use SimpleSquid\Nova\Fields\Enum\Tests\TestCase; | ||
|
||
class NoCastsFilterTest extends TestCase | ||
{ | ||
private $filter; | ||
|
||
private $models = []; | ||
|
||
private $results = []; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->setUpDatabase($this->app, 'string'); | ||
|
||
$this->filter = new MockFilter(new EnumFilter('enum', StringEnum::class)); | ||
|
||
$this->models[0] = NoCastsModel::create(['enum' => StringEnum::Moderator]); | ||
|
||
$this->models[1] = NoCastsModel::create(['enum' => StringEnum::Moderator]); | ||
|
||
$this->models[2] = NoCastsModel::create(['enum' => StringEnum::Administrator]); | ||
|
||
$this->results = [ | ||
StringEnum::Moderator => [0, 1], | ||
StringEnum::Administrator => [2], | ||
StringEnum::Subscriber => [], | ||
]; | ||
} | ||
|
||
/** @test */ | ||
public function it_contains_all_the_filter_values() | ||
{ | ||
foreach (StringEnum::getValues() as $enum) { | ||
$this->filter->assertHasOption($enum); | ||
} | ||
} | ||
|
||
/** @test */ | ||
public function it_returns_the_correct_results() | ||
{ | ||
foreach ($this->results as $enum => $models) { | ||
$response = $this->filter->apply(NoCastsModel::class, $enum); | ||
|
||
$response->assertCount(count($models)); | ||
|
||
foreach ($models as $contain) { | ||
$response->assertContains($this->models[$contain]); | ||
} | ||
|
||
foreach (array_diff(array_keys($this->models), $models) as $missing) { | ||
$response->assertMissing($this->models[$missing]); | ||
} | ||
} | ||
} | ||
} |