Author: ts
Date: Mon Jan 21 19:13:26 2008
New Revision: 7218

Log:
- Implemented removeRelatedObject() for multi-relation handling.

Modified:
    trunk/PersistentObject/src/handlers/delete_handler.php
    trunk/PersistentObject/src/persistent_session.php
    trunk/PersistentObject/tests/multi_relation_test.php

Modified: trunk/PersistentObject/src/handlers/delete_handler.php
==============================================================================
--- trunk/PersistentObject/src/handlers/delete_handler.php [iso-8859-1] 
(original)
+++ trunk/PersistentObject/src/handlers/delete_handler.php [iso-8859-1] Mon Jan 
21 19:13:26 2008
@@ -123,19 +123,41 @@
      * @throws ezcPersistentRelationNotFoundException
      *         if the deisred relation is not defined.
      */
-    public function removeRelatedObject( $object, $relatedObject )
+    public function removeRelatedObject( $object, $relatedObject, 
$relationName = null )
     {
         $class        = get_class( $object );
         $relatedClass = get_class( $relatedObject );
+
         $def          = $this->definitionManager->fetchDefinition( $class );
-        $relatedDef = $this->definitionManager->fetchDefinition( get_class( 
$relatedObject ) );
+        $relatedDef   = $this->definitionManager->fetchDefinition( get_class( 
$relatedObject ) );
 
         // Sanity checks.
         if ( !isset( $def->relations[$relatedClass] ) )
         {
             throw new ezcPersistentRelationNotFoundException( $class, 
$relatedClass );
         }
-        if ( isset( $def->relations[$relatedClass]->reverse ) && 
$def->relations[$relatedClass]->reverse === true )
+
+        $relation = $def->relations[$relatedClass];
+        
+        // New multi-relations for a single class
+        if ( $relation instanceof ezcPersistentRelationCollection )
+        {
+            if ( $relationName === null )
+            {
+                throw new ezcPersistentUndeterministicRelationException( 
$relatedClass );
+            }
+            if ( !isset( $relation[$relationName] ) )
+            {
+                throw new ezcPersistentRelationNotFoundException(
+                    $class,
+                    $relatedClass,
+                    $relationName
+                );
+            }
+            $relation = $relation[$relationName];
+        }
+        
+        if ( isset( $relation->reverse ) && $relation->reverse === true )
         {
             throw new ezcPersistentRelationOperationNotSupportedException(
                 $class,
@@ -148,7 +170,7 @@
         $objectState        = $this->session->getObjectState( $object );
         $relatedObjectState = $this->session->getObjectState( $relatedObject );
 
-        switch ( get_class( ( $relation = $def->relations[get_class( 
$relatedObject )] ) ) )
+        switch ( get_class( $relation ) )
         {
             case "ezcPersistentOneToManyRelation":
             case "ezcPersistentOneToOneRelation":

Modified: trunk/PersistentObject/src/persistent_session.php
==============================================================================
--- trunk/PersistentObject/src/persistent_session.php [iso-8859-1] (original)
+++ trunk/PersistentObject/src/persistent_session.php [iso-8859-1] Mon Jan 21 
19:13:26 2008
@@ -414,9 +414,9 @@
      * @throws ezcPersistentRelationNotFoundException
      *         if the deisred relation is not defined.
      */
-    public function addRelatedObject( $object, $relatedObject, $relatioName = 
null )
-    {
-        return $this->saveHandler->addRelatedObject( $object, $relatedObject, 
$relatioName );
+    public function addRelatedObject( $object, $relatedObject, $relationName = 
null )
+    {
+        return $this->saveHandler->addRelatedObject( $object, $relatedObject, 
$relationName );
     }
 
     /**
@@ -508,9 +508,9 @@
      * @throws ezcPersistentRelationNotFoundException
      *         if the deisred relation is not defined.
      */
-    public function removeRelatedObject( $object, $relatedObject )
-    {
-        return $this->deleteHandler->removeRelatedObject( $object, 
$relatedObject );
+    public function removeRelatedObject( $object, $relatedObject, 
$relationName = null )
+    {
+        return $this->deleteHandler->removeRelatedObject( $object, 
$relatedObject, $relationName );
     }
 
     /**

Modified: trunk/PersistentObject/tests/multi_relation_test.php
==============================================================================
--- trunk/PersistentObject/tests/multi_relation_test.php [iso-8859-1] (original)
+++ trunk/PersistentObject/tests/multi_relation_test.php [iso-8859-1] Mon Jan 
21 19:13:26 2008
@@ -46,7 +46,7 @@
 
     public function teardown()
     {
-        // MultiRelationTestPerson::cleanup();
+        MultiRelationTestPerson::cleanup();
     }
 
     public function testLoad()
@@ -100,6 +100,20 @@
         );
     }
 
+    public function testSave()
+    {
+        $newChild = new MultiRelationTestPerson();
+        $newChild->name = "New child";
+
+        $this->session->save( $newChild );
+
+        $this->assertEquals(
+            6,
+            $newChild->id,
+            'New MultiRelationTestPerson saved with incorrect ID.'
+        );
+    }
+
     public function testGetRelatedObjectsFailureWithoutRelationName()
     {
         $mother = $this->session->load( 'MultiRelationTestPerson', 1 );
@@ -218,18 +232,21 @@
         );
     }
 
-    public function testSave()
+    public function testAddObjectsFailureWithoutRelationName()
     {
         $newChild = new MultiRelationTestPerson();
         $newChild->name = "New child";
 
         $this->session->save( $newChild );
 
-        $this->assertEquals(
-            6,
-            $newChild->id,
-            'New MultiRelationTestPerson saved with incorrect ID.'
-        );
+        $mother = $this->session->load( 'MultiRelationTestPerson', 1 );
+
+        try
+        {
+            $this->session->addRelatedObject( $mother, $newChild );
+            $this->fail( 'Exception not thrown on addRelatedObject() without 
relation name.' );
+        }
+        catch ( ezcPersistentUndeterministicRelationException $e ) {}
     }
 
     public function testAddRelatedObjectOneToManySuccess()
@@ -287,6 +304,116 @@
             'Incorrect perso ID in relation record.'
         );
     }
+    
+    public function testRemoveObjectsFailureWithoutRelationName()
+    {
+        $mother   = $this->session->load( 'MultiRelationTestPerson', 1 );
+        $children = $this->session->getRelatedObjects( $mother, 
'MultiRelationTestPerson', 'mothers_children' );
+
+        try
+        {
+            $this->session->removeRelatedObject( $mother, $children[0] );
+            $this->fail( 'Exception not thrown on removeRelatedObject() 
without relation name.' );
+        }
+        catch ( ezcPersistentUndeterministicRelationException $e ) {}
+    }
+
+    public function testRemoveRelatedObjectSuccessMotherChildren()
+    {
+        $mother   = $this->session->load( 'MultiRelationTestPerson', 1 );
+        $children = $this->session->getRelatedObjects( $mother, 
'MultiRelationTestPerson', 'mothers_children' );
+
+        foreach( $children as $child )
+        {
+            $this->session->removeRelatedObject( $mother, $child, 
'mothers_children' );
+
+            $this->assertNull(
+                $child->mother,
+                "MultiRelationTestPerson child {$child->id} not correctly 
removed from mother."
+            );
+            $this->assertNotNull(
+                $child->father,
+                "MultiRelationTestPerson child {$child->id} also removed from 
father instead from mother only."
+            );
+        }
+    }
+
+    public function testRemoveRelatedObjectSuccessFatherChildren()
+    {
+        $father   = $this->session->load( 'MultiRelationTestPerson', 2 );
+        $children = $this->session->getRelatedObjects( $father, 
'MultiRelationTestPerson', 'fathers_children' );
+
+        foreach( $children as $child )
+        {
+            $this->session->removeRelatedObject( $father, $child, 
'fathers_children' );
+
+            $this->assertNull(
+                $child->father,
+                "MultiRelationTestPerson child {$child->id} not correctly 
removed from father."
+            );
+            $this->assertNotNull(
+                $child->mother,
+                "MultiRelationTestPerson child {$child->id} also removed from 
mother instead from father only."
+            );
+        }
+    }
+    
+    public function testRemoveRelatedObjectFailureChildMother()
+    {
+        $child   = $this->session->load( 'MultiRelationTestPerson', 3 );
+        $mother = $this->session->getRelatedObject( $child, 
'MultiRelationTestPerson', 'mother' );
+        
+        try
+        {
+            $this->session->removeRelatedObject( $child, $mother, 'mother' );
+            $this->fail( "MultiRelationTestPerson correctly removed from 
mother although relation is marked reverse." );
+        }
+        catch ( ezcPersistentRelationOperationNotSupportedException $e ) {}
+    }
+    
+    public function testRemoveRelatedObjectFailureChildFather()
+    {
+        $child  = $this->session->load( 'MultiRelationTestPerson', 4 );
+        $father = $this->session->getRelatedObject( $child, 
'MultiRelationTestPerson', 'father' );
+        
+        try
+        {
+            $this->session->removeRelatedObject( $child, $father, 'father' );
+            $this->fail( "MultiRelationTestPerson correctly removed from 
father although relation is marked reverse." );
+        }
+        catch ( ezcPersistentRelationOperationNotSupportedException $e ) {}
+    }
+
+    public function testRemoveRelatedObjectSuccessSiblings()
+    {
+        $child    = $this->session->load( 'MultiRelationTestPerson', 3 );
+        $siblings = $this->session->getRelatedObjects( $child, 
'MultiRelationTestPerson', 'siblings' );
+
+        foreach ( $siblings as $sibling )
+        {
+            $this->session->removeRelatedObject( $child, $sibling, 'siblings' 
);
+        }
+
+        $q = $this->session->database->createSelectQuery();
+        $q->select( '*' )
+          ->from( 'PO_sibling' )
+          ->where(
+            $q->expr->eq(
+                $this->session->database->quoteIdentifier( 'person' ),
+                $q->bindValue( $child->id )
+            )
+          );
+
+        $stmt = $q->prepare();
+        $stmt->execute();
+        $rows = $stmt->fetchAll( PDO::FETCH_ASSOC );
+
+        $this->assertEquals(
+            0,
+            count( $rows ),
+            'Incorrect number of relation records after removing all siblings.'
+        );
+    }
 }
 
 ?>


-- 
svn-components mailing list
svn-components@lists.ez.no
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to