From e58e47278ffab8a37fd7e53817c5b068cdb98d46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Sun, 31 Dec 2023 13:08:29 +0100 Subject: [PATCH] tests --- src/Fields/Radio.php | 15 +++++------ tests/Fields/RadioTest.php | 27 +++++++++++++++++++ tests/Fields/RangeTest.php | 23 ++++++++++++++++ tests/Fields/TextTest.php | 50 +++++++++++++++++++++++++++++++++++ tests/Fields/TextareaTest.php | 41 ++++++++++++++++++++++++++++ 5 files changed, 147 insertions(+), 9 deletions(-) create mode 100644 tests/Fields/RadioTest.php create mode 100644 tests/Fields/RangeTest.php create mode 100644 tests/Fields/TextTest.php create mode 100644 tests/Fields/TextareaTest.php diff --git a/src/Fields/Radio.php b/src/Fields/Radio.php index 61014f79..c5b5a97c 100644 --- a/src/Fields/Radio.php +++ b/src/Fields/Radio.php @@ -9,14 +9,11 @@ class Radio extends Checkbox */ public function newOption(mixed $value, string $label): Option { - $option = parent::newOption($value, $label); - - $option->setAttributes([ - 'type' => 'radio', - 'class' => 'form-check__control', - 'name' => $this->getModelAttribute(), - ]); - - return $option; + return parent::newOption($value, $label) + ->class('form-check__control') + ->setAttributes([ + 'type' => 'radio', + 'name' => $this->getModelAttribute(), + ]); } } diff --git a/tests/Fields/RadioTest.php b/tests/Fields/RadioTest.php new file mode 100644 index 00000000..2e17ad9b --- /dev/null +++ b/tests/Fields/RadioTest.php @@ -0,0 +1,27 @@ +field = new Radio('Role'); + } + + public function test_a_checkbox_field_makes_new_option(): void + { + $option = $this->field->newOption('test', 'Test'); + + $this->assertEmpty( + array_diff(['value' => 'test'], $option->jsonSerialize()) + ); + } +} diff --git a/tests/Fields/RangeTest.php b/tests/Fields/RangeTest.php new file mode 100644 index 00000000..78f792ec --- /dev/null +++ b/tests/Fields/RangeTest.php @@ -0,0 +1,23 @@ +field = new Range('Age'); + } + + public function test_a_range_field_has_range_type(): void + { + $this->assertSame('range', $this->field->getAttribute('type')); + } +} diff --git a/tests/Fields/TextTest.php b/tests/Fields/TextTest.php new file mode 100644 index 00000000..cc5e585e --- /dev/null +++ b/tests/Fields/TextTest.php @@ -0,0 +1,50 @@ +field = new Text('Name'); + } + + public function test_a_text_field_has_text_type(): void + { + $this->assertSame('text', $this->field->getAttribute('type')); + } + + public function test_a_text_field_has_size_attribute(): void + { + $this->assertNull($this->field->getAttribute('size')); + + $this->field->size(10); + + $this->assertSame(10, $this->field->getAttribute('size')); + } + + public function test_a_text_field_has_minlength_attribute(): void + { + $this->assertNull($this->field->getAttribute('minlength')); + + $this->field->minlength(10); + + $this->assertSame(10, $this->field->getAttribute('minlength')); + } + + public function test_a_text_field_has_maxlength_attribute(): void + { + $this->assertNull($this->field->getAttribute('maxlength')); + + $this->field->maxlength(10); + + $this->assertSame(10, $this->field->getAttribute('maxlength')); + } +} diff --git a/tests/Fields/TextareaTest.php b/tests/Fields/TextareaTest.php new file mode 100644 index 00000000..4a7d075b --- /dev/null +++ b/tests/Fields/TextareaTest.php @@ -0,0 +1,41 @@ +field = new Textarea('Content'); + } + + public function test_a_textarea_field_has_textarea_template(): void + { + $this->assertSame('root::fields.textarea', $this->field->getTemplate()); + } + + public function test_a_textarea_field_has_rows_attribute(): void + { + $this->assertNull($this->field->getAttribute('rows')); + + $this->field->rows(10); + + $this->assertSame(10, $this->field->getAttribute('rows')); + } + + public function test_a_textarea_field_has_cols_attribute(): void + { + $this->assertNull($this->field->getAttribute('cols')); + + $this->field->cols(10); + + $this->assertSame(10, $this->field->getAttribute('cols')); + } +}