[+BUGFIX] Extbase (Persistence): Wrote unit tests for the AbstractEntity to ensure...
authorSebastian Kurfürst <sebastian@typo3.org>
Fri, 18 Sep 2009 09:21:20 +0000 (09:21 +0000)
committerSebastian Kurfürst <sebastian@typo3.org>
Fri, 18 Sep 2009 09:21:20 +0000 (09:21 +0000)
[+BUGFIX] Extbase (Persistence): Changed some doc comments.

typo3/sysext/extbase/Classes/Dispatcher.php
typo3/sysext/extbase/Classes/DomainObject/AbstractEntity.php
typo3/sysext/extbase/Classes/Persistence/Backend.php
typo3/sysext/extbase/Classes/Persistence/Storage/Typo3DbBackend.php
typo3/sysext/extbase/Tests/DomainObject/AbstractEntity_testcase.php [new file with mode: 0644]

index 3a1ebdd..d9d1dd2 100644 (file)
@@ -188,6 +188,7 @@ class Tx_Extbase_Dispatcher {
                try {
                        self::$reflectionService->setCache($cacheManager->getCache('cache_extbase_reflection'));
                } catch (t3lib_cache_exception_NoSuchCache $exception) {
+
                        $GLOBALS['typo3CacheFactory']->create(
                                'cache_extbase_reflection',
                                't3lib_cache_frontend_VariableFrontend',
index 2d93506..09e3c43 100644 (file)
@@ -104,13 +104,19 @@ abstract class Tx_Extbase_DomainObject_AbstractEntity extends Tx_Extbase_DomainO
         * @return array
         */
        public function _getDirtyProperties() {
-               // FIXME: We persist more than we'd like to. See _isDirty for the correct check.
                if (!is_array($this->_cleanProperties)) throw new Tx_Extbase_Persistence_Exception_CleanStateNotMemorized('The clean state of the object "' . get_class($this) . '" has not been memorized before asking _isDirty().', 1233309106);
                if ($this->uid !== NULL && $this->uid != $this->_cleanProperties['uid']) throw new Tx_Extbase_Persistence_Exception_TooDirty('The uid "' . $this->uid . '" has been modified, that is simply too much.', 1222871239);
                $dirtyProperties = array();
                foreach ($this->_cleanProperties as $propertyName => $propertyValue) {
-                       if ($this->$propertyName !== $propertyValue) {
-                               $dirtyProperties[$propertyName] = $this->$propertyName;
+                       if (is_object($this->$propertyName)) {
+                               // In case it is an object, we do a simple comparison (!=) as we want cloned objects to return the same values.
+                               if ($this->$propertyName != $propertyValue) {
+                                       $dirtyProperties[$propertyName] = $this->$propertyName;
+                               }
+                       } else {
+                               if ($this->$propertyName !== $propertyValue) {
+                                       $dirtyProperties[$propertyName] = $this->$propertyName;
+                               }
                        }
                }
                return $dirtyProperties;
index e3a5ccd..5b8fce2 100644 (file)
@@ -360,6 +360,7 @@ class Tx_Extbase_Persistence_Backend implements Tx_Extbase_Persistence_BackendIn
                        }
                }
 
+               // TODO: We should only register the object if it was really updated.
                $this->identityMap->registerObject($object, $object->getUid());
                $object->_memorizeCleanState();
        }
index 259fd60..f971e96 100644 (file)
@@ -419,7 +419,7 @@ class Tx_Extbase_Persistence_Storage_Typo3DbBackend implements Tx_Extbase_Persis
                        $tableName = $operand->getSelectorName();
                        // FIXME Discuss the translation from propertyName to columnName
                        if ($source instanceof Tx_Extbase_Persistence_QOM_SelectorInterface) {
-                                $className = $source->getNodeTypeName();
+                               $className = $source->getNodeTypeName();
                        } else {
                                $className = '';
                        }
diff --git a/typo3/sysext/extbase/Tests/DomainObject/AbstractEntity_testcase.php b/typo3/sysext/extbase/Tests/DomainObject/AbstractEntity_testcase.php
new file mode 100644 (file)
index 0000000..d17c160
--- /dev/null
@@ -0,0 +1,127 @@
+<?php
+/***************************************************************
+*  Copyright notice
+*
+*  (c) 2009 Sebastian Kurf\9frst <sebastian@typo3.org>
+*  All rights reserved
+*
+*  This script is part of the TYPO3 project. The TYPO3 project is
+*  free software; you can redistribute it and/or modify
+*  it under the terms of the GNU General Public License as published by
+*  the Free Software Foundation; either version 2 of the License, or
+*  (at your option) any later version.
+*
+*  The GNU General Public License can be found at
+*  http://www.gnu.org/copyleft/gpl.html.
+*
+*  This script is distributed in the hope that it will be useful,
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*  GNU General Public License for more details.
+*
+*  This copyright notice MUST APPEAR in all copies of the script!
+***************************************************************/
+
+class Tx_Extbase_DomainObject_AbstractEntity_testcase extends Tx_Extbase_Base_testcase {
+
+       /**
+        * @test
+        */
+       public function objectIsNotDirtyAfterCallingMemorizeCleanStateWithSimpleProperties() {
+               $domainObjectName = uniqid('DomainObject_');
+               eval('class ' . $domainObjectName . ' extends Tx_Extbase_DomainObject_AbstractEntity {
+                       public $foo;
+                       public $bar;
+               }');
+               $domainObject = new $domainObjectName();
+               $domainObject->foo = 'Test';
+               $domainObject->bar = 'It is raining outside';
+               
+               $domainObject->_memorizePropertyCleanState('foo');
+               $domainObject->_memorizePropertyCleanState('bar');
+               $this->assertFalse($domainObject->_isDirty());
+       }
+
+       /**
+        * @test
+        */
+       public function objectIsDirtyAfterCallingMemorizeCleanStateWithSimplePropertiesAndModifyingThePropertiesAfterwards() {
+               $domainObjectName = uniqid('DomainObject_');
+               eval('class ' . $domainObjectName . ' extends Tx_Extbase_DomainObject_AbstractEntity {
+                       public $foo;
+                       public $bar;
+               }');
+               $domainObject = new $domainObjectName();
+               $domainObject->foo = 'Test';
+               $domainObject->bar = 'It is raining outside';
+
+               $domainObject->_memorizePropertyCleanState('foo');
+               $domainObject->_memorizePropertyCleanState('bar');
+               $domainObject->bar = 'Now it is sunny.';
+               $this->assertTrue($domainObject->_isDirty());
+
+               // We can check here that _getDirtyProperties returns the correct values as well.
+               $this->assertSame(array('bar' => 'Now it is sunny.'), $domainObject->_getDirtyProperties());
+       }
+
+       /**
+        * @test
+        */
+       public function objectIsNotDirtyAfterCallingMemorizeCleanStateWithObjectProperties() {
+               $domainObjectName = uniqid('DomainObject_');
+               eval('class ' . $domainObjectName . ' extends Tx_Extbase_DomainObject_AbstractEntity {
+                       public $foo;
+                       public $bar;
+               }');
+               $domainObject = new $domainObjectName();
+               $domainObject->foo = new DateTime();
+               $domainObject->bar = 'It is raining outside';
+
+               $domainObject->_memorizePropertyCleanState('foo');
+               $domainObject->_memorizePropertyCleanState('bar');
+               $this->assertFalse($domainObject->_isDirty());
+       }
+
+       /**
+        * @test
+        */
+       public function getDirtyPropertiesReturnsNoPropertiesIfObjectWithObjectPropertiesIsClean() {
+               $domainObjectName = uniqid('DomainObject_');
+               eval('class ' . $domainObjectName . ' extends Tx_Extbase_DomainObject_AbstractEntity {
+                       public $foo;
+                       public $bar;
+               }');
+               $domainObject = new $domainObjectName();
+               $domainObject->foo = new DateTime();
+               $domainObject->bar = 'It is raining outside';
+               $domainObject->_memorizePropertyCleanState('foo');
+               $domainObject->_memorizePropertyCleanState('bar');
+               $this->assertEquals(array(), $domainObject->_getDirtyProperties());
+       }
+
+       /**
+        * @test
+        */
+       public function objectIsNotDirtyAfterCallingMemorizeCleanStateWithOtherDomainObjectsAsProperties() {
+               $domainObjectName = uniqid('DomainObject_');
+               eval('class ' . $domainObjectName . ' extends Tx_Extbase_DomainObject_AbstractEntity {
+                       public $foo;
+                       public $bar;
+               }');
+
+               $secondDomainObjectName = uniqid('DomainObject_');
+               eval('class ' . $secondDomainObjectName . ' extends Tx_Extbase_DomainObject_AbstractEntity {
+                       public $foo;
+                       public $bar;
+               }');
+
+               $domainObject = new $domainObjectName();
+               $domainObject->foo = new $secondDomainObjectName;
+               $domainObject->bar = 'It is raining outside';
+
+               $domainObject->_memorizePropertyCleanState('foo');
+               $domainObject->_memorizePropertyCleanState('bar');
+               $this->assertFalse($domainObject->_isDirty());
+       }
+}
+?>
\ No newline at end of file