Changeset 925
- Timestamp:
- 09/18/07 21:55:07 (1 year ago)
- Files:
-
- trunk/src/main/php/net/stubbles/rdbms/persistence/eraser/stubDatabaseEraser.php (modified) (2 diffs)
- trunk/src/test/php/net/stubbles/rdbms/RDBMSTestSuite.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/rdbms/persistence/MockNoTableAnnotationPersistable.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/rdbms/persistence/MockSinglePrimaryKeyPersistable.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/rdbms/persistence/eraser/stubDatabaseEraserTestCase.php (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/rdbms/persistence/eraser/stubDatabaseEraser.php
r913 r925 79 79 * delete an entity from database by its primary keys 80 80 * 81 * @param stubObject$entity81 * @param object $entity 82 82 * @throws stubDatabaseEraserException 83 83 * @throws stubPersistenceException 84 84 */ 85 public function deleteByPrimaryKeys( stubObject$entity)85 public function deleteByPrimaryKeys($entity) 86 86 { 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))); 88 92 if ($entityClass->hasAnnotation('Entity') === false) { 89 93 throw new stubPersistenceException('Class ' . $entity->getClassName() . ' is not an entity.'); … … 134 138 $table = $this->getTableDescription($entityClass)->getName(); 135 139 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)); 137 141 $deletedRows = $result->count(); 138 142 $result->free(); trunk/src/test/php/net/stubbles/rdbms/RDBMSTestSuite.php
r903 r925 45 45 # $this->addTestFile($dir . '/persistence/stubSetterMethodHelperTestCase.php'); 46 46 $this->addTestFile($dir . '/persistence/creator/stubDatabaseCreatorTestCase.php'); 47 #$this->addTestFile($dir . '/persistence/eraser/stubDatabaseEraserTestCase.php');47 $this->addTestFile($dir . '/persistence/eraser/stubDatabaseEraserTestCase.php'); 48 48 # $this->addTestFile($dir . '/persistence/finder/stubDatabaseFinderTestCase.php'); 49 49 # $this->addTestFile($dir . '/persistence/serializer/stubDatabaseSerializerTestCase.php'); trunk/src/test/php/net/stubbles/rdbms/persistence/MockNoTableAnnotationPersistable.php
r903 r925 15 15 class MockNoTableAnnotationPersistable extends stubBaseObject 16 16 { 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 17 34 /** 18 35 * returns the primary key trunk/src/test/php/net/stubbles/rdbms/persistence/MockSinglePrimaryKeyPersistable.php
r903 r925 28 28 */ 29 29 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 } 30 46 31 47 /** trunk/src/test/php/net/stubbles/rdbms/persistence/eraser/stubDatabaseEraserTestCase.php
r913 r925 16 16 require_once dirname(__FILE__) . '/../MockSinglePrimaryKeyPersistable.php'; 17 17 /** 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 */ 24 class MockNoPrimaryKeyEntity extends stubBaseObject 25 { 26 public function getFoo() { return 'foo'; } 27 } 28 /** 18 29 * Test for net.stubbles.rdbms.persistence.eraser.stubDatabaseEraser. 19 30 * … … 103 114 * check that a class that is missing the DBTable annotation throws an exception 104 115 */ 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() 106 135 { 107 136 $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()); 118 138 } 119 139 … … 122 142 */ 123 143 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() 124 157 { 125 158 $entity = new MockSinglePrimaryKeyPersistable(); … … 142 175 $mockResult->setReturnValueAt(0, 'count', 0); 143 176 $mockResult->setReturnValueAt(1, 'count', 1); 144 $data = $this->dbEraser->deleteByCriterion($mockCriterion, 'MockSinglePrimaryKeyPersistable');177 $data = $this->dbEraser->deleteByCriterion($mockCriterion, new stubReflectionClass('MockNoTableAnnotationPersistable')); 145 178 $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')); 147 199 $this->assertEqual($data, 1); 148 200 $this->assertEqual($this->mockQueryBuilder->getDeleteTable(), 'foo'); … … 156 208 { 157 209 $this->expectException('stubPersistenceException'); 158 $this->dbEraser->deleteByCriterion(new MockstubCriterion(), 'MockNoEntityAnnotationPersistable');210 $this->dbEraser->deleteByCriterion(new MockstubCriterion(), new stubReflectionClass('MockNoEntityAnnotationPersistable')); 159 211 } 160 212 }
