TYPO3 CMS  TYPO3_7-6
GroupedForViewHelperTest.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 
18 {
22  protected $viewHelper;
23 
24  protected function setUp()
25  {
26  parent::setUp();
27  $this->viewHelper = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\GroupedForViewHelper::class, ['renderChildren']);
28  $this->injectDependenciesIntoViewHelper($this->viewHelper);
29  $this->viewHelper->initializeArguments();
30  }
31 
36  {
37  $this->assertEquals('', $this->viewHelper->render(null, 'foo', 'bar'));
38  }
39 
44  {
45  $this->assertEquals('', $this->viewHelper->render([], 'foo', 'bar'));
46  }
47 
53  {
54  $object = new \stdClass();
55 
56  $this->viewHelper->render($object, 'innerVariable', 'someKey');
57  }
58 
63  {
64  $photoshop = ['name' => 'Adobe Photoshop', 'license' => 'commercial'];
65  $typo3 = ['name' => 'TYPO3', 'license' => 'GPL'];
66  $office = ['name' => 'Microsoft Office', 'license' => 'commercial'];
67  $drupal = ['name' => 'Drupal', 'license' => 'GPL'];
68  $wordpress = ['name' => 'Wordpress', 'license' => 'GPL'];
69 
70  $products = ['photoshop' => $photoshop, 'typo3' => $typo3, 'office' => $office, 'drupal' => $drupal, 'wordpress' => $wordpress];
71 
72  $this->templateVariableContainer->expects($this->at(0))->method('add')->with('myGroupKey', 'commercial');
73  $this->templateVariableContainer->expects($this->at(1))->method('add')->with('products', ['photoshop' => $photoshop, 'office' => $office]);
74  $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('myGroupKey');
75  $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('products');
76  $this->templateVariableContainer->expects($this->at(4))->method('add')->with('myGroupKey', 'GPL');
77  $this->templateVariableContainer->expects($this->at(5))->method('add')->with('products', ['typo3' => $typo3, 'drupal' => $drupal, 'wordpress' => $wordpress]);
78  $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('myGroupKey');
79  $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('products');
80 
81  $this->viewHelper->render($products, 'products', 'license', 'myGroupKey');
82  }
83 
88  {
89  $photoshop = new \ArrayObject(['name' => 'Adobe Photoshop', 'license' => 'commercial']);
90  $typo3 = new \ArrayObject(['name' => 'TYPO3', 'license' => 'GPL']);
91  $office = new \ArrayObject(['name' => 'Microsoft Office', 'license' => 'commercial']);
92  $drupal = new \ArrayObject(['name' => 'Drupal', 'license' => 'GPL']);
93  $wordpress = new \ArrayObject(['name' => 'Wordpress', 'license' => 'GPL']);
94 
95  $products = new \ArrayObject(['photoshop' => $photoshop, 'typo3' => $typo3, 'office' => $office, 'drupal' => $drupal, 'wordpress' => $wordpress]);
96 
97  $this->templateVariableContainer->expects($this->at(0))->method('add')->with('myGroupKey', 'commercial');
98  $this->templateVariableContainer->expects($this->at(1))->method('add')->with('products', ['photoshop' => $photoshop, 'office' => $office]);
99  $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('myGroupKey');
100  $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('products');
101  $this->templateVariableContainer->expects($this->at(4))->method('add')->with('myGroupKey', 'GPL');
102  $this->templateVariableContainer->expects($this->at(5))->method('add')->with('products', ['typo3' => $typo3, 'drupal' => $drupal, 'wordpress' => $wordpress]);
103  $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('myGroupKey');
104  $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('products');
105 
106  $this->viewHelper->render($products, 'products', 'license', 'myGroupKey');
107  }
108 
113  {
114  $photoshop = new \stdClass();
115  $photoshop->name = 'Adobe Photoshop';
116  $photoshop->license = 'commercial';
117  $typo3 = new \stdClass();
118  $typo3->name = 'TYPO3';
119  $typo3->license = 'GPL';
120  $office = new \stdClass();
121  $office->name = 'Microsoft Office';
122  $office->license = 'commercial';
123  $drupal = new \stdClass();
124  $drupal->name = 'Drupal';
125  $drupal->license = 'GPL';
126  $wordpress = new \stdClass();
127  $wordpress->name = 'Wordpress';
128  $wordpress->license = 'GPL';
129 
130  $products = ['photoshop' => $photoshop, 'typo3' => $typo3, 'office' => $office, 'drupal' => $drupal, 'wordpress' => $wordpress];
131 
132  $this->templateVariableContainer->expects($this->at(0))->method('add')->with('myGroupKey', 'commercial');
133  $this->templateVariableContainer->expects($this->at(1))->method('add')->with('products', ['photoshop' => $photoshop, 'office' => $office]);
134  $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('myGroupKey');
135  $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('products');
136  $this->templateVariableContainer->expects($this->at(4))->method('add')->with('myGroupKey', 'GPL');
137  $this->templateVariableContainer->expects($this->at(5))->method('add')->with('products', ['typo3' => $typo3, 'drupal' => $drupal, 'wordpress' => $wordpress]);
138  $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('myGroupKey');
139  $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('products');
140 
141  $this->viewHelper->render($products, 'products', 'license', 'myGroupKey');
142  }
143 
148  {
149  $customer1 = new \stdClass();
150  $customer1->name = 'Anton Abel';
151 
152  $customer2 = new \stdClass();
153  $customer2->name = 'Balthasar Bux';
154 
155  $invoice1 = ['date' => new \DateTime('1980-12-13'), 'customer' => $customer1];
156  $invoice2 = ['date' => new \DateTime('2010-07-01'), 'customer' => $customer1];
157  $invoice3 = ['date' => new \DateTime('2010-07-04'), 'customer' => $customer2];
158 
159  $invoices = ['invoice1' => $invoice1, 'invoice2' => $invoice2, 'invoice3' => $invoice3];
160 
161  $this->templateVariableContainer->expects($this->at(0))->method('add')->with('myGroupKey', $customer1);
162  $this->templateVariableContainer->expects($this->at(1))->method('add')->with('invoices', ['invoice1' => $invoice1, 'invoice2' => $invoice2]);
163  $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('myGroupKey');
164  $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('invoices');
165  $this->templateVariableContainer->expects($this->at(4))->method('add')->with('myGroupKey', $customer2);
166  $this->templateVariableContainer->expects($this->at(5))->method('add')->with('invoices', ['invoice3' => $invoice3]);
167  $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('myGroupKey');
168  $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('invoices');
169 
170  $this->viewHelper->render($invoices, 'invoices', 'customer', 'myGroupKey');
171  }
172 
176  public function renderGroupsMultidimensionalArrayByPropertyPath()
177  {
178  $customer1 = new \stdClass();
179  $customer1->name = 'Anton Abel';
180 
181  $customer2 = new \stdClass();
182  $customer2->name = 'Balthasar Bux';
183 
184  $invoice1 = new \stdClass();
185  $invoice1->customer = $customer1;
186 
187  $invoice2 = new \stdClass();
188  $invoice2->customer = $customer1;
189 
190  $invoice3 = new \stdClass();
191  $invoice3->customer = $customer2;
192 
193  $invoices = ['invoice1' => $invoice1, 'invoice2' => $invoice2, 'invoice3' => $invoice3];
194  $groupBy = 'customer.name';
196  $accessibleMock = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\GroupedForViewHelper::class, ['dummy']);
197  $expectedResult = [
198  'keys' => [
199  'Anton Abel' => 'Anton Abel',
200  'Balthasar Bux' => 'Balthasar Bux'
201  ],
202  'values' => [
203  'Anton Abel' => [
204  'invoice1' => $invoice1,
205  'invoice2' => $invoice2
206  ],
207  'Balthasar Bux' => [
208  'invoice3' => $invoice3
209  ]
210  ]
211  ];
212  $this->assertSame($expectedResult, $accessibleMock->_callRef('groupElements', $invoices, $groupBy));
213  }
214 
219  {
220  $customer1 = new \stdClass();
221  $customer1->name = 'Anton Abel';
222 
223  $customer2 = new \stdClass();
224  $customer2->name = 'Balthasar Bux';
225 
226  $invoice1 = new \stdClass();
227  $invoice1->date = new \DateTime('1980-12-13');
228  $invoice1->customer = $customer1;
229 
230  $invoice2 = new \stdClass();
231  $invoice2->date = new \DateTime('2010-07-01');
232  $invoice2->customer = $customer1;
233 
234  $invoice3 = new \stdClass();
235  $invoice3->date = new \DateTime('2010-07-04');
236  $invoice3->customer = $customer2;
237 
238  $invoices = ['invoice1' => $invoice1, 'invoice2' => $invoice2, 'invoice3' => $invoice3];
239 
240  $this->templateVariableContainer->expects($this->at(0))->method('add')->with('myGroupKey', $customer1);
241  $this->templateVariableContainer->expects($this->at(1))->method('add')->with('invoices', ['invoice1' => $invoice1, 'invoice2' => $invoice2]);
242  $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('myGroupKey');
243  $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('invoices');
244  $this->templateVariableContainer->expects($this->at(4))->method('add')->with('myGroupKey', $customer2);
245  $this->templateVariableContainer->expects($this->at(5))->method('add')->with('invoices', ['invoice3' => $invoice3]);
246  $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('myGroupKey');
247  $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('invoices');
248 
249  $this->viewHelper->render($invoices, 'invoices', 'customer', 'myGroupKey');
250  }
251 
256  {
257  $date1 = new \DateTime('2010-07-01');
258  $date2 = new \DateTime('2010-07-04');
259 
260  $invoice1 = new \stdClass();
261  $invoice1->date = $date1;
262  $invoice1->id = 12340;
263 
264  $invoice2 = new \stdClass();
265  $invoice2->date = $date1;
266  $invoice2->id = 12341;
267 
268  $invoice3 = new \stdClass();
269  $invoice3->date = $date2;
270  $invoice3->id = 12342;
271 
272  $invoices = ['invoice1' => $invoice1, 'invoice2' => $invoice2, 'invoice3' => $invoice3];
273 
274  $this->templateVariableContainer->expects($this->at(0))->method('add')->with('myGroupKey', $date1);
275  $this->templateVariableContainer->expects($this->at(1))->method('add')->with('invoices', ['invoice1' => $invoice1, 'invoice2' => $invoice2]);
276  $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('myGroupKey');
277  $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('invoices');
278  $this->templateVariableContainer->expects($this->at(4))->method('add')->with('myGroupKey', $date2);
279  $this->templateVariableContainer->expects($this->at(5))->method('add')->with('invoices', ['invoice3' => $invoice3]);
280  $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('myGroupKey');
281  $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('invoices');
282 
283  $this->viewHelper->render($invoices, 'invoices', 'date', 'myGroupKey');
284  }
285 
290  {
291  $photoshop = ['name' => 'Adobe Photoshop', 'license' => 'commercial'];
292  $typo3 = ['name' => 'TYPO3', 'license' => 'GPL'];
293  $office = ['name' => 'Microsoft Office', 'license' => 'commercial'];
294 
295  $products = ['photoshop' => $photoshop, 'typo3' => $typo3, 'office' => $office];
296 
297  $this->templateVariableContainer->expects($this->at(0))->method('add')->with('groupKey', null);
298  $this->templateVariableContainer->expects($this->at(1))->method('add')->with('innerKey', $products);
299  $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('groupKey');
300  $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('innerKey');
301 
302  $this->viewHelper->render($products, 'innerKey', 'NonExistingKey');
303  }
304 
310  {
311  $values = ['some', 'simple', 'array'];
312 
313  $this->viewHelper->render($values, 'innerVariable', 'someKey');
314  }
315 }
injectDependenciesIntoViewHelper(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper $viewHelper)
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)