TYPO3 API  SVNRelease
GroupedForViewHelperTest.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*                                                                        *
00004  * This script belongs to the FLOW3 package "Fluid".                      *
00005  *                                                                        *
00006  * It is free software; you can redistribute it and/or modify it under    *
00007  * the terms of the GNU Lesser General Public License as published by the *
00008  * Free Software Foundation, either version 3 of the License, or (at your *
00009  * option) any later version.                                             *
00010  *                                                                        *
00011  * This script is distributed in the hope that it will be useful, but     *
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
00013  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
00014  * General Public License for more details.                               *
00015  *                                                                        *
00016  * You should have received a copy of the GNU Lesser General Public       *
00017  * License along with the script.                                         *
00018  * If not, see http://www.gnu.org/licenses/lgpl.html                      *
00019  *                                                                        *
00020  * The TYPO3 project - inspiring people to share!                         *
00021  *                                                                        */
00022 
00023 
00024 require_once(dirname(__FILE__) . '/ViewHelperBaseTestcase.php');
00025 
00026 /**
00027  * Testcase for CycleViewHelper
00028  *
00029  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
00030  */
00031 class Tx_Fluid_Tests_Unit_ViewHelpers_GroupedForViewHelperTest extends Tx_Fluid_ViewHelpers_ViewHelperBaseTestcase {
00032 
00033     /**
00034      * var Tx_Fluid_ViewHelpers_GroupedForViewHelper
00035      */
00036     protected $viewHelper;
00037 
00038     public function setUp() {
00039         parent::setUp();
00040         $this->viewHelper = $this->getMock('Tx_Fluid_ViewHelpers_GroupedForViewHelper', array('renderChildren'));
00041         $this->injectDependenciesIntoViewHelper($this->viewHelper);
00042         $this->viewHelper->initializeArguments();
00043     }
00044 
00045     /**
00046      * @test
00047      * @author Bastian Waidelich <bastian@typo3.org>
00048      */
00049     public function renderReturnsEmptyStringIfObjectIsNull() {
00050         $this->assertEquals('', $this->viewHelper->render(NULL, 'foo', 'bar'));
00051     }
00052 
00053     /**
00054      * @test
00055      * @author Bastian Waidelich <bastian@typo3.org>
00056      */
00057     public function renderReturnsEmptyStringIfObjectIsEmptyArray() {
00058         $this->assertEquals('', $this->viewHelper->render(array(), 'foo', 'bar'));
00059     }
00060 
00061     /**
00062      * @test
00063      * @expectedException Tx_Fluid_Core_ViewHelper_Exception
00064      * @author Bastian Waidelich <bastian@typo3.org>
00065      */
00066     public function renderThrowsExceptionWhenPassingObjectsToEachThatAreNotTraversable() {
00067         $object = new stdClass();
00068 
00069         $this->viewHelper->render($object, 'innerVariable', 'someKey');
00070     }
00071 
00072     /**
00073      * @test
00074      * @author Bastian Waidelich <bastian@typo3.org>
00075      */
00076     public function renderGroupsMultidimensionalArrayAndPreservesKeys() {
00077         $photoshop = array('name' => 'Adobe Photoshop', 'license' => 'commercial');
00078         $typo3 = array('name' => 'TYPO3', 'license' => 'GPL');
00079         $office = array('name' => 'Microsoft Office', 'license' => 'commercial');
00080         $drupal = array('name' => 'Drupal', 'license' => 'GPL');
00081         $wordpress = array('name' => 'Wordpress', 'license' => 'GPL');
00082 
00083         $products = array('photoshop' => $photoshop, 'typo3' => $typo3, 'office' => $office, 'drupal' => $drupal, 'wordpress' => $wordpress);
00084 
00085         $this->templateVariableContainer->expects($this->at(0))->method('add')->with('myGroupKey', 'commercial');
00086         $this->templateVariableContainer->expects($this->at(1))->method('add')->with('products', array('photoshop' => $photoshop, 'office' => $office));
00087         $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('myGroupKey');
00088         $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('products');
00089         $this->templateVariableContainer->expects($this->at(4))->method('add')->with('myGroupKey', 'GPL');
00090         $this->templateVariableContainer->expects($this->at(5))->method('add')->with('products', array('typo3' => $typo3, 'drupal' => $drupal, 'wordpress' => $wordpress));
00091         $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('myGroupKey');
00092         $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('products');
00093 
00094         $this->viewHelper->render($products, 'products', 'license', 'myGroupKey');
00095     }
00096 
00097     /**
00098      * @test
00099      * @author Bastian Waidelich <bastian@typo3.org>
00100      */
00101     public function renderGroupsMultidimensionalArrayObjectAndPreservesKeys() {
00102         $photoshop = new ArrayObject(array('name' => 'Adobe Photoshop', 'license' => 'commercial'));
00103         $typo3 = new ArrayObject(array('name' => 'TYPO3', 'license' => 'GPL'));
00104         $office = new ArrayObject(array('name' => 'Microsoft Office', 'license' => 'commercial'));
00105         $drupal = new ArrayObject(array('name' => 'Drupal', 'license' => 'GPL'));
00106         $wordpress = new ArrayObject(array('name' => 'Wordpress', 'license' => 'GPL'));
00107 
00108         $products = new ArrayObject(array('photoshop' => $photoshop, 'typo3' => $typo3, 'office' => $office, 'drupal' => $drupal, 'wordpress' => $wordpress));
00109 
00110         $this->templateVariableContainer->expects($this->at(0))->method('add')->with('myGroupKey', 'commercial');
00111         $this->templateVariableContainer->expects($this->at(1))->method('add')->with('products', array('photoshop' => $photoshop, 'office' => $office));
00112         $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('myGroupKey');
00113         $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('products');
00114         $this->templateVariableContainer->expects($this->at(4))->method('add')->with('myGroupKey', 'GPL');
00115         $this->templateVariableContainer->expects($this->at(5))->method('add')->with('products', array('typo3' => $typo3, 'drupal' => $drupal, 'wordpress' => $wordpress));
00116         $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('myGroupKey');
00117         $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('products');
00118 
00119         $this->viewHelper->render($products, 'products', 'license', 'myGroupKey');
00120     }
00121 
00122     /**
00123      * @test
00124      * @author Bastian Waidelich <bastian@typo3.org>
00125      */
00126     public function renderGroupsArrayOfObjectsAndPreservesKeys() {
00127         $photoshop = new stdClass();
00128         $photoshop->name = 'Adobe Photoshop';
00129         $photoshop->license = 'commercial';
00130         $typo3 = new stdClass();
00131         $typo3->name = 'TYPO3';
00132         $typo3->license = 'GPL';
00133         $office = new stdClass();
00134         $office->name = 'Microsoft Office';
00135         $office->license = 'commercial';
00136         $drupal = new stdClass();
00137         $drupal->name = 'Drupal';
00138         $drupal->license = 'GPL';
00139         $wordpress = new stdClass();
00140         $wordpress->name = 'Wordpress';
00141         $wordpress->license = 'GPL';
00142 
00143         $products = array('photoshop' => $photoshop, 'typo3' => $typo3, 'office' => $office, 'drupal' => $drupal, 'wordpress' => $wordpress);
00144 
00145         $this->templateVariableContainer->expects($this->at(0))->method('add')->with('myGroupKey', 'commercial');
00146         $this->templateVariableContainer->expects($this->at(1))->method('add')->with('products', array('photoshop' => $photoshop, 'office' => $office));
00147         $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('myGroupKey');
00148         $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('products');
00149         $this->templateVariableContainer->expects($this->at(4))->method('add')->with('myGroupKey', 'GPL');
00150         $this->templateVariableContainer->expects($this->at(5))->method('add')->with('products', array('typo3' => $typo3, 'drupal' => $drupal, 'wordpress' => $wordpress));
00151         $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('myGroupKey');
00152         $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('products');
00153 
00154         $this->viewHelper->render($products, 'products', 'license', 'myGroupKey');
00155     }
00156 
00157     /**
00158      * @test
00159      * @author Bastian Waidelich <bastian@typo3.org>
00160      */
00161     public function renderGroupsMultidimensionalArrayByObjectKey() {
00162         $customer1 = new stdClass();
00163         $customer1->name = 'Anton Abel';
00164 
00165         $customer2 = new stdClass();
00166         $customer2->name = 'Balthasar Bux';
00167 
00168         $invoice1 = array('date' => new DateTime('1980-12-13'), 'customer' => $customer1);
00169         $invoice2 = array('date' => new DateTime('2010-07-01'), 'customer' => $customer1);
00170         $invoice3 = array('date' => new DateTime('2010-07-04'), 'customer' => $customer2);
00171 
00172         $invoices = array('invoice1' => $invoice1, 'invoice2' => $invoice2, 'invoice3' => $invoice3);
00173 
00174         $this->templateVariableContainer->expects($this->at(0))->method('add')->with('myGroupKey', $customer1);
00175         $this->templateVariableContainer->expects($this->at(1))->method('add')->with('invoices', array('invoice1' => $invoice1, 'invoice2' => $invoice2));
00176         $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('myGroupKey');
00177         $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('invoices');
00178         $this->templateVariableContainer->expects($this->at(4))->method('add')->with('myGroupKey', $customer2);
00179         $this->templateVariableContainer->expects($this->at(5))->method('add')->with('invoices', array('invoice3' => $invoice3));
00180         $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('myGroupKey');
00181         $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('invoices');
00182 
00183         $this->viewHelper->render($invoices, 'invoices', 'customer', 'myGroupKey');
00184     }
00185 
00186     /**
00187      * @test
00188      * @author Bastian Waidelich <bastian@typo3.org>
00189      */
00190     public function renderGroupsMultidimensionalObjectByObjectKey() {
00191         $customer1 = new stdClass();
00192         $customer1->name = 'Anton Abel';
00193 
00194         $customer2 = new stdClass();
00195         $customer2->name = 'Balthasar Bux';
00196 
00197         $invoice1 = new stdClass();
00198         $invoice1->date = new DateTime('1980-12-13');
00199         $invoice1->customer = $customer1;
00200 
00201         $invoice2 = new stdClass();
00202         $invoice2->date = new DateTime('2010-07-01');
00203         $invoice2->customer = $customer1;
00204 
00205         $invoice3 = new stdClass();
00206         $invoice3->date = new DateTime('2010-07-04');
00207         $invoice3->customer = $customer2;
00208 
00209         $invoices = array('invoice1' => $invoice1, 'invoice2' => $invoice2, 'invoice3' => $invoice3);
00210 
00211         $this->templateVariableContainer->expects($this->at(0))->method('add')->with('myGroupKey', $customer1);
00212         $this->templateVariableContainer->expects($this->at(1))->method('add')->with('invoices', array('invoice1' => $invoice1, 'invoice2' => $invoice2));
00213         $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('myGroupKey');
00214         $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('invoices');
00215         $this->templateVariableContainer->expects($this->at(4))->method('add')->with('myGroupKey', $customer2);
00216         $this->templateVariableContainer->expects($this->at(5))->method('add')->with('invoices', array('invoice3' => $invoice3));
00217         $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('myGroupKey');
00218         $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('invoices');
00219 
00220         $this->viewHelper->render($invoices, 'invoices', 'customer', 'myGroupKey');
00221     }
00222 
00223     /**
00224      * @test
00225      * @author Bastian Waidelich <bastian@typo3.org>
00226      */
00227     public function groupingByAKeyThatDoesNotExistCreatesASingleGroup() {
00228         $photoshop = array('name' => 'Adobe Photoshop', 'license' => 'commercial');
00229         $typo3 = array('name' => 'TYPO3', 'license' => 'GPL');
00230         $office = array('name' => 'Microsoft Office', 'license' => 'commercial');
00231 
00232         $products = array('photoshop' => $photoshop, 'typo3' => $typo3, 'office' => $office);
00233 
00234         $this->templateVariableContainer->expects($this->at(0))->method('add')->with('groupKey', NULL);
00235         $this->templateVariableContainer->expects($this->at(1))->method('add')->with('innerKey', $products);
00236         $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('groupKey');
00237         $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('innerKey');
00238 
00239         $this->viewHelper->render($products, 'innerKey', 'NonExistingKey');
00240     }
00241 
00242     /**
00243      * @test
00244      * @expectedException Tx_Fluid_Core_ViewHelper_Exception
00245      * @author Bastian Waidelich <bastian@typo3.org>
00246      */
00247     public function renderThrowsExceptionWhenPassingOneDimensionalArraysToEach() {
00248         $values = array('some', 'simple', 'array');
00249 
00250         $this->viewHelper->render($values, 'innerVariable', 'someKey');
00251     }
00252 }
00253 
00254 ?>