From ed41c5d0c1fefbcd248f2ce3c2216213b9727b86 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Mon, 13 Feb 2023 17:52:20 +0100 Subject: [PATCH] Test cloning select elements --- tests/FormElement/SelectElementTest.php | 111 ++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/tests/FormElement/SelectElementTest.php b/tests/FormElement/SelectElementTest.php index 3f8689c2..60999e38 100644 --- a/tests/FormElement/SelectElementTest.php +++ b/tests/FormElement/SelectElementTest.php @@ -8,6 +8,7 @@ use ipl\I18n\NoopTranslator; use ipl\I18n\StaticTranslator; use ipl\Tests\Html\TestCase; +use ReflectionFunction; use UnexpectedValueException; class SelectElementTest extends TestCase @@ -615,4 +616,114 @@ public function testGetOptionReturnsPreviouslySetOption() $this->assertNull($select->getOption('')->getValue()); $this->assertSame('car', $select->getOption('car')->getValue()); } + + public function testDirectCloning() + { + $select = new SelectElement('select', [ + 'options' => [ + null => 'Please choose', + 'option1' => 'Option 1', + 'option2' => 'Option 2', + 'option3' => 'Option 3', + 'option4' => 'Option 4' + ], + 'disabledOptions' => ['option3', 'option4'] + ]); + $select->setValue('option1'); + + $clone = (clone $select) + ->setDisabledOptions([]) + ->setValue('option2'); + + $selectHtml = <<<'HTML' + +HTML; + $this->assertHtml($selectHtml, $select, 'Modifying the cloned element also affects the original element'); + + $cloneHtml = <<<'HTML' + +HTML; + $this->assertHtml($cloneHtml, $clone, 'Modifying the cloned element does not have the expected result'); + + $assembledClone = (clone $select) + ->setDisabledOptions([]) + ->setValue('option2'); + $this->assertHtml( + $cloneHtml, + $assembledClone, + 'Modifying the clone of the already assembled element does not have the expected result' + ); + } + + public function testImplicitCloning() + { + $select = new SelectElement('select', [ + 'options' => [ + null => 'Please choose', + 'option1' => 'Option 1', + 'option2' => 'Option 2', + 'option3' => 'Option 3', + 'option4' => 'Option 4' + ], + 'disabledOptions' => ['option3', 'option4'] + ]); + $select->setValue('option1'); + $form = (new Form()) + ->addElement($select); + + $clone = clone $form; + $clone + ->getElement('select') + ->setDisabledOptions([]) + ->setValue('option2'); + + $formHtml = <<<'HTML' +
+ +
+HTML; + $this->assertHtml($formHtml, $form, 'Modifying the cloned element also affects the original element'); + + $cloneHtml = <<<'HTML' +
+ +
+HTML; + $this->assertHtml($cloneHtml, $clone, 'Modifying the cloned element does not have the expected result'); + + $assembledClone = clone $form; + $assembledClone + ->getElement('select') + ->setDisabledOptions([]) + ->setValue('option2'); + $this->assertHtml( + $cloneHtml, + $assembledClone, + 'Modifying the clone of the already assembled element does not have the expected result' + ); + } }