Changeset 951 for trunk

Show
Ignore:
Timestamp:
10/01/07 23:39:52 (1 year ago)
Author:
mikey
Message:

finally got rid of the stubPersistable interface, persistence api now works completely annotation-based

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/experiments/people/mikey/persistence/MyNewsArticle.php

    r881 r951  
    33 * class to test the persistence api 
    44 *  
     5 * @Entity 
    56 * @DBTable(name='news', type='InnoDB') 
    67 */ 
    7 class MyNewsArticle extends stubAbstractPersistable 
     8class MyNewsArticle 
    89{ 
    910    /** 
     11     * id of the news article 
     12     * 
     13     * @var  int 
     14     */ 
     15    protected $id; 
     16    /** 
    1017     * headline of the news article 
    1118     * 
     
    5158 
    5259    /** 
    53      * returns the id of the news 
    54      * 
    55      * @return  int 
    56      * @DBColumn(name='news_id', type='int', size=10, isUnsigned=true, isPrimaryKey=true) 
     60     * sets the id of the news article 
     61     * 
     62     * @param  int  $id 
     63     */ 
     64    public function setId($id) 
     65    { 
     66        $this->id = $id; 
     67    } 
     68 
     69    /** 
     70     * returns the id of the news article 
     71     * 
     72     * @return  int 
     73     * @Id 
     74     * @DBColumn(name='news_id', type='int', size=10, isUnsigned=true) 
    5775     */ 
    5876    public function getId() 
     
    206224    { 
    207225        return $this->status; 
    208     } 
    209  
    210     /** 
    211      * returns a list of all instances with status $status 
    212      * 
    213      * @param   stubDatabaseFinder  $finder  the finder to use 
    214      * @param   int                 $status  status that the instances must have 
    215      * @return  stubDatabaseFinderResult<MyNewsArticle> 
    216      */ 
    217     public static function findByStatus(stubDatabaseFinder $finder, $status) 
    218     { 
    219         $equalCriterion = new stubEqualCriterion('status', $status, 'news'); 
    220         return $finder->findByCriterion($equalCriterion, __CLASS__); 
    221226    } 
    222227} 
  • trunk/experiments/people/mikey/persistence/createTable.php

    r512 r951  
    33require_once '../../../../src/main/php/net/stubbles/stubClassLoader.php'; 
    44stubClassLoader::load('net.stubbles.rdbms.rdbms', 
    5                       'net.stubbles.rdbms.persistence.stubAbstractPersistable', 
    65                      'net.stubbles.rdbms.persistence.creator.stubDatabaseCreator' 
    76); 
     
    109 
    1110$creator = stubDatabaseCreator::getInstance(stubDatabaseConnectionPool::getConnection()); 
    12 $creator->createTable('MyNewsArticle'); 
     11$creator->createTable(new stubReflectionClass('MyNewsArticle')); 
    1312?> 
  • trunk/experiments/people/mikey/persistence/deleteEntry.php

    r655 r951  
    33require_once '../../../../src/main/php/net/stubbles/stubClassLoader.php'; 
    44stubClassLoader::load('net.stubbles.rdbms.rdbms', 
    5                       'net.stubbles.rdbms.persistence.stubAbstractPersistable', 
    65                      'net.stubbles.rdbms.persistence.eraser.stubDatabaseEraser', 
    76                      'net.stubbles.rdbms.persistence.finder.stubDatabaseFinder' 
  • trunk/experiments/people/mikey/persistence/fetchEntry.php

    r512 r951  
    33require_once '../../../../src/main/php/net/stubbles/stubClassLoader.php'; 
    44stubClassLoader::load('net.stubbles.rdbms.rdbms', 
    5                       'net.stubbles.rdbms.persistence.stubAbstractPersistable', 
    65                      'net.stubbles.rdbms.persistence.finder.stubDatabaseFinder' 
    76); 
     
    98require_once 'connection.php'; 
    109 
    11 $newsArticle = new MyNewsArticle(); 
    12 $newsArticle->setId(1); 
    13 echo "Existing before finder:\n"; 
    14 var_dump($newsArticle); 
    15  
    1610$finder = stubDatabaseFinder::getInstance(stubDatabaseConnectionPool::getConnection()); 
    17 $finder->findByPrimaryKeys($newsArticle); 
     11$newsArticle = $finder->findByPrimaryKeys(new stubReflectionClass('MyNewsArticle'), array('id' => 1)); 
    1812echo "Existing after finder:\n"; 
    1913var_dump($newsArticle); 
    2014 
    21 $newsArticle = new MyNewsArticle(); 
    22 $newsArticle->setId(100); 
    23 echo "Non-existing before finder:\n"; 
    24 var_dump($newsArticle); 
    25 $finder->findByPrimaryKeys($newsArticle); 
     15$newsArticle = $finder->findByPrimaryKeys(new stubReflectionClass('MyNewsArticle'), array('id' => 100)); 
    2616echo "Non-existing after finder:\n"; 
    2717var_dump($newsArticle); 
  • trunk/experiments/people/mikey/persistence/insertEntry.php

    r512 r951  
    33require_once '../../../../src/main/php/net/stubbles/stubClassLoader.php'; 
    44stubClassLoader::load('net.stubbles.rdbms.rdbms', 
    5                       'net.stubbles.rdbms.persistence.stubAbstractPersistable', 
    65                      'net.stubbles.rdbms.persistence.serializer.stubDatabaseSerializer' 
    76); 
  • trunk/experiments/people/mikey/persistence/listEntries.php

    r881 r951  
    44stubClassLoader::load('net.stubbles.rdbms.rdbms', 
    55                      'net.stubbles.rdbms.criteria.stubEqualCriterion', 
    6                       'net.stubbles.rdbms.persistence.stubAbstractPersistable', 
    76                      'net.stubbles.rdbms.persistence.finder.stubDatabaseFinder' 
    87); 
     
    1110 
    1211$finder       = stubDatabaseFinder::getInstance(stubDatabaseConnectionPool::getConnection()); 
    13 $newsArticles = MyNewsArticle::findByStatus($finder, 5); 
     12$newsArticles = $finder->findByCriterion(new stubEqualCriterion('status', 5, 'news'), new stubReflectionClass('MyNewsArticle')); 
    1413var_dump($newsArticles->count()); 
    1514foreach ($newsArticles as $newsArticle) { 
  • trunk/experiments/people/mikey/persistence/updateEntry.php

    r512 r951  
    33require_once '../../../../src/main/php/net/stubbles/stubClassLoader.php'; 
    44stubClassLoader::load('net.stubbles.rdbms.rdbms', 
    5                       'net.stubbles.rdbms.persistence.stubAbstractPersistable', 
    65                      'net.stubbles.rdbms.persistence.finder.stubDatabaseFinder', 
    76                      'net.stubbles.rdbms.persistence.serializer.stubDatabaseSerializer' 
     
    109require_once 'connection.php'; 
    1110 
    12 $newsArticle = new MyNewsArticle(); 
    13 $newsArticle->setId(4); 
    14  
    1511$finder = stubDatabaseFinder::getInstance(stubDatabaseConnectionPool::getConnection()); 
    16 $finder->findByPrimaryKeys($newsArticle); 
     12$newsArticle = $finder->findByPrimaryKeys(new stubReflectionClass('MyNewsArticle'), array('id' => 4)); 
    1713echo "After finder:\n"; 
    1814var_dump($newsArticle); 
  • trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinder.php

    r928 r951  
    7979 
    8080    /** 
    81      * get a persistable object from database by its primary keys 
     81     * get an entity from database by its primary keys 
    8282     * 
    8383     * @param   stubBaseReflectionClass      $entityClass  class information about the entity 
     
    114114 
    115115    /** 
    116      * finds all instances of $persistableClassName by given criterion 
     116     * finds all instances of $entityClass by given criterion 
    117117     * 
    118118     * @param   stubCriterion                $criterion 
    119      * @param   string                       $persistableClassName  non qualified classname of the persistable class to find instances of 
    120      * @param   array                        $arguments             optional  arguments for constructor 
     119     * @param   string                       $entityClass  entity class to find instances of 
     120     * @param   array                        $arguments    optional  arguments for constructor 
    121121     * @return  stubDatabaseFinderResult     list of instances of $entityClass found with $criterion 
    122122     * @throws  stubDatabaseFinderException 
  • trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinderResult.php

    r912 r951  
    88 */ 
    99stubClassLoader::load('net.stubbles.rdbms.persistence.finder.stubDatabaseFinderException', 
    10                       'net.stubbles.rdbms.persistence.stubPersistable', 
    1110                      'net.stubbles.reflection.stubReflectionClass' 
    1211); 
  • trunk/src/main/php/net/stubbles/rdbms/persistence/serializer/stubDatabaseSerializer.php

    r913 r951  
    8989        } 
    9090         
    91         $entityClass = (($entity instanceof stubObject) ? ($entity->getClass()) : (new stubReflectionClass($entity))); 
     91        $entityClass = (($entity instanceof stubObject) ? ($entity->getClass()) : (new stubReflectionObject($entity))); 
    9292        if ($entityClass->hasAnnotation('Entity') === false) { 
    93             throw new stubPersistenceException('Class ' . $entity->getClassName() . ' is not an entity.'); 
     93            throw new stubPersistenceException('Class ' . $entityClass->getFullQualifiedClassName() . ' is not an entity.'); 
    9494        } 
    9595         
    9696        $tableRow         = new stubDatabaseTableRow($this->getTableDescription($entityClass)->getName()); 
    97         $methods          = $refObject->getMethods(); 
     97        $methods          = $entityClass->getMethods(); 
    9898        $singlePrimaryKey = null; 
    9999        $defaultValues    = array(); 
     
    107107                $value = $method->invoke($entity); 
    108108            } catch (ReflectionException $re) { 
    109                 throw new stubDatabaseSerializerException('Can not get return value of ' . $entity->getClassName() . '::' . $method->getName() . '(), invocation failed.', $re); 
     109                throw new stubDatabaseSerializerException('Can not get return value of ' . $entityClass->getFullQualifiedClassName() . '::' . $method->getName() . '(), invocation failed.', $re); 
    110110            } 
    111111             
     
    113113                if (null === $value) { 
    114114                    if (null !== $singlePrimaryKey) { 
    115                         throw new stubDatabaseSerializerException('Persistence error for ' . $entity->getClassName() . ': only one primary key can be null, but at least two primary keys are null: ' . $singlePrimaryKey['propertyName'] . ' and ' . $method->getName()); 
     115                        throw new stubDatabaseSerializerException('Persistence error for ' . $entityClass->getFullQualifiedClassName() . ': only one primary key can be null, but at least two primary keys are null: ' . $singlePrimaryKey['propertyName'] . ' and ' . $method->getName()); 
    116116                    } 
    117117                     
    118                     $singlePrimaryKey = array('propertyName' => $method->getName(), 
    119                                               'tableName'    => $tableName 
     118                    $singlePrimaryKey = array('setterMethod' => stubSetterMethodHelper::create($column, $entityClass, $method->getName()), 
     119                                              'tableName'    => $tableRow->getTableName() 
    120120                                        ); 
    121121                    continue; 
    122122                } else { 
    123                     $tableRow->addCriterion(new stubEqualCriterion($column->getName(), $value, $tableName)); 
     123                    $tableRow->addCriterion(new stubEqualCriterion($column->getName(), $value, $tableRow->getTableName())); 
    124124                } 
    125125            } elseif (null === $value) { 
    126                 $value = $column->getDefaultValue(); 
    127                 if ($column->isNullable() === false && null === $value) { 
    128                     throw new stubDatabaseSerializerException('Persistence error for ' . $entity->getClassName() . ': column ' . $column->getName() . ' is not allowed to be null but return value from method ' . $method->getName() . ' and default value are both null.'); 
     126                $defaultValue = $column->getDefaultValue(); 
     127                if ($column->isNullable() === false && null === $defaultValue) { 
     128                    throw new stubDatabaseSerializerException('Persistence error for ' . $entityClass->getFullQualifiedClassName() . ': column ' . $column->getName() . ' is not allowed to be null but return value from method ' . $method->getName() . ' and default value are both null.'); 
    129129                } 
    130130                 
    131131                $defaultValues[] = array('setterMethod' => stubSetterMethodHelper::create($column, $entityClass, $method->getName()), 
    132                                          'value'        => $value 
     132                                         'value'        => $value, 
     133                                         'defaultValue' => $defaultValue, 
     134                                         'column'       => $column->getName() 
    133135                                   ); 
    134             } 
    135              
    136             $tableRow->setColumn($column->getName(), $value); 
    137         } 
    138          
    139         if ($tableRow->hasCriterion() === false) { 
    140             foreach ($defaultValues as $defaultValue) { 
    141                 $defaultValue['setterMethod']->invoke($entity, $defaultValue['value']); 
     136            } else { 
     137                $tableRow->setColumn($column->getName(), $value); 
    142138            } 
    143139        } 
    144140         
    145141        try { 
    146             $this->processQueries($this->getQuery($tableRow), $entity, $singlePrimaryKey); 
     142            $this->processQueries($this->getQuery($tableRow, $entity, $defaultValues), $entity, $singlePrimaryKey); 
    147143        } catch (stubDatabaseException $dbe) { 
    148             throw new stubDatabaseSerializerException('Can not persist ' . $entity->getClassName() . ': a database error occured.', $dbe); 
     144            throw new stubDatabaseSerializerException('Can not persist ' . $entityClass->getFullQualifiedClassName() . ': a database error occured.', $dbe); 
    149145        } 
    150146    } 
     
    154150     * 
    155151     * @param   stubDatabaseTableRow  $tableRow 
     152     * @param   array                 $defaultValues 
    156153     * @return  array<string,string> 
    157154     * @throws   
    158155     */ 
    159     protected function getQuery(stubDatabaseTableRow $tableRow
     156    protected function getQuery(stubDatabaseTableRow $tableRow, $entity, array $defaultValues
    160157    { 
    161158        $queryBuilder = stubDatabaseQueryBuilderFactory::create($this->connection); 
    162159        try { 
     160            // criterion for update exists => update 
    163161            if ($tableRow->hasCriterion() === true) { 
    164                 return $queryBuilder->createUpdate(array($tableRow)); 
     162                // set any null values 
     163                foreach ($defaultValues as $defaultValue) { 
     164                    $tableRow->setColumn($defaultValue['column'], $defaultValue['value']); 
     165                } 
     166                 
     167                return $queryBuilder->createUpdate(array($tableRow->getTableName() => $tableRow)); 
    165168            } 
    166169             
    167             return $queryBuilder->createInsert(array($tableRow)); 
     170            // fill default values into entity and table row 
     171            foreach ($defaultValues as $defaultValue) { 
     172                $defaultValue['setterMethod']->invokeArgs($entity, array($defaultValue['defaultValue'])); 
     173                $tableRow->setColumn($defaultValue['column'], $defaultValue['defaultValue']); 
     174            } 
     175 
     176            return $queryBuilder->createInsert(array($tableRow->getTableName() => $tableRow)); 
    168177        } catch (stubIllegalArgumentException $iae) { 
    169178            throw new stubDatabaseSerializerException('Creating the queries failed.', $iae); 
     
    175184     * 
    176185     * @param   array<string,string>   $queries           list of queries to process 
    177      * @param   stubPersistable        $persistable       the persistable object 
     186     * @param   object                 $entity            the entity to process the queries for 
    178187     * @param   array<string,string>   $singlePrimaryKey  optional  information about the single primary key 
    179188     * @throws  stubDatabaseException 
    180189     */ 
    181     protected function processQueries(array $queries, stubPersistable $persistable, array $singlePrimaryKey = null) 
     190    protected function processQueries(array $queries, $entity, array $singlePrimaryKey = null) 
    182191    { 
    183192        foreach ($queries as $tableName => $query) { 
    184193            $this->connection->exec($query); 
    185194            if (null !== $singlePrimaryKey && $singlePrimaryKey['tableName'] == $tableName) { 
    186                 $persistable->setId($this->connection->getLastInsertId()); 
     195                $singlePrimaryKey['setterMethod']->invokeArgs($entity, array($this->connection->getLastInsertId())); 
    187196            } 
    188197        } 
  • trunk/src/test/php/net/stubbles/rdbms/RDBMSTestSuite.php

    r928 r951  
    4343         
    4444        // persistence 
    45        # $this->addTestFile($dir . '/persistence/stubSetterMethodHelperTestCase.php'); 
     45        $this->addTestFile($dir . '/persistence/stubSetterMethodHelperTestCase.php'); 
    4646        $this->addTestFile($dir . '/persistence/creator/stubDatabaseCreatorTestCase.php'); 
    4747        $this->addTestFile($dir . '/persistence/eraser/stubDatabaseEraserTestCase.php'); 
    4848        $this->addTestFile($dir . '/persistence/finder/stubDatabaseFinderTestCase.php'); 
    49        # $this->addTestFile($dir . '/persistence/serializer/stubDatabaseSerializerTestCase.php'); 
     49        $this->addTestFile($dir . '/persistence/serializer/stubDatabaseSerializerTestCase.php'); 
    5050         
    5151        // querybuilder 
  • trunk/src/test/php/net/stubbles/rdbms/persistence/MockNoEntityAnnotationEntity.php

    r899 r951  
    11<?php 
    22/** 
    3  * This is a mocked persistable that is missing the DBTable annotation. 
     3 * This is a mocked entity that is missing the @Entity annotation. 
    44 * 
    55 * @package     stubbles 
     
    77 */ 
    88/** 
    9  * This is a mocked persistable that is missing the DBTable annotation. 
     9 * This is a mocked entity that is missing the @Entity annotation. 
    1010 * 
    1111 * @package     stubbles 
    1212 * @subpackage  rdbms_persistence_test 
    1313 */ 
    14 class MockNoEntityAnnotationPersistable extends stubBaseObject 
     14class MockNoEntityAnnotationEntity extends stubBaseObject 
    1515{ 
    1616    // intentionally empty 
  • trunk/src/test/php/net/stubbles/rdbms/persistence/MockNoTableAnnotationEntity.php

    r928 r951  
    11<?php 
    22/** 
    3  * This is a mocked persistable that is missing the DBTable annotation. 
     3 * This is a mocked entity that is missing the @DBTable annotation. 
    44 * 
    55 * @package     stubbles 
     
    77 */ 
    88/** 
    9  * This is a mocked persistable that is missing the DBTable annotation. 
     9 * This is a mocked entity that is missing the @DBTable annotation. 
    1010 * 
    1111 * @package     stubbles 
     
    1313 * @Entity 
    1414 */ 
    15 class MockNoTableAnnotationPersistable extends stubBaseObject 
     15class MockNoTableAnnotationEntity extends stubBaseObject 
    1616{ 
    1717    /** 
  • trunk/src/test/php/net/stubbles/rdbms/persistence/MockSinglePrimaryKeyEntity.php

    r925 r951  
    11<?php 
    22/** 
    3  * This is a mocked persistable that has some annotations. 
     3 * This is a mocked entity that has some annotations. 
    44 * 
    55 * @package     stubbles 
     
    77 */ 
    88/** 
    9  * This is a mocked persistable that has some annotations. 
     9 * This is a mocked entity that has some annotations. 
    1010 * 
    1111 * @package     stubbles 
     
    1414 * @DBTable(name='foo') 
    1515 */ 
    16 class MockSinglePrimaryKeyPersistable extends stubBaseObject 
     16class MockSinglePrimaryKeyEntity extends stubBaseObject 
    1717{ 
    1818    /** 
  • trunk/src/test/php/net/stubbles/rdbms/persistence/creator/stubDatabaseCreatorTestCase.php

    r907 r951  
    1010Mock::generate('stubDatabaseConnection'); 
    1111require_once dirname(__FILE__) . '/../../querybuilder/TeststubDatabaseQueryBuilder.php'; 
    12 require_once dirname(__FILE__) . '/../MockNoEntityAnnotationPersistable.php'; 
    13 require_once dirname(__FILE__) . '/../MockNoTableAnnotationPersistable.php'; 
    14 require_once dirname(__FILE__) . '/../MockSinglePrimaryKeyPersistable.php'; 
     12require_once dirname(__FILE__) . '/../MockNoEntityAnnotationEntity.php'; 
     13require_once dirname(__FILE__) . '/../MockNoTableAnnotationEntity.php'; 
     14require_once dirname(__FILE__) . '/../MockSinglePrimaryKeyEntity.php'; 
    1515/** 
    1616 * Test for net.stubbles.rdbms.persistence.creator.stubDatabaseCreator 
     
    104104    { 
    105105        $this->expectException('stubPersistenceException'); 
    106         $this->dbCreator->createTable(new stubReflectionClass('MockNoEntityAnnotationPersistable')); 
     106        $this->dbCreator->createTable(new stubReflectionClass('MockNoEntityAnnotationEntity')); 
    107107    } 
    108108 
     
    112112    public function testClassWithoutDBTableAnnotation() 
    113113    { 
    114         $this->dbCreator->createTable(new stubReflectionClass('MockNoTableAnnotationPersistable')); 
     114        $this->dbCreator->createTable(new stubReflectionClass('MockNoTableAnnotationEntity')); 
    115115        $tableDescription = $this->mockQueryBuilder->getTableDescription(); 
    116         $this->assertEqual($tableDescription->getName(), 'MockNoTableAnnotationPersistables'); 
     116        $this->assertEqual($tableDescription->getName(), 'MockNoTableAnnotationEntitys'); 
    117117        $columns = $tableDescription->getColumns(); 
    118118        $this->assertEqual(count($columns), 6); 
     
    148148    public function testClassWithDBTableAnnotation() 
    149149    { 
    150         $this->dbCreator->createTable(new stubReflectionClass('MockSinglePrimaryKeyPersistable')); 
     150        $this->dbCreator->createTable(new stubReflectionClass('MockSinglePrimaryKeyEntity')); 
    151151        $tableDescription = $this->mockQueryBuilder->getTableDescription(); 
    152152        $this->assertEqual($tableDescription->getName(), 'foo'); 
  • trunk/src/test/php/net/stubbles/rdbms/persistence/eraser/stubDatabaseEraserTestCase.php

    r949 r951  
    1212Mock::generate('stubCriterion'); 
    1313require_once dirname(__FILE__) . '/../../querybuilder/TeststubDatabaseQueryBuilder.php'; 
    14 require_once dirname(__FILE__) . '/../MockNoEntityAnnotationPersistable.php'; 
    15 require_once dirname(__FILE__) . '/../MockNoTableAnnotationPersistable.php'; 
    16 require_once dirname(__FILE__) . '/../MockSinglePrimaryKeyPersistable.php'; 
     14require_once dirname(__FILE__) . '/../MockNoEntityAnnotationEntity.php'; 
     15require_once dirname(__FILE__) . '/../MockNoTableAnnotationEntity.php'; 
     16require_once dirname(__FILE__) . '/../MockSinglePrimaryKeyEntity.php'; 
    1717/** 
    1818 * mock entity to test an entity that has no primary keys 
     
    112112 
    113113    /** 
    114      * check that a class that is missing the DBTable annotation throws an exception 
     114     * check that a cnon-object throws an exception 
    115115     */ 
    116116    public function testByPrimaryKeysClassWithNonObject() 
     
    126126    { 
    127127        $this->expectException('stubPersistenceException'); 
    128         $this->dbEraser->deleteByPrimaryKeys(new MockNoEntityAnnotationPersistable()); 
     128        $this->dbEraser->deleteByPrimaryKeys(new MockNoEntityAnnotationEntity()); 
    129129    } 
    130130 
     
    143143    public function testByPrimaryKeys() 
    144144    { 
    145         $entity = new MockNoTableAnnotationPersistable(); 
     145        $entity = new MockNoTableAnnotationEntity(); 
    146146        $entity->setId('mock'); 
    147147        $this->mockConnection->setReturnValue('query', new MockstubDatabaseResult()); 
    148148        $this->dbEraser->deleteByPrimaryKeys($entity); 
    149         $this->assertEqual($this->mockQueryBuilder->getDeleteTable(), 'MockNoTableAnnotationPersistables'); 
    150         $this->assertEqual($this->mockQueryBuilder->getDeleteCriterion()->toSQL(), "(`MockNoTableAnnotationPersistables`.`id` = 'mock')"); 
     149        $this->assertEqual($this->mockQueryBuilder->getDeleteTable(), 'MockNoTableAnnotationEntitys'); 
     150        $this->assertEqual($this->mockQueryBuilder->getDeleteCriterion()->toSQL(), "(`MockNoTableAnnotationEntitys`.`id` = 'mock')"); 
    151151    } 
    152152 
     
    156156    public function testByPrimaryKeysWithDBTableAnnotation() 
    157157    { 
    158         $entity = new MockSinglePrimaryKeyPersistable(); 
     158        $entity = new MockSinglePrimaryKeyEntity(); 
    159159        $entity->setId('mock'); 
    160160        $this->mockConnection->setReturnValue('query', new MockstubDatabaseResult()); 
     
    175175        $mockResult->setReturnValueAt(0, 'count', 0); 
    176176        $mockResult->setReturnValueAt(1, 'count', 1); 
    177         $data = $this->dbEraser->deleteByCriterion($mockCriterion, new stubReflectionClass('MockNoTableAnnotationPersistable')); 
     177        $data = $this->dbEraser->deleteByCriterion($mockCriterion, new stubReflectionClass('MockNoTableAnnotationEntity')); 
    178178        $this->assertEqual($data, 0); 
    179         $data = $this->dbEraser->deleteByCriterion($mockCriterion, new stubReflectionClass('MockNoTableAnnotationPersistable')); 
     179        $data = $this->dbEraser->deleteByCriterion($mockCriterion, new stubReflectionClass('MockNoTableAnnotationEntity')); 
    180180        $this->assertEqual($data, 1); 
    181         $this->assertEqual($this->mockQueryBuilder->getDeleteTable(), 'MockNoTableAnnotationPersistables'); 
     181        $this->assertEqual($this->mockQueryBuilder->getDeleteTable(), 'MockNoTableAnnotationEntitys'); 
    182182        $this->assertEqual($this->mockQueryBuilder->getDeleteCriterion()->toSQL(), "example"); 
    183183    } 
     
    194194        $mockResult->setReturnValueAt(0, 'count', 0); 
    195195        $mockResult->setReturnValueAt(1, 'count', 1); 
    196         $data = $this->dbEraser->deleteByCriterion($mockCriterion, new stubReflectionClass('MockSinglePrimaryKeyPersistable')); 
     196        $data = $this->dbEraser->deleteByCriterion($mockCriterion, new stubReflectionClass('MockSinglePrimaryKeyEntity')); 
    197197        $this->assertEqual($data, 0); 
    198         $data = $this->dbEraser->deleteByCriterion($mockCriterion, new stubReflectionClass('MockSinglePrimaryKeyPersistable')); 
     198        $data = $this->dbEraser->deleteByCriterion($mockCriterion, new stubReflectionClass('MockSinglePrimaryKeyEntity')); 
    199199        $this->assertEqual($data, 1); 
    200200        $this->assertEqual($this->mockQueryBuilder->getDeleteTable(), 'foo'); 
     
    208208    { 
    209209        $this->expectException('stubPersistenceException'); 
    210         $this->dbEraser->deleteByCriterion(new MockstubCriterion(), new stubReflectionClass('MockNoEntityAnnotationPersistable')); 
     210        $this->dbEraser->deleteByCriterion(new MockstubCriterion(), new stubReflectionClass('MockNoEntityAnnotationEntity')); 
    211211    } 
    212212} 
  • trunk/src/test/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinderTestCase.php

    r949 r951  
    1212Mock::generate('stubCriterion'); 
    1313require_once dirname(__FILE__) . '/../../querybuilder/TeststubDatabaseQueryBuilder.php'; 
    14 require_once dirname(__FILE__) . '/../MockNoTableAnnotationPersistable.php'; 
    15 require_once dirname(__FILE__) . '/../MockSinglePrimaryKeyPersistable.php'; 
    16 require_once dirname(__FILE__) . '/../MockNoEntityAnnotationPersistable.php'; 
     14require_once dirname(__FILE__) . '/../MockNoTableAnnotationEntity.php'; 
     15require_once dirname(__FILE__) . '/../MockSinglePrimaryKeyEntity.php'; 
     16require_once dirname(__FILE__) . '/../MockNoEntityAnnotationEntity.php'; 
    1717/** 
    18  * This is a persistable that requires arguments in the constructor. 
     18 * This is an entity that requires arguments in the constructor. 
    1919 * 
    2020 * @Entity 
    2121 * @DBTable(name='bar') 
    2222 */ 
    23 class MockInstanceArgsPersistable extends stubBaseObject 
     23class MockInstanceArgsEntity extends stubBaseObject 
    2424{ 
    2525    protected $arg1; 
     
    145145    { 
    146146        $this->expectException('stubPersistenceException'); 
    147         $this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockNoEntityAnnotationPersistable'), array()); 
     147        $this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockNoEntityAnnotationEntity'), array()); 
    148148    } 
    149149 
     
    157157        $mockResult->setReturnValueAt(0, 'fetch', false); 
    158158        $mockResult->setReturnValueAt(1, 'fetch', array('id' => 'mock', 'bar' => 'Here is bar.', 'default' => 'And this is default.')); 
    159         $this->assertNull($this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockSinglePrimaryKeyPersistable'), array('id' => 'mock'))); 
    160         $singlePrimaryKey = $this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockSinglePrimaryKeyPersistable'), array('id' => 'mock')); 
     159        $this->assertNull($this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockSinglePrimaryKeyEntity'), array('id' => 'mock'))); 
     160        $singlePrimaryKey = $this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockSinglePrimaryKeyEntity'), array('id' => 'mock')); 
    161161        $this->assertEqual($singlePrimaryKey->getId(), 'mock'); 
    162162        $this->assertEqual($singlePrimaryKey->withAnnotation(), 'Here is bar.'); 
     
    175175        $mockResult->setReturnValueAt(0, 'fetch', false); 
    176176        $mockResult->setReturnValueAt(1, 'fetch', array('id' => 'mock', 'bar' => 'Here is bar.', 'defaultValue' => 'And this is default.')); 
    177         $this->assertNull($this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockNoTableAnnotationPersistable'), array('id' => 'mock'))); 
    178         $entity = $this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockNoTableAnnotationPersistable'), array('id' => 'mock')); 
     177        $this->assertNull($this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockNoTableAnnotationEntity'), array('id' => 'mock'))); 
     178        $entity = $this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockNoTableAnnotationEntity'), array('id' => 'mock')); 
    179179        $this->assertEqual($entity->getId(), 'mock'); 
    180180        $this->assertEqual($entity->withAnnotation(), 'Here is bar.'); 
    181181        $this->assertEqual($entity->getDefaultValue(), 'And this is default.'); 
    182182        $select = $this->mockQueryBuilder->getSelect(); 
    183         $this->assertEqual($select->getBaseTableName(), 'MockNoTableAnnotationPersistables'); 
     183        $this->assertEqual($select->getBaseTableName(), 'MockNoTableAnnotationEntitys'); 
    184184    } 
    185185 
     
    194194        $this->mockConnection->setReturnValue('query', $mockResult); 
    195195        $mockResult->setReturnValue('fetch', array()); 
    196         $entity = $this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockInstanceArgsPersistable'), array(), array('foo', 'bar')); 
     196        $entity = $this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockInstanceArgsEntity'), array(), array('foo', 'bar')); 
    197197        $this->assertEqual($entity->getArg1(), 'foo'); 
    198198        $this->assertEqual($entity->getArg2(), 'bar'); 
    199         $entity = $this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockInstanceArgsPersistable'), array(), array('foo')); 
     199        $entity = $this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockInstanceArgsEntity'), array(), array('foo')); 
    200200        $this->assertEqual($entity->getArg1(), 'foo'); 
    201201        $this->assertNull($entity->getArg2()); 
     
    208208    { 
    209209        $this->expectException('stubPersistenceException'); 
    210         $this->dbFinder->findByCriterion(new MockstubCriterion(), new stubReflectionClass('MockNoEntityAnnotationPersistable')); 
     210        $this->dbFinder->findByCriterion(new MockstubCriterion(), new stubReflectionClass('MockNoEntityAnnotationEntity')); 
    211211    } 
    212212 
     
    222222        $mockResult->setReturnValueAt(0, 'fetchAll', false); 
    223223        $mockResult->setReturnValueAt(1, 'fetchAll', array(array('bar' => 'Here is bar.', 'default' => 'And this is default.'))); 
    224         $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockSinglePrimaryKeyPersistable')); 
     224        $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockSinglePrimaryKeyEntity')); 
    225225        $this->assertEqual($finderResult->count(), 0); 
    226         $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockSinglePrimaryKeyPersistable')); 
     226        $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockSinglePrimaryKeyEntity')); 
    227227        $this->assertEqual($finderResult->count(), 1); 
    228228        $data = $finderResult->current(); 
     
    243243        $this->mockConnection->setReturnValue('query', $mockResult); 
    244244        $mockResult->setReturnValue('fetchAll', array(array())); 
    245         $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockInstanceArgsPersistable'), array('foo', 'bar')); 
     245        $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockInstanceArgsEntity'), array('foo', 'bar')); 
    246246        $data = $finderResult->current(); 
    247247        $this->assertEqual($data->getArg1(), 'foo'); 
    248248        $this->assertEqual($data->getArg2(), 'bar'); 
    249         $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockInstanceArgsPersistable'), array('foo')); 
     249        $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockInstanceArgsEntity'), array('foo')); 
    250250        $data = $finderResult->current(); 
    251251        $this->assertEqual($data->getArg1(), 'foo'); 
  • trunk/src/test/php/net/stubbles/rdbms/persistence/serializer/stubDatabaseSerializerTestCase.php

    r482 r951  
    1010Mock::generate('stubDatabaseConnection'); 
    1111require_once dirname(__FILE__) . '/../../querybuilder/TeststubDatabaseQueryBuilder.php'; 
    12 require_once dirname(__FILE__) . '/../MockNoTableAnnotationPersistable.php'; 
    13 require_once dirname(__FILE__) . '/../MockSinglePrimaryKeyPersistable.php'; 
    14 require_once dirname(__FILE__) . '/../MockMultiplePrimaryKeyPersistable.php'; 
     12require_once dirname(__FILE__) . '/../MockNoEntityAnnotationEntity.php'; 
     13require_once dirname(__FILE__) . '/../MockSinglePrimaryKeyEntity.php'; 
    1514/** 
    1615 * Test for net.stubbles.rdbms.persistence.serializer.stubDatabaseSerializer 
     
    9998 
    10099    /** 
    101      * check that a class that is missing the DBTable annotation throws an exception 
     100     * check that a non-object throws an exception 
    102101     */ 
    103     public function testClassWithoutDBTableAnnotation() 
     102    public function testSerializeNonObject() 
    104103    { 
    105         $this->expectException('stubDatabaseSerializerException'); 
    106         $this->dbSerializer->serialize(new MockNoTableAnnotationPersistable()); 
     104        $this->expectException('stubIllegalArgumentException'); 
     105        $this->dbSerializer->serialize('foo'); 
     106    } 
     107 
     108    /** 
     109     * test that trying to find a class that does not have an entity annotation throws an exception 
     110     */ 
     111    public function testSerializeNonEntity() 
     112    { 
     113        $this->expectException('stubPersistenceException'); 
     114        $this->dbSerializer->serialize(new MockNoEntityAnnotationEntity()); 
    107115    } 
    108116 
     
    112120    public function testInsertWithSinglePrimaryKey() 
    113121    { 
    114         $singlePrimaryKeyPersistable = new MockSinglePrimaryKeyPersistable(); 
     122        $singlePrimaryKeyEntity = new MockSinglePrimaryKeyEntity(); 
    115123        $this->mockConnection->setReturnValue('getLastInsertId', 'mockId'); 
    116         $this->mockConnection->expectCallcount('exec', 2); 
    117         $this->mockQueryBuilder->setInsertQueries(array('foo' => 'foo', 'baz' => 'baz')); 
    118         $this->dbSerializer->serialize($singlePrimaryKeyPersistable); 
     124        $this->mockConnection->expectCallcount('exec', 1); 
     125        $this->mockQueryBuilder->setInsertQueries(array('foo' => 'foo')); 
     126        $this->dbSerializer->serialize($singlePrimaryKeyEntity); 
    119127        $this->assertEqual($this->mockQueryBuilder->getCallCount('createInsert'), 1); 
    120128        $this->assertEqual($this->mockQueryBuilder->getCallCount('createUpdate'), 0); 
    121129        $tableRows = $this->mockQueryBuilder->getInsertTableRows(); 
    122         $this->assertEqual(count($tableRows), 2); 
     130        $this->assertEqual(count($tableRows), 1); 
    123131        $this->assertTrue(isset($tableRows['foo'])); 
    124         $this->assertTrue(isset($tableRows['blub'])); 
    125         $this->assertEqual($singlePrimaryKeyPersistable->getId(), 'mockId'); 
    126         $this->assertTrue($singlePrimaryKeyPersistable->isPersistent()); 
     132        $this->assertEqual($singlePrimaryKeyEntity->getId(), 'mockId'); 
    127133        $this->assertEqual($tableRows['foo']->getColumns(), array('bar' => 'this is bar', 'default' => 'example')); 
    128         $this->assertEqual($tableRows['blub']->getColumns(), array('baz' => 'this is baz')); 
    129134        $this->assertFalse($tableRows['foo']->hasCriterion()); 
    130135    } 
     
    135140    public function testUpdateWithSinglePrimaryKey() 
    136141    { 
    137         $singlePrimaryKeyPersistable = new MockSinglePrimaryKeyPersistable(); 
    138         $singlePrimaryKeyPersistable->setId('mockId'); 
    139         $singlePrimaryKeyPersistable->setPersistent(true); 
    140         $singlePrimaryKeyPersistable->setDefaultValue('anotherExample'); 
     142        $singlePrimaryKeyEntity = new MockSinglePrimaryKeyEntity(); 
     143        $singlePrimaryKeyEntity->setId('mockId'); 
     144        $singlePrimaryKeyEntity->setDefaultValue('anotherExample'); 
    141145        $this->mockConnection->expectNever('getLastInsertId'); 
    142         $this->mockConnection->expectCallcount('exec', 2); 
    143         $this->mockQueryBuilder->setUpdateQueries(array('foo' => 'foo', 'baz' => 'baz')); 
    144         $this->dbSerializer->serialize($singlePrimaryKeyPersistable); 
    145         $this->assertEqual($this->mockQueryBuilder->getCallCount('createInsert'), 0); 
    146         $this->assertEqual($this->mockQueryBuilder->getCallCount('createUpdate'), 1); 
    147         $tableRows = $this->mockQueryBuilder->getUpdateTableRows(); 
    148         $this->assertEqual(count($tableRows), 2); 
    149         $this->assertTrue(isset($tableRows['foo'])); 
    150         $this->assertTrue(isset($tableRows['blub'])); 
    151         $this->assertEqual($singlePrimaryKeyPersistable->getId(), 'mockId'); 
    152         $this->assertTrue($singlePrimaryKeyPersistable->isPersistent()); 
    153         $this->assertEqual($tableRows['foo']->getColumns(), array('id' => 'mockId', 'bar' => 'this is bar', 'default' => 'anotherExample'));