Changeset 925

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

finished refactoring of eraser without stubPersistable

Files:

Legend:

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

    r913 r925  
    7979     * delete an entity from database by its primary keys 
    8080     * 
    81      * @param   stubObject                   $entity 
     81     * @param   object                       $entity 
    8282     * @throws  stubDatabaseEraserException 
    8383     * @throws  stubPersistenceException 
    8484     */ 
    85     public function deleteByPrimaryKeys(stubObject $entity) 
     85    public function deleteByPrimaryKeys($entity) 
    8686    { 
    87         $entityClass = $entity->getClass(); 
     87        if (is_object($entity) === false) { 
     88            throw new stubIllegalArgumentException('Can only delete objects.'); 
     89        } 
     90         
     91        $entityClass = (($entity instanceof stubObject) ? ($entity->getClass()) : (new stubReflectionClass($entity))); 
    8892        if ($entityClass->hasAnnotation('Entity') === false) { 
    8993            throw new stubPersistenceException('Class ' . $entity->getClassName() . ' is not an entity.'); 
     
    134138        $table = $this->getTableDescription($entityClass)->getName(); 
    135139        try { 
    136             $result = $this->connection->query(stubDatabaseQueryBuilderFactory::create($this->connection)->createDelete($table, $criterion)); 
     140            $result      = $this->connection->query(stubDatabaseQueryBuilderFactory::create($this->connection)->createDelete($table, $criterion)); 
    137141            $deletedRows = $result->count(); 
    138142            $result->free(); 
  • trunk/src/test/php/net/stubbles/rdbms/RDBMSTestSuite.php

    r903 r925  
    4545       # $this->addTestFile($dir . '/persistence/stubSetterMethodHelperTestCase.php'); 
    4646        $this->addTestFile($dir . '/persistence/creator/stubDatabaseCreatorTestCase.php'); 
    47        # $this->addTestFile($dir . '/persistence/eraser/stubDatabaseEraserTestCase.php'); 
     47        $this->addTestFile($dir . '/persistence/eraser/stubDatabaseEraserTestCase.php'); 
    4848       # $this->addTestFile($dir . '/persistence/finder/stubDatabaseFinderTestCase.php'); 
    4949       # $this->addTestFile($dir . '/persistence/serializer/stubDatabaseSerializerTestCase.php'); 
  • trunk/src/test/php/net/stubbles/rdbms/persistence/MockNoTableAnnotationPersistable.php

    r903 r925  
    1515class MockNoTableAnnotationPersistable extends stubBaseObject 
    1616{ 
     17    /** 
     18     * id of the entity 
     19     * 
     20     * @var  string 
     21     */ 
     22    protected $id; 
     23 
     24    /** 
     25     * sets the id 
     26     * 
     27     * @param  string  $id 
     28     */ 
     29    public function setId($id) 
     30    { 
     31        $this->id = $id; 
     32    } 
     33 
    1734    /** 
    1835     * returns the primary key 
  • trunk/src/test/php/net/stubbles/rdbms/persistence/MockSinglePrimaryKeyPersistable.php

    r903 r925  
    2828     */ 
    2929    protected $bar          = 'this is bar'; 
     30    /** 
     31     * id of the entity 
     32     * 
     33     * @var  string 
     34     */ 
     35    protected $id; 
     36 
     37    /** 
     38     * sets the id 
     39     * 
     40     * @param  string  $id 
     41     */ 
     42    public function setId($id) 
     43    { 
     44        $this->id = $id; 
     45    } 
    3046 
    3147    /** 
  • trunk/src/test/php/net/stubbles/rdbms/persistence/eraser/stubDatabaseEraserTestCase.php

    r913 r925  
    1616require_once dirname(__FILE__) . '/../MockSinglePrimaryKeyPersistable.php'; 
    1717/** 
     18 * mock entity to test an entity that has no primary keys 
     19 * 
     20 * @package     stubbles 
     21 * @subpackage  rdbms_persistence_eraser_test 
     22 * @Entity 
     23 */ 
     24class MockNoPrimaryKeyEntity extends stubBaseObject 
     25{ 
     26    public function getFoo() { return 'foo'; } 
     27} 
     28/** 
    1829 * Test for net.stubbles.rdbms.persistence.eraser.stubDatabaseEraser. 
    1930 * 
     
    103114     * check that a class that is missing the DBTable annotation throws an exception 
    104115     */ 
    105     public function testByPrimaryKeysClassWithoutDBTableAnnotation() 
     116    public function testByPrimaryKeysClassWithNonObject() 
     117    { 
     118        $this->expectException('stubIllegalArgumentException'); 
     119        $this->dbEraser->deleteByPrimaryKeys('foo'); 
     120    } 
     121 
     122    /** 
     123     * test that trying to delete a class that does not implement stubPersistable throws an exception 
     124     */ 
     125    public function testByPrimaryKeysNonEntityClass() 
     126    { 
     127        $this->expectException('stubPersistenceException'); 
     128        $this->dbEraser->deleteByPrimaryKeys(new MockNoEntityAnnotationPersistable()); 
     129    } 
     130 
     131    /** 
     132     * check that a class that is missing any primary keys throws an exception 
     133     */ 
     134    public function testByPrimaryKeysClassWithoutIdAnnotation() 
    106135    { 
    107136        $this->expectException('stubDatabaseEraserException'); 
    108         $this->dbEraser->deleteByPrimaryKeys(new MockNoTableAnnotationPersistable()); 
    109     } 
    110  
    111     /** 
    112      * check that a class that is missing the DBTable annotation throws an exception 
    113      */ 
    114     public function testByCriterionClassWithoutDBTableAnnotation() 
    115     { 
    116         $this->expectException('stubDatabaseEraserException'); 
    117         $this->dbEraser->deleteByCriterion(new MockstubCriterion(), 'MockNoTableAnnotationPersistable'); 
     137        $this->dbEraser->deleteByPrimaryKeys(new MockNoPrimaryKeyEntity()); 
    118138    } 
    119139 
     
    122142     */ 
    123143    public function testByPrimaryKeys() 
     144    { 
     145        $entity = new MockNoTableAnnotationPersistable(); 
     146        $entity->setId('mock'); 
     147        $this->mockConnection->setReturnValue('query', new MockstubDatabaseResult()); 
     148        $this->dbEraser->deleteByPrimaryKeys($entity); 
     149        $this->assertEqual($this->mockQueryBuilder->getDeleteTable(), 'MockNoTableAnnotationPersistables'); 
     150        $this->assertEqual($this->mockQueryBuilder->getDeleteCriterion()->toSQL(), "(`MockNoTableAnnotationPersistables`.`id` = 'mock')"); 
     151    } 
     152 
     153    /** 
     154     * test that deleting data of an object with its primary keys 
     155     */ 
     156    public function testByPrimaryKeysWithDBTableAnnotation() 
    124157    { 
    125158        $entity = new MockSinglePrimaryKeyPersistable(); 
     
    142175        $mockResult->setReturnValueAt(0, 'count', 0); 
    143176        $mockResult->setReturnValueAt(1, 'count', 1); 
    144         $data = $this->dbEraser->deleteByCriterion($mockCriterion, 'MockSinglePrimaryKeyPersistable'); 
     177        $data = $this->dbEraser->deleteByCriterion($mockCriterion, new stubReflectionClass('MockNoTableAnnotationPersistable')); 
    145178        $this->assertEqual($data, 0); 
    146         $data = $this->dbEraser->deleteByCriterion($mockCriterion, 'MockSinglePrimaryKeyPersistable'); 
     179        $data = $this->dbEraser->deleteByCriterion($mockCriterion, new stubReflectionClass('MockNoTableAnnotationPersistable')); 
     180        $this->assertEqual($data, 1); 
     181        $this->assertEqual($this->mockQueryBuilder->getDeleteTable(), 'MockNoTableAnnotationPersistables'); 
     182        $this->assertEqual($this->mockQueryBuilder->getDeleteCriterion()->toSQL(), "example"); 
     183    } 
     184 
     185    /** 
     186     * test that deleting data of an object with a criterion works as expected 
     187     */ 
     188    public function testByCriterionWithDBTableAnnotation() 
     189    { 
     190        $mockCriterion = new MockstubCriterion(); 
     191        $mockCriterion->setReturnValue('toSQL', 'example'); 
     192        $mockResult = new MockstubDatabaseResult(); 
     193        $this->mockConnection->setReturnValue('query', $mockResult); 
     194        $mockResult->setReturnValueAt(0, 'count', 0); 
     195        $mockResult->setReturnValueAt(1, 'count', 1); 
     196        $data = $this->dbEraser->deleteByCriterion($mockCriterion, new stubReflectionClass('MockSinglePrimaryKeyPersistable')); 
     197        $this->assertEqual($data, 0); 
     198        $data = $this->dbEraser->deleteByCriterion($mockCriterion, new stubReflectionClass('MockSinglePrimaryKeyPersistable')); 
    147199        $this->assertEqual($data, 1); 
    148200        $this->assertEqual($this->mockQueryBuilder->getDeleteTable(), 'foo'); 
     
    156208    { 
    157209        $this->expectException('stubPersistenceException'); 
    158         $this->dbEraser->deleteByCriterion(new MockstubCriterion(), 'MockNoEntityAnnotationPersistable'); 
     210        $this->dbEraser->deleteByCriterion(new MockstubCriterion(), new stubReflectionClass('MockNoEntityAnnotationPersistable')); 
    159211    } 
    160212}