TYPO3 CMS  TYPO3_7-6
CheckboxViewHelperTest.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License, either version 3 *
9  * of the License, or (at your option) any later version. *
10  * *
11  * The TYPO3 project - inspiring people to share! *
12  * */
13 
17 
22 {
26  protected $viewHelper;
27 
28  protected function setUp()
29  {
30  parent::setUp();
31  $this->viewHelper = $this->getAccessibleMock(CheckboxViewHelper::class, ['setErrorClassAttribute', 'getName', 'getValueAttribute', 'isObjectAccessorMode', 'getPropertyValue', 'registerFieldNameForFormTokenGeneration']);
32  $this->arguments['property'] = '';
33  $this->injectDependenciesIntoViewHelper($this->viewHelper);
34  $this->viewHelper->initializeArguments();
35  }
36 
41  {
42  $mockTagBuilder = $this->getMock(TagBuilder::class, ['setTagName', 'addAttribute']);
43  $mockTagBuilder->expects($this->once())->method('setTagName')->with('input');
44  $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
45  $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
46  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('foo');
47  $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
48 
49  $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
50  $this->viewHelper->expects($this->any())->method('getValueAttribute')->will($this->returnValue('bar'));
51  $this->viewHelper->_set('tag', $mockTagBuilder);
52 
53  $this->viewHelper->initialize();
54  $this->viewHelper->render();
55  }
56 
61  {
62  $mockTagBuilder = $this->getMock(TagBuilder::class, ['setTagName', 'addAttribute']);
63  $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
64  $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
65  $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
66  $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
67 
68  $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
69  $this->viewHelper->expects($this->any())->method('getValueAttribute')->will($this->returnValue('bar'));
70  $this->viewHelper->_set('tag', $mockTagBuilder);
71 
72  $this->viewHelper->initialize();
73  $this->viewHelper->render(true);
74  }
75 
80  {
81  $mockTagBuilder = $this->getMock(TagBuilder::class, ['setTagName', 'addAttribute']);
82  $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
83  $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
84  $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
85 
86  $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
87  $this->viewHelper->expects($this->any())->method('getValueAttribute')->will($this->returnValue('bar'));
88  $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(true));
89  $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue(true));
90  $this->viewHelper->_set('tag', $mockTagBuilder);
91 
92  $this->viewHelper->initialize();
93  $this->viewHelper->render(true);
94  $this->viewHelper->render(false);
95  }
96 
101  {
102  $mockTagBuilder = $this->getMock(TagBuilder::class, ['setTagName', 'addAttribute']);
103  $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
104  $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
105  $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
106  $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
107 
108  $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
109  $this->viewHelper->expects($this->any())->method('getValueAttribute')->will($this->returnValue('bar'));
110  $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(true));
111  $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue(true));
112  $this->viewHelper->_set('tag', $mockTagBuilder);
113 
114  $this->viewHelper->initialize();
115  $this->viewHelper->render();
116  }
117 
122  {
123  $mockTagBuilder = $this->getMock(TagBuilder::class, ['setTagName', 'addAttribute']);
124  $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
125  $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo[]');
126  $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('foo[]');
127  $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
128 
129  $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
130  $this->viewHelper->expects($this->any())->method('getValueAttribute')->will($this->returnValue('bar'));
131  $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(true));
132  $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue([]));
133  $this->viewHelper->_set('tag', $mockTagBuilder);
134 
135  $this->viewHelper->initialize();
136  $this->viewHelper->render();
137  }
138 
143  {
144  $mockTagBuilder = $this->getMock(TagBuilder::class, ['setTagName', 'addAttribute']);
145  $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
146  $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo[]');
147  $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
148  $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
149 
150  $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
151  $this->viewHelper->expects($this->any())->method('getValueAttribute')->will($this->returnValue('bar'));
152  $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(true));
153  $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue(['foo', 'bar', 'baz']));
154  $this->viewHelper->_set('tag', $mockTagBuilder);
155 
156  $this->viewHelper->initialize();
157  $this->viewHelper->render();
158  }
159 
164  {
165  $mockTagBuilder = $this->getMock(TagBuilder::class, ['setTagName', 'addAttribute']);
166  $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
167  $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo[]');
168  $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
169  $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
170 
171  $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
172  $this->viewHelper->expects($this->any())->method('getValueAttribute')->will($this->returnValue('bar'));
173  $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(true));
174  $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue(new \ArrayObject(['foo', 'bar', 'baz'])));
175  $this->viewHelper->_set('tag', $mockTagBuilder);
176 
177  $this->viewHelper->initialize();
178  $this->viewHelper->render();
179  }
180 
185  {
186  $mockTagBuilder = $this->getMock(TagBuilder::class, ['setTagName', 'addAttribute']);
187  $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
188  $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
189  $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
190  $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
191 
192  $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
193  $this->viewHelper->expects($this->any())->method('getValueAttribute')->will($this->returnValue('bar'));
194  $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(true));
195  $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue(new \stdClass()));
196  $this->viewHelper->_set('tag', $mockTagBuilder);
197 
198  $this->viewHelper->initialize();
199  $this->viewHelper->render();
200  }
201 
206  {
207  $mockTagBuilder = $this->getMock(TagBuilder::class, ['setTagName', 'addAttribute']);
208  $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
209  $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo[]');
210  $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 2);
211  $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
212 
213  $object1 = new \stdClass();
214  $object2 = new \stdClass();
215  $object3 = new \stdClass();
216 
217  $this->viewHelper->expects($this->any())->method('getName')->willReturn('foo');
218  $this->viewHelper->expects($this->any())->method('getValueAttribute')->willReturn(2);
219  $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->willReturn(true);
220  $this->viewHelper->expects($this->any())->method('getPropertyValue')->willReturn([$object1, $object2, $object3]);
221  $this->viewHelper->_set('tag', $mockTagBuilder);
222 
223  $mockPersistenceManager = $this->getMock(PersistenceManagerInterface::class);
224  $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValueMap([
225  [$object1, 1],
226  [$object2, 2],
227  [$object3, 3],
228  ]));
229  $this->viewHelper->_set('persistenceManager', $mockPersistenceManager);
230 
231  $this->viewHelper->initialize();
232  $this->viewHelper->render();
233  }
234 
239  {
240  $this->viewHelper->expects($this->once())->method('setErrorClassAttribute');
241  $this->viewHelper->render();
242  }
243 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)
injectDependenciesIntoViewHelper(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper $viewHelper)