Skip to content

Commit

Permalink
RadioElementTest: Add cloning test
Browse files Browse the repository at this point in the history
  • Loading branch information
TAINCER committed Feb 14, 2023
1 parent 3847921 commit 550b199
Showing 1 changed file with 73 additions and 35 deletions.
108 changes: 73 additions & 35 deletions tests/FormElement/RadioElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class RadioElementTest extends TestCase
public function testRendersElementCorrectly()
{
$radio = new RadioElement('test', [
'label' => 'Test',
'options' => [
'label' => 'Test',
'options' => [
'foo' => 'Foo',
'bar' => 'Bar',
'yes' => 'Yes'
Expand All @@ -35,8 +35,8 @@ public function testRendersElementCorrectly()
public function testNumbersOfTypeIntOrStringAsOptionKeysAreHandledEqually()
{
$radio = new RadioElement('test', [
'label' => 'Test',
'options' => [
'label' => 'Test',
'options' => [
'1' => 'Foo',
2 => 'Bar',
3 => 'Yes'
Expand Down Expand Up @@ -77,13 +77,13 @@ public function testNumbersOfTypeIntOrStringAsOptionKeysAreHandledEqually()
public function testSetValueAddsTheCheckedAttribute()
{
$radio = new RadioElement('test', [
'label' => 'Test',
'options' => [
'label' => 'Test',
'options' => [
'foo' => 'Foo',
'bar' => 'Bar',
'yes' => 'Yes'
],
'value' => 'bar'
'value' => 'bar'
]);

$html = <<<'HTML'
Expand Down Expand Up @@ -115,9 +115,9 @@ public function testSetValueAddsTheCheckedAttribute()
public function testDisabledRadioOptions()
{
$radio = new RadioElement('test', [
'label' => 'Test',
'disabledOptions' => ['foo', 'bar', 'yes'],
'options' => [
'label' => 'Test',
'disabledOptions' => ['foo', 'bar', 'yes'],
'options' => [
'foo' => 'Foo',
'bar' => 'Bar',
'yes' => 'Yes',
Expand All @@ -134,14 +134,14 @@ public function testDisabledRadioOptions()
$this->assertHtml($html, $radio);

$radio = new RadioElement('test', [
'label' => 'Test',
'options' => [
'label' => 'Test',
'options' => [
'foo' => 'Foo',
'bar' => 'Bar',
'yes' => 'Yes',
'no' => 'No'
],
'value' => 'bar'
'value' => 'bar'
]);

$radio->getOption('yes')->setDisabled();
Expand All @@ -168,9 +168,9 @@ public function testDisabledRadioOptions()
public function testNonCallbackAttributesOfTheElementAreAppliedToEachOption()
{
$radio = new RadioElement('test', [
'label' => 'Test',
'class' => 'blue',
'options' => [
'label' => 'Test',
'class' => 'blue',
'options' => [
'foo' => 'Foo',
'bar' => 'Bar',
'yes' => 'Yes',
Expand All @@ -192,8 +192,8 @@ public function testNonCallbackAttributesOfTheElementAreAppliedToEachOption()
public function testAddCssClassToTheLabelOfASpecificOption()
{
$radio = new RadioElement('test', [
'label' => 'Test',
'options' => [
'label' => 'Test',
'options' => [
'foo' => 'Foo',
'bar' => 'Bar',
'yes' => 'Yes',
Expand All @@ -219,9 +219,9 @@ public function testAddCssClassToTheLabelOfASpecificOption()
public function testAddAttributesToASpecificOption()
{
$radio = new RadioElement('test', [
'label' => 'Test',
'class' => 'blue',
'options' => [
'label' => 'Test',
'class' => 'blue',
'options' => [
'foo' => 'Foo',
'bar' => 'Bar',
'yes' => 'Yes',
Expand All @@ -245,13 +245,13 @@ public function testRadioNotValidIfCheckedValueIsInvalid()
{
StaticTranslator::$instance = new NoopTranslator();
$radio = new RadioElement('test', [
'label' => 'Test',
'options' => [
'label' => 'Test',
'options' => [
'foo' => 'Foo',
'bar' => 'Bar',
'yes' => 'Yes'
],
'value' => 'bar'
'value' => 'bar'
]);

$this->assertTrue($radio->isValid());
Expand All @@ -267,13 +267,13 @@ public function testRadioNotValidIfCheckedValueIsDisabled()
{
StaticTranslator::$instance = new NoopTranslator();
$radio = new RadioElement('test', [
'label' => 'Test',
'options' => [
'label' => 'Test',
'options' => [
'foo' => 'Foo',
'bar' => 'Bar',
'yes' => 'Yes'
],
'value' => 'bar'
'value' => 'bar'
]);

$radio->setValue('yes');
Expand All @@ -287,11 +287,11 @@ public function testNullAndTheEmptyStringAreEquallyHandled()
$form = new Form();
$form->addElement('radio', 'radio', [
'options' => ['' => 'Please choose'],
'value' => ''
'value' => ''
]);
$form->addElement('radio', 'radio2', [
'options' => [null => 'Please choose'],
'value' => null
'value' => null
]);

/** @var RadioElement $radio */
Expand Down Expand Up @@ -364,12 +364,12 @@ public function testSetOptionsResetsOptions()
public function testOrderOfOptionsAndDisabledOptionsDoesNotMatter()
{
$radio = new RadioElement('test', [
'label' => 'Test',
'options' => [
'label' => 'Test',
'options' => [
'foo' => 'Foo',
'bar' => 'Bar'
],
'disabledOptions' => ['foo', 'bar']
'disabledOptions' => ['foo', 'bar']
]);

$html = <<<'HTML'
Expand All @@ -379,9 +379,9 @@ public function testOrderOfOptionsAndDisabledOptionsDoesNotMatter()
$this->assertHtml($html, $radio);

$radio = new RadioElement('test', [
'disabledOptions' => ['foo', 'bar'],
'label' => 'Test',
'options' => [
'disabledOptions' => ['foo', 'bar'],
'label' => 'Test',
'options' => [
'foo' => 'Foo',
'bar' => 'Bar'
]
Expand Down Expand Up @@ -451,4 +451,42 @@ public function testGetOptionGetValueAndElementGetValueHandleNullAndTheEmptyStri
$this->assertNull($radio->getValue());
$this->assertNull($radio->getOption(null)->getValue());
}

public function testCloning(): void
{
$form = new Form();

$radio = new RadioElement('radio', [
'options' => [
'key1' => 'value1',
'key2' => 'value2'
]
]);

$clone = clone $radio;
$clone->setName('clone');
$clone->setOptions([
'key3' => 'value3',
'key4' => 'value4'
]);

$form
->addElement($radio)
->addElement($clone)
->populate([
'radio' => 'key1',
'clone' => 'key4'
]);

$expected = <<<'HTML'
<form method="POST">
<label class="radio-label"><input checked="checked" name="radio" type="radio" value="key1"/>value1</label>
<label class="radio-label"><input name="radio" type="radio" value="key2"/>value2</label>
<label class="radio-label"><input name="clone" type="radio" value="key3"/>value3</label>
<label class="radio-label"><input checked="checked" name="clone" type="radio" value="key4"/>value4</label>
</form>
HTML;

$this->assertHtml($expected, $form);
}
}

0 comments on commit 550b199

Please sign in to comment.