Changeset 1611

Show
Ignore:
Timestamp:
05/30/08 11:12:34 (4 months ago)
Author:
mikey
Message:

removed possibility of setting arguments via constructor on entities fetched from database: an entity's constructor should not require any arguments

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinder.php

    r1311 r1611  
    8181     * get an entity from database by its primary keys 
    8282     * 
    83      * @param   stubBaseReflectionClass      $entityClass  class information about the entity 
    84      * @param   array                        $primaryKeys  list of primary keys (name => value) 
    85      * @param   array                        $arguments    arguments for the constructor 
     83     * @param   stubBaseReflectionClass   $entityClass  class information about the entity 
     84     * @param   array                     $primaryKeys  list of primary keys (name => value) 
    8685     * @return  object 
    8786     * @throws  stubPersistenceException 
    8887     */ 
    89     public function findByPrimaryKeys(stubBaseReflectionClass $entityClass, array $primaryKeys, array $arguments = null
     88    public function findByPrimaryKeys(stubBaseReflectionClass $entityClass, array $primaryKeys
    9089    { 
    9190        if ($entityClass->hasAnnotation('Entity') === false) { 
     
    9998        } 
    10099         
    101         $entity = ((null === $arguments) ? ($entityClass->newInstance()) : ($entityClass->newInstanceArgs($arguments))); 
     100        $entity = $entityClass->newInstance(); 
    102101        $setterMethodHelper->applySetterMethods($entity, $data); 
    103102        return $entity; 
     
    107106     * finds all instances of $entityClass by given criterion 
    108107     * 
    109      * @param   stubCriterion                $criterion 
    110      * @param   string                       $entityClass  entity class to find instances of 
    111      * @param   array                        $arguments    optional  arguments for constructor 
    112      * @return  stubDatabaseFinderResult     list of instances of $entityClass found with $criterion 
     108     * @param   stubCriterion             $criterion 
     109     * @param   string                    $entityClass  entity class to find instances of 
     110     * @return  stubDatabaseFinderResult  list of instances of $entityClass found with $criterion 
    113111     * @throws  stubPersistenceException 
    114112     */ 
    115     public function findByCriterion(stubCriterion $criterion, stubBaseReflectionClass $entityClass, array $arguments = null
     113    public function findByCriterion(stubCriterion $criterion, stubBaseReflectionClass $entityClass
    116114    { 
    117115        if ($entityClass->hasAnnotation('Entity') === false) { 
     
    121119        $setterMethodHelper = new stubSetterMethodHelper($entityClass); 
    122120        $data               = $this->fetchData($entityClass, $setterMethodHelper, $criterion); 
    123         $finderResult       = new stubDatabaseFinderResult($entityClass, $data, $setterMethodHelper, $arguments); 
     121        $finderResult       = new stubDatabaseFinderResult($entityClass, $data, $setterMethodHelper); 
    124122        return $finderResult; 
    125123    } 
     
    128126     * finds all instances of $entityClass 
    129127     * 
    130      * @param   string                       $entityClass  entity class to find instances of 
    131      * @param   array                        $arguments    optional  arguments for constructor 
    132      * @return  stubDatabaseFinderResult     list of instances of $entityClass found 
     128     * @param   string                    $entityClass  entity class to find instances of 
     129     * @return  stubDatabaseFinderResult  list of instances of $entityClass found 
    133130     * @throws  stubPersistenceException 
    134131     */ 
    135     public function findAll(stubBaseReflectionClass $entityClass, array $arguments = null
     132    public function findAll(stubBaseReflectionClass $entityClass
    136133    { 
    137134        if ($entityClass->hasAnnotation('Entity') === false) { 
     
    141138        $setterMethodHelper = new stubSetterMethodHelper($entityClass); 
    142139        $data               = $this->fetchData($entityClass, $setterMethodHelper); 
    143         $finderResult       = new stubDatabaseFinderResult($entityClass, $data, $setterMethodHelper, $arguments); 
     140        $finderResult       = new stubDatabaseFinderResult($entityClass, $data, $setterMethodHelper); 
    144141        return $finderResult; 
    145142    } 
  • trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinderResult.php

    r1311 r1611  
    3636     */ 
    3737    protected $setterMethodHelper; 
    38     /** 
    39      * list of arguments for entity constructor 
    40      * 
    41      * @var  array<mixed> 
    42      */ 
    43     protected $arguments; 
    4438 
    4539    /** 
     
    4943     * @param  array                    $result              result set from the database query 
    5044     * @param  stubSetterMethodHelper   $setterMethodHelper  list of setter methods for entity 
    51      * @param  array                    $arguments           optional  list of arguments for entity constructor 
    5245     */ 
    53     public function __construct(stubBaseReflectionClass $entityClass, array $result, stubSetterMethodHelper $setterMethodHelper, array $arguments = null
     46    public function __construct(stubBaseReflectionClass $entityClass, array $result, stubSetterMethodHelper $setterMethodHelper
    5447    { 
    5548        $this->entityClass        = $entityClass; 
    5649        $this->resultIterator     = new ArrayIterator($result); 
    5750        $this->setterMethodHelper = $setterMethodHelper; 
    58         $this->arguments          = $arguments; 
    5951    } 
    6052 
     
    8880    { 
    8981        try { 
    90             $entity = ((null === $this->arguments) ? ($this->entityClass->newInstance()) : ($this->entityClass->newInstanceArgs($this->arguments))); 
     82            $entity = $this->entityClass->newInstance(); 
    9183        } catch (ReflectionException $re) { 
    9284            throw new stubDatabaseFinderException('Can not create a new instance of ' . $this->entityClass->getFullQualifiedClassName(), $re); 
  • trunk/src/test/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinderTestCase.php

    r1307 r1611  
    1515require_once dirname(__FILE__) . '/../MockNoEntityAnnotationEntity.php'; 
    1616/** 
    17  * This is an entity that requires arguments in the constructor. 
    18  * 
    19  * @Entity 
    20  * @DBTable(name='bar') 
    21  */ 
    22 class MockInstanceArgsEntity extends stubBaseObject 
    23 { 
    24     protected $arg1; 
    25     protected $arg2; 
    26      
    27     public function __construct($arg1, $arg2 = null) 
    28     { 
    29         $this->arg1 = $arg1; 
    30         $this->arg2 = $arg2; 
    31     } 
    32      
    33     /** 
    34      * returns the first argument 
    35      * 
    36      * @return  string 
    37      * @Transient 
    38      */ 
    39     public function getArg1() 
    40     { 
    41         return $this->arg1; 
    42     } 
    43  
    44     /** 
    45      * returns the second argument 
    46      * 
    47      * @return  string 
    48      * @Transient 
    49      */ 
    50     public function getArg2() 
    51     { 
    52         return $this->arg2; 
    53     } 
    54 } 
    55 /** 
    5617 * Test for net::stubbles::rdbms::persistence::finder::stubDatabaseFinder. 
    5718 * 
     
    195156        $this->assertEquals('And this is default.', $entity->getDefaultValue()); 
    196157        $this->assertEquals('MockNoTableAnnotationEntitys', $this->mockQueryBuilder->getSelect()->getBaseTableName()); 
    197     } 
    198  
    199     /** 
    200      * test that instance args are transmitted correct 
    201      * 
    202      * @test 
    203      */ 
    204     public function byPrimaryKeysWithInstanceArguments() 
    205     { 
    206         $mockCriterion = $this->getMock('stubCriterion'); 
    207         $mockCriterion->expects($this->any())->method('toSQL')->will($this->returnValue('example')); 
    208         $mockResult = $this->getMock('stubDatabaseResult'); 
    209         $this->mockConnection->expects($this->any())->method('query')->will($this->returnValue($mockResult)); 
    210         $mockResult->expects($this->exactly(2)) 
    211                    ->method('fetch') 
    212                    ->will($this->returnValue(array('foo' => 'notUsed'))); 
    213         $entity = $this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockInstanceArgsEntity'), array(), array('foo', 'bar')); 
    214         $this->assertEquals('foo', $entity->getArg1()); 
    215         $this->assertEquals('bar', $entity->getArg2()); 
    216         $entity = $this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockInstanceArgsEntity'), array(), array('foo')); 
    217         $this->assertEquals('foo', $entity->getArg1()); 
    218         $this->assertNull($entity->getArg2()); 
    219158    } 
    220159 
     
    257196 
    258197    /** 
    259      * test that instance args are transmitted correct 
    260      * 
    261      * @test 
    262      */ 
    263     public function byCriterionWithInstanceArguments() 
    264     { 
    265         $mockCriterion = $this->getMock('stubCriterion'); 
    266         $mockCriterion->expects($this->any())->method('toSQL')->will($this->returnValue('example')); 
    267         $mockResult = $this->getMock('stubDatabaseResult'); 
    268         $this->mockConnection->expects($this->any())->method('query')->will($this->returnValue($mockResult)); 
    269         $mockResult->expects($this->exactly(2)) 
    270                    ->method('fetchAll') 
    271                    ->will($this->returnValue(array(array()))); 
    272         $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockInstanceArgsEntity'), array('foo', 'bar')); 
    273         $data = $finderResult->current(); 
    274         $this->assertEquals('foo', $data->getArg1()); 
    275         $this->assertEquals('bar', $data->getArg2()); 
    276         $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockInstanceArgsEntity'), array('foo')); 
    277         $data = $finderResult->current(); 
    278         $this->assertEquals('foo', $data->getArg1()); 
    279         $this->assertNull($data->getArg2()); 
    280         $select = $this->mockQueryBuilder->getSelect(); 
    281         $this->assertEquals('bar', $select->getBaseTableName()); 
    282         $this->assertTrue($select->hasCriterion()); 
    283     } 
    284  
    285     /** 
    286198     * test that finding data for all instances of an object works as expected 
    287199     * 
     
    306218        $this->assertFalse($select->hasCriterion()); 
    307219    } 
    308  
    309     /** 
    310      * test that instance args are transmitted correct 
    311      * 
    312      * @test 
    313      */ 
    314     public function findAllWithInstanceArguments() 
    315     { 
    316         $mockResult = $this->getMock('stubDatabaseResult'); 
    317         $this->mockConnection->expects($this->any())->method('query')->will($this->returnValue($mockResult)); 
    318         $mockResult->expects($this->exactly(2)) 
    319                    ->method('fetchAll') 
    320                    ->will($this->returnValue(array(array()))); 
    321         $finderResult = $this->dbFinder->findAll(new stubReflectionClass('MockInstanceArgsEntity'), array('foo', 'bar')); 
    322         $data = $finderResult->current(); 
    323         $this->assertEquals('foo', $data->getArg1()); 
    324         $this->assertEquals('bar', $data->getArg2()); 
    325         $finderResult = $this->dbFinder->findAll(new stubReflectionClass('MockInstanceArgsEntity'), array('foo')); 
    326         $data = $finderResult->current(); 
    327         $this->assertEquals('foo', $data->getArg1()); 
    328         $this->assertNull($data->getArg2()); 
    329         $select = $this->mockQueryBuilder->getSelect(); 
    330         $this->assertEquals('bar', $select->getBaseTableName()); 
    331         $this->assertFalse($select->hasCriterion()); 
    332     } 
    333220} 
    334221?>