[TASK] Add test for GroupedForViewHelper to support DateTime Objects
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / GroupedForViewHelperTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers;
3
4 /* *
5 * This script is backported from the FLOW3 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 require_once dirname(__FILE__) . '/ViewHelperBaseTestcase.php';
14
15 /**
16 * Testcase for GroupedForViewHelper.
17 */
18 class GroupedForViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase {
19
20 /**
21 * @var \TYPO3\CMS\Fluid\ViewHelpers\GroupedForViewHelper
22 */
23 protected $viewHelper;
24
25 public function setUp() {
26 parent::setUp();
27 $this->viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\GroupedForViewHelper', array('renderChildren'));
28 $this->injectDependenciesIntoViewHelper($this->viewHelper);
29 $this->viewHelper->initializeArguments();
30 }
31
32 /**
33 * @test
34 */
35 public function renderReturnsEmptyStringIfObjectIsNull() {
36 $this->assertEquals('', $this->viewHelper->render(NULL, 'foo', 'bar'));
37 }
38
39 /**
40 * @test
41 */
42 public function renderReturnsEmptyStringIfObjectIsEmptyArray() {
43 $this->assertEquals('', $this->viewHelper->render(array(), 'foo', 'bar'));
44 }
45
46 /**
47 * @test
48 * @expectedException \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
49 */
50 public function renderThrowsExceptionWhenPassingObjectsToEachThatAreNotTraversable() {
51 $object = new \stdClass();
52 $this->viewHelper->render($object, 'innerVariable', 'someKey');
53 }
54
55 /**
56 * @test
57 */
58 public function renderGroupsMultidimensionalArrayAndPreservesKeys() {
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 $products = array('photoshop' => $photoshop, 'typo3' => $typo3, 'office' => $office, 'drupal' => $drupal, 'wordpress' => $wordpress);
65 $this->templateVariableContainer->expects($this->at(0))->method('add')->with('myGroupKey', 'commercial');
66 $this->templateVariableContainer->expects($this->at(1))->method('add')->with('products', array('photoshop' => $photoshop, 'office' => $office));
67 $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('myGroupKey');
68 $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('products');
69 $this->templateVariableContainer->expects($this->at(4))->method('add')->with('myGroupKey', 'GPL');
70 $this->templateVariableContainer->expects($this->at(5))->method('add')->with('products', array('typo3' => $typo3, 'drupal' => $drupal, 'wordpress' => $wordpress));
71 $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('myGroupKey');
72 $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('products');
73 $this->viewHelper->render($products, 'products', 'license', 'myGroupKey');
74 }
75
76 /**
77 * @test
78 */
79 public function renderGroupsMultidimensionalArrayObjectAndPreservesKeys() {
80 $photoshop = new \ArrayObject(array('name' => 'Adobe Photoshop', 'license' => 'commercial'));
81 $typo3 = new \ArrayObject(array('name' => 'TYPO3', 'license' => 'GPL'));
82 $office = new \ArrayObject(array('name' => 'Microsoft Office', 'license' => 'commercial'));
83 $drupal = new \ArrayObject(array('name' => 'Drupal', 'license' => 'GPL'));
84 $wordpress = new \ArrayObject(array('name' => 'Wordpress', 'license' => 'GPL'));
85 $products = new \ArrayObject(array('photoshop' => $photoshop, 'typo3' => $typo3, 'office' => $office, 'drupal' => $drupal, 'wordpress' => $wordpress));
86 $this->templateVariableContainer->expects($this->at(0))->method('add')->with('myGroupKey', 'commercial');
87 $this->templateVariableContainer->expects($this->at(1))->method('add')->with('products', array('photoshop' => $photoshop, 'office' => $office));
88 $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('myGroupKey');
89 $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('products');
90 $this->templateVariableContainer->expects($this->at(4))->method('add')->with('myGroupKey', 'GPL');
91 $this->templateVariableContainer->expects($this->at(5))->method('add')->with('products', array('typo3' => $typo3, 'drupal' => $drupal, 'wordpress' => $wordpress));
92 $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('myGroupKey');
93 $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('products');
94 $this->viewHelper->render($products, 'products', 'license', 'myGroupKey');
95 }
96
97 /**
98 * @test
99 */
100 public function renderGroupsArrayOfObjectsAndPreservesKeys() {
101 $photoshop = new \stdClass();
102 $photoshop->name = 'Adobe Photoshop';
103 $photoshop->license = 'commercial';
104 $typo3 = new \stdClass();
105 $typo3->name = 'TYPO3';
106 $typo3->license = 'GPL';
107 $office = new \stdClass();
108 $office->name = 'Microsoft Office';
109 $office->license = 'commercial';
110 $drupal = new \stdClass();
111 $drupal->name = 'Drupal';
112 $drupal->license = 'GPL';
113 $wordpress = new \stdClass();
114 $wordpress->name = 'Wordpress';
115 $wordpress->license = 'GPL';
116 $products = array('photoshop' => $photoshop, 'typo3' => $typo3, 'office' => $office, 'drupal' => $drupal, 'wordpress' => $wordpress);
117 $this->templateVariableContainer->expects($this->at(0))->method('add')->with('myGroupKey', 'commercial');
118 $this->templateVariableContainer->expects($this->at(1))->method('add')->with('products', array('photoshop' => $photoshop, 'office' => $office));
119 $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('myGroupKey');
120 $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('products');
121 $this->templateVariableContainer->expects($this->at(4))->method('add')->with('myGroupKey', 'GPL');
122 $this->templateVariableContainer->expects($this->at(5))->method('add')->with('products', array('typo3' => $typo3, 'drupal' => $drupal, 'wordpress' => $wordpress));
123 $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('myGroupKey');
124 $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('products');
125 $this->viewHelper->render($products, 'products', 'license', 'myGroupKey');
126 }
127
128 /**
129 * @test
130 */
131 public function renderGroupsMultidimensionalArrayByObjectKey() {
132 $customer1 = new \stdClass();
133 $customer1->name = 'Anton Abel';
134 $customer2 = new \stdClass();
135 $customer2->name = 'Balthasar Bux';
136 $invoice1 = array('date' => new \DateTime('1980-12-13'), 'customer' => $customer1);
137 $invoice2 = array('date' => new \DateTime('2010-07-01'), 'customer' => $customer1);
138 $invoice3 = array('date' => new \DateTime('2010-07-04'), 'customer' => $customer2);
139 $invoices = array('invoice1' => $invoice1, 'invoice2' => $invoice2, 'invoice3' => $invoice3);
140 $this->templateVariableContainer->expects($this->at(0))->method('add')->with('myGroupKey', $customer1);
141 $this->templateVariableContainer->expects($this->at(1))->method('add')->with('invoices', array('invoice1' => $invoice1, 'invoice2' => $invoice2));
142 $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('myGroupKey');
143 $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('invoices');
144 $this->templateVariableContainer->expects($this->at(4))->method('add')->with('myGroupKey', $customer2);
145 $this->templateVariableContainer->expects($this->at(5))->method('add')->with('invoices', array('invoice3' => $invoice3));
146 $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('myGroupKey');
147 $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('invoices');
148 $this->viewHelper->render($invoices, 'invoices', 'customer', 'myGroupKey');
149 }
150
151 /**
152 * @test
153 */
154 public function renderGroupsMultidimensionalArrayByPropertyPath() {
155 $customer1 = new \stdClass();
156 $customer1->name = 'Anton Abel';
157 $customer2 = new \stdClass();
158 $customer2->name = 'Balthasar Bux';
159 $invoice1 = new \stdClass();
160 $invoice1->customer = $customer1;
161 $invoice2 = new \stdClass();
162 $invoice2->customer = $customer1;
163 $invoice3 = new \stdClass();
164 $invoice3->customer = $customer2;
165 $invoices = array('invoice1' => $invoice1, 'invoice2' => $invoice2, 'invoice3' => $invoice3);
166 $groupBy = 'customer.name';
167 /** @var \TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\TYPO3\CMS\Fluid\ViewHelpers\GroupedForViewHelper $accessibleMock */
168 $accessibleMock = $this->getAccessibleMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\GroupedForViewHelper', array('dummy'));
169 $expectedResult = array(
170 'keys' => array(
171 'Anton Abel' => 'Anton Abel',
172 'Balthasar Bux' => 'Balthasar Bux'
173 ),
174 'values' => array(
175 'Anton Abel' => array(
176 'invoice1' => $invoice1,
177 'invoice2' => $invoice2
178 ),
179 'Balthasar Bux' => array(
180 'invoice3' => $invoice3
181 )
182 )
183 );
184 $this->assertSame($expectedResult, $accessibleMock->_callRef('groupElements', $invoices, $groupBy));
185 }
186
187 /**
188 * @test
189 */
190 public function renderGroupsMultidimensionalObjectByObjectKey() {
191 $customer1 = new \stdClass();
192 $customer1->name = 'Anton Abel';
193 $customer2 = new \stdClass();
194 $customer2->name = 'Balthasar Bux';
195 $invoice1 = new \stdClass();
196 $invoice1->date = new \DateTime('1980-12-13');
197 $invoice1->customer = $customer1;
198 $invoice2 = new \stdClass();
199 $invoice2->date = new \DateTime('2010-07-01');
200 $invoice2->customer = $customer1;
201 $invoice3 = new \stdClass();
202 $invoice3->date = new \DateTime('2010-07-04');
203 $invoice3->customer = $customer2;
204 $invoices = array('invoice1' => $invoice1, 'invoice2' => $invoice2, 'invoice3' => $invoice3);
205 $this->templateVariableContainer->expects($this->at(0))->method('add')->with('myGroupKey', $customer1);
206 $this->templateVariableContainer->expects($this->at(1))->method('add')->with('invoices', array('invoice1' => $invoice1, 'invoice2' => $invoice2));
207 $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('myGroupKey');
208 $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('invoices');
209 $this->templateVariableContainer->expects($this->at(4))->method('add')->with('myGroupKey', $customer2);
210 $this->templateVariableContainer->expects($this->at(5))->method('add')->with('invoices', array('invoice3' => $invoice3));
211 $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('myGroupKey');
212 $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('invoices');
213 $this->viewHelper->render($invoices, 'invoices', 'customer', 'myGroupKey');
214 }
215
216 /**
217 * @test
218 */
219 public function renderGroupsMultidimensionalObjectByDateTimeObject() {
220
221 $date1 = new \DateTime('2010-07-01');
222 $date2 = new \DateTime('2010-07-04');
223
224 $invoice1 = new \stdClass();
225 $invoice1->date = $date1;
226 $invoice1->id = 12340;
227
228 $invoice2 = new \stdClass();
229 $invoice2->date = $date1;
230 $invoice2->id = 12341;
231
232 $invoice3 = new \stdClass();
233 $invoice3->date = $date2;
234 $invoice3->id = 12342;
235
236 $invoices = array('invoice1' => $invoice1, 'invoice2' => $invoice2, 'invoice3' => $invoice3);
237
238 $this->templateVariableContainer->expects($this->at(0))->method('add')->with('myGroupKey', $date1);
239 $this->templateVariableContainer->expects($this->at(1))->method('add')->with('invoices', array('invoice1' => $invoice1, 'invoice2' => $invoice2));
240 $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('myGroupKey');
241 $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('invoices');
242 $this->templateVariableContainer->expects($this->at(4))->method('add')->with('myGroupKey', $date2);
243 $this->templateVariableContainer->expects($this->at(5))->method('add')->with('invoices', array('invoice3' => $invoice3));
244 $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('myGroupKey');
245 $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('invoices');
246
247 $this->viewHelper->render($invoices, 'invoices', 'date', 'myGroupKey');
248 }
249
250 /**
251 * @test
252 */
253 public function groupingByAKeyThatDoesNotExistCreatesASingleGroup() {
254 $photoshop = array('name' => 'Adobe Photoshop', 'license' => 'commercial');
255 $typo3 = array('name' => 'TYPO3', 'license' => 'GPL');
256 $office = array('name' => 'Microsoft Office', 'license' => 'commercial');
257 $products = array('photoshop' => $photoshop, 'typo3' => $typo3, 'office' => $office);
258 $this->templateVariableContainer->expects($this->at(0))->method('add')->with('groupKey', NULL);
259 $this->templateVariableContainer->expects($this->at(1))->method('add')->with('innerKey', $products);
260 $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('groupKey');
261 $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('innerKey');
262 $this->viewHelper->render($products, 'innerKey', 'NonExistingKey');
263 }
264
265 /**
266 * @test
267 * @expectedException \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
268 */
269 public function renderThrowsExceptionWhenPassingOneDimensionalArraysToEach() {
270 $values = array('some', 'simple', 'array');
271 $this->viewHelper->render($values, 'innerVariable', 'someKey');
272 }
273 }
274
275 ?>