Author: Tobias Schlitt
Date: 2006-09-29 22:01:39 +0200 (Fri, 29 Sep 2006)
New Revision: 3614

Log:
- Added implementation of first relation and getRelatedObjects() for it.

Added:
   trunk/PersistentObject/src/exceptions/relation_not_found.php
   trunk/PersistentObject/tests/data/relation.dba
   trunk/PersistentObject/tests/data/relation_test.php
   trunk/PersistentObject/tests/data/relation_test.sql
   trunk/PersistentObject/tests/data/relation_test_address.php
   trunk/PersistentObject/tests/data/relation_test_employer.php
   trunk/PersistentObject/tests/data/relation_test_person.php
   trunk/PersistentObject/tests/data/relationtestaddress.php
   trunk/PersistentObject/tests/data/relationtestemployer.php
   trunk/PersistentObject/tests/data/relationtestperson.php
Modified:
   trunk/PersistentObject/src/interfaces/definition_manager.php
   trunk/PersistentObject/src/persistent_autoload.php
   trunk/PersistentObject/src/persistent_session.php
   trunk/PersistentObject/src/structs/persistent_object_definition.php
   trunk/PersistentObject/tests/one_to_many_relation_test.php

Added: trunk/PersistentObject/src/exceptions/relation_not_found.php
===================================================================
--- trunk/PersistentObject/src/exceptions/relation_not_found.php        
2006-09-29 09:40:37 UTC (rev 3613)
+++ trunk/PersistentObject/src/exceptions/relation_not_found.php        
2006-09-29 20:01:39 UTC (rev 3614)
@@ -0,0 +1,34 @@
+<?php
+/**
+ * File containing the ezcPersistentObjectException class
+ *
+ * @package PersistentObject
+ * @version //autogen//
+ * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+/**
+ * General exception class for the PersistentObject package.
+ *
+ * All exceptions in the persistent object package are derived from this 
exception.
+ *
+ * @package PersistentObject
+ * @version //autogen//
+ */
+class ezcPersistentRelationNotFoundException extends 
ezcPersistentObjectException
+{
+
+    /**
+     * Constructs a new ezcPersistentRelationNotFoundException for the class 
$class
+     * which does not have a relation for $relatedClass.
+     *
+     * @param string $class
+     * @param string $relatedClass
+     * @return void
+     */
+    public function __construct( $class, $relatedClass )
+    {
+        parent::__construct( "Class <{$class}> does not have a relation to 
<{$relatedClass}>" );
+    }
+}
+?>


Property changes on: 
trunk/PersistentObject/src/exceptions/relation_not_found.php
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: trunk/PersistentObject/src/interfaces/definition_manager.php
===================================================================
--- trunk/PersistentObject/src/interfaces/definition_manager.php        
2006-09-29 09:40:37 UTC (rev 3613)
+++ trunk/PersistentObject/src/interfaces/definition_manager.php        
2006-09-29 20:01:39 UTC (rev 3614)
@@ -45,6 +45,10 @@
         {
             $def->columns[$field->columnName] = $field;
         }
+        if ( isset( $def->idProperty ) )
+        {
+            $def->columns[$def->idProperty->columnName] = 
$def->idProperty->propertyName;
+        }
         return $def;
     }
 }

Modified: trunk/PersistentObject/src/persistent_autoload.php
===================================================================
--- trunk/PersistentObject/src/persistent_autoload.php  2006-09-29 09:40:37 UTC 
(rev 3613)
+++ trunk/PersistentObject/src/persistent_autoload.php  2006-09-29 20:01:39 UTC 
(rev 3614)
@@ -21,6 +21,7 @@
     'ezcPersistentObjectException' => 
'PersistentObject/exceptions/persistent_object_exception.php',
     'ezcPersistentSessionNotFoundException' => 
'PersistentObject/exceptions/session_not_found.php',
     'ezcPersistentDefinitionNotFoundException' => 
'PersistentObject/exceptions/definition_not_found.php',
+    'ezcPersistentRelationNotFoundException' => 
'PersistentObject/exceptions/relation_not_found.php',
     'ezcPersistentObjectNotPersistentException' => 
'PersistentObject/exceptions/not_persistent.php',
     'ezcPersistentObjectAlreadyPersistentException' => 
'PersistentObject/exceptions/already_persistent.php',
     'ezcPersistentQueryException' => 
'PersistentObject/exceptions/query_exception.php',

Modified: trunk/PersistentObject/src/persistent_session.php
===================================================================
--- trunk/PersistentObject/src/persistent_session.php   2006-09-29 09:40:37 UTC 
(rev 3613)
+++ trunk/PersistentObject/src/persistent_session.php   2006-09-29 20:01:39 UTC 
(rev 3614)
@@ -293,6 +293,11 @@
      * If you are retrieving large result set, consider using findIterator()
      * instead.
      *
+     * Example:
+     * <code>
+     *
+     * </code>
+     *
      * @throws ezcPersistentDefinitionNotFoundException if there is no such 
persistent class.
      * @throws ezcPersistentQueryException if the find query failed
      * @param ezcQuerySelect $query
