Skip to content

Commit

Permalink
Add tests for lack of casts.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdpoulter committed Jan 25, 2021
1 parent 64d52a9 commit 9fb6a0f
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tests/Examples/NoCastsModel.php
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;
}
60 changes: 60 additions & 0 deletions tests/Fields/NoCastsFieldTest.php
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]);
}
}
89 changes: 89 additions & 0 deletions tests/Filters/NoCastsBooleanFilterTest.php
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]);
}
}
}
}
65 changes: 65 additions & 0 deletions tests/Filters/NoCastsFilterTest.php
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]);
}
}
}
}

0 comments on commit 9fb6a0f

Please sign in to comment.