Author: Frederik Holljen
Date: 2007-05-07 23:05:11 +0200 (Mon, 07 May 2007)
New Revision: 5139

Log:
- Fixed issue: #010152: Persistent Object and manual generator: string primary 
keys

Added:
   trunk/PersistentObject/tests/data/string_identifier/
   trunk/PersistentObject/tests/data/string_identifier/main_table_class.php
   trunk/PersistentObject/tests/data/string_identifier/maintable.php
   trunk/PersistentObject/tests/data/string_identifier/rel1.php
   trunk/PersistentObject/tests/data/string_identifier/rel2.php
   trunk/PersistentObject/tests/data/string_identifier/rel_class.php
   trunk/PersistentObject/tests/data/string_identifier/table.dba
   trunk/PersistentObject/tests/string_identifier_test.php
Modified:
   trunk/PersistentObject/ChangeLog
   trunk/PersistentObject/src/persistent_session.php
   trunk/PersistentObject/tests/one_to_one_relation_test.php
   trunk/PersistentObject/tests/suite.php

Modified: trunk/PersistentObject/ChangeLog
===================================================================
--- trunk/PersistentObject/ChangeLog    2007-05-07 14:23:19 UTC (rev 5138)
+++ trunk/PersistentObject/ChangeLog    2007-05-07 21:05:11 UTC (rev 5139)
@@ -1,6 +1,12 @@
-1.3beta1 - [RELEASEDATE]
+1.3rc1 - [RELEASEDATE]
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
+- Fixed issue: #010152: Persistent Object and manual generator: string primary 
keys
+       
+       
+1.3beta1 - Monday 07 May 2007
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
 - Fixed testcase for manual generator giving failure for postgresql on correct
   behaviour (INSERT violating NULL contraint does give an error...).
 - Fixed issue #9999: idProperty missing in definition file causes unclear

