TYPO3 CMS  TYPO3_6-2
All Classes Namespaces Files Functions Variables Pages
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  public function setUp() {
25  parent::setUp();
26  $this->viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\GroupedForViewHelper', array('renderChildren'));
27  $this->injectDependenciesIntoViewHelper($this->viewHelper);
28  $this->viewHelper->initializeArguments();
29  }
30 
35  $this->assertEquals('', $this->viewHelper->render(NULL, 'foo', 'bar'));
36  }
37 
42  $this->assertEquals('', $this->viewHelper->render(array(), 'foo', 'bar'));
43  }
44 
50  $object = new \stdClass();
51 
52  $this->viewHelper->render($object, 'innerVariable', 'someKey');
53  }
54 
59  $photoshop = array('name' => 'Adobe Photoshop', 'license' => 'commercial');
60  $typo3 = array('name' => 'TYPO3', 'license' => 'GPL');
61  $office = array('name' => 'Microsoft Office', 'license' => 'commercial');
62  $drupal = array('name' => 'Drupal', 'license' => 'GPL');
63  $wordpress = array('name' => 'Wordpress', 'license' => 'GPL');
64 
65  $products = array('photoshop' => $photoshop, 'typo3' => $typo3, 'office' => $office, 'drupal' => $drupal, 'wordpress' => $wordpress);
66 
67  $this->templateVariableContainer->expects($this->at(0))->method('add')->with('myGroupKey', 'commercial');
68  $this->templateVariableContainer->expects($this->at(1))->method('add')->with('products', array('photoshop' => $photoshop, 'office' => $office));
69  $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('myGroupKey');
70  $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('products');
71  $this->templateVariableContainer->expects($this->at(4))->method('add')->with('myGroupKey', 'GPL');
72  $this->templateVariableContainer->expects($this->at(5))->method('add')->with('products', array('typo3' => $typo3, 'drupal' => $drupal, 'wordpress' => $wordpress));
73  $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('myGroupKey');
74  $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('products');
75 
76  $this->viewHelper->render($products, 'products', 'license', 'myGroupKey');
77  }
78 
83  $photoshop = new \ArrayObject(array('name' => 'Adobe Photoshop', 'license' => 'commercial'));
84  $typo3 = new \ArrayObject(array('name' => 'TYPO3', 'license' => 'GPL'));
85  $office = new \ArrayObject(array('name' => 'Microsoft Office', 'license' => 'commercial'));
86  $drupal = new \ArrayObject(array('name' => 'Drupal', 'license' => 'GPL'));
87  $wordpress = new \ArrayObject(array('name' => 'Wordpress', 'license' => 'GPL'));
88 
89  $products = new \ArrayObject(array('photoshop' => $photoshop, 'typo3' => $typo3, 'office' => $office, 'drupal' => $drupal, 'wordpress' => $wordpress));
90 
91  $this->templateVariableContainer->expects($this->at(0))->method('add')->with('myGroupKey', 'commercial');
92  $this->templateVariableContainer->expects($this->at(1))->method('add')->with('products', array('photoshop' => $photoshop, 'office' => $office));
93  $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('myGroupKey');
94  $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('products');
95  $this->templateVariableContainer->expects($this->at(4))->method('add')->with('myGroupKey', 'GPL');
96  $this->templateVariableContainer->expects($this->at(5))->method('add')->with('products', array('typo3' => $typo3, 'drupal' => $drupal, 'wordpress' => $wordpress));
97  $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('myGroupKey');
98  $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('products');
99 
100  $this->viewHelper->render($products, 'products', 'license', 'myGroupKey');
101  }
102 
107  $photoshop = new \stdClass();
108  $photoshop->name = 'Adobe Photoshop';
109  $photoshop->license = 'commercial';
110  $typo3 = new \stdClass();
111  $typo3->name = 'TYPO3';
112  $typo3->license = 'GPL';
113  $office = new \stdClass();
114  $office->name = 'Microsoft Office';
115  $office->license = 'commercial';
116  $drupal = new \stdClass();
117  $drupal->name = 'Drupal';
118  $drupal->license = 'GPL';
119  $wordpress = new \stdClass();
120  $wordpress->name = 'Wordpress';
121  $wordpress->license = 'GPL';
122 
123  $products = array('photoshop' => $photoshop, 'typo3' => $typo3, 'office' => $office, 'drupal' => $drupal, 'wordpress' => $wordpress);
124 
125  $this->templateVariableContainer->expects($this->at(0))->method('add')->with('myGroupKey', 'commercial');
126  $this->templateVariableContainer->expects($this->at(1))->method('add')->with('products', array('photoshop' => $photoshop, 'office' => $office));
127  $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('myGroupKey');
128  $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('products');
129  $this->templateVariableContainer->expects($this->at(4))->method('add')->with('myGroupKey', 'GPL');
130  $this->templateVariableContainer->expects($this->at(5))->method('add')->with('products', array('typo3' => $typo3, 'drupal' => $drupal, 'wordpress' => $wordpress));
131  $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('myGroupKey');
132  $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('products');
133 
134  $this->viewHelper->render($products, 'products', 'license', 'myGroupKey');
135  }
136 
141  $customer1 = new \stdClass();
142  $customer1->name = 'Anton Abel';
143 
144  $customer2 = new \stdClass();
145  $customer2->name = 'Balthasar Bux';
146 
147  $invoice1 = array('date' => new \DateTime('1980-12-13'), 'customer' => $customer1);
148  $invoice2 = array('date' => new \DateTime('2010-07-01'), 'customer' => $customer1);
149  $invoice3 = array('date' => new \DateTime('2010-07-04'), 'customer' => $customer2);
150 
151  $invoices = array('invoice1' => $invoice1, 'invoice2' => $invoice2, 'invoice3' => $invoice3);
152 
153  $this->templateVariableContainer->expects($this->at(0))->method('add')->with('myGroupKey', $customer1);
154  $this->templateVariableContainer->expects($this->at(1))->method('add')->with('invoices', array('invoice1' => $invoice1, 'invoice2' => $invoice2));
155  $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('myGroupKey');
156  $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('invoices');
157  $this->templateVariableContainer->expects($this->at(4))->method('add')->with('myGroupKey', $customer2);
158  $this->templateVariableContainer->expects($this->at(5))->method('add')->with('invoices', array('invoice3' => $invoice3));
159  $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('myGroupKey');
160  $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('invoices');
161 
162  $this->viewHelper->render($invoices, 'invoices', 'customer', 'myGroupKey');
163  }
164 
168  public function renderGroupsMultidimensionalArrayByPropertyPath() {
169  $customer1 = new \stdClass();
170  $customer1->name = 'Anton Abel';
171 
172  $customer2 = new \stdClass();
173  $customer2->name = 'Balthasar Bux';
174 
175  $invoice1 = new \stdClass();
176  $invoice1->customer = $customer1;
177 
178  $invoice2 = new \stdClass();
179  $invoice2->customer = $customer1;
180 
181  $invoice3 = new \stdClass();
182  $invoice3->customer = $customer2;
183 
184  $invoices = array('invoice1' => $invoice1, 'invoice2' => $invoice2, 'invoice3' => $invoice3);
185  $groupBy = 'customer.name';
187  $accessibleMock = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\GroupedForViewHelper', array('dummy'));
188  $expectedResult = array(
189  'keys' => array(
190  'Anton Abel' => 'Anton Abel',
191  'Balthasar Bux' => 'Balthasar Bux'
192  ),
193  'values' => array(
194  'Anton Abel' => array(
195  'invoice1' => $invoice1,
196  'invoice2' => $invoice2
197  ),
198  'Balthasar Bux' => array(
199  'invoice3' => $invoice3
200  )
201  )
202  );
203  $this->assertSame($expectedResult, $accessibleMock->_callRef('groupElements', $invoices, $groupBy));
204  }
205 
210  $customer1 = new \stdClass();
211  $customer1->name = 'Anton Abel';
212 
213  $customer2 = new \stdClass();
214  $customer2->name = 'Balthasar Bux';
215 
216  $invoice1 = new \stdClass();
217  $invoice1->date = new \DateTime('1980-12-13');
218  $invoice1->customer = $customer1;
219 
220  $invoice2 = new \stdClass();
221  $invoice2->date = new \DateTime('2010-07-01');
222  $invoice2->customer = $customer1;
223 
224  $invoice3 = new \stdClass();
225  $invoice3->date = new \DateTime('2010-07-04');
226  $invoice3->customer = $customer2;
227 
228  $invoices = array('invoice1' => $invoice1, 'invoice2' => $invoice2, 'invoice3' => $invoice3);
229 
230  $this->templateVariableContainer->expects($this->at(0))->method('add')->with('myGroupKey', $customer1);
231  $this->templateVariableContainer->expects($this->at(1))->method('add')->with('invoices', array('invoice1' => $invoice1, 'invoice2' => $invoice2));
232  $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('myGroupKey');
233  $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('invoices');
234  $this->templateVariableContainer->expects($this->at(4))->method('add')->with('myGroupKey', $customer2);
235  $this->templateVariableContainer->expects($this->at(5))->method('add')->with('invoices', array('invoice3' => $invoice3));
236  $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('myGroupKey');
237  $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('invoices');
238 
239  $this->viewHelper->render($invoices, 'invoices', 'customer', 'myGroupKey');
240  }
241 
246 
247  $date1 = new \DateTime('2010-07-01');
248  $date2 = new \DateTime('2010-07-04');
249 
250  $invoice1 = new \stdClass();
251  $invoice1->date = $date1;
252  $invoice1->id = 12340;
253 
254  $invoice2 = new \stdClass();
255  $invoice2->date = $date1;
256  $invoice2->id = 12341;
257 
258  $invoice3 = new \stdClass();
259  $invoice3->date = $date2;
260  $invoice3->id = 12342;
261 
262  $invoices = array('invoice1' => $invoice1, 'invoice2' => $invoice2, 'invoice3' => $invoice3);
263 
264  $this->templateVariableContainer->expects($this->at(0))->method('add')->with('myGroupKey', $date1);
265  $this->templateVariableContainer->expects($this->at(1))->method('add')->with('invoices', array('invoice1' => $invoice1, 'invoice2' => $invoice2));
266  $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('myGroupKey');
267  $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('invoices');
268  $this->templateVariableContainer->expects($this->at(4))->method('add')->with('myGroupKey', $date2);
269  $this->templateVariableContainer->expects($this->at(5))->method('add')->with('invoices', array('invoice3' => $invoice3));
270  $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('myGroupKey');
271  $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('invoices');
272 
273  $this->viewHelper->render($invoices, 'invoices', 'date', 'myGroupKey');
274  }
275 
280  $photoshop = array('name' => 'Adobe Photoshop', 'license' => 'commercial');
281  $typo3 = array('name' => 'TYPO3', 'license' => 'GPL');
282  $office = array('name' => 'Microsoft Office', 'license' => 'commercial');
283 
284  $products = array('photoshop' => $photoshop, 'typo3' => $typo3, 'office' => $office);
285 
286  $this->templateVariableContainer->expects($this->at(0))->method('add')->with('groupKey', NULL);
287  $this->templateVariableContainer->expects($this->at(1))->method('add')->with('innerKey', $products);
288  $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('groupKey');
289  $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('innerKey');
290 
291  $this->viewHelper->render($products, 'innerKey', 'NonExistingKey');
292  }
293 
299  $values = array('some', 'simple', 'array');
300 
301  $this->viewHelper->render($values, 'innerVariable', 'someKey');
302  }
303 }
injectDependenciesIntoViewHelper(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper $viewHelper)
getAccessibleMock( $originalClassName, array $methods=array(), array $arguments=array(), $mockClassName='', $callOriginalConstructor=TRUE, $callOriginalClone=TRUE, $callAutoload=TRUE)