TYPO3 CMS  TYPO3_8-7
CheckboxViewHelperTest.php
Go to the documentation of this file.
1 <?php
3 
8 
12 class CheckboxViewHelperTest extends ViewHelperBaseTestcase
13 {
14 
18  protected $viewHelper;
19 
20  protected function setUp()
21  {
22  parent::setUp();
23  $this->viewHelper = new CheckboxViewHelper();
24  $this->injectDependenciesIntoViewHelper($this->viewHelper);
25  }
26 
31  {
32  $this->setArgumentsUnderTest(
33  $this->viewHelper,
34  [
35  'name' => 'foo',
36  ]
37  );
38  $expectedResult = '<input type="hidden" name="foo" value="" /><input type="checkbox" name="foo" value="" />';
39  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
40  $this->assertEquals($expectedResult, $actualResult);
41  }
42 
47  {
48  $this->setArgumentsUnderTest(
49  $this->viewHelper,
50  [
51  'name' => 'foo',
52  'checked' => true,
53  ]
54  );
55  $expectedResult = '<input type="hidden" name="foo" value="" /><input type="checkbox" name="foo" value="" checked="checked" />';
56  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
57  $this->assertEquals($expectedResult, $actualResult);
58  }
59 
66  {
67  $this->setArgumentsUnderTest(
68  $this->viewHelper,
69  [
70  'value' => 'foo',
71  'property' => 'someProperty',
72  'checked' => true
73  ]
74  );
75 
76  $formObject = new \stdClass();
77  $formObject->someProperty = false;
78 
79  $this->stubVariableContainer($formObject);
80  $this->stubRequestWithoutMappingErrors();
81 
82  $result = $this->viewHelper->initializeArgumentsAndRender();
83 
84  $this->assertContains('<input type="hidden" name="fieldPrefix[objectName][someProperty]" value="" />', $result);
85  $this->assertContains('<input type="checkbox" name="fieldPrefix[objectName][someProperty]" value="foo" checked="checked" />', $result);
86  }
87 
94  {
95  $this->setArgumentsUnderTest(
96  $this->viewHelper,
97  [
98  'value' => 'foo',
99  'property' => 'someProperty',
100  ]
101  );
102 
103  $formObject = new \stdClass();
104  $formObject->someProperty = true;
105 
106  $this->stubVariableContainer($formObject);
107  $this->stubRequestWithoutMappingErrors();
108 
109  $result = $this->viewHelper->initializeArgumentsAndRender();
110 
111  $this->assertContains('<input type="hidden" name="fieldPrefix[objectName][someProperty]" value="" />', $result);
112  $this->assertContains(
113  '<input type="checkbox" name="fieldPrefix[objectName][someProperty]" value="foo" checked="checked" />',
114  $result
115  );
116  }
117 
122  {
123  $this->setArgumentsUnderTest(
124  $this->viewHelper,
125  [
126  'value' => 'foo',
127  'property' => 'someProperty',
128  ]
129  );
130 
131  $formObject = new \stdClass();
132  $formObject->someProperty = [];
133 
134  $this->stubVariableContainer($formObject);
135  $this->stubRequestWithoutMappingErrors();
136 
137  $result = $this->viewHelper->initializeArgumentsAndRender();
138 
139  $this->assertContains(
140  '<input type="checkbox" name="fieldPrefix[objectName][someProperty][]" value="foo" />',
141  $result
142  );
143  }
144 
149  {
150  $this->setArgumentsUnderTest(
151  $this->viewHelper,
152  [
153  'value' => 'foo',
154  'property' => 'someProperty',
155  'checked' => true
156  ]
157  );
158 
159  $formObject = new \stdClass();
160  $formObject->someProperty = ['foo'];
161 
162  $this->stubVariableContainer($formObject);
163  $this->stubRequestWithoutMappingErrors();
164 
165  $result = $this->viewHelper->initializeArgumentsAndRender();
166 
167  $this->assertContains(
168  '<input type="checkbox" name="fieldPrefix[objectName][someProperty]" value="foo" checked="checked" />',
169  $result
170  );
171  }
172 
177  {
178  $this->setArgumentsUnderTest(
179  $this->viewHelper,
180  [
181  'value' => 'bar',
182  'property' => 'someProperty',
183  'checked' => true
184  ]
185  );
186 
187  $formObject = new \ArrayObject(['foo', 'bar', 'baz']);
188 
189  $this->stubVariableContainer($formObject);
190  $this->stubRequestWithoutMappingErrors();
191 
192  $result = $this->viewHelper->initializeArgumentsAndRender();
193 
194  $this->assertContains(
195  '<input type="checkbox" name="fieldPrefix[objectName][someProperty]" value="bar" checked="checked" />',
196  $result
197  );
198  }
199 
204  {
205  $this->setArgumentsUnderTest(
206  $this->viewHelper,
207  [
208  'value' => 'foo',
209  'property' => 'someProperty',
210  ]
211  );
212 
213  $formObject = new \stdClass();
214  $formObject->someProperty = 'bar';
215 
216  $this->stubVariableContainer($formObject);
217  $this->stubRequestWithoutMappingErrors();
218 
219  $result = $this->viewHelper->initializeArgumentsAndRender();
220 
221  $this->assertContains(
222  '<input type="checkbox" name="fieldPrefix[objectName][someProperty]" value="foo" checked="checked" />',
223  $result
224  );
225  }
226 
231  {
232  $this->setArgumentsUnderTest(
233  $this->viewHelper,
234  [
235  'value' => 'foo',
236  'property' => 'someProperty',
237  ]
238  );
239 
240  $formObject = new \stdClass();
241  $formObject->someProperty = null;
242 
243  $this->stubVariableContainer($formObject);
244  $this->stubRequestWithoutMappingErrors();
245 
246  $result = $this->viewHelper->initializeArgumentsAndRender();
247 
248  $this->assertContains(
249  '<input type="checkbox" name="fieldPrefix[objectName][someProperty]" value="foo" />',
250  $result
251  );
252  }
253 
258  {
259  $this->setArgumentsUnderTest(
260  $this->viewHelper,
261  [
262  'value' => 2,
263  'property' => 'someProperty',
264  ]
265  );
266 
267  $property1 = new \stdClass();
268  $property2 = new \stdClass();
269  $property3 = new \stdClass();
270 
271  $persistenceManager = $this->prophesize(PersistenceManager::class);
272  $persistenceManager->getIdentifierByObject($property1)->willReturn(1);
273  $persistenceManager->getIdentifierByObject($property2)->willReturn(2);
274  $persistenceManager->getIdentifierByObject($property3)->willReturn(3);
275  $this->viewHelper->injectPersistenceManager($persistenceManager->reveal());
276 
277  $formObject = new \stdClass();
278  $formObject->someProperty = [$property1, $property2, $property3];
279 
280  $this->stubVariableContainer($formObject);
281  $this->stubRequestWithoutMappingErrors();
282 
283  $result = $this->viewHelper->initializeArgumentsAndRender();
284 
285  $this->assertContains(
286  '<input type="checkbox" name="fieldPrefix[objectName][someProperty][]" value="2" />',
287  $result
288  );
289  }
290 
295  {
296  $this->setArgumentsUnderTest(
297  $this->viewHelper,
298  [
299  'value' => 'foo',
300  'property' => 'someProperty',
301  'errorClass' => 'error',
302  ]
303  );
304 
305  $formObject = new \stdClass();
306  $formObject->someProperty = null;
307 
308  $this->stubVariableContainer($formObject);
309  $this->stubRequestWithMappingErrors();
310 
311  $result = $this->viewHelper->initializeArgumentsAndRender();
312 
313  $this->assertContains(
314  '<input type="checkbox" name="fieldPrefix[objectName][someProperty]" value="foo" class="error" />',
315  $result
316  );
317  }
318 
323  {
324  $this->setArgumentsUnderTest(
325  $this->viewHelper,
326  [
327  'value' => 'foo',
328  'property' => 'someProperty',
329  ]
330  );
331 
332  $formObject = new \stdClass();
333  $formObject->someProperty = null;
334 
335  $this->stubVariableContainer($formObject);
336  $this->stubRequestWithMappingErrors();
337 
338  $result = $this->viewHelper->initializeArgumentsAndRender();
339 
340  $this->assertContains(
341  '<input type="checkbox" name="fieldPrefix[objectName][someProperty]" value="foo" class="f3-form-error" />',
342  $result
343  );
344  }
345 
350  {
351  $this->setArgumentsUnderTest(
352  $this->viewHelper,
353  [
354  'value' => 'foo',
355  'property' => 'someProperty',
356  'class' => 'css_class'
357  ]
358  );
359 
360  $formObject = new \stdClass();
361  $formObject->someProperty = null;
362 
363  $this->stubVariableContainer($formObject);
364  $this->stubRequestWithMappingErrors();
365 
366  $result = $this->viewHelper->initializeArgumentsAndRender();
367 
368  $this->assertContains(
369  '<input class="css_class f3-form-error" type="checkbox" name="fieldPrefix[objectName][someProperty]" value="foo" />',
370  $result
371  );
372  }
373 }