‪TYPO3CMS  10.4
OptionViewHelperTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
22 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
23 use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;
24 
28 class ‪OptionViewHelperTest extends ViewHelperBaseTestcase
29 {
33  protected ‪$viewHelper;
34 
35  protected function ‪setUp(): void
36  {
37  parent::setUp();
38  $this->viewHelper = $this->getMockBuilder(OptionViewHelper::class)
39  ->onlyMethods(['registerFieldNameForFormTokenGeneration', 'renderChildren'])
40  ->getMock();
41  $this->viewHelperVariableContainer->get(SelectViewHelper::class, 'registerFieldNameForFormTokenGeneration')->willReturn('');
42  $this->arguments['selected'] = null;
43  $this->arguments['value'] = null;
44  $this->tagBuilder = new TagBuilder();
45  $this->viewHelper->setTagBuilder($this->tagBuilder);
46  $this->injectDependenciesIntoViewHelper($this->viewHelper);
47  }
48 
52  public function ‪optionTagNameIsSet(): void
53  {
54  $tagBuilder = $this->createMock(TagBuilder::class);
55  $this->viewHelperVariableContainer->get(SelectViewHelper::class, 'selectedValue')->willReturn('');
56  $tagBuilder->expects(self::atLeastOnce())->method('setTagName')->with('option');
57  $this->viewHelper->setTagBuilder($tagBuilder);
58  $this->viewHelper->initializeArgumentsAndRender();
59  }
60 
65  {
66  $this->viewHelper->expects(self::once())->method('renderChildren')->willReturn('Option Label');
67  $this->viewHelperVariableContainer->get(SelectViewHelper::class, 'selectedValue')->willReturn('');
68  $expected = '<option value="Option Label">Option Label</option>';
69  self::assertEquals($expected, $this->viewHelper->initializeArgumentsAndRender());
70  }
71 
75  public function ‪valueCanBeOverwrittenByArgument(): void
76  {
77  $this->arguments['value'] = 'value';
78  $this->viewHelper->setArguments($this->arguments);
79  $this->viewHelperVariableContainer->get(SelectViewHelper::class, 'selectedValue')->willReturn('');
80  $this->viewHelper->expects(self::once())->method('renderChildren')->willReturn('Option Label');
81  $expected = '<option value="value">Option Label</option>';
82  self::assertEquals($expected, $this->viewHelper->initializeArgumentsAndRender());
83  }
84 
89  {
90  $this->arguments['selected'] = null;
91  $this->viewHelper->setArguments($this->arguments);
92  $this->viewHelperVariableContainer->get(SelectViewHelper::class, 'selectedValue')->willReturn('Option Label');
93  $this->viewHelper->expects(self::once())->method('renderChildren')->willReturn('Option Label');
94 
95  $expected = '<option selected="selected" value="Option Label">Option Label</option>';
96  self::assertEquals($expected, $this->viewHelper->initializeArgumentsAndRender());
97  }
98 
100  {
101  return [
102  'string value, string selection' => [
103  'val1', 'val1'
104  ],
105  'string value, int selection' => [
106  '1', 1
107  ],
108  'string value, int array selection' => [
109  '1', [1]
110  ],
111  'string value, iterable selection with int' => [
112  '1', (new \ArrayObject([1]))->getIterator()
113  ],
114  'string value, array selection' => [
115  'val1', ['val1']
116  ],
117  'string value, iterable selection' => [
118  'val1', (new \ArrayObject(['val1']))->getIterator()
119  ],
120  'int value, array selection' => [
121  1, ['1']
122  ]
123  ];
124  }
125 
132  public function ‪selectedIsAddedToSelectedOptionForProvidedValue($value, $selected): void
133  {
134  $this->arguments['selected'] = null;
135  $this->arguments['value'] = $value;
136  $this->viewHelper->expects(self::once())->method('renderChildren')->willReturn('Option Label');
137  $this->viewHelper->setArguments($this->arguments);
138  $this->viewHelperVariableContainer->get(SelectViewHelper::class, 'selectedValue')->willReturn($selected);
139 
140  $expected = '<option value="' . $value . '" selected="selected">Option Label</option>';
141  self::assertEquals($expected, $this->viewHelper->initializeArgumentsAndRender());
142  }
143 
148  {
149  $this->arguments['selected'] = null;
150  $this->viewHelper->setArguments($this->arguments);
151  $this->viewHelperVariableContainer->get(SelectViewHelper::class, 'selectedValue')->willReturn('');
152  $this->viewHelper->expects(self::once())->method('renderChildren')->willReturn('Option Label');
153 
154  $expected = '<option value="Option Label">Option Label</option>';
155  self::assertEquals($expected, $this->viewHelper->initializeArgumentsAndRender());
156  }
157 
159  {
160  return [
161  'string value, string selection' => [
162  '1',
163  '1-2'
164  ],
165  'string value, array selection' => [
166  'val1',
167  ['val3']
168  ],
169  'string value, iterable selection' => [
170  'val1',
171  (new \ArrayObject(['val3']))->getIterator()
172  ],
173  'int value, array selection' => [
174  1,
175  ['1-2']
176  ]
177  ];
178  }
179 
186  public function ‪selectedIsNotAddedToSelectedOptionForProvidedValue($value, $selected): void
187  {
188  $this->arguments['selected'] = null;
189  $this->arguments['value'] = $value;
190  $this->viewHelper->expects(self::once())->method('renderChildren')->willReturn('Option Label');
191  $this->viewHelper->setArguments($this->arguments);
192  $this->viewHelperVariableContainer->get(SelectViewHelper::class, 'selectedValue')->willReturn($selected);
193 
194  $expected = '<option value="' . $value . '">Option Label</option>';
195  self::assertEquals($expected, $this->viewHelper->initializeArgumentsAndRender());
196  }
197 
202  {
203  $this->arguments['selected'] = false;
204  $this->viewHelper->setArguments($this->arguments);
205  $this->viewHelper->expects(self::once())->method('renderChildren')->willReturn('Option Label');
206 
207  $expected = '<option value="Option Label">Option Label</option>';
208  self::assertEquals($expected, $this->viewHelper->initializeArgumentsAndRender());
209  }
210 
214  public function ‪selectedIsAddedToOptionForExplicitOverride(): void
215  {
216  $this->arguments['selected'] = true;
217  $this->viewHelper->setArguments($this->arguments);
218  $this->viewHelper->expects(self::once())->method('renderChildren')->willReturn('Option Label');
219 
220  $expected = '<option selected="selected" value="Option Label">Option Label</option>';
221  self::assertEquals($expected, $this->viewHelper->initializeArgumentsAndRender());
222  }
223 }
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Select\OptionViewHelperTest\valueCanBeOverwrittenByArgument
‪valueCanBeOverwrittenByArgument()
Definition: OptionViewHelperTest.php:74
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Select\OptionViewHelperTest\childrenContentIsUsedAsValueAndLabelByDefault
‪childrenContentIsUsedAsValueAndLabelByDefault()
Definition: OptionViewHelperTest.php:63
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Select\OptionViewHelperTest\$viewHelper
‪OptionViewHelper PHPUnit Framework MockObject MockObject $viewHelper
Definition: OptionViewHelperTest.php:32
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Select\OptionViewHelperTest\selectedIsAddedToSelectedOptionForProvidedValue
‪selectedIsAddedToSelectedOptionForProvidedValue($value, $selected)
Definition: OptionViewHelperTest.php:131
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Select\OptionViewHelperTest\selectedIsNotAddedToUnselectedOptionForNoSelectionOverride
‪selectedIsNotAddedToUnselectedOptionForNoSelectionOverride()
Definition: OptionViewHelperTest.php:146
‪TYPO3\CMS\Fluid\ViewHelpers\Form\Select\OptionViewHelper
Definition: OptionViewHelper.php:25
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Select\OptionViewHelperTest\setUp
‪setUp()
Definition: OptionViewHelperTest.php:34
‪TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper
Definition: SelectViewHelper.php:95
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Select\OptionViewHelperTest\selectedIsAddedToSelectedOptionForProvidedValueDataProvider
‪selectedIsAddedToSelectedOptionForProvidedValueDataProvider()
Definition: OptionViewHelperTest.php:98
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Select
Definition: OptionViewHelperTest.php:18
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Select\OptionViewHelperTest\selectedIsNotAddedToOptionForExplicitOverride
‪selectedIsNotAddedToOptionForExplicitOverride()
Definition: OptionViewHelperTest.php:200
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Select\OptionViewHelperTest\selectedIsNotAddedToSelectedOptionForProvidedValue
‪selectedIsNotAddedToSelectedOptionForProvidedValue($value, $selected)
Definition: OptionViewHelperTest.php:185
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Select\OptionViewHelperTest\selectedIsAddedToOptionForExplicitOverride
‪selectedIsAddedToOptionForExplicitOverride()
Definition: OptionViewHelperTest.php:213
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Select\OptionViewHelperTest
Definition: OptionViewHelperTest.php:29
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Select\OptionViewHelperTest\optionTagNameIsSet
‪optionTagNameIsSet()
Definition: OptionViewHelperTest.php:51
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Select\OptionViewHelperTest\selectedIsNotAddedToSelectedOptionForProvidedValueDataProvider
‪selectedIsNotAddedToSelectedOptionForProvidedValueDataProvider()
Definition: OptionViewHelperTest.php:157
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Select\OptionViewHelperTest\selectedIsAddedToSelectedOptionForNoSelectionOverride
‪selectedIsAddedToSelectedOptionForNoSelectionOverride()
Definition: OptionViewHelperTest.php:87