Changeset 928

Show
Ignore:
Timestamp:
09/18/07 23:21:03 (1 year ago)
Author:
mikey
Message:

refactored finder to get rid of stubPersistable

Files:

Legend:

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

    r913 r928  
    11<?php 
    22/** 
    3  * Class for finding the data of a persistable object within a database. 
     3 * Class for finding the data of an entity object within a database. 
    44 * 
    55 * @author      Frank Kleine <mikey@stubbles.net> 
     
    1515                      'net.stubbles.rdbms.persistence.finder.stubDatabaseFinderResult', 
    1616                      'net.stubbles.rdbms.querybuilder.stubDatabaseQueryBuilderFactory', 
    17                       'net.stubbles.rdbms.querybuilder.stubDatabaseSelect', 
    18                       'net.stubbles.reflection.reflection' 
     17                      'net.stubbles.rdbms.querybuilder.stubDatabaseSelect' 
    1918); 
    2019/** 
    21  * Class for finding the data of a persistable object within a database. 
     20 * Class for finding the data of an entity object within a database. 
    2221 * 
    2322 * @package     stubbles 
     
    168167            } 
    169168             
    170             if ($column->isPrimaryKey() === true && isset($primaryKeys[$column->getName()]) === true) { 
    171                 $select->addCriterion(new stubEqualCriterion($column->getName(), $primaryKeys[$column->getName(), $select->getBaseTableName())); 
     169            if ($column->isPrimaryKey() === true && isset($primaryKeys[$this->getPropertyName($method->getName())]) === true) { 
     170                $select->addCriterion(new stubEqualCriterion($column->getName(), $primaryKeys[$this->getPropertyName($method->getName())], $select->getBaseTableName())); 
     171            } elseif ($column->isPrimaryKey() === true && isset($primaryKeys[$column->getName()]) === true) { 
     172                $select->addCriterion(new stubEqualCriterion($column->getName(), $primaryKeys[$column->getName()], $select->getBaseTableName())); 
    172173            } 
    173174             
  • trunk/src/main/php/net/stubbles/rdbms/persistence/stubPersistenceHelper.php

    r911 r928  
    7878        } else { 
    7979            $column     = new stubDatabaseTableColumn(); 
    80             $columnName = str_replace('get', '', $method->getName()); 
    81             $column->setName(strtolower($columnName{0}) . substr($columnName, 1)); 
     80            $column->setName($this->getPropertyName($method->getName())); 
    8281            $returnType = $method->getReturnType(); 
    8382            if (null === $returnType) { 
     
    122121        return $column; 
    123122    } 
     123 
     124    /** 
     125     * creates the property name from the name of the method 
     126     * 
     127     * @param   string  $methodName 
     128     * @return  string 
     129     */ 
     130    protected function getPropertyName($methodName) 
     131    { 
     132        $propertyName = str_replace('is', '', str_replace('get', '', $methodName)); 
     133        return strtolower($propertyName{0}) . substr($propertyName, 1); 
     134    } 
    124135} 
    125136?> 
  • trunk/src/test/php/net/stubbles/rdbms/RDBMSTestSuite.php

    r925 r928  
    4646        $this->addTestFile($dir . '/persistence/creator/stubDatabaseCreatorTestCase.php'); 
    4747        $this->addTestFile($dir . '/persistence/eraser/stubDatabaseEraserTestCase.php'); 
    48        # $this->addTestFile($dir . '/persistence/finder/stubDatabaseFinderTestCase.php'); 
     48        $this->addTestFile($dir . '/persistence/finder/stubDatabaseFinderTestCase.php'); 
    4949       # $this->addTestFile($dir . '/persistence/serializer/stubDatabaseSerializerTestCase.php'); 
    5050         
  • trunk/src/test/php/net/stubbles/rdbms/persistence/MockNoTableAnnotationPersistable.php

    r925 r928  
    100100 
    101101    /** 
     102     * setter method to prevent exceptions 
     103     * 
     104     * @param  int  $intValue 
     105     */ 
     106    public function setIntValue($intValue) 
     107    { 
     108        // ignore this 
     109    } 
     110 
     111    /** 
    102112     * method that has a no annotation 
    103113     * 
     
    110120 
    111121    /** 
     122     * setter method to prevent exceptions 
     123     * 
     124     * @param  bool  $boolValue 
     125     */ 
     126    public function setBoolValue($boolValue) 
     127    { 
     128        // ignore this 
     129    } 
     130 
     131    /** 
    112132     * method that has a no annotation 
    113133     * 
     
    117137    { 
    118138        return 3.13; 
     139    } 
     140 
     141    /** 
     142     * setter method to prevent exceptions 
     143     * 
     144     * @param  float  $floatValue 
     145     */ 
     146    public function setFloatValue($floatValue) 
     147    { 
     148        // ignore this 
    119149    } 
    120150 
  • trunk/src/test/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinderTestCase.php

    r881 r928  
    11<?php 
    22/** 
    3  * Test for net.stubbles.rdbms.persistence.finder.stubDatabaseFinder 
     3 * Test for net.stubbles.rdbms.persistence.finder.stubDatabaseFinder. 
    44 * 
    55 * @author      Frank Kleine <mikey@stubbles.net> 
     
    1414require_once dirname(__FILE__) . '/../MockNoTableAnnotationPersistable.php'; 
    1515require_once dirname(__FILE__) . '/../MockSinglePrimaryKeyPersistable.php'; 
    16 require_once dirname(__FILE__) . '/../TestNonPersistable.php'; 
     16require_once dirname(__FILE__) . '/../MockNoEntityAnnotationPersistable.php'; 
    1717/** 
    1818 * This is a persistable that requires arguments in the constructor. 
    1919 * 
     20 * @Entity 
    2021 * @DBTable(name='bar') 
    2122 */ 
    22 class MockInstanceArgsPersistable extends stubAbstractPersistable 
     23class MockInstanceArgsPersistable extends stubBaseObject 
    2324{ 
    2425    protected $arg1; 
     
    3132    } 
    3233     
     34    /** 
     35     * returns the first argument 
     36     * 
     37     * @return  string 
     38     * @Transient 
     39     */ 
    3340    public function getArg1() 
    3441    { 
    3542        return $this->arg1; 
    3643    } 
    37      
     44 
     45    /** 
     46     * returns the second argument 
     47     * 
     48     * @return  string 
     49     * @Transient 
     50     */ 
    3851    public function getArg2() 
    3952    { 
     
    4255} 
    4356/** 
    44  * Test for net.stubbles.rdbms.persistence.finder.stubDatabaseFinder 
     57 * Test for net.stubbles.rdbms.persistence.finder.stubDatabaseFinder. 
    4558 * 
    4659 * @package     stubbles 
     
    127140 
    128141    /** 
    129      * check that a class that is missing the DBTable annotation throws an exception 
    130      */ 
    131     public function testByPrimaryKeysClassWithoutDBTableAnnotation() 
    132     { 
    133         $this->expectException('stubDatabaseFinderException'); 
    134         $this->dbFinder->findByPrimaryKeys(new MockNoTableAnnotationPersistable()); 
    135     } 
    136  
    137     /** 
    138      * check that a class that is missing the DBTable annotation throws an exception 
    139      */ 
    140     public function testByCriterionClassWithoutDBTableAnnotation() 
    141     { 
    142         $this->expectException('stubDatabaseFinderException'); 
    143         $this->dbFinder->findByCriterion(new MockstubCriterion(), 'MockNoTableAnnotationPersistable'); 
     142     * test that trying to find a class that does not implement stubPersistable throws an exception 
     143     */ 
     144    public function testByPrimaryKeysNonEntity() 
     145    { 
     146        $this->expectException('stubPersistenceException'); 
     147        $this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockNoEntityAnnotationPersistable'), array()); 
    144148    } 
    145149 
     
    149153    public function testByPrimaryKeys() 
    150154    { 
    151         $singlePrimaryKey = new MockSinglePrimaryKeyPersistable(); 
    152         $singlePrimaryKey->setId('mock'); 
    153155        $mockResult = new MockstubDatabaseResult(); 
    154156        $this->mockConnection->setReturnValue('query', $mockResult); 
    155157        $mockResult->setReturnValueAt(0, 'fetch', false); 
    156         $mockResult->setReturnValueAt(1, 'fetch', array('bar' => 'Here is bar.', 'default' => 'And this is default.')); 
    157         $this->dbFinder->findByPrimaryKeys($singlePrimaryKey); 
    158         $this->assertFalse($singlePrimaryKey->isPersistent()); 
    159         $this->assertEqual($singlePrimaryKey->withAnnotation(), 'this is bar'); 
    160         $this->assertNull($singlePrimaryKey->withDefaultValue()); 
    161         $this->dbFinder->findByPrimaryKeys($singlePrimaryKey); 
    162         $this->assertTrue($singlePrimaryKey->isPersistent()); 
     158        $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')); 
     161        $this->assertEqual($singlePrimaryKey->getId(), 'mock'); 
    163162        $this->assertEqual($singlePrimaryKey->withAnnotation(), 'Here is bar.'); 
    164163        $this->assertEqual($singlePrimaryKey->withDefaultValue(), 'And this is default.'); 
    165164        $select = $this->mockQueryBuilder->getSelect(); 
    166165        $this->assertEqual($select->getBaseTableName(), 'foo'); 
    167         $this->assertTrue($select->hasJoins()); 
    168         $this->assertEqual(array_keys($select->getJoins()), array('blub')); 
    169         $this->assertEqual($select->getCriterion()->toSQL(), "(`foo`.`id` = 'mock')"); 
    170     } 
    171  
    172     /** 
    173      * test that finding data of an object with a criterion works as expected 
    174      */ 
    175     public function testByCriterion() 
     166    } 
     167 
     168    /** 
     169     * test that finding data of an object with its primary keys 
     170     */ 
     171    public function testByPrimaryKeysWithoutTableAnnotation() 
     172    { 
     173        $mockResult = new MockstubDatabaseResult(); 
     174        $this->mockConnection->setReturnValue('query', $mockResult); 
     175        $mockResult->setReturnValueAt(0, 'fetch', false); 
     176        $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')); 
     179        $this->assertEqual($entity->getId(), 'mock'); 
     180        $this->assertEqual($entity->withAnnotation(), 'Here is bar.'); 
     181        $this->assertEqual($entity->getDefaultValue(), 'And this is default.'); 
     182        $select = $this->mockQueryBuilder->getSelect(); 
     183        $this->assertEqual($select->getBaseTableName(), 'MockNoTableAnnotationPersistables'); 
     184    } 
     185 
     186    /** 
     187     * test that instance args are transmitted correct 
     188     */ 
     189    public function testByPrimaryKeysWithInstanceArguments() 
    176190    { 
    177191        $mockCriterion = new MockstubCriterion(); 
     
    179193        $mockResult = new MockstubDatabaseResult(); 
    180194        $this->mockConnection->setReturnValue('query', $mockResult); 
     195        $mockResult->setReturnValue('fetch', array()); 
     196        $entity = $this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockInstanceArgsPersistable'), array(), array('foo', 'bar')); 
     197        $this->assertEqual($entity->getArg1(), 'foo'); 
     198        $this->assertEqual($entity->getArg2(), 'bar'); 
     199        $entity = $this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockInstanceArgsPersistable'), array(), array('foo')); 
     200        $this->assertEqual($entity->getArg1(), 'foo'); 
     201        $this->assertNull($entity->getArg2()); 
     202    } 
     203 
     204    /** 
     205     * test that trying to find a class that does not implement stubPersistable throws an exception 
     206     */ 
     207    public function testByCriterionNonEntity() 
     208    { 
     209        $this->expectException('stubPersistenceException'); 
     210        $this->dbFinder->findByCriterion(new MockstubCriterion(), new stubReflectionClass('MockNoEntityAnnotationPersistable')); 
     211    } 
     212 
     213    /** 
     214     * test that finding data of an object with a criterion works as expected 
     215     */ 
     216    public function testByCriterion() 
     217    { 
     218        $mockCriterion = new MockstubCriterion(); 
     219        $mockCriterion->setReturnValue('toSQL', 'example'); 
     220        $mockResult = new MockstubDatabaseResult(); 
     221        $this->mockConnection->setReturnValue('query', $mockResult); 
    181222        $mockResult->setReturnValueAt(0, 'fetchAll', false); 
    182223        $mockResult->setReturnValueAt(1, 'fetchAll', array(array('bar' => 'Here is bar.', 'default' => 'And this is default.'))); 
    183         $finderResult = $this->dbFinder->findByCriterion($mockCriterion, 'MockSinglePrimaryKeyPersistable'); 
     224        $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockSinglePrimaryKeyPersistable')); 
    184225        $this->assertEqual($finderResult->count(), 0); 
    185         $finderResult = $this->dbFinder->findByCriterion($mockCriterion, 'MockSinglePrimaryKeyPersistable'); 
     226        $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockSinglePrimaryKeyPersistable')); 
    186227        $this->assertEqual($finderResult->count(), 1); 
    187228        $data = $finderResult->current(); 
    188         $this->assertTrue($data->isPersistent()); 
    189229        $this->assertEqual($data->withAnnotation(), 'Here is bar.'); 
    190230        $this->assertEqual($data->withDefaultValue(), 'And this is default.'); 
    191231        $select = $this->mockQueryBuilder->getSelect(); 
    192232        $this->assertEqual($select->getBaseTableName(), 'foo'); 
    193         $this->assertTrue($select->hasJoins()); 
    194         $this->assertEqual(array_keys($select->getJoins()), array('blub')); 
    195         $this->assertEqual($select->getCriterion()->toSQL(), "(example)"); 
    196     } 
    197  
    198     /** 
    199      * test that trying to find a class that does not implement stubPersistable throws an exception 
    200      */ 
    201     public function testByCriterionNonPersistableClass() 
    202     { 
    203         $this->expectException('stubDatabaseFinderException'); 
    204         $this->dbFinder->findByCriterion(new MockstubCriterion(), 'TestNonPersistable'); 
    205233    } 
    206234 
     
    215243        $this->mockConnection->setReturnValue('query', $mockResult); 
    216244        $mockResult->setReturnValue('fetchAll', array(array())); 
    217         $finderResult = $this->dbFinder->findByCriterion($mockCriterion, 'MockInstanceArgsPersistable', array('foo', 'bar')); 
     245        $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockInstanceArgsPersistable'), array('foo', 'bar')); 
    218246        $data = $finderResult->current(); 
    219247        $this->assertEqual($data->getArg1(), 'foo'); 
    220248        $this->assertEqual($data->getArg2(), 'bar'); 
    221         $finderResult = $this->dbFinder->findByCriterion($mockCriterion, 'MockInstanceArgsPersistable', array('foo')); 
     249        $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockInstanceArgsPersistable'), array('foo')); 
    222250        $data = $finderResult->current(); 
    223251        $this->assertEqual($data->getArg1(), 'foo');