diff --git a/tests/Examples/NoCastsModel.php b/tests/Examples/NoCastsModel.php new file mode 100644 index 0000000..dcd8100 --- /dev/null +++ b/tests/Examples/NoCastsModel.php @@ -0,0 +1,14 @@ +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]); + } +} diff --git a/tests/Filters/NoCastsBooleanFilterTest.php b/tests/Filters/NoCastsBooleanFilterTest.php new file mode 100644 index 0000000..050797e --- /dev/null +++ b/tests/Filters/NoCastsBooleanFilterTest.php @@ -0,0 +1,89 @@ +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]); + } + } + } +} diff --git a/tests/Filters/NoCastsFilterTest.php b/tests/Filters/NoCastsFilterTest.php new file mode 100644 index 0000000..8d1e288 --- /dev/null +++ b/tests/Filters/NoCastsFilterTest.php @@ -0,0 +1,65 @@ +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]); + } + } + } +}