* wrote some unit tests for the Persistence Session (using a patched version of tx_ph...
authorJochen Rau <j.rau@web.de>
Wed, 4 Feb 2009 22:48:43 +0000 (22:48 +0000)
committerJochen Rau <j.rau@web.de>
Wed, 4 Feb 2009 22:48:43 +0000 (22:48 +0000)
* added some checks (Exceptions) for wrong registration of objects (e.g. trying to register added after registered the same object as reconstituted)

typo3/sysext/extbase/Classes/Persistence/TX_EXTMVC_Persistence_Session.php
typo3/sysext/extbase/Tests/TX_EXTMVC_Persistence_Session_testcase.php [new file with mode: 0644]

index e8bb640..e85c38e 100644 (file)
@@ -77,6 +77,7 @@ class TX_EXTMVC_Persistence_Session implements t3lib_singleton {
         * @author Jochen Rau <jochen.rau@typoplanet.de>
         */
        public function registerAddedObject(TX_EXTMVC_DomainObject_AbstractDomainObject $object) {
+               if ($this->reconstitutedObjects->contains($object)) throw new InvalidArgumentException('The object was registered as reconstituted and can therefore not be registered as added.');
                $this->removedObjects->detach($object);
                $this->addedObjects->attach($object);
        }
@@ -180,6 +181,7 @@ class TX_EXTMVC_Persistence_Session implements t3lib_singleton {
         * @author Jochen Rau <jochen.rau@typoplanet.de>
         */
        public function registerReconstitutedObject(TX_EXTMVC_DomainObject_AbstractDomainObject $object) {
+               if ($this->addedObjects->contains($object)) throw new InvalidArgumentException('The object was registered as added and can therefore not be registered as reconstituted.');
                $this->reconstitutedObjects->attach($object);
                $object->_memorizeCleanState();
        }
diff --git a/typo3/sysext/extbase/Tests/TX_EXTMVC_Persistence_Session_testcase.php b/typo3/sysext/extbase/Tests/TX_EXTMVC_Persistence_Session_testcase.php
new file mode 100644 (file)
index 0000000..a0a8467
--- /dev/null
@@ -0,0 +1,128 @@
+<?php
+declare(ENCODING = 'utf-8');
+
+/*                                                                        *
+ * This script belongs to the FLOW3 framework.                            *
+ *                                                                        *
+ * It is free software; you can redistribute it and/or modify it under    *
+ * the terms of the GNU Lesser General Public License as published by the *
+ * Free Software Foundation, either version 3 of the License, or (at your *
+ * option) any later version.                                             *
+ *                                                                        *
+ * This script is distributed in the hope that it will be useful, but     *
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
+ * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
+ * General Public License for more details.                               *
+ *                                                                        *
+ * You should have received a copy of the GNU Lesser General Public       *
+ * License along with the script.                                         *
+ * If not, see http://www.gnu.org/licenses/lgpl.html                      *
+ *                                                                        *
+ * The TYPO3 project - inspiring people to share!                         *
+ *                                                                        */
+
+require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Persistence/TX_EXTMVC_Persistence_Session.php');
+require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/DomainObject/TX_EXTMVC_DomainObject_Entity.php');
+
+class TX_EXTMVC_Persistence_Session_testcase extends tx_phpunit_testcase {
+
+       public function test_NewSessionIsEmpty() {
+               $session = new TX_EXTMVC_Persistence_Session;
+               $addedObjects = $session->getAddedObjects();
+               $removedObjects = $session->getRemovedObjects();
+               $reconstitutedObjects = $session->getReconstitutedObjects();
+               $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
+               $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
+               $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
+       }
+
+       public function test_ObjectCanBeRegisteredAsAdded() {
+               $session = new TX_EXTMVC_Persistence_Session;
+               $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
+               $session->registerAddedObject($entity);
+               $addedObjects = $session->getAddedObjects();
+               $removedObjects = $session->getRemovedObjects();
+               $reconstitutedObjects = $session->getReconstitutedObjects();
+               
+               $this->assertEquals(1, count($addedObjects), 'The added objects storage holds 0 or more than 1 objects.');
+               $this->assertSame($entity, $addedObjects[0], 'The returned object was not the same as the registered one.');
+               $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
+               $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
+       }
+
+       public function test_ObjectCanBeRegisteredAsRemoved() {
+               $session = new TX_EXTMVC_Persistence_Session;
+               $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
+               $session->registerRemovedObject($entity);
+               $addedObjects = $session->getAddedObjects();
+               $removedObjects = $session->getRemovedObjects();
+               $reconstitutedObjects = $session->getReconstitutedObjects();
+               
+               $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
+               $this->assertEquals(1, count($removedObjects), 'The removed objects storage holds 0 or more than 1 objects.');
+               $this->assertSame($entity, $removedObjects[0], 'The returned object was not the same as the registered one.');
+               $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
+       }
+
+       public function test_ObjectCanBeRegisteredAsReconstituted() {
+               $session = new TX_EXTMVC_Persistence_Session;
+               $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
+               $session->registerReconstitutedObject($entity);
+               $addedObjects = $session->getAddedObjects();
+               $removedObjects = $session->getRemovedObjects();
+               $reconstitutedObjects = $session->getReconstitutedObjects();
+               
+               $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
+               $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
+               $this->assertEquals(1, count($reconstitutedObjects), 'The reconstituted objects storage holds 0 or more than 1 objects.');
+               $this->assertSame($entity, $reconstitutedObjects[0], 'The returned object was not the same as the registered one.');
+       }
+
+       public function test_ObjectCanBeRemovedAfterBeingAdded() {
+               $session = new TX_EXTMVC_Persistence_Session;
+               $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
+               $session->registerAddedObject($entity);
+               $session->registerRemovedObject($entity);
+               $addedObjects = $session->getAddedObjects();
+               $removedObjects = $session->getRemovedObjects();
+               $reconstitutedObjects = $session->getReconstitutedObjects();
+               
+               $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
+               $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
+               $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
+       }
+
+       public function test_AnObjectCanBeRemovedAfterBeingAdded() {
+               $session = new TX_EXTMVC_Persistence_Session;
+               $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
+               $session->registerAddedObject($entity);
+               $session->registerRemovedObject($entity);
+               $addedObjects = $session->getAddedObjects();
+               $removedObjects = $session->getRemovedObjects();
+               $reconstitutedObjects = $session->getReconstitutedObjects();
+               
+               $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
+               $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
+               $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
+       }
+
+       public function test_TryingToRegisterReconstitutedObjectsAsAddedResultsInAnException() {
+               $this->setExpectedException('InvalidArgumentException');
+               $session = new TX_EXTMVC_Persistence_Session;
+               $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
+               $session->registerReconstitutedObject($entity);
+               $session->registerAddedObject($entity);
+       }
+
+       public function test_TryingToRegisterAddedObjectsAsReconstitutedResultsInAnException() {
+               $this->setExpectedException('InvalidArgumentException');
+               $session = new TX_EXTMVC_Persistence_Session;
+               $entity1 = $this->getMock('TX_EXTMVC_DomainObject_Entity');
+               $entity2 = $this->getMock('TX_EXTMVC_DomainObject_Entity');
+               $session->registerAddedObject($entity1);
+               $session->registerReconstitutedObject($entity1);
+       }
+
+       
+}
+?>
\ No newline at end of file