Modified: trunk/PersistentObject/src/persistent_session.php
===================================================================
--- trunk/PersistentObject/src/persistent_session.php   2007-05-07 14:23:19 UTC 
(rev 5138)
+++ trunk/PersistentObject/src/persistent_session.php   2007-05-07 21:05:11 UTC 
(rev 5139)
@@ -784,11 +784,6 @@
      */
     public function loadIntoObject( $pObject, $id )
     {
-        if ( !is_numeric( $id ) )
-        {
-            throw new ezcPersistentQueryException( "The parameter 'id' was not 
a valid integer." );
-        }
-
         $def = $this->definitionManager->fetchDefinition( get_class( $pObject 
) ); // propagate exception
         $q = $this->database->createSelectQuery();
         $q->select( $this->getColumnsFromDefinition( $def ) )
@@ -826,7 +821,7 @@
         else
         {
             $class = get_class( $pObject );
-            throw new ezcPersistentQueryException( "No such object $class with 
id $id." );
+            throw new ezcPersistentQueryException( "No object of class 
'$class' with id '$id'." );
         }
     }
 
@@ -946,6 +941,7 @@
      * If the object is a new object an INSERT INTO query will be executed. If 
the
      * object is persistent already it will be updated with an UPDATE query.
      *
+     * @throws ezcPersistentDefinitionNotFoundException if the definition of 
the persistent object could not be loaded
      * @throws ezcPersistentException if $pObject is not of a valid persistent 
object type.
      * @throws ezcPersistentException if any of the definition requirements 
are not met.
      * @throws ezcPersistentException if the insert or update query failed.
@@ -956,9 +952,21 @@
     {
         $def = $this->definitionManager->fetchDefinition( get_class( $pObject 
) );// propagate exception
         $state = $pObject->getState();
-        $idValue = $state[$def->idProperty->propertyName];
-        if ( $idValue === null )
+
+        // fetch the id generator
+        $idGenerator = null;
+        if ( ezcBaseFeatures::classExists( $def->idProperty->generator->class 
) )
         {
+            $idGenerator = new $def->idProperty->generator->class;
+            if ( !( $idGenerator instanceof ezcPersistentIdentifierGenerator ) 
)
+            {
+                throw new ezcPersistentIdentifierGenerationException( 
get_class( $pObject ),
+                                                                      "Could 
not initialize identifier generator: ". "{$def->idProperty->generator->class} 
." );
+            }
+        }
+
+        if ( !$idGenerator->checkPersistence( $def, $this->database, $state ) )
+        {
             $this->save( $pObject );
         }
         else
@@ -1095,7 +1103,8 @@
             {
                 if ( $value !== null )
                 {
-                    $typedState[$name] = (int) $value;
+                    /* TODO, default to int */
+                    $typedState[$name] = $value;
                     continue;
                 }
             }

Added: trunk/PersistentObject/tests/data/string_identifier/main_table_class.php
===================================================================
--- trunk/PersistentObject/tests/data/string_identifier/main_table_class.php    
2007-05-07 14:23:19 UTC (rev 5138)
+++ trunk/PersistentObject/tests/data/string_identifier/main_table_class.php    
2007-05-07 21:05:11 UTC (rev 5139)
@@ -0,0 +1,101 @@
+<?php
+ezcTestRunner::addFileToFilter( __FILE__ );
+
+/**
+CREATE TABLE main_table
+(
+  id varchar(255),
+  data varchar(255),
+  PRIMARY KEY (id)
+) TYPE=InnoDB;
+
+CREATE TABLE rel
+(
+  id varchar(255),
+  fk varchar(255),
+  PRIMARY KEY (id)
+) TYPE=InnoDB;
+
+CREATE TABLE link
+(
+   main_id varchar(255),
+   rel_id varchar(255)
+) TYPE=InnoDB;
+*/
+
+class MainTable
+{
+    public $id = null;
+    public $data = null;
+
+    /**
+     * Inserts some data to use for testing.
+     */
+    public static function insertCleanData()
+    {
+    }
+
+    /**
+     * Saves the schema from database to file.
+     *
+     * Use this method if you have changed the definition of the persistent 
object
+     * and need to update the file on disk.
+     */
+    public static function saveSchema()
+    {
+        $db = ezcDbInstance::get();
+        $schema = ezcDbSchema::createFromDb( $db );
+        $schema->writeToFile( 'array', dirname( __FILE__ ) . '/table.dba' );
+    }
+
+    /**
+     * Loads the schema from file into the database.
+     */
+    public static function setupTable()
+    {
+        $db = ezcDbInstance::get();
+        // Load schema
+        $schema = ezcDbSchema::createFromFile( 'array', dirname( __FILE__ ) . 
'/table.dba' );
+        $schema->writeToDb( $db );
+    }
+
+    public static function cleanup()
+    {
+        $db = ezcDbInstance::get();
+        $db->exec( 'DROP TABLE' . $db->quoteIdentifier( 'main_table' ) );
+        $db->exec( 'DROP TABLE' . $db->quoteIdentifier( 'rel' ) );
+        $db->exec( 'DROP TABLE' . $db->quoteIdentifier( 'link' ) );
+        if ( $db->getName() === 'oracle' )
+        {
+            $db->exec( "DROP SEQUENCE " . $db->quoteIdentifier( 
"main_table_id_seq" ) );
+            $db->exec( "DROP SEQUENCE " . $db->quoteIdentifier( "rel_id_seq" ) 
);
+        }
+    }
+
+    /*
+    public function saveSqlSchemas()
+    {
+        $db = ezcDbInstance::get();
+        $schema = ezcDbSchema::createFromFile( 'php', dirname( __FILE__ ) . 
'/persistent_test_object.dba' );
+        $schema->writeToFile( dirname( __FILE__ ) . 
'/persistent_test_object-pgsql.sql', 'pgsql-file', 'schema' );
+    }
+    */
+
+    public function setState( array $state )
+    {
+        foreach ( $state as $key => $value )
+        {
+            $this->$key = $value;
+        }
+    }
+
+    public function getState()
+    {
+        $result = array();
+        $result['id'] = $this->id;
+        $result['data'] = $this->data;
+        return $result;
+    }
+}
+
+?>


Property changes on: 
trunk/PersistentObject/tests/data/string_identifier/main_table_class.php
___________________________________________________________________
Name: svn:eol-style
   + native

Added: trunk/PersistentObject/tests/data/string_identifier/maintable.php
===================================================================
--- trunk/PersistentObject/tests/data/string_identifier/maintable.php   
2007-05-07 14:23:19 UTC (rev 5138)
+++ trunk/PersistentObject/tests/data/string_identifier/maintable.php   
2007-05-07 21:05:11 UTC (rev 5139)
@@ -0,0 +1,50 @@
+<?php
+ezcTestRunner::addFileToFilter( __FILE__ );
+
+/*
+ * Holds the definition for Table
+ * This definition is used by the keywords test.
+ */
+// build definition
+$def = new ezcPersistentObjectDefinition();
+$def->table = "main_table";
+$def->class = "MainTable";
+
+$def->idProperty = new ezcPersistentObjectIdProperty;
+$def->idProperty->columnName = 'id';
+$def->idProperty->propertyName = 'id';
+$def->idProperty->visibility = ezcPersistentObjectProperty::VISIBILITY_PRIVATE;
+$def->idProperty->generator = new ezcPersistentGeneratorDefinition( 
'ezcPersistentManualGenerator' );
+
+$def->properties['data'] = new ezcPersistentObjectProperty;
+$def->properties['data']->columnName = 'data';
+$def->properties['data']->propertyName = 'data';
+$def->properties['data']->propertyType = 
ezcPersistentObjectProperty::PHP_TYPE_INT;
+$def->properties['data']->defaultValue = 0;
+$def->properties['data']->visibility = 
ezcPersistentObjectProperty::VISIBILITY_PRIVATE;
+$def->properties['data']->isRequired = false;
+
+$def->relations["Rel1"] = new ezcPersistentOneToManyRelation(
+     "main_table",
+     "rel"
+);
+$def->relations["Rel1"]->columnMap = array(
+     new ezcPersistentSingleTableMap(
+         "id",
+         "fk"
+     ),
+ );
+
+$def->relations["Rel2"] = new ezcPersistentManyToManyRelation(
+     "main_table",
+     "rel",
+     "link"
+);
+
+$def->relations["Rel2"]->columnMap = array(
+    new ezcPersistentDoubleTableMap( "id", "main_id", "rel_id", "id" ),
+);
+
+return $def;
+
+?>


Property changes on: 
trunk/PersistentObject/tests/data/string_identifier/maintable.php
___________________________________________________________________
Name: svn:eol-style
   + native

Added: trunk/PersistentObject/tests/data/string_identifier/rel1.php
===================================================================
--- trunk/PersistentObject/tests/data/string_identifier/rel1.php        
2007-05-07 14:23:19 UTC (rev 5138)
+++ trunk/PersistentObject/tests/data/string_identifier/rel1.php        
2007-05-07 21:05:11 UTC (rev 5139)
@@ -0,0 +1,29 @@
+<?php
+ezcTestRunner::addFileToFilter( __FILE__ );
+
+/*
+ * Holds the definition for the class Where
+ * This definition is used by the keywords test.
+ */
+// build definition
+$def = new ezcPersistentObjectDefinition();
+$def->table = "rel";
+$def->class = "Rel1";
+
+$def->idProperty = new ezcPersistentObjectIdProperty;
+$def->idProperty->columnName = 'id';
+$def->idProperty->propertyName = 'id';
+$def->idProperty->visibility = ezcPersistentObjectProperty::VISIBILITY_PRIVATE;
+$def->idProperty->generator = new ezcPersistentGeneratorDefinition( 
'ezcPersistentManualGenerator' );
+
+$def->properties['fk'] = new ezcPersistentObjectProperty;
+$def->properties['fk']->columnName = 'fk';
+$def->properties['fk']->propertyName = 'fk';
+$def->properties['fk']->propertyType = 
ezcPersistentObjectProperty::PHP_TYPE_STRING;
+$def->properties['fk']->defaultValue = 0;
+$def->properties['fk']->visibility = 
ezcPersistentObjectProperty::VISIBILITY_PRIVATE;
+$def->properties['fk']->isRequired = false;
+
+return $def;
+
+?>


Property changes on: 
trunk/PersistentObject/tests/data/string_identifier/rel1.php
___________________________________________________________________
Name: svn:eol-style
   + native

Added: trunk/PersistentObject/tests/data/string_identifier/rel2.php
===================================================================
--- trunk/PersistentObject/tests/data/string_identifier/rel2.php        
2007-05-07 14:23:19 UTC (rev 5138)
+++ trunk/PersistentObject/tests/data/string_identifier/rel2.php        
2007-05-07 21:05:11 UTC (rev 5139)
@@ -0,0 +1,29 @@
+<?php
+ezcTestRunner::addFileToFilter( __FILE__ );
+
+/*
+ * Holds the definition for the class Where
+ * This definition is used by the keywords test.
+ */
+// build definition
+$def = new ezcPersistentObjectDefinition();
+$def->table = "rel";
+$def->class = "Rel2";
+
+$def->idProperty = new ezcPersistentObjectIdProperty;
+$def->idProperty->columnName = 'id';
+$def->idProperty->propertyName = 'id';
+$def->idProperty->visibility = ezcPersistentObjectProperty::VISIBILITY_PRIVATE;
+$def->idProperty->generator = new ezcPersistentGeneratorDefinition( 
'ezcPersistentManualGenerator' );
+
+$def->properties['fk'] = new ezcPersistentObjectProperty;
+$def->properties['fk']->columnName = 'fk';
+$def->properties['fk']->propertyName = 'fk';
+$def->properties['fk']->propertyType = 
ezcPersistentObjectProperty::PHP_TYPE_STRING;
+$def->properties['fk']->defaultValue = 0;
+$def->properties['fk']->visibility = 
ezcPersistentObjectProperty::VISIBILITY_PRIVATE;
+$def->properties['fk']->isRequired = false;
+
+return $def;
+
+?>


Property changes on: 
trunk/PersistentObject/tests/data/string_identifier/rel2.php
___________________________________________________________________
Name: svn:eol-style
   + native

Added: trunk/PersistentObject/tests/data/string_identifier/rel_class.php
===================================================================
--- trunk/PersistentObject/tests/data/string_identifier/rel_class.php   
2007-05-07 14:23:19 UTC (rev 5138)
+++ trunk/PersistentObject/tests/data/string_identifier/rel_class.php   
2007-05-07 21:05:11 UTC (rev 5139)
@@ -0,0 +1,29 @@
+<?php
+ezcTestRunner::addFileToFilter( __FILE__ );
+
+class Rel1
+{
+    public $id = null;
+    public $fk = null;
+
+    public function setState( array $state )
+    {
+        foreach ( $state as $key => $value )
+        {
+            $this->$key = $value;
+        }
+    }
+
+    public function getState()
+    {
+        $result = array();
+        $result['id'] = $this->id;
+        $result['fk'] = $this->fk;
+        return $result;
+    }
+}
+
+class Rel2 extends Rel1
+{
+}
+?>


Property changes on: 
trunk/PersistentObject/tests/data/string_identifier/rel_class.php
___________________________________________________________________
Name: svn:eol-style
   + native

Added: trunk/PersistentObject/tests/data/string_identifier/table.dba
===================================================================
--- trunk/PersistentObject/tests/data/string_identifier/table.dba       
2007-05-07 14:23:19 UTC (rev 5138)
+++ trunk/PersistentObject/tests/data/string_identifier/table.dba       
2007-05-07 21:05:11 UTC (rev 5139)
@@ -0,0 +1,113 @@
+<?php return array (
+  0 => 
+  array (
+    'link' => 
+    ezcDbSchemaTable::__set_state(array(
+       'fields' => 
+      array (
+        'main_id' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'text',
+           'length' => 255,
+           'notNull' => false,
+           'default' => NULL,
+           'autoIncrement' => false,
+           'unsigned' => false,
+        )),
+        'rel_id' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'text',
+           'length' => 255,
+           'notNull' => false,
+           'default' => NULL,
+           'autoIncrement' => false,
+           'unsigned' => false,
+        )),
+      ),
+       'indexes' => 
+      array (
+      ),
+    )),
+    'main_table' => 
+    ezcDbSchemaTable::__set_state(array(
+       'fields' => 
+      array (
+        'data' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'text',
+           'length' => 255,
+           'notNull' => false,
+           'default' => NULL,
+           'autoIncrement' => false,
+           'unsigned' => false,
+        )),
+        'id' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'text',
+           'length' => 255,
+           'notNull' => true,
+           'default' => NULL,
+           'autoIncrement' => false,
+           'unsigned' => false,
+        )),
+      ),
+       'indexes' => 
+      array (
+        'primary' => 
+        ezcDbSchemaIndex::__set_state(array(
+           'indexFields' => 
+          array (
+            'id' => 
+            ezcDbSchemaIndexField::__set_state(array(
+               'sorting' => NULL,
+            )),
+          ),
+           'primary' => true,
+           'unique' => true,
+        )),
+      ),
+    )),
+    'rel' => 
+    ezcDbSchemaTable::__set_state(array(
+       'fields' => 
+      array (
+        'fk' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'text',
+           'length' => 255,
+           'notNull' => false,
+           'default' => NULL,
+           'autoIncrement' => false,
+           'unsigned' => false,
+        )),
+        'id' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'text',
+           'length' => 255,
+           'notNull' => true,
+           'default' => NULL,
+           'autoIncrement' => false,
+           'unsigned' => false,
+        )),
+      ),
+       'indexes' => 
+      array (
+        'primary' => 
+        ezcDbSchemaIndex::__set_state(array(
+           'indexFields' => 
+          array (
+            'id' => 
+            ezcDbSchemaIndexField::__set_state(array(
+               'sorting' => NULL,
+            )),
+          ),
+           'primary' => true,
+           'unique' => true,
+        )),
+      ),
+    )),
+  ),
+  1 => 
+  array (
+  ),
+); ?>
\ No newline at end of file

