TYPO3 API  SVNRelease
ViewHelperNodeComparatorTest.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  * Testcase for ViewHelperNode's evaluateBooleanExpression()
00025  *
00026  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
00027  */
00028 class Tx_Fluid_Tests_Unit_Core_Parser_SyntaxTree_ViewHelperNodeComparatorTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
00029 
00030     /**
00031      * @var Tx_Fluid_Core_Parser_SyntaxTree_ViewHelperNode
00032      */
00033     protected $viewHelperNode;
00034 
00035     /**
00036      * @var Tx_Fluid_Core_Rendering_RenderingContextInterface
00037      */
00038     protected $renderingContext;
00039 
00040     /**
00041      * Setup fixture
00042      * @author Karsten Dambekalns <karsten@typo3.org>
00043      */
00044     public function setUp() {
00045         $this->viewHelperNode = $this->getAccessibleMock('Tx_Fluid_Core_Parser_SyntaxTree_ViewHelperNode', array('dummy'), array(), '', FALSE);
00046         $this->renderingContext = $this->getMock('Tx_Fluid_Core_Rendering_RenderingContextInterface');
00047     }
00048 
00049     /**
00050      * @test
00051      * @expectedException Tx_Fluid_Core_Parser_Exception
00052      * @author Karsten Dambekalns <karsten@typo3.org>
00053      */
00054     public function havingMoreThanThreeElementsInTheSyntaxTreeThrowsException() {
00055         $rootNode = $this->getMock('Tx_Fluid_Core_Parser_SyntaxTree_RootNode');
00056         $rootNode->expects($this->once())->method('getChildNodes')->will($this->returnValue(array(1,2,3,4)));
00057 
00058         $this->viewHelperNode->_call('evaluateBooleanExpression', $rootNode, $this->renderingContext);
00059     }
00060 
00061     /**
00062      * @test
00063      * @author Karsten Dambekalns <karsten@typo3.org>
00064      */
00065     public function comparingEqualNumbersReturnsTrue() {
00066         $rootNode = new Tx_Fluid_Core_Parser_SyntaxTree_RootNode();
00067         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('5'));
00068         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('=='));
00069         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('5'));
00070 
00071         $this->assertTrue($this->viewHelperNode->_call('evaluateBooleanExpression', $rootNode, $this->renderingContext));
00072     }
00073 
00074     /**
00075      * @test
00076      * @author Karsten Dambekalns <karsten@typo3.org>
00077      */
00078     public function comparingUnequalNumbersReturnsFalse() {
00079         $rootNode = new Tx_Fluid_Core_Parser_SyntaxTree_RootNode();
00080         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('5'));
00081         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('=='));
00082         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('3'));
00083 
00084         $this->assertFalse($this->viewHelperNode->_call('evaluateBooleanExpression', $rootNode, $this->renderingContext));
00085     }
00086 
00087     /**
00088      * @test
00089      * @author Bastian Waidelich <bastian@typo3.org>
00090      */
00091     public function notEqualReturnsFalseIfNumbersAreEqual() {
00092         $rootNode = new Tx_Fluid_Core_Parser_SyntaxTree_RootNode();
00093         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('5'));
00094         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('!='));
00095         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('5'));
00096 
00097         $this->assertFalse($this->viewHelperNode->_call('evaluateBooleanExpression', $rootNode, $this->renderingContext));
00098     }
00099 
00100     /**
00101      * @test
00102      * @author Bastian Waidelich <bastian@typo3.org>
00103      */
00104     public function notEqualReturnsTrueIfNumbersAreNotEqual() {
00105         $rootNode = new Tx_Fluid_Core_Parser_SyntaxTree_RootNode();
00106         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('5'));
00107         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('!='));
00108         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('3'));
00109 
00110         $this->assertTrue($this->viewHelperNode->_call('evaluateBooleanExpression', $rootNode, $this->renderingContext));
00111     }
00112 
00113     /**
00114      * @test
00115      * @author Sebastian Kurfürst <sebastian@typo3.org>
00116      */
00117     public function oddNumberModulo2ReturnsTrue() {
00118         $rootNode = new Tx_Fluid_Core_Parser_SyntaxTree_RootNode();
00119         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('43'));
00120         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('%'));
00121         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('2'));
00122 
00123         $this->assertTrue($this->viewHelperNode->_call('evaluateBooleanExpression', $rootNode, $this->renderingContext));
00124     }
00125 
00126     /**
00127      * @test
00128      * @author Sebastian Kurfürst <sebastian@typo3.org>
00129      */
00130     public function evenNumberModulo2ReturnsFalse() {
00131         $rootNode = new Tx_Fluid_Core_Parser_SyntaxTree_RootNode();
00132         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('42'));
00133         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('%'));
00134         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('2'));
00135 
00136         $this->assertFalse($this->viewHelperNode->_call('evaluateBooleanExpression', $rootNode, $this->renderingContext));
00137     }
00138 
00139     /**
00140      * @test
00141      * @author Sebastian Kurfürst <sebastian@typo3.org>
00142      */
00143     public function greaterThanReturnsTrueIfNumberIsReallyGreater() {
00144         $rootNode = new Tx_Fluid_Core_Parser_SyntaxTree_RootNode();
00145         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('10'));
00146         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('>'));
00147         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('9'));
00148 
00149         $this->assertTrue($this->viewHelperNode->_call('evaluateBooleanExpression', $rootNode, $this->renderingContext));
00150     }
00151 
00152     /**
00153      * @test
00154      * @author Sebastian Kurfürst <sebastian@typo3.org>
00155      */
00156     public function greaterThanReturnsFalseIfNumberIsEqual() {
00157         $rootNode = new Tx_Fluid_Core_Parser_SyntaxTree_RootNode();
00158         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('10'));
00159         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('>'));
00160         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('10'));
00161 
00162         $this->assertFalse($this->viewHelperNode->_call('evaluateBooleanExpression', $rootNode, $this->renderingContext));
00163     }
00164 
00165     /**
00166      * @test
00167      * @author Sebastian Kurfürst <sebastian@typo3.org>
00168      */
00169     public function greaterOrEqualsReturnsTrueIfNumberIsReallyGreater() {
00170         $rootNode = new Tx_Fluid_Core_Parser_SyntaxTree_RootNode();
00171         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('10'));
00172         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('>='));
00173         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('9'));
00174 
00175         $this->assertTrue($this->viewHelperNode->_call('evaluateBooleanExpression', $rootNode, $this->renderingContext));
00176     }
00177 
00178     /**
00179      * @test
00180      * @author Sebastian Kurfürst <sebastian@typo3.org>
00181      */
00182     public function greaterOrEqualsReturnsTrueIfNumberIsEqual() {
00183         $rootNode = new Tx_Fluid_Core_Parser_SyntaxTree_RootNode();
00184         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('10'));
00185         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('>='));
00186         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('10'));
00187 
00188         $this->assertTrue($this->viewHelperNode->_call('evaluateBooleanExpression', $rootNode, $this->renderingContext));
00189     }
00190 
00191     /**
00192      * @test
00193      * @author Sebastian Kurfürst <sebastian@typo3.org>
00194      */
00195     public function greaterOrEqualsReturnFalseIfNumberIsSmaller() {
00196         $rootNode = new Tx_Fluid_Core_Parser_SyntaxTree_RootNode();
00197         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('10'));
00198         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('>='));
00199         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('11'));
00200 
00201         $this->assertFalse($this->viewHelperNode->_call('evaluateBooleanExpression', $rootNode, $this->renderingContext));
00202     }
00203 
00204     /**
00205      * @test
00206      * @author Sebastian Kurfürst <sebastian@typo3.org>
00207      */
00208     public function lessThanReturnsTrueIfNumberIsReallyless() {
00209         $rootNode = new Tx_Fluid_Core_Parser_SyntaxTree_RootNode();
00210         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('9'));
00211         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('<'));
00212         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('10'));
00213 
00214         $this->assertTrue($this->viewHelperNode->_call('evaluateBooleanExpression', $rootNode, $this->renderingContext));
00215     }
00216 
00217     /**
00218      * @test
00219      * @author Sebastian Kurfürst <sebastian@typo3.org>
00220      */
00221     public function lessThanReturnsFalseIfNumberIsEqual() {
00222         $rootNode = new Tx_Fluid_Core_Parser_SyntaxTree_RootNode();
00223         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('10'));
00224         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('<'));
00225         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('10'));
00226 
00227         $this->assertFalse($this->viewHelperNode->_call('evaluateBooleanExpression', $rootNode, $this->renderingContext));
00228     }
00229 
00230     /**
00231      * @test
00232      * @author Sebastian Kurfürst <sebastian@typo3.org>
00233      */
00234     public function lessOrEqualsReturnsTrueIfNumberIsReallyLess() {
00235         $rootNode = new Tx_Fluid_Core_Parser_SyntaxTree_RootNode();
00236         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('9'));
00237         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('<='));
00238         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('10'));
00239 
00240         $this->assertTrue($this->viewHelperNode->_call('evaluateBooleanExpression', $rootNode, $this->renderingContext));
00241     }
00242 
00243     /**
00244      * @test
00245      * @author Sebastian Kurfürst <sebastian@typo3.org>
00246      */
00247     public function lessOrEqualsReturnsTrueIfNumberIsEqual() {
00248         $rootNode = new Tx_Fluid_Core_Parser_SyntaxTree_RootNode();
00249         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('10'));
00250         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('<='));
00251         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('10'));
00252 
00253         $this->assertTrue($this->viewHelperNode->_call('evaluateBooleanExpression', $rootNode, $this->renderingContext));
00254     }
00255 
00256     /**
00257      * @test
00258      * @author Sebastian Kurfürst <sebastian@typo3.org>
00259      */
00260     public function lessOrEqualsReturnFalseIfNumberIsBigger() {
00261         $rootNode = new Tx_Fluid_Core_Parser_SyntaxTree_RootNode();
00262         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('11'));
00263         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('<='));
00264         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('10'));
00265 
00266         $this->assertFalse($this->viewHelperNode->_call('evaluateBooleanExpression', $rootNode, $this->renderingContext));
00267     }
00268 
00269     /**
00270      * @test
00271      * @author Sebastian Kurfürst <sebastian@typo3.org>
00272      */
00273     public function lessOrEqualsReturnFalseIfComparingWithANegativeNumber() {
00274         $rootNode = new Tx_Fluid_Core_Parser_SyntaxTree_RootNode();
00275         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('11 <= -2.1'));
00276 
00277         $this->assertFalse($this->viewHelperNode->_call('evaluateBooleanExpression', $rootNode, $this->renderingContext));
00278     }
00279 
00280     /**
00281      * @test
00282      * @author Sebastian Kurfürst <sebastian@typo3.org>
00283      */
00284     public function objectsAreComparedStrictly() {
00285         $object1 = new stdClass();
00286         $object2 = new stdClass();
00287 
00288         $rootNode = new Tx_Fluid_Core_Parser_SyntaxTree_RootNode();
00289 
00290         $object1Node = $this->getMock('Tx_Fluid_Core_Parser_SyntaxTree_ObjectAccessorNode', array('evaluate'));
00291         $object1Node->expects($this->any())->method('evaluate')->will($this->returnValue($object1));
00292 
00293         $object2Node = $this->getMock('Tx_Fluid_Core_Parser_SyntaxTree_ObjectAccessorNode', array('evaluate'));
00294         $object2Node->expects($this->any())->method('evaluate')->will($this->returnValue($object2));
00295 
00296         $rootNode->addChildNode($object1Node);
00297         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('=='));
00298         $rootNode->addChildNode($object2Node);
00299 
00300         $this->assertFalse($this->viewHelperNode->_call('evaluateBooleanExpression', $rootNode, $this->renderingContext));
00301     }
00302 
00303     /**
00304      * @test
00305      * @author Sebastian Kurfürst <sebastian@typo3.org>
00306      */
00307     public function objectsAreComparedStrictlyInUnequalComparison() {
00308         $object1 = new stdClass();
00309         $object2 = new stdClass();
00310 
00311         $rootNode = new Tx_Fluid_Core_Parser_SyntaxTree_RootNode();
00312 
00313         $object1Node = $this->getMock('Tx_Fluid_Core_Parser_SyntaxTree_ObjectAccessorNode', array('evaluate'));
00314         $object1Node->expects($this->any())->method('evaluate')->will($this->returnValue($object1));
00315 
00316         $object2Node = $this->getMock('Tx_Fluid_Core_Parser_SyntaxTree_ObjectAccessorNode', array('evaluate'));
00317         $object2Node->expects($this->any())->method('evaluate')->will($this->returnValue($object2));
00318 
00319         $rootNode->addChildNode($object1Node);
00320         $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('!='));
00321         $rootNode->addChildNode($object2Node);
00322 
00323         $this->assertTrue($this->viewHelperNode->_call('evaluateBooleanExpression', $rootNode, $this->renderingContext));
00324     }
00325 }
00326 
00327 ?>