From fd50e5d5751340196b67127b6809307a087b90df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Wed, 22 Nov 2023 11:52:50 +0100 Subject: [PATCH] tests wip --- src/Fields/Field.php | 1 - tests/Fields/FieldTest.php | 193 +++++++++++++++++++++++++++++++++++++ tests/TestCase.php | 4 + 3 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 tests/Fields/FieldTest.php diff --git a/src/Fields/Field.php b/src/Fields/Field.php index 232b5e7e3..f41268808 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -524,7 +524,6 @@ public function jsonSerialize(): mixed public function toArray(): array { return [ - 'attrs' => $this->newAttributeBag(), 'attribute' => $this->getModelAttribute(), 'help' => $this->help, 'label' => $this->label, diff --git a/tests/Fields/FieldTest.php b/tests/Fields/FieldTest.php new file mode 100644 index 000000000..c15b84ff5 --- /dev/null +++ b/tests/Fields/FieldTest.php @@ -0,0 +1,193 @@ +field = new Text('Title'); + + $this->model = new class(['title' => 'Test Model']) extends Model { + protected $guarded = []; + + public function save(array $options = []): bool + { + $this->fireModelEvent('saving'); + + $this->fireModelEvent('saved'); + + return true; + } + }; + } + + public function test_a_field_has_attributes(): void + { + $this->assertSame('title', $this->field->getAttribute('name')); + + $this->assertTrue($this->field->hasAttribute('name')); + $this->assertFalse($this->field->hasAttribute('foo')); + + $this->assertSame( + ['name' => 'title', 'id' => 'title', 'class' => 'form-control', 'type' => 'text'], + $this->field->getAttributes() + ); + + $this->assertNull($this->field->getAttribute('max')); + $this->field->setAttributes(['max' => 30]); + $this->assertSame(30, $this->field->getAttribute('max')); + + $this->field->removeAttributes(['max']); + $this->assertNull($this->field->getAttribute('max')); + + $this->field->clearAttributes(); + $this->assertEmpty($this->field->getAttributes()); + } + + public function test_a_field_has_value(): void + { + $this->assertSame( + $this->model->title, + $this->field->resolveValue($this->app['request'], $this->model) + ); + + $this->field->value(fn (): string => '__fake__'); + + $this->assertSame( + '__fake__', + $this->field->resolveValue($this->app['request'], $this->model) + ); + } + + public function test_a_field_has_formatted_value(): void + { + $this->assertSame( + 'Test Model', + $this->field->resolveFormat($this->app['request'], $this->model) + ); + + $this->field->format(function ($request, $model, $value): string { + return strtoupper($value); + }); + + $this->assertSame( + 'TEST MODEL', + $this->field->resolveFormat($this->app['request'], $this->model) + ); + } + + public function test_a_field_persists_model_attribute(): void + { + $this->app['request']->merge(['title' => 'Persisted']); + + $this->assertSame('Test Model', $this->model->title); + + $this->field->persist( + $this->app['request'], $this->model, $this->field->getValueForHydrate($this->app['request']) + ); + + $this->model->save(); + + $this->assertSame('Persisted', $this->model->title); + } + + public function test_a_field_hydrates_model_attribute(): void + { + $this->assertSame('Test Model', $this->model->title); + + $this->field->resolveHydrate($this->app['request'], $this->model, 'Hydrated'); + + $this->assertSame('Hydrated', $this->model->title); + + $this->field->hydrate(function ($request, $model, $value) { + $model->setAttribute($this->field->getModelAttribute(), strtoupper($value)); + }); + + $this->field->resolveHydrate($this->app['request'], $this->model, 'Test'); + + $this->assertSame('TEST', $this->model->title); + } + + public function test_a_field_has_validation_rules(): void + { + $this->field->rules(['required']) + ->createRules(['unique:users']) + ->updateRules(['unique:users,1']); + + $this->assertSame( + [$this->field->getValidationKey() => ['required', 'unique:users']], + $this->field->toValidate($this->app['request'], $this->model) + ); + + $this->model->exists = true; + $this->assertSame( + [$this->field->getModelAttribute() => ['required', 'unique:users,1']], + $this->field->toValidate($this->app['request'], $this->model) + ); + } + + public function test_a_field_has_display_representation(): void + { + $this->assertSame( + array_merge($this->field->toArray(), [ + 'searchable' => $this->field->isSearchable(), + 'sortable' => $this->field->isSortable(), + 'value' => $this->model->title, + 'formattedValue' => $this->model->title, + ]), + $this->field->toDisplay($this->app['request'], $this->model) + ); + } + + public function test_a_field_has_input_representation(): void + { + $this->assertSame( + json_encode(array_merge($this->field->toDisplay($this->app['request'], $this->model), [ + 'attrs' => $this->field->newAttributeBag()->class([ + 'form-control--invalid' => $this->field->invalid($this->app['request']), + ]), + 'error' => $this->field->error($this->app['request']), + 'invalid' => $this->field->invalid($this->app['request']), + ])), + json_encode($this->field->toInput($this->app['request'], $this->model)) + ); + } + + public function test_a_field_can_be_searchable(): void + { + $this->assertFalse($this->field->isSearchable()); + + $this->field->searchable(); + + $this->assertTrue($this->field->isSearchable()); + + $this->field->searchable(fn (): bool => false); + + $this->assertFalse($this->field->isSearchable()); + } + + public function test_a_field_can_be_sortable(): void + { + $this->assertFalse($this->field->isSortable()); + + $this->field->sortable(); + + $this->assertTrue($this->field->isSortable()); + + $this->field->sortable(fn (): bool => false); + + $this->assertFalse($this->field->isSortable()); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 3aed3c0c5..2deac264d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -17,6 +17,10 @@ public function setUp(): void $this->app['router']->getRoutes()->refreshNameLookups(); + $this->startSession(); + + $this->app['request']->setLaravelSession($this->app['session']); + $this->withoutVite(); Storage::fake('local');