Modified: trunk/PersistentObject/tests/one_to_one_relation_test.php
===================================================================
--- trunk/PersistentObject/tests/one_to_one_relation_test.php   2007-05-07 
14:23:19 UTC (rev 5138)
+++ trunk/PersistentObject/tests/one_to_one_relation_test.php   2007-05-07 
21:05:11 UTC (rev 5139)
@@ -301,7 +301,6 @@
         ) );
 
         $this->session->addRelatedObject( $person, $birthday );
-        
         try
         {
             $this->session->update( $birthday );
@@ -310,7 +309,6 @@
         {
             // This exception is correct. The object is new and should not be 
updated.
         }
-        
         try
         {
             // The birthday record should not exist

Added: trunk/PersistentObject/tests/string_identifier_test.php
===================================================================
--- trunk/PersistentObject/tests/string_identifier_test.php     2007-05-07 
14:23:19 UTC (rev 5138)
+++ trunk/PersistentObject/tests/string_identifier_test.php     2007-05-07 
21:05:11 UTC (rev 5139)
@@ -0,0 +1,280 @@
+<?php
+/**
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @version //autogentag//
+ * @filesource
+ * @package PersistentObject
+ * @subpackage Tests
+ */
+ezcTestRunner::addFileToFilter( __FILE__ );
+
+require_once "data/string_identifier/main_table_class.php";
+require_once "data/string_identifier/rel_class.php";
+
+/**
+ * These tests check if persistent object works properly with string 
identifiers.
+ *
+ * @package PersistentObject
+ * @subpackage Tests
+ */
+class ezcPersistentStringIdentifierTest extends ezcTestCase
+{
+    private $session = null;
+
+    protected function setUp()
+    {
+        try
+        {
+            $db = ezcDbInstance::get();
+        }
+        catch ( Exception $e )
+        {
+            $this->markTestSkipped( 'There was no database configured' );
+        }
+
+        MainTable::setupTable();
+//        MainTable::saveSchema();
+        $this->session = new ezcPersistentSession( ezcDbInstance::get(),
+                                                   new 
ezcPersistentCodeManager( dirname( __FILE__ ) . "/data/string_identifier" ) );
+    }
+
+    protected function tearDown()
+    {
+        MainTable::cleanup();
+    }
+
+    public static function suite()
+    {
+        return new PHPUnit_Framework_TestSuite( 
'ezcPersistentStringIdentifierTest' );
+    }
+
+    // Test saving a valid object
+    public function testSave()
+    {
+        $object = new MainTable();
+        $object->id = "id";
+        $object->data = "42";
+        $this->session->save( $object );
+
+        $this->assertEquals( "id", $object->id );
+
+        $object2 = $this->session->loadIfExists( 'MainTable', "id" );
+        $this->assertNotEquals( NULL, $object2 );
+        $this->assertEquals( "42", $object2->data );
+    }
+
+    public function testUpdate()
+    {
+        $object = new MainTable();
+        $object->id = "id";
+        $object->data = "42";
+        $this->session->save( $object );
+
+        $this->assertEquals( "id", $object->id );
+
+        $object2 = $this->session->loadIfExists( 'MainTable', "id" );
+        $this->assertNotEquals( NULL, $object2 );
+        $this->assertEquals( "42", $object2->data );
+
+        $object2->data = "99";
+        $this->session->update( $object2 );
+
+        $object3 = $this->session->loadIfExists( 'MainTable', "id" );
+        $this->assertNotEquals( NULL, $object3 );
+        $this->assertEquals( "99", $object3->data );
+    }
+
+    public function testSaveOrUpdateSave()
+    {
+        $object = new MainTable();
+        $object->id = "id";
+        $object->data = "42";
+        $this->session->saveOrUpdate( $object );
+
+        $this->assertEquals( "id", $object->id );
+
+        $object2 = $this->session->loadIfExists( 'MainTable', "id" );
+        $this->assertNotEquals( NULL, $object2 );
+        $this->assertEquals( "42", $object2->data );
+    }
+
+    public function testSaveOrUpdateUpdate()
+    {
+        $object = new MainTable();
+        $object->id = "id";
+        $object->data = "42";
+        $this->session->save( $object );
+
+        $this->assertEquals( "id", $object->id );
+
+        $object2 = $this->session->loadIfExists( 'MainTable', "id" );
+        $this->assertNotEquals( NULL, $object2 );
+        $this->assertEquals( "42", $object2->data );
+
+        $object2->data = "99";
+        $this->session->saveOrUpdate( $object2 );
+
+        $object3 = $this->session->loadIfExists( 'MainTable', "id" );
+        $this->assertNotEquals( NULL, $object3 );
+        $this->assertEquals( "99", $object3->data );
+    }
+
+    public function testDelete()
+    {
+        $object = new MainTable();
+        $object->id = "id";
+        $object->data = "42";
+        $this->session->save( $object );
+
+        $this->assertEquals( "id", $object->id );
+
+        $object2 = $this->session->loadIfExists( 'MainTable', "id" );
+        $this->assertNotEquals( NULL, $object2 );
+        $this->assertEquals( "42", $object2->data );
+
+        $this->session->delete( $object2 );
+
+        $this->assertNull( $this->session->loadIfExists( 'MainTable', "id" ) );
+    }
+
+    public function testSaveAlias()
+    {
+/*        $object = new Sequence();
+        $object->trigger = "42";
+        $this->session->save( $object );
+
+        $this->assertEquals( 1, $object->column );
+
+        $object2 = $this->session->loadIfExists( 'Sequence', 1 );
+        $this->assertNotEquals( NULL, $object2 );
+        $this->assertEquals( "42", $object2->trigger );*/
+    }
+
+    public function test1NGetRelatedObject()
+    {
+        $object = new MainTable();
+        $object->id = "id";
+        $object->data = "42";
+        $this->session->save( $object );
+
+        $rel = new Rel1();
+        $rel->id = "rel_id";
+        $rel->fk = "id"; // correct relation
+        $this->session->save( $rel );
+
+        $relation = $this->session->getRelatedObjects( $object, "Rel1" );
+        $this->assertNotEquals( count( $relation ), 0 );
+    }
+
+    public function test1NGetRelatedObjects()
+    {
+        $object = new MainTable();
+        $object->id = "id";
+        $object->data = "42";
+        $this->session->save( $object );
+
+        $rel = new Rel1();
+        $rel->id = "rel_id";
+        $rel->fk = "id"; // correct relation
+        $this->session->save( $rel );
+
+        $relations = $this->session->getRelatedObjects( $object, "Rel1" );
+        $this->assertEquals( 1, count( $relations ) );
+    }
+
+    public function test1NAddAndRemoveRelatedObject()
+    {
+        $object = new MainTable();
+        $object->id = "id";
+        $object->data = "42";
+        $this->session->save( $object );
+
+        $rel = new Rel1();
+        $rel->id = "rel_id";
+        $rel->fk = "id"; // correct relation
+        $this->session->save( $rel );
+        // First let's remove the old relation
+        $this->session->removeRelatedObject( $object, $rel );
+
+        // Let's create a new object to relate to
+        $object2 = new MainTable();
+        $object2->id = "id2";
+        $object2->data = "99";
+        $this->session->save( $object2 ); // id 2
+
+        $this->session->addRelatedObject( $object2, $rel );
+        $this->session->update( $rel );
+
+        // test that it worked
+        $relations = $this->session->getRelatedObjects( $object, "Rel1" );
+        $this->assertEquals( 0, count( $relations ) );
+
+        $relations = $this->session->getRelatedObjects( $object2, "Rel1" );
+        $this->assertEquals( 1, count( $relations ) );
+    }
+
+    public function testNMAddRelatedObject()
+    {
+        $object = new MainTable();
+        $object->id = "id";
+        $object->data = "42";
+        $this->session->save( $object );
+
+        $rel = new Rel2();
+        $rel->id = "rel_id";
+        $this->session->save( $rel );
+
+        $this->session->addRelatedObject( $object, $rel );
+    }
+
+    public function testNMgetRelatedObject()
+    {
+        $object = new MainTable();
+        $object->id = "id";
+        $object->data = "42";
+        $this->session->save( $object );
+
+        $rel = new Rel2();
+        $rel->id = "rel_id";
+        $this->session->save( $rel );
+
+        $this->session->addRelatedObject( $object, $rel );
+        $this->assertNotEquals( count( $this->session->getRelatedObject( 
$object, "Rel2" ) ), 0 );
+    }
+
+    public function testNMgetRelatedObjects()
+    {
+        $object = new MainTable();
+        $object->id = "id";
+        $object->data = "42";
+        $this->session->save( $object );
+
+        $rel = new Rel2();
+        $rel->id = "rel_id";
+        $this->session->save( $rel );
+
+        $this->session->addRelatedObject( $object, $rel );
+        $this->assertEquals( 1, count( $this->session->getRelatedObjects( 
$object, "Rel2" ) ) );
+    }
+
+    public function testNMRemoveRelatedObject()
+    {
+        $object = new MainTable();
+        $object->id = "id";
+        $object->data = "42";
+        $this->session->save( $object );
+
+        $rel = new Rel2();
+        $rel->id = "rel_id";
+        $this->session->save( $rel );
+
+
+        $this->session->addRelatedObject( $object, $rel );
+        $this->session->removeRelatedObject( $object, $rel );
+
+        $this->assertEquals( 0, count( $this->session->getRelatedObjects( 
$object, "Rel2" ) ) );
+    }
+}
+
+?>


Property changes on: trunk/PersistentObject/tests/string_identifier_test.php
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: trunk/PersistentObject/tests/suite.php
===================================================================
--- trunk/PersistentObject/tests/suite.php      2007-05-07 14:23:19 UTC (rev 
5138)
+++ trunk/PersistentObject/tests/suite.php      2007-05-07 21:05:11 UTC (rev 
5139)
@@ -25,6 +25,7 @@
 require_once( 'one_to_one_relation_test.php' );
 require_once( 'many_to_many_relation.php' );
 require_once( 'keyword_test.php' );
+require_once( 'string_identifier_test.php' );
 
 /**
  * @package PersistentObject
@@ -50,6 +51,7 @@
         $this->addTest( ezcPersistentManyToOneRelationTest::suite() );
         $this->addTest( ezcPersistentManyToManyRelationTest::suite() );
         $this->addTest( ezcPersistentKeywordTest::suite() );
+        $this->addTest( ezcPersistentStringIdentifierTest::suite() );
     }
 
     public static function suite()

-- 
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to