@@ -325,6 +330,52 @@
         return $result;
     }
 
+    /**
+     * Returns the related objects of a given class for an object. 
+     * This method returns the related objects of type $relatedClass for the
+     * object $object.
+     *
+     * Example:
+     * <code>
+     * $person = $session->load( "Person", 1 );
+     * $relatedAddresses = $session->getRelatedObjects( $person, "Address" );
+     * </code>
+     * 
+     * @param mixed $object         The object to fetch related objects for. 
+     * @param mixed $relatedClass   The class of the related objects to fetch.
+     * @return array(int=>$relatedClass)
+     */
+    public function getRelatedObjects( $object, $relatedClass )
+    {
+        $def = $this->definitionManager->fetchDefinition( ( $class = 
get_class( $object ) ) );
+        if ( !isset( $def->relations[$relatedClass] ) )
+        {
+            throw new ezcPersistentRelationNotFoundException( $class, 
$relatedClass );
+        }
+        $relation = $def->relations[$relatedClass];
+        
+        $query = $this->createFindQuery( $relatedClass );
+
+        // @todo: Implement other relations!
+        switch ( get_class( $relation ) )
+        {
+            case "ezcPersistentOneToManyRelation":
+                foreach ( $relation->columnMap as $map )
+                {
+                    $query->where(
+                        $query->expr->eq(
+                            $this->database->quoteIdentifier( 
"{$map->destinationColumn}" ),
+                            $query->bindValue( 
$object->{$def->columns[$map->sourceColumn]} )
+                        )
+                    );
+                }
+                break;
+            default:
+                throw new Exception( "Still in development phase, " . 
get_class( $relation ) . " not implemented, yet." );
+        }
+        return $this->find( $query, $relatedClass );
+    }
+
     /*
      * Returns the result of the query $query as an object iterator.
      *

Modified: trunk/PersistentObject/src/structs/persistent_object_definition.php
===================================================================
--- trunk/PersistentObject/src/structs/persistent_object_definition.php 
2006-09-29 09:40:37 UTC (rev 3613)
+++ trunk/PersistentObject/src/structs/persistent_object_definition.php 
2006-09-29 20:01:39 UTC (rev 3614)
@@ -58,6 +58,15 @@
     public $columns = array();
 
     /**
+     * Contains the relations of this object. An array indexed by class names
+     * of the related object, assigned to a instance of a class derived from
+     * ezcPersistentRelation.
+     * 
+     * @var array(string=>ezcPersistentRelation)
+     */
+    public $relations = array();
+
+    /**
      * Constructs a new PersistentObjectDefinition.
      *
      * The parameters $key and $incrementKey are not used any more and will be 
removed
@@ -69,11 +78,13 @@
                                  $key = '',
                                  $class = '',
                                  $incrementKey = '',
-                                 array $properties = array() )
+                                 array $properties = array(),
+                                 array $relations = array() )
     {
         $this->table = $table;
         $this->class = $class;
         $this->properties = $properties;
+        $this->relations = $relations;
     }
 
     /**
@@ -95,7 +106,8 @@
                                                   $array['primaryKey'],
                                                   $array['class'],
                                                   $array['incrementKey'],
-                                                  $array['properties'] );
+                                                  $array['properties'],
+                                                  $array['relations'] );
     }
 }
 ?>

Added: trunk/PersistentObject/tests/data/relation.dba
===================================================================
--- trunk/PersistentObject/tests/data/relation.dba      2006-09-29 09:40:37 UTC 
(rev 3613)
+++ trunk/PersistentObject/tests/data/relation.dba      2006-09-29 20:01:39 UTC 
(rev 3614)
@@ -0,0 +1,213 @@
+<?php return array (
+  0 => 
+  array (
+    'PO_addresses' => 
+    ezcDbSchemaTable::__set_state(array(
+       'fields' => 
+      array (
+        'city' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'text',
+           'length' => 100,
+           'notNull' => true,
+           'default' => NULL,
+           'autoIncrement' => false,
+           'unsigned' => false,
+        )),
+        'id' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'integer',
+           'length' => 0,
+           'notNull' => true,
+           'default' => NULL,
+           'autoIncrement' => true,
+           'unsigned' => false,
+        )),
+        'street' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'text',
+           'length' => 100,
+           'notNull' => true,
+           'default' => NULL,
+           'autoIncrement' => false,
+           'unsigned' => false,
+        )),
+        'type' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'text',
+           'length' => 10,
+           'notNull' => true,
+           'default' => NULL,
+           'autoIncrement' => false,
+           'unsigned' => false,
+        )),
+        'zip' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'text',
+           'length' => 5,
+           '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,
+        )),
+      ),
+    )),
+    'PO_employers' => 
+    ezcDbSchemaTable::__set_state(array(
+       'fields' => 
+      array (
+        'id' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'integer',
+           'length' => 0,
+           'notNull' => true,
+           'default' => NULL,
+           'autoIncrement' => true,
+           'unsigned' => false,
+        )),
+        'name' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'text',
+           'length' => 100,
+           '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,
+        )),
+      ),
+    )),
+    'PO_persons' => 
+    ezcDbSchemaTable::__set_state(array(
+       'fields' => 
+      array (
+        'employer' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'integer',
+           'length' => 0,
+           'notNull' => true,
+           'default' => NULL,
+           'autoIncrement' => false,
+           'unsigned' => false,
+        )),
+        'firstname' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'text',
+           'length' => 100,
+           'notNull' => true,
+           'default' => NULL,
+           'autoIncrement' => false,
+           'unsigned' => false,
+        )),
+        'id' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'integer',
+           'length' => 0,
+           'notNull' => true,
+           'default' => NULL,
+           'autoIncrement' => true,
+           'unsigned' => false,
+        )),
+        'surename' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'text',
+           'length' => 100,
+           '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,
+        )),
+      ),
+    )),
+    'PO_persons_addresses' => 
+    ezcDbSchemaTable::__set_state(array(
+       'fields' => 
+      array (
+        'address_id' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'integer',
+           'length' => 0,
+           'notNull' => true,
+           'default' => NULL,
+           'autoIncrement' => false,
+           'unsigned' => false,
+        )),
+        'person_id' => 
+        ezcDbSchemaField::__set_state(array(
+           'type' => 'integer',
+           'length' => 0,
+           'notNull' => true,
+           'default' => NULL,
+           'autoIncrement' => false,
+           'unsigned' => false,
+        )),
+      ),
+       'indexes' => 
+      array (
+        'primary' => 
+        ezcDbSchemaIndex::__set_state(array(
+           'indexFields' => 
+          array (
+            'address_id' => 
+            ezcDbSchemaIndexField::__set_state(array(
+               'sorting' => NULL,
+            )),
+            'person_id' => 
+            ezcDbSchemaIndexField::__set_state(array(
+               'sorting' => NULL,
+            )),
+          ),
+           'primary' => true,
+           'unique' => true,
+        )),
+      ),
+    )),
+  ),
+  1 => 
+  array (
+  ),
+); ?>
\ No newline at end of file


Property changes on: trunk/PersistentObject/tests/data/relation.dba
___________________________________________________________________
Name: svn:eol-style
   + native

Added: trunk/PersistentObject/tests/data/relation_test.php
===================================================================
--- trunk/PersistentObject/tests/data/relation_test.php 2006-09-29 09:40:37 UTC 
(rev 3613)
+++ trunk/PersistentObject/tests/data/relation_test.php 2006-09-29 20:01:39 UTC 
(rev 3614)
@@ -0,0 +1,101 @@
+<?php
+
+ezcTestRunner::addFileToFilter( __FILE__ );
+
+/*
+CREATE TABLE `PO_addresses` (
+  `id` tinyint(3) unsigned NOT NULL auto_increment,
+  `street` varchar(100) NOT NULL,
+  `zip` varchar(5) NOT NULL,
+  `city` varchar(100) NOT NULL,
+  `type` varchar(10) NOT NULL,
+  PRIMARY KEY  (`id`)
+) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
+
+CREATE TABLE `PO_employers` (
+  `id` tinyint(4) NOT NULL auto_increment,
+  `name` varchar(100) NOT NULL,
+  PRIMARY KEY  (`id`)
+) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
+
+CREATE TABLE `PO_persons` (
+  `id` tinyint(4) NOT NULL auto_increment,
+  `firstname` varchar(100) NOT NULL,
+  `surename` varchar(100) NOT NULL,
+  `employer` tinyint(4) NOT NULL,
+  PRIMARY KEY  (`id`)
+) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
+
+CREATE TABLE `PO_persons_addresses` (
+  `person_id` tinyint(4) NOT NULL,
+  `address_id` tinyint(4) NOT NULL,
+  PRIMARY KEY  (`person_id`,`address_id`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8;
+*/
+
+class RelationTest
+{
+    /**
+     * Insert data for the test
+     */
+    public static function insertData()
+    {
+        $db = ezcDbInstance::get();
+
+        $db->exec( "INSERT INTO `PO_addresses` (`street`, `zip`, `city`, 
`type`) VALUES ('Httproad 23', '12345', 'Internettown', 'work');" );
+        $db->exec( "INSERT INTO `PO_addresses` (`street`, `zip`, `city`, 
`type`) VALUES ('Ftpstreet 42', '12345', 'Internettown', 'work');" );
+        $db->exec( "INSERT INTO `PO_addresses` (`street`, `zip`, `city`, 
`type`) VALUES ('Phpavenue 21', '12345', 'Internettown', 'private');" );
+        $db->exec( "INSERT INTO `PO_addresses` (`street`, `zip`, `city`, 
`type`) VALUES ('Pythonstreet 13', '12345', 'Internettown', 'private');" );
+
+        $db->exec( "INSERT INTO `PO_employers` (`name`) VALUES ('Great Web 2.0 
company');" );
+        $db->exec( "INSERT INTO `PO_employers` (`name`) VALUES ('Oldschool Web 
1.x company');" );
+
+        $db->exec( "INSERT INTO `PO_persons` (`firstname`, `surename`, 
`employer`) VALUES ('Theodor', 'Gopher', 2);" );
+        $db->exec( "INSERT INTO `PO_persons` (`firstname`, `surename`, 
`employer`) VALUES ('Frederick', 'Ajax', 1);" );
+        $db->exec( "INSERT INTO `PO_persons` (`firstname`, `surename`, 
`employer`) VALUES ('Raymond', 'Socialweb', 1);" );
+
+        $db->exec( "INSERT INTO `PO_persons_addresses` ( `person_id`, 
`address_id`) VALUES ( 1, 1);" );
+        $db->exec( "INSERT INTO `PO_persons_addresses` ( `person_id`, 
`address_id`) VALUES ( 1, 2);" );
+        $db->exec( "INSERT INTO `PO_persons_addresses` ( `person_id`, 
`address_id`) VALUES ( 1, 4);" );
+        $db->exec( "INSERT INTO `PO_persons_addresses` ( `person_id`, 
`address_id`) VALUES ( 2, 1);" );
+        $db->exec( "INSERT INTO `PO_persons_addresses` ( `person_id`, 
`address_id`) VALUES ( 2, 3);" );
+        $db->exec( "INSERT INTO `PO_persons_addresses` ( `person_id`, 
`address_id`) VALUES ( 2, 4);" );
+        $db->exec( "INSERT INTO `PO_persons_addresses` ( `person_id`, 
`address_id`) VALUES ( 3, 4);" );
+    }
+
+    /**
+     * 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 function saveSchema()
+    {
+        $db = ezcDbInstance::get();
+        $schema = ezcDbSchema::createFromDb( $db );
+        $schema->writeToFile( "array", dirname( __FILE__ ) . "/relation.dba" );
+    }
+
+    /**
+     * Loads the schema from file into the database.
+     */
+    public static function setupTables()
+    {
+        $db = ezcDbInstance::get();
+        $schema = ezcDbSchema::createFromFile( "array", dirname( __FILE__ ) . 
"/relation.dba" );
+        $schema->writeToDb( $db );
+    }
+
+    public static function cleanup()
+    {
+        $db = ezcDbInstance::get();
+        $db->exec( "DROP TABLE PO_addresses" );
+        $db->exec( "DROP TABLE PO_employers" );
+        $db->exec( "DROP TABLE PO_persons" );
+        $db->exec( "DROP TABLE PO_persons_addresses" );
+    }
+
+}
+
+
+?>


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

Added: trunk/PersistentObject/tests/data/relation_test.sql
===================================================================
--- trunk/PersistentObject/tests/data/relation_test.sql 2006-09-29 09:40:37 UTC 
(rev 3613)
+++ trunk/PersistentObject/tests/data/relation_test.sql 2006-09-29 20:01:39 UTC 
(rev 3614)
@@ -0,0 +1,99 @@
+-- phpMyAdmin SQL Dump
+-- version 2.6.3-rc1
+-- http://www.phpmyadmin.net
+-- 
+-- Host: localhost
+-- Generation Time: Sep 29, 2006 at 07:21 PM
+-- Server version: 5.0.24
+-- PHP Version: 5.1.6-pl4-gentoo
+-- 
+-- Database: `test`
+-- 
+
+-- --------------------------------------------------------
+
+-- 
+-- Table structure for table `PO_addresses`
+-- 
+
+CREATE TABLE `PO_addresses` (
+  `id` tinyint(3) unsigned NOT NULL auto_increment,
+  `street` varchar(100) NOT NULL,
+  `zip` varchar(5) NOT NULL,
+  `city` varchar(100) NOT NULL,
+  `type` varchar(10) NOT NULL,
+  PRIMARY KEY  (`id`)
+) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
+
+-- 
+-- Dumping data for table `PO_addresses`
+-- 
+
+INSERT INTO `PO_addresses` (`street`, `zip`, `city`, `type`) VALUES ('Httproad 
23', '12345', 'Internettown', 'work');
+INSERT INTO `PO_addresses` (`street`, `zip`, `city`, `type`) VALUES 
('Ftpstreet 42', '12345', 'Internettown', 'work');
+INSERT INTO `PO_addresses` (`street`, `zip`, `city`, `type`) VALUES 
('Phpavenue 21', '12345', 'Internettown', 'private');
+INSERT INTO `PO_addresses` (`street`, `zip`, `city`, `type`) VALUES 
('Pythonstreet 13', '12345', 'Internettown', 'private');
+
+-- --------------------------------------------------------
+
+-- 
+-- Table structure for table `PO_employers`
+-- 
+
+CREATE TABLE `PO_employers` (
+  `id` tinyint(4) NOT NULL auto_increment,
+  `name` varchar(100) NOT NULL,
+  PRIMARY KEY  (`id`)
+) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
+
+-- 
+-- Dumping data for table `PO_employers`
+-- 
+
+INSERT INTO `PO_employers` (`name`) VALUES ('Great Web 2.0 company');
+INSERT INTO `PO_employers` (`name`) VALUES ('Oldschool Web 1.x company');
+
+-- --------------------------------------------------------
+
+-- 
+-- Table structure for table `PO_persons`
+-- 
+
+CREATE TABLE `PO_persons` (
+  `id` tinyint(4) NOT NULL auto_increment,
+  `firstname` varchar(100) NOT NULL,
+  `surename` varchar(100) NOT NULL,
+  `employer` tinyint(4) NOT NULL,
+  PRIMARY KEY  (`id`)
+) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
+
+-- 
+-- Dumping data for table `PO_persons`
+-- 
+
+INSERT INTO `PO_persons` (`firstname`, `surename`, `employer`) VALUES 
('Derick', 'Gopher', 2);
+INSERT INTO `PO_persons` (`firstname`, `surename`, `employer`) VALUES 
('Frederick', 'Ajax', 1);
+INSERT INTO `PO_persons` (`firstname`, `surename`, `employer`) VALUES 
('Raymond', 'Socialweb', 1);
+
+-- --------------------------------------------------------
+
+-- 
+-- Table structure for table `PO_persons_addresses`
+-- 
+
+CREATE TABLE `PO_persons_addresses` (
+  `person_id` tinyint(4) NOT NULL,
+  `address_id` tinyint(4) NOT NULL,
+  PRIMARY KEY  (`person_id`,`address_id`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8;
+
+-- 
+-- Dumping data for table `PO_persons_addresses`
+-- 
+
+INSERT INTO `PO_persons_addresses` (`person_id`, `address_id`) VALUES (1, 1);
+INSERT INTO `PO_persons_addresses` (`person_id`, `address_id`) VALUES (1, 2);
+INSERT INTO `PO_persons_addresses` (`person_id`, `address_id`) VALUES (1, 4);
+INSERT INTO `PO_persons_addresses` (`person_id`, `address_id`) VALUES (2, 1);
+INSERT INTO `PO_persons_addresses` (`person_id`, `address_id`) VALUES (2, 3);
+INSERT INTO `PO_persons_addresses` (`person_id`, `address_id`) VALUES (2, 4);


Property changes on: trunk/PersistentObject/tests/data/relation_test.sql
___________________________________________________________________
Name: svn:eol-style
   + native

Added: trunk/PersistentObject/tests/data/relation_test_address.php
===================================================================
--- trunk/PersistentObject/tests/data/relation_test_address.php 2006-09-29 
09:40:37 UTC (rev 3613)
+++ trunk/PersistentObject/tests/data/relation_test_address.php 2006-09-29 
20:01:39 UTC (rev 3614)
@@ -0,0 +1,36 @@
+<?php
+
+ezcTestRunner::addFileToFilter( __FILE__ );
+
+require_once dirname( __FILE__ ) . "/relation_test.php";
+
+class RelationTestAddress extends RelationTest
+{
+    public $id      = null;
+    public $street  = null;
+    public $zip     = null;
+    public $city    = null;
+    public $type    = null;
+
+
+    public function setState( array $state )
+    {
+        foreach ( $state as $key => $value )
+        {
+            $this->$key = $value;
+        }
+    }
+
+    public function getState()
+    {
+        return array(
+            "id"        => $this->id,
+            "street"    => $this->street,
+            "zip"       => $this->zip,
+            "city"      => $this->city,
+            "type"      => $this->type,
+        );
+    }
+}
+
+?>


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

Added: trunk/PersistentObject/tests/data/relation_test_employer.php
===================================================================
--- trunk/PersistentObject/tests/data/relation_test_employer.php        
2006-09-29 09:40:37 UTC (rev 3613)
+++ trunk/PersistentObject/tests/data/relation_test_employer.php        
2006-09-29 20:01:39 UTC (rev 3614)
@@ -0,0 +1,29 @@
+<?php
+
+ezcTestRunner::addFileToFilter( __FILE__ );
+
+require_once dirname( __FILE__ ) . "/relation_test.php";
+
+class RelationTestEmployer extends RelationTest
+{
+    public $id      = null;
+    public $name    = null;
+
+    public function setState( array $state )
+    {
+        foreach ( $state as $key => $value )
+        {
+            $this->$key = $value;
+        }
+    }
+
+    public function getState()
+    {
+        return array(
+            "id"   => $this->id,
+            "name" => $this->firstname,
+        );
+    }
+}
+
+?>


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

Added: trunk/PersistentObject/tests/data/relation_test_person.php
===================================================================
--- trunk/PersistentObject/tests/data/relation_test_person.php  2006-09-29 
09:40:37 UTC (rev 3613)
+++ trunk/PersistentObject/tests/data/relation_test_person.php  2006-09-29 
20:01:39 UTC (rev 3614)
@@ -0,0 +1,44 @@
+<?php
+
+ezcTestRunner::addFileToFilter( __FILE__ );
+
+require_once dirname( __FILE__ ) . "/relation_test.php";
+
+class RelationTestPerson extends RelationTest
+{
+    public $id          = null;
+    public $firstname   = null;
+    public $surename    = null;
+    public $employer    = null;
+
+
+    public function setState( array $state )
+    {
+        foreach ( $state as $key => $value )
+        {
+            $this->$key = $value;
+        }
+    }
+
+    public function getState()
+    {
+        return array(
+            "id"            => $this->id,
+            "firstname"     => $this->firstname,
+            "surename"      => $this->surename,
+            "employer"      => $this->employer,
+        );
+    }
+
+    public static function __set_state( array $state  )
+    {
+        $person = new RelationTestPerson();
+        foreach ( $state as $key => $value )
+        {
+            $person->$key = $value;
+        }
+        return $person;
+    }
+}
+
+?>


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

Added: trunk/PersistentObject/tests/data/relationtestaddress.php
===================================================================
--- trunk/PersistentObject/tests/data/relationtestaddress.php   2006-09-29 
09:40:37 UTC (rev 3613)
+++ trunk/PersistentObject/tests/data/relationtestaddress.php   2006-09-29 
20:01:39 UTC (rev 3614)
@@ -0,0 +1,35 @@
+<?php
+ezcTestRunner::addFileToFilter( __FILE__ );
+
+$def = new ezcPersistentObjectDefinition();
+$def->table = "PO_addresses";
+$def->class = "RelationTestAddress";
+
+$def->idProperty                = new ezcPersistentObjectIdProperty;
+$def->idProperty->columnName    = 'id';
+$def->idProperty->propertyName  = 'id';
+$def->idProperty->generator     = new ezcPersistentGeneratorDefinition( 
'ezcPersistentSequenceGenerator' );
+
+$def->properties['street']                 = new ezcPersistentObjectProperty;
+$def->properties['street']->columnName     = 'street';
+$def->properties['street']->propertyName   = 'street';
+$def->properties['street']->propertyType   = 
ezcPersistentObjectProperty::PHP_TYPE_STRING;
+
+$def->properties['zip']                 = new ezcPersistentObjectProperty;
+$def->properties['zip']->columnName     = 'zip';
+$def->properties['zip']->propertyName   = 'zip';
+$def->properties['zip']->propertyType   = 
ezcPersistentObjectProperty::PHP_TYPE_STRING;
+
+$def->properties['city']                 = new ezcPersistentObjectProperty;
+$def->properties['city']->columnName     = 'city';
+$def->properties['city']->propertyName   = 'city';
+$def->properties['city']->propertyType   = 
ezcPersistentObjectProperty::PHP_TYPE_STRING;
+
+$def->properties['type']                 = new ezcPersistentObjectProperty;
+$def->properties['type']->columnName     = 'type';
+$def->properties['type']->propertyName   = 'type';
+$def->properties['type']->propertyType   = 
ezcPersistentObjectProperty::PHP_TYPE_STRING;
+
+return $def;
+
+?>


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

Added: trunk/PersistentObject/tests/data/relationtestemployer.php
===================================================================
--- trunk/PersistentObject/tests/data/relationtestemployer.php  2006-09-29 
09:40:37 UTC (rev 3613)
+++ trunk/PersistentObject/tests/data/relationtestemployer.php  2006-09-29 
20:01:39 UTC (rev 3614)
@@ -0,0 +1,25 @@
+<?php
+ezcTestRunner::addFileToFilter( __FILE__ );
+
+$def = new ezcPersistentObjectDefinition();
+$def->table = "PO_employers";
+$def->class = "RelationTestEmployer";
+
+$def->idProperty                = new ezcPersistentObjectIdProperty;
+$def->idProperty->columnName    = 'id';
+$def->idProperty->propertyName  = 'id';
+$def->idProperty->generator     = new ezcPersistentGeneratorDefinition( 
'ezcPersistentSequenceGenerator' );
+
+$def->properties['name']                 = new ezcPersistentObjectProperty;
+$def->properties['name']->columnName     = 'name';
+$def->properties['name']->propertyName   = 'name';
+$def->properties['name']->propertyType   = 
ezcPersistentObjectProperty::PHP_TYPE_STRING;
+
+$def->relations["RelationTestPerson"]                = new 
ezcPersistentOneToManyRelation( "employers", "persons" );
+$def->relations["RelationTestPerson"]->columnMap     = array(
+    new ezcPersistentSingleTableMap( "id", "employer" ),
+);
+
+return $def;
+
+?>


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

Added: trunk/PersistentObject/tests/data/relationtestperson.php
===================================================================
--- trunk/PersistentObject/tests/data/relationtestperson.php    2006-09-29 
09:40:37 UTC (rev 3613)
+++ trunk/PersistentObject/tests/data/relationtestperson.php    2006-09-29 
20:01:39 UTC (rev 3614)
@@ -0,0 +1,30 @@
+<?php
+ezcTestRunner::addFileToFilter( __FILE__ );
+
+$def = new ezcPersistentObjectDefinition();
+$def->table = "PO_persons";
+$def->class = "RelationTestPerson";
+
+$def->idProperty                = new ezcPersistentObjectIdProperty;
+$def->idProperty->columnName    = 'id';
+$def->idProperty->propertyName  = 'id';
+$def->idProperty->generator     = new ezcPersistentGeneratorDefinition( 
'ezcPersistentSequenceGenerator' );
+
+$def->properties['firstname']                 = new 
ezcPersistentObjectProperty;
+$def->properties['firstname']->columnName     = 'firstname';
+$def->properties['firstname']->propertyName   = 'firstname';
+$def->properties['firstname']->propertyType   = 
ezcPersistentObjectProperty::PHP_TYPE_STRING;
+
+$def->properties['surename']                 = new ezcPersistentObjectProperty;
+$def->properties['surename']->columnName     = 'surename';
+$def->properties['surename']->propertyName   = 'surename';
+$def->properties['surename']->propertyType   = 
ezcPersistentObjectProperty::PHP_TYPE_STRING;
+
+$def->properties['employer']                 = new ezcPersistentObjectProperty;
+$def->properties['employer']->columnName     = 'employer';
+$def->properties['employer']->propertyName   = 'employer';
+$def->properties['employer']->propertyType   = 
ezcPersistentObjectProperty::PHP_TYPE_INT;
+
+return $def;
+
+?>


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

Modified: trunk/PersistentObject/tests/one_to_many_relation_test.php
===================================================================
--- trunk/PersistentObject/tests/one_to_many_relation_test.php  2006-09-29 
09:40:37 UTC (rev 3613)
+++ trunk/PersistentObject/tests/one_to_many_relation_test.php  2006-09-29 
20:01:39 UTC (rev 3614)
@@ -8,6 +8,9 @@
  * @subpackage Tests
  */
 
+require_once dirname( __FILE__ ) . "/data/relation_test_employer.php";
+require_once dirname( __FILE__ ) . "/data/relation_test_person.php";
+
 /**
  * Tests ezcPersistentOneToManyRelation class.
  *
@@ -16,244 +19,75 @@
  */
 class ezcPersistentOneToManyRelationTest extends ezcTestCase
 {
+
+    private $session;
+
     public static function suite()
     {
         return new ezcTestSuite( "ezcPersistentOneToManyRelationTest" );
     }
 
-    public function testConstructoreSuccess()
+    public function setup()
     {
-        $relation = new ezcPersistentOneToManyRelation( "persons", "addresses" 
);
-        $this->assertEquals(
-            "persons",
-            $relation->sourceTable
+        RelationTestEmployer::setupTables();
+        RelationTestEmployer::insertData();
+        $this->session = new ezcPersistentSession(
+            ezcDbInstance::get(),
+            new ezcPersistentCodeManager( dirname( __FILE__ ) . "/data/" )
         );
-        $this->assertEquals(
-            "addresses",
-            $relation->destinationTable
-        );
-        $this->assertEquals(
-            false,
-            $relation->reverse
-        );
-        $this->assertEquals(
-            array(),
-            $relation->columnMap
-        );
-        $this->assertEquals(
-            false,
-            $relation->cascade
-        );
     }
 
-    public function testConstructoreFailureInvalidSourceTable()
+    public function teardown()
     {
-        try
-        {
-            $relation = new ezcPersistentOneToManyRelation( array(), 
"addresses" );
-        }
-        catch ( ezcBaseValueException $e )
-        {
-            return;
-        }
-        $this->fail( "Exception not thrown on invalid value for 
ezcPersistentOneToManyRelation->sourceTable in ctor." );
+        RelationTestEmployer::cleanup();
     }
 
-    public function testConstructoreFailureInvalidDestinationTable()
+    public function testGetRelatedObjectsEmployer1()
     {
-        try
-        {
-            $relation = new ezcPersistentOneToManyRelation( "persons", array() 
);
-        }
-        catch ( ezcBaseValueException $e )
-        {
-            return;
-        }
-        $this->fail( "Exception not thrown on invalid value for 
ezcPersistentOneToManyRelation->destinationTable in ctor." );
-    }
+        $employer = $this->session->load( "RelationTestEmployer", 1 );
 
-    public function testGetAccessSuccess()
-    {
-        $relation = new ezcPersistentOneToManyRelation( "persons", "addresses" 
);
-        $relation->sourceTable = "employers";
-        $relation->destinationTable = "employees";
-        /* Skipped here, is teste dedicatedly
-         * $relation->columnMap = array( ... );
-         */
-        $relation->reverse = true;
-        $relation->cascade = true;
+        $res = array(
+          0 => 
+          RelationTestPerson::__set_state(array(
+             'id' => '2',
+             'firstname' => 'Frederick',
+             'surename' => 'Ajax',
+             'employer' => '1',
+          )),
+          1 => 
+          RelationTestPerson::__set_state(array(
+             'id' => '3',
+             'firstname' => 'Raymond',
+             'surename' => 'Socialweb',
+             'employer' => '1',
+          )),
+        );
 
         $this->assertEquals(
-            "employers",
-            $relation->sourceTable
+            $res,
+            $this->session->getRelatedObjects( $employer, "RelationTestPerson" 
),
+            "Related RelationTestPerson objects not fetched correctly."
         );
-        $this->assertEquals(
-            "employees",
-            $relation->destinationTable
-        );
-        $this->assertEquals(
-            array(),
-            $relation->columnMap
-        );
-        $this->assertEquals(
-            true,
-            $relation->reverse
-        );
-        $this->assertEquals(
-            true,
-            $relation->cascade
-        );
     }
 
-    public function testGetAccessFailureNonExistents()
+    public function testGetRelatedObjectsEmployer2()
     {
-        $relation = new ezcPersistentOneToManyRelation( "persons", "addresses" 
);
-        try
-        {
-            $foo = $relation->nonExistent;
-        }
-        catch ( ezcBasePropertyNotFoundException $e )
-        {
-            return;
-        }
-        $this->fail( "Exception not thrown on access of non existent 
property." );
-    }
-
-    public function testSetAccessSuccess()
-    {
-        $relation = new ezcPersistentOneToManyRelation( "persons", "addresses" 
);
-        $relation->sourceTable = "employers";
-        $relation->destinationTable = "employees";
-        $relation->columnMap = array(
-            new ezcPersistentSingleTableMap( "id", "person_id" ),
+        $employer = $this->session->load( "RelationTestEmployer", 2 );
+        $res = array(
+            0 => 
+            RelationTestPerson::__set_state(array(
+                'id' => '1',
+                'firstname' => 'Theodor',
+                'surename' => 'Gopher',
+                'employer' => '2',
+            )),
         );
-        $relation->reverse = true;
-        $relation->cascade = true;
-
         $this->assertEquals(
-            "employers",
-            $relation->sourceTable
+            $res,
+            $this->session->getRelatedObjects( $employer, "RelationTestPerson" 
),
+            "Related RelationTestPerson objects not fetched correctly."
         );
-        $this->assertEquals(
-            "employees",
-            $relation->destinationTable
-        );
-        $this->assertEquals(
-            array(
-                new ezcPersistentSingleTableMap( "id", "person_id" ),
-            ),
-            $relation->columnMap
-        );
-        $this->assertEquals(
-            true,
-            $relation->reverse
-        );
-        $this->assertEquals(
-            true,
-            $relation->cascade
-        );
     }
-
-    public function testSetAccessFailureSourceTable()
-    {
-        $relation = new ezcPersistentOneToManyRelation( "persons", "addresses" 
);
-        try
-        {
-            $relation->sourceTable = array();
-        }
-        catch ( ezcBaseValueException $e )
-        {
-            return;
-        }
-        $this->fail( "Exception not thrown on invalid value for 
ezcPersistentOneToManyRelation->sourceTable." );
-    }
-
-    public function testSetAccessFailureDestinationTable()
-    {
-        $relation = new ezcPersistentOneToManyRelation( "persons", "addresses" 
);
-        try
-        {
-            $relation->destinationTable = array();
-        }
-        catch ( ezcBaseValueException $e )
-        {
-            return;
-        }
-        $this->fail( "Exception not thrown on invalid value for 
ezcPersistentOneToManyRelation->destinationTable." );
-    }
-    
-    public function testSetAccessFailureReverse()
-    {
-        $relation = new ezcPersistentOneToManyRelation( "persons", "addresses" 
);
-        try
-        {
-            $relation->reverse = array();
-        }
-        catch ( ezcBaseValueException $e )
-        {
-            return;
-        }
-        $this->fail( "Exception not thrown on invalid value for 
ezcPersistentOneToManyRelation->reverse." );
-    }
-    
-    public function testSetAccessFailureCascade()
-    {
-        $relation = new ezcPersistentOneToManyRelation( "persons", "addresses" 
);
-        try
-        {
-            $relation->cascade = array();
-        }
-        catch ( ezcBaseValueException $e )
-        {
-            return;
-        }
-        $this->fail( "Exception not thrown on invalid value for 
ezcPersistentOneToManyRelation->cascade." );
-    }
-    
-    public function testSetAccessFailureColumnMapInvalidType()
-    {
-        $relation = new ezcPersistentOneToManyRelation( "persons", "addresses" 
);
-        try
-        {
-            $relation->columnMap = array( "I am not an object" );
-        }
-        catch ( ezcBaseValueException $e )
-        {
-            return;
-        }
-        $this->fail( "Exception not thrown on invalid value for 
ezcPersistentOneToManyRelation->columnMap." );
-    }
-    
-    public function testSetAccessFailureColumnMapInvalidArraySize()
-    {
-        $relation = new ezcPersistentOneToManyRelation( "persons", "addresses" 
);
-        try
-        {
-            $relation->columnMap = array();
-        }
-        catch ( ezcBaseValueException $e )
-        {
-            return;
-        }
-        $this->fail( "Exception not thrown on invalid value for 
ezcPersistentOneToManyRelation->columnMap." );
-    }
-    
-    public function testSetAccessFailureColumnMapInvalidClass()
-    {
-        $relation = new ezcPersistentOneToManyRelation( "persons", "addresses" 
);
-        try
-        {
-            $relation->columnMap = array(
-                new StdClass(),
-                new StdClass(),
-            );
-        }
-        catch ( ezcBaseValueException $e )
-        {
-            return;
-        }
-        $this->fail( "Exception not thrown on invalid value for 
ezcPersistentOneToManyRelation->columnMap." );
-    }
 }
 
 ?>

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

Reply via email to