[+BUGFIX] Fixed comparison of Objects
authorSebastian Kurfürst <sebastian@typo3.org>
Tue, 25 Jan 2011 14:44:38 +0000 (14:44 +0000)
committerSebastian Kurfürst <sebastian@typo3.org>
Tue, 25 Jan 2011 14:44:38 +0000 (14:44 +0000)
Objects in comparison expressions were compared lazily
with == before this change. Now, they are compared with
===, which is the expected behavior on objects.

typo3/sysext/fluid/Classes/Core/Parser/SyntaxTree/ViewHelperNode.php
typo3/sysext/fluid/Tests/Unit/Core/Parser/SyntaxTree/ViewHelperNodeComparatorTest.php

index 18c8eef..df2e09a 100644 (file)
@@ -290,9 +290,15 @@ class Tx_Fluid_Core_Parser_SyntaxTree_ViewHelperNode extends Tx_Fluid_Core_Parse
        protected function evaluateComparator($comparator, $leftSide, $rightSide) {
                switch ($comparator) {
                        case '==':
+                               if (is_object($leftSide) && is_object($rightSide)) {
+                                       return ($leftSide === $rightSide);
+                               }
                                return ($leftSide == $rightSide);
                                break;
                        case '!=':
+                               if (is_object($leftSide) && is_object($rightSide)) {
+                                       return ($leftSide !== $rightSide);
+                               }
                                return ($leftSide != $rightSide);
                                break;
                        case '%':
index 2218446..c34e8ec 100644 (file)
@@ -276,6 +276,52 @@ class Tx_Fluid_Tests_Unit_Core_Parser_SyntaxTree_ViewHelperNodeComparatorTest ex
 
                $this->assertFalse($this->viewHelperNode->_call('evaluateBooleanExpression', $rootNode, $this->renderingContext));
        }
+
+       /**
+        * @test
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        */
+       public function objectsAreComparedStrictly() {
+               $object1 = new stdClass();
+               $object2 = new stdClass();
+
+               $rootNode = new Tx_Fluid_Core_Parser_SyntaxTree_RootNode();
+
+               $object1Node = $this->getMock('Tx_Fluid_Core_Parser_SyntaxTree_ObjectAccessorNode', array('evaluate'));
+               $object1Node->expects($this->any())->method('evaluate')->will($this->returnValue($object1));
+
+               $object2Node = $this->getMock('Tx_Fluid_Core_Parser_SyntaxTree_ObjectAccessorNode', array('evaluate'));
+               $object2Node->expects($this->any())->method('evaluate')->will($this->returnValue($object2));
+
+               $rootNode->addChildNode($object1Node);
+               $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('=='));
+               $rootNode->addChildNode($object2Node);
+
+               $this->assertFalse($this->viewHelperNode->_call('evaluateBooleanExpression', $rootNode, $this->renderingContext));
+       }
+
+       /**
+        * @test
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        */
+       public function objectsAreComparedStrictlyInUnequalComparison() {
+               $object1 = new stdClass();
+               $object2 = new stdClass();
+
+               $rootNode = new Tx_Fluid_Core_Parser_SyntaxTree_RootNode();
+
+               $object1Node = $this->getMock('Tx_Fluid_Core_Parser_SyntaxTree_ObjectAccessorNode', array('evaluate'));
+               $object1Node->expects($this->any())->method('evaluate')->will($this->returnValue($object1));
+
+               $object2Node = $this->getMock('Tx_Fluid_Core_Parser_SyntaxTree_ObjectAccessorNode', array('evaluate'));
+               $object2Node->expects($this->any())->method('evaluate')->will($this->returnValue($object2));
+
+               $rootNode->addChildNode($object1Node);
+               $rootNode->addChildNode(new Tx_Fluid_Core_Parser_SyntaxTree_TextNode('!='));
+               $rootNode->addChildNode($object2Node);
+
+               $this->assertTrue($this->viewHelperNode->_call('evaluateBooleanExpression', $rootNode, $this->renderingContext));
+       }
 }
 
 ?>
\ No newline at end of file