From 8d8856a023f08975cc069a863a4ff669243b3d96 Mon Sep 17 00:00:00 2001 From: Tortue Torche Date: Wed, 19 May 2021 16:54:13 +0200 Subject: [PATCH] Add tests for Bootstrap 5 floating labels Related to commit 21b30f78c1a25c40acc33877173c22445a2190bf Reference: https://getbootstrap.com/docs/5.0/forms/floating-labels/ --- tests/Fields/FloatingLabelTest.php | 173 +++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 tests/Fields/FloatingLabelTest.php diff --git a/tests/Fields/FloatingLabelTest.php b/tests/Fields/FloatingLabelTest.php new file mode 100644 index 00000000..7104af31 --- /dev/null +++ b/tests/Fields/FloatingLabelTest.php @@ -0,0 +1,173 @@ +former->framework('TwitterBootstrap5'); + $this->former->vertical_open()->__toString(); + } + + //////////////////////////////////////////////////////////////////// + ////////////////////////////// MATCHERS //////////////////////////// + //////////////////////////////////////////////////////////////////// + + /** + * Matches a floating label + * + * @return array + */ + public function matchFloatingLabel() + { + return array( + 'tag' => 'label', + ); + } + + /** + * Matches an input text tag + * + * @return array + */ + public function matchFloatingInputText() + { + return array( + 'tag' => 'input', + 'attributes' => array( + 'id' => 'foo', + 'class' => 'form-control', + 'placeholder' => 'Dummy placeholder', + 'type' => 'text', + 'name' => 'foo', + ), + ); + } + + /** + * Matches a textarea tag + * + * @return array + */ + public function matchFloatingTextarea() + { + return array( + 'tag' => 'textarea', + 'attributes' => array( + 'id' => 'foo', + 'class' => 'form-control', + 'placeholder' => 'Dummy placeholder', + 'name' => 'foo', + ), + ); + } + + /** + * Matches a select tag + * + * @return array + */ + public function matchFloatingSelect() + { + return array( + 'tag' => 'select', + 'children' => array( + 'count' => 4, + 'only' => array('tag' => 'option'), + ), + 'attributes' => array( + 'id' => 'foo', + 'class' => 'form-select', + 'name' => 'foo', + ), + ); + } + + //////////////////////////////////////////////////////////////////// + ////////////////////////////// ASSERTIONS ////////////////////////// + //////////////////////////////////////////////////////////////////// + + /** + * Matches a Form Floating Label Group + * + * @param string $input + * @param string $label + * + * @return boolean + */ + protected function formFloatingLabelGroup( + $input = '', + $label = '' + ) { + return '
'.$input.$label.'
'; + } + + //////////////////////////////////////////////////////////////////// + //////////////////////////////// TESTS ///////////////////////////// + //////////////////////////////////////////////////////////////////// + + public function testCanCreateFloatingLabelInputText() + { + $input = $this->former + ->text('foo') + ->placeholder('Dummy placeholder') + ->floatingLabel() + ->__toString(); + + $this->assertHTML($this->matchFloatingLabel(), $input); + $this->assertHTML($this->matchFloatingInputText(), $input); + + $matcher = $this->formFloatingLabelGroup(); + $this->assertEquals($matcher, $input); + } + + public function testCanCreateFloatingLabelTextarea() + { + $textarea = $this->former + ->textarea('foo') + ->placeholder('Dummy placeholder') + ->floatingLabel() + ->__toString(); + + $this->assertHTML($this->matchFloatingLabel(), $textarea); + $this->assertHTML($this->matchFloatingTextarea(), $textarea); + + $matcher = $this->formFloatingLabelGroup(''); + $this->assertEquals($matcher, $textarea); + } + + public function testCanCreateFloatingLabelSelect() + { + $selectElement = $this->former + ->select('foo') + ->options($this->options) + ->placeholder('Choose an option') + ->floatingLabel(); + + $select = $selectElement->__toString(); + + $this->assertHTML($this->matchFloatingLabel(), $select); + $this->assertHTML($this->matchFloatingSelect(), $select); + + $placeholderOption = Element::create('option', 'Choose an option', array('value' => '', 'disabled' => 'disabled', 'selected' => 'selected')); + + $options = array($placeholderOption); + foreach ($this->options as $key => $option) { + $options[] = Element::create('option', $option, array('value' => $key)); + } + $this->assertEquals($selectElement->getOptions(), $options); + + $matcher = $this->formFloatingLabelGroup(''); + $this->assertEquals($matcher, $select); + } +}