- Timestamp:
- 10/01/07 23:39:52 (1 year ago)
- Files:
-
- trunk/experiments/people/mikey/persistence/MyNewsArticle.php (modified) (3 diffs)
- trunk/experiments/people/mikey/persistence/createTable.php (modified) (2 diffs)
- trunk/experiments/people/mikey/persistence/deleteEntry.php (modified) (1 diff)
- trunk/experiments/people/mikey/persistence/fetchEntry.php (modified) (2 diffs)
- trunk/experiments/people/mikey/persistence/insertEntry.php (modified) (1 diff)
- trunk/experiments/people/mikey/persistence/listEntries.php (modified) (2 diffs)
- trunk/experiments/people/mikey/persistence/updateEntry.php (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinder.php (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinderResult.php (modified) (1 diff)
- trunk/src/main/php/net/stubbles/rdbms/persistence/serializer/stubDatabaseSerializer.php (modified) (5 diffs)
- trunk/src/main/php/net/stubbles/rdbms/persistence/stubAbstractPersistable.php (deleted)
- trunk/src/main/php/net/stubbles/rdbms/persistence/stubPersistable.php (deleted)
- trunk/src/test/php/net/stubbles/rdbms/RDBMSTestSuite.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/rdbms/persistence/MockMultiplePrimaryKeyPersistable.php (deleted)
- trunk/src/test/php/net/stubbles/rdbms/persistence/MockNoEntityAnnotationEntity.php (moved) (moved from trunk/src/test/php/net/stubbles/rdbms/persistence/MockNoEntityAnnotationPersistable.php) (2 diffs)
- trunk/src/test/php/net/stubbles/rdbms/persistence/MockNoTableAnnotationEntity.php (moved) (moved from trunk/src/test/php/net/stubbles/rdbms/persistence/MockNoTableAnnotationPersistable.php) (3 diffs)
- trunk/src/test/php/net/stubbles/rdbms/persistence/MockSinglePrimaryKeyEntity.php (moved) (moved from trunk/src/test/php/net/stubbles/rdbms/persistence/MockSinglePrimaryKeyPersistable.php) (3 diffs)
- trunk/src/test/php/net/stubbles/rdbms/persistence/TestNonPersistable.php (deleted)
- trunk/src/test/php/net/stubbles/rdbms/persistence/creator/stubDatabaseCreatorTestCase.php (modified) (4 diffs)
- trunk/src/test/php/net/stubbles/rdbms/persistence/eraser/stubDatabaseEraserTestCase.php (modified) (8 diffs)
- trunk/src/test/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinderTestCase.php (modified) (8 diffs)
- trunk/src/test/php/net/stubbles/rdbms/persistence/serializer/stubDatabaseSerializerTestCase.php (modified) (5 diffs)
- trunk/src/test/php/net/stubbles/rdbms/persistence/stubSetterMethodHelperTestCase.php (modified) (3 diffs)
- trunk/src/test/php/net/stubbles/rdbms/querybuilder/stubDatabaseSelectTestCase.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/rdbms/querybuilder/stubDatabaseTableRowTestCase.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/experiments/people/mikey/persistence/MyNewsArticle.php
r881 r951 3 3 * class to test the persistence api 4 4 * 5 * @Entity 5 6 * @DBTable(name='news', type='InnoDB') 6 7 */ 7 class MyNewsArticle extends stubAbstractPersistable8 class MyNewsArticle 8 9 { 9 10 /** 11 * id of the news article 12 * 13 * @var int 14 */ 15 protected $id; 16 /** 10 17 * headline of the news article 11 18 * … … 51 58 52 59 /** 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) 57 75 */ 58 76 public function getId() … … 206 224 { 207 225 return $this->status; 208 }209 210 /**211 * returns a list of all instances with status $status212 *213 * @param stubDatabaseFinder $finder the finder to use214 * @param int $status status that the instances must have215 * @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__);221 226 } 222 227 } trunk/experiments/people/mikey/persistence/createTable.php
r512 r951 3 3 require_once '../../../../src/main/php/net/stubbles/stubClassLoader.php'; 4 4 stubClassLoader::load('net.stubbles.rdbms.rdbms', 5 'net.stubbles.rdbms.persistence.stubAbstractPersistable',6 5 'net.stubbles.rdbms.persistence.creator.stubDatabaseCreator' 7 6 ); … … 10 9 11 10 $creator = stubDatabaseCreator::getInstance(stubDatabaseConnectionPool::getConnection()); 12 $creator->createTable( 'MyNewsArticle');11 $creator->createTable(new stubReflectionClass('MyNewsArticle')); 13 12 ?> trunk/experiments/people/mikey/persistence/deleteEntry.php
r655 r951 3 3 require_once '../../../../src/main/php/net/stubbles/stubClassLoader.php'; 4 4 stubClassLoader::load('net.stubbles.rdbms.rdbms', 5 'net.stubbles.rdbms.persistence.stubAbstractPersistable',6 5 'net.stubbles.rdbms.persistence.eraser.stubDatabaseEraser', 7 6 'net.stubbles.rdbms.persistence.finder.stubDatabaseFinder' trunk/experiments/people/mikey/persistence/fetchEntry.php
r512 r951 3 3 require_once '../../../../src/main/php/net/stubbles/stubClassLoader.php'; 4 4 stubClassLoader::load('net.stubbles.rdbms.rdbms', 5 'net.stubbles.rdbms.persistence.stubAbstractPersistable',6 5 'net.stubbles.rdbms.persistence.finder.stubDatabaseFinder' 7 6 ); … … 9 8 require_once 'connection.php'; 10 9 11 $newsArticle = new MyNewsArticle();12 $newsArticle->setId(1);13 echo "Existing before finder:\n";14 var_dump($newsArticle);15 16 10 $finder = stubDatabaseFinder::getInstance(stubDatabaseConnectionPool::getConnection()); 17 $ finder->findByPrimaryKeys($newsArticle);11 $newsArticle = $finder->findByPrimaryKeys(new stubReflectionClass('MyNewsArticle'), array('id' => 1)); 18 12 echo "Existing after finder:\n"; 19 13 var_dump($newsArticle); 20 14 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)); 26 16 echo "Non-existing after finder:\n"; 27 17 var_dump($newsArticle); trunk/experiments/people/mikey/persistence/insertEntry.php
r512 r951 3 3 require_once '../../../../src/main/php/net/stubbles/stubClassLoader.php'; 4 4 stubClassLoader::load('net.stubbles.rdbms.rdbms', 5 'net.stubbles.rdbms.persistence.stubAbstractPersistable',6 5 'net.stubbles.rdbms.persistence.serializer.stubDatabaseSerializer' 7 6 ); trunk/experiments/people/mikey/persistence/listEntries.php
r881 r951 4 4 stubClassLoader::load('net.stubbles.rdbms.rdbms', 5 5 'net.stubbles.rdbms.criteria.stubEqualCriterion', 6 'net.stubbles.rdbms.persistence.stubAbstractPersistable',7 6 'net.stubbles.rdbms.persistence.finder.stubDatabaseFinder' 8 7 ); … … 11 10 12 11 $finder = stubDatabaseFinder::getInstance(stubDatabaseConnectionPool::getConnection()); 13 $newsArticles = MyNewsArticle::findByStatus($finder, 5);12 $newsArticles = $finder->findByCriterion(new stubEqualCriterion('status', 5, 'news'), new stubReflectionClass('MyNewsArticle')); 14 13 var_dump($newsArticles->count()); 15 14 foreach ($newsArticles as $newsArticle) { trunk/experiments/people/mikey/persistence/updateEntry.php
r512 r951 3 3 require_once '../../../../src/main/php/net/stubbles/stubClassLoader.php'; 4 4 stubClassLoader::load('net.stubbles.rdbms.rdbms', 5 'net.stubbles.rdbms.persistence.stubAbstractPersistable',6 5 'net.stubbles.rdbms.persistence.finder.stubDatabaseFinder', 7 6 'net.stubbles.rdbms.persistence.serializer.stubDatabaseSerializer' … … 10 9 require_once 'connection.php'; 11 10 12 $newsArticle = new MyNewsArticle();13 $newsArticle->setId(4);14 15 11 $finder = stubDatabaseFinder::getInstance(stubDatabaseConnectionPool::getConnection()); 16 $ finder->findByPrimaryKeys($newsArticle);12 $newsArticle = $finder->findByPrimaryKeys(new stubReflectionClass('MyNewsArticle'), array('id' => 4)); 17 13 echo "After finder:\n"; 18 14 var_dump($newsArticle); trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinder.php
r928 r951 79 79 80 80 /** 81 * get a persistable objectfrom database by its primary keys81 * get an entity from database by its primary keys 82 82 * 83 83 * @param stubBaseReflectionClass $entityClass class information about the entity … … 114 114 115 115 /** 116 * finds all instances of $ persistableClassNameby given criterion116 * finds all instances of $entityClass by given criterion 117 117 * 118 118 * @param stubCriterion $criterion 119 * @param string $ persistableClassName non qualified classname of the persistableclass to find instances of120 * @param array $arguments optional arguments for constructor119 * @param string $entityClass entity class to find instances of 120 * @param array $arguments optional arguments for constructor 121 121 * @return stubDatabaseFinderResult list of instances of $entityClass found with $criterion 122 122 * @throws stubDatabaseFinderException trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinderResult.php
r912 r951 8 8 */ 9 9 stubClassLoader::load('net.stubbles.rdbms.persistence.finder.stubDatabaseFinderException', 10 'net.stubbles.rdbms.persistence.stubPersistable',11 10 'net.stubbles.reflection.stubReflectionClass' 12 11 ); trunk/src/main/php/net/stubbles/rdbms/persistence/serializer/stubDatabaseSerializer.php
r913 r951 89 89 } 90 90 91 $entityClass = (($entity instanceof stubObject) ? ($entity->getClass()) : (new stubReflection Class($entity)));91 $entityClass = (($entity instanceof stubObject) ? ($entity->getClass()) : (new stubReflectionObject($entity))); 92 92 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.'); 94 94 } 95 95 96 96 $tableRow = new stubDatabaseTableRow($this->getTableDescription($entityClass)->getName()); 97 $methods = $ refObject->getMethods();97 $methods = $entityClass->getMethods(); 98 98 $singlePrimaryKey = null; 99 99 $defaultValues = array(); … … 107 107 $value = $method->invoke($entity); 108 108 } 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); 110 110 } 111 111 … … 113 113 if (null === $value) { 114 114 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()); 116 116 } 117 117 118 $singlePrimaryKey = array(' propertyName' => $method->getName(),119 'tableName' => $table Name118 $singlePrimaryKey = array('setterMethod' => stubSetterMethodHelper::create($column, $entityClass, $method->getName()), 119 'tableName' => $tableRow->getTableName() 120 120 ); 121 121 continue; 122 122 } else { 123 $tableRow->addCriterion(new stubEqualCriterion($column->getName(), $value, $table Name));123 $tableRow->addCriterion(new stubEqualCriterion($column->getName(), $value, $tableRow->getTableName())); 124 124 } 125 125 } 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.'); 129 129 } 130 130 131 131 $defaultValues[] = array('setterMethod' => stubSetterMethodHelper::create($column, $entityClass, $method->getName()), 132 'value' => $value 132 'value' => $value, 133 'defaultValue' => $defaultValue, 134 'column' => $column->getName() 133 135 ); 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); 142 138 } 143 139 } 144 140 145 141 try { 146 $this->processQueries($this->getQuery($tableRow ), $entity, $singlePrimaryKey);142 $this->processQueries($this->getQuery($tableRow, $entity, $defaultValues), $entity, $singlePrimaryKey); 147 143 } 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); 149 145 } 150 146 } … … 154 150 * 155 151 * @param stubDatabaseTableRow $tableRow 152 * @param array $defaultValues 156 153 * @return array<string,string> 157 154 * @throws 158 155 */ 159 protected function getQuery(stubDatabaseTableRow $tableRow )156 protected function getQuery(stubDatabaseTableRow $tableRow, $entity, array $defaultValues) 160 157 { 161 158 $queryBuilder = stubDatabaseQueryBuilderFactory::create($this->connection); 162 159 try { 160 // criterion for update exists => update 163 161 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)); 165 168 } 166 169 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)); 168 177 } catch (stubIllegalArgumentException $iae) { 169 178 throw new stubDatabaseSerializerException('Creating the queries failed.', $iae); … … 175 184 * 176 185 * @param array<string,string> $queries list of queries to process 177 * @param stubPersistable $persistable the persistable object186 * @param object $entity the entity to process the queries for 178 187 * @param array<string,string> $singlePrimaryKey optional information about the single primary key 179 188 * @throws stubDatabaseException 180 189 */ 181 protected function processQueries(array $queries, stubPersistable $persistable, array $singlePrimaryKey = null)190 protected function processQueries(array $queries, $entity, array $singlePrimaryKey = null) 182 191 { 183 192 foreach ($queries as $tableName => $query) { 184 193 $this->connection->exec($query); 185 194 if (null !== $singlePrimaryKey && $singlePrimaryKey['tableName'] == $tableName) { 186 $ persistable->setId($this->connection->getLastInsertId());195 $singlePrimaryKey['setterMethod']->invokeArgs($entity, array($this->connection->getLastInsertId())); 187 196 } 188 197 } trunk/src/test/php/net/stubbles/rdbms/RDBMSTestSuite.php
r928 r951 43 43 44 44 // persistence 45 #$this->addTestFile($dir . '/persistence/stubSetterMethodHelperTestCase.php');45 $this->addTestFile($dir . '/persistence/stubSetterMethodHelperTestCase.php'); 46 46 $this->addTestFile($dir . '/persistence/creator/stubDatabaseCreatorTestCase.php'); 47 47 $this->addTestFile($dir . '/persistence/eraser/stubDatabaseEraserTestCase.php'); 48 48 $this->addTestFile($dir . '/persistence/finder/stubDatabaseFinderTestCase.php'); 49 #$this->addTestFile($dir . '/persistence/serializer/stubDatabaseSerializerTestCase.php');49 $this->addTestFile($dir . '/persistence/serializer/stubDatabaseSerializerTestCase.php'); 50 50 51 51 // querybuilder trunk/src/test/php/net/stubbles/rdbms/persistence/MockNoEntityAnnotationEntity.php
r899 r951 1 1 <?php 2 2 /** 3 * This is a mocked persistable that is missing the DBTableannotation.3 * This is a mocked entity that is missing the @Entity annotation. 4 4 * 5 5 * @package stubbles … … 7 7 */ 8 8 /** 9 * This is a mocked persistable that is missing the DBTableannotation.9 * This is a mocked entity that is missing the @Entity annotation. 10 10 * 11 11 * @package stubbles 12 12 * @subpackage rdbms_persistence_test 13 13 */ 14 class MockNoEntityAnnotation Persistableextends stubBaseObject14 class MockNoEntityAnnotationEntity extends stubBaseObject 15 15 { 16 16 // intentionally empty trunk/src/test/php/net/stubbles/rdbms/persistence/MockNoTableAnnotationEntity.php
r928 r951 1 1 <?php 2 2 /** 3 * This is a mocked persistable that is missing theDBTable annotation.3 * This is a mocked entity that is missing the @DBTable annotation. 4 4 * 5 5 * @package stubbles … … 7 7 */ 8 8 /** 9 * This is a mocked persistable that is missing theDBTable annotation.9 * This is a mocked entity that is missing the @DBTable annotation. 10 10 * 11 11 * @package stubbles … … 13 13 * @Entity 14 14 */ 15 class MockNoTableAnnotation Persistableextends stubBaseObject15 class MockNoTableAnnotationEntity extends stubBaseObject 16 16 { 17 17 /** trunk/src/test/php/net/stubbles/rdbms/persistence/MockSinglePrimaryKeyEntity.php
r925 r951 1 1 <?php 2 2 /** 3 * This is a mocked persistablethat has some annotations.3 * This is a mocked entity that has some annotations. 4 4 * 5 5 * @package stubbles … … 7 7 */ 8 8 /** 9 * This is a mocked persistablethat has some annotations.9 * This is a mocked entity that has some annotations. 10 10 * 11 11 * @package stubbles … … 14 14 * @DBTable(name='foo') 15 15 */ 16 class MockSinglePrimaryKey Persistableextends stubBaseObject16 class MockSinglePrimaryKeyEntity extends stubBaseObject 17 17 { 18 18 /** trunk/src/test/php/net/stubbles/rdbms/persistence/creator/stubDatabaseCreatorTestCase.php
r907 r951 10 10 Mock::generate('stubDatabaseConnection'); 11 11 require_once dirname(__FILE__) . '/../../querybuilder/TeststubDatabaseQueryBuilder.php'; 12 require_once dirname(__FILE__) . '/../MockNoEntityAnnotation Persistable.php';13 require_once dirname(__FILE__) . '/../MockNoTableAnnotation Persistable.php';14 require_once dirname(__FILE__) . '/../MockSinglePrimaryKey Persistable.php';12 require_once dirname(__FILE__) . '/../MockNoEntityAnnotationEntity.php'; 13 require_once dirname(__FILE__) . '/../MockNoTableAnnotationEntity.php'; 14 require_once dirname(__FILE__) . '/../MockSinglePrimaryKeyEntity.php'; 15 15 /** 16 16 * Test for net.stubbles.rdbms.persistence.creator.stubDatabaseCreator … … 104 104 { 105 105 $this->expectException('stubPersistenceException'); 106 $this->dbCreator->createTable(new stubReflectionClass('MockNoEntityAnnotation Persistable'));106 $this->dbCreator->createTable(new stubReflectionClass('MockNoEntityAnnotationEntity')); 107 107 } 108 108 … … 112 112 public function testClassWithoutDBTableAnnotation() 113 113 { 114 $this->dbCreator->createTable(new stubReflectionClass('MockNoTableAnnotation Persistable'));114 $this->dbCreator->createTable(new stubReflectionClass('MockNoTableAnnotationEntity')); 115 115 $tableDescription = $this->mockQueryBuilder->getTableDescription(); 116 $this->assertEqual($tableDescription->getName(), 'MockNoTableAnnotation Persistables');116 $this->assertEqual($tableDescription->getName(), 'MockNoTableAnnotationEntitys'); 117 117 $columns = $tableDescription->getColumns(); 118 118 $this->assertEqual(count($columns), 6); … … 148 148 public function testClassWithDBTableAnnotation() 149 149 { 150 $this->dbCreator->createTable(new stubReflectionClass('MockSinglePrimaryKey Persistable'));150 $this->dbCreator->createTable(new stubReflectionClass('MockSinglePrimaryKeyEntity')); 151 151 $tableDescription = $this->mockQueryBuilder->getTableDescription(); 152 152 $this->assertEqual($tableDescription->getName(), 'foo'); trunk/src/test/php/net/stubbles/rdbms/persistence/eraser/stubDatabaseEraserTestCase.php
r949 r951 12 12 Mock::generate('stubCriterion'); 13 13 require_once dirname(__FILE__) . '/../../querybuilder/TeststubDatabaseQueryBuilder.php'; 14 require_once dirname(__FILE__) . '/../MockNoEntityAnnotation Persistable.php';15 require_once dirname(__FILE__) . '/../MockNoTableAnnotation Persistable.php';16 require_once dirname(__FILE__) . '/../MockSinglePrimaryKey Persistable.php';14 require_once dirname(__FILE__) . '/../MockNoEntityAnnotationEntity.php'; 15 require_once dirname(__FILE__) . '/../MockNoTableAnnotationEntity.php'; 16 require_once dirname(__FILE__) . '/../MockSinglePrimaryKeyEntity.php'; 17 17 /** 18 18 * mock entity to test an entity that has no primary keys … … 112 112 113 113 /** 114 * check that a c lass that is missing the DBTable annotationthrows an exception114 * check that a cnon-object throws an exception 115 115 */ 116 116 public function testByPrimaryKeysClassWithNonObject() … … 126 126 { 127 127 $this->expectException('stubPersistenceException'); 128 $this->dbEraser->deleteByPrimaryKeys(new MockNoEntityAnnotation Persistable());128 $this->dbEraser->deleteByPrimaryKeys(new MockNoEntityAnnotationEntity()); 129 129 } 130 130 … … 143 143 public function testByPrimaryKeys() 144 144 { 145 $entity = new MockNoTableAnnotation Persistable();145 $entity = new MockNoTableAnnotationEntity(); 146 146 $entity->setId('mock'); 147 147 $this->mockConnection->setReturnValue('query', new MockstubDatabaseResult()); 148 148 $this->dbEraser->deleteByPrimaryKeys($entity); 149 $this->assertEqual($this->mockQueryBuilder->getDeleteTable(), 'MockNoTableAnnotation Persistables');150 $this->assertEqual($this->mockQueryBuilder->getDeleteCriterion()->toSQL(), "(`MockNoTableAnnotation Persistables`.`id` = 'mock')");149 $this->assertEqual($this->mockQueryBuilder->getDeleteTable(), 'MockNoTableAnnotationEntitys'); 150 $this->assertEqual($this->mockQueryBuilder->getDeleteCriterion()->toSQL(), "(`MockNoTableAnnotationEntitys`.`id` = 'mock')"); 151 151 } 152 152 … … 156 156 public function testByPrimaryKeysWithDBTableAnnotation() 157 157 { 158 $entity = new MockSinglePrimaryKey Persistable();158 $entity = new MockSinglePrimaryKeyEntity(); 159 159 $entity->setId('mock'); 160 160 $this->mockConnection->setReturnValue('query', new MockstubDatabaseResult()); … … 175 175 $mockResult->setReturnValueAt(0, 'count', 0); 176 176 $mockResult->setReturnValueAt(1, 'count', 1); 177 $data = $this->dbEraser->deleteByCriterion($mockCriterion, new stubReflectionClass('MockNoTableAnnotation Persistable'));177 $data = $this->dbEraser->deleteByCriterion($mockCriterion, new stubReflectionClass('MockNoTableAnnotationEntity')); 178 178 $this->assertEqual($data, 0); 179 $data = $this->dbEraser->deleteByCriterion($mockCriterion, new stubReflectionClass('MockNoTableAnnotation Persistable'));179 $data = $this->dbEraser->deleteByCriterion($mockCriterion, new stubReflectionClass('MockNoTableAnnotationEntity')); 180 180 $this->assertEqual($data, 1); 181 $this->assertEqual($this->mockQueryBuilder->getDeleteTable(), 'MockNoTableAnnotation Persistables');181 $this->assertEqual($this->mockQueryBuilder->getDeleteTable(), 'MockNoTableAnnotationEntitys'); 182 182 $this->assertEqual($this->mockQueryBuilder->getDeleteCriterion()->toSQL(), "example"); 183 183 } … … 194 194 $mockResult->setReturnValueAt(0, 'count', 0); 195 195 $mockResult->setReturnValueAt(1, 'count', 1); 196 $data = $this->dbEraser->deleteByCriterion($mockCriterion, new stubReflectionClass('MockSinglePrimaryKey Persistable'));196 $data = $this->dbEraser->deleteByCriterion($mockCriterion, new stubReflectionClass('MockSinglePrimaryKeyEntity')); 197 197 $this->assertEqual($data, 0); 198 $data = $this->dbEraser->deleteByCriterion($mockCriterion, new stubReflectionClass('MockSinglePrimaryKey Persistable'));198 $data = $this->dbEraser->deleteByCriterion($mockCriterion, new stubReflectionClass('MockSinglePrimaryKeyEntity')); 199 199 $this->assertEqual($data, 1); 200 200 $this->assertEqual($this->mockQueryBuilder->getDeleteTable(), 'foo'); … … 208 208 { 209 209 $this->expectException('stubPersistenceException'); 210 $this->dbEraser->deleteByCriterion(new MockstubCriterion(), new stubReflectionClass('MockNoEntityAnnotation Persistable'));210 $this->dbEraser->deleteByCriterion(new MockstubCriterion(), new stubReflectionClass('MockNoEntityAnnotationEntity')); 211 211 } 212 212 } trunk/src/test/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinderTestCase.php
r949 r951 12 12 Mock::generate('stubCriterion'); 13 13 require_once dirname(__FILE__) . '/../../querybuilder/TeststubDatabaseQueryBuilder.php'; 14 require_once dirname(__FILE__) . '/../MockNoTableAnnotation Persistable.php';15 require_once dirname(__FILE__) . '/../MockSinglePrimaryKey Persistable.php';16 require_once dirname(__FILE__) . '/../MockNoEntityAnnotation Persistable.php';14 require_once dirname(__FILE__) . '/../MockNoTableAnnotationEntity.php'; 15 require_once dirname(__FILE__) . '/../MockSinglePrimaryKeyEntity.php'; 16 require_once dirname(__FILE__) . '/../MockNoEntityAnnotationEntity.php'; 17 17 /** 18 * This is a persistablethat requires arguments in the constructor.18 * This is an entity that requires arguments in the constructor. 19 19 * 20 20 * @Entity 21 21 * @DBTable(name='bar') 22 22 */ 23 class MockInstanceArgs Persistableextends stubBaseObject23 class MockInstanceArgsEntity extends stubBaseObject 24 24 { 25 25 protected $arg1; … … 145 145 { 146 146 $this->expectException('stubPersistenceException'); 147 $this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockNoEntityAnnotation Persistable'), array());147 $this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockNoEntityAnnotationEntity'), array()); 148 148 } 149 149 … … 157 157 $mockResult->setReturnValueAt(0, 'fetch', false); 158 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('MockSinglePrimaryKey Persistable'), array('id' => 'mock')));160 $singlePrimaryKey = $this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockSinglePrimaryKey Persistable'), 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')); 161 161 $this->assertEqual($singlePrimaryKey->getId(), 'mock'); 162 162 $this->assertEqual($singlePrimaryKey->withAnnotation(), 'Here is bar.'); … … 175 175 $mockResult->setReturnValueAt(0, 'fetch', false); 176 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('MockNoTableAnnotation Persistable'), array('id' => 'mock')));178 $entity = $this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockNoTableAnnotation Persistable'), 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')); 179 179 $this->assertEqual($entity->getId(), 'mock'); 180 180 $this->assertEqual($entity->withAnnotation(), 'Here is bar.'); 181 181 $this->assertEqual($entity->getDefaultValue(), 'And this is default.'); 182 182 $select = $this->mockQueryBuilder->getSelect(); 183 $this->assertEqual($select->getBaseTableName(), 'MockNoTableAnnotation Persistables');183 $this->assertEqual($select->getBaseTableName(), 'MockNoTableAnnotationEntitys'); 184 184 } 185 185 … … 194 194 $this->mockConnection->setReturnValue('query', $mockResult); 195 195 $mockResult->setReturnValue('fetch', array()); 196 $entity = $this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockInstanceArgs Persistable'), array(), array('foo', 'bar'));196 $entity = $this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockInstanceArgsEntity'), array(), array('foo', 'bar')); 197 197 $this->assertEqual($entity->getArg1(), 'foo'); 198 198 $this->assertEqual($entity->getArg2(), 'bar'); 199 $entity = $this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockInstanceArgs Persistable'), array(), array('foo'));199 $entity = $this->dbFinder->findByPrimaryKeys(new stubReflectionClass('MockInstanceArgsEntity'), array(), array('foo')); 200 200 $this->assertEqual($entity->getArg1(), 'foo'); 201 201 $this->assertNull($entity->getArg2()); … … 208 208 { 209 209 $this->expectException('stubPersistenceException'); 210 $this->dbFinder->findByCriterion(new MockstubCriterion(), new stubReflectionClass('MockNoEntityAnnotation Persistable'));210 $this->dbFinder->findByCriterion(new MockstubCriterion(), new stubReflectionClass('MockNoEntityAnnotationEntity')); 211 211 } 212 212 … … 222 222 $mockResult->setReturnValueAt(0, 'fetchAll', false); 223 223 $mockResult->setReturnValueAt(1, 'fetchAll', array(array('bar' => 'Here is bar.', 'default' => 'And this is default.'))); 224 $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockSinglePrimaryKey Persistable'));224 $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockSinglePrimaryKeyEntity')); 225 225 $this->assertEqual($finderResult->count(), 0); 226 $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockSinglePrimaryKey Persistable'));226 $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockSinglePrimaryKeyEntity')); 227 227 $this->assertEqual($finderResult->count(), 1); 228 228 $data = $finderResult->current(); … … 243 243 $this->mockConnection->setReturnValue('query', $mockResult); 244 244 $mockResult->setReturnValue('fetchAll', array(array())); 245 $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockInstanceArgs Persistable'), array('foo', 'bar'));245 $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockInstanceArgsEntity'), array('foo', 'bar')); 246 246 $data = $finderResult->current(); 247 247 $this->assertEqual($data->getArg1(), 'foo'); 248 248 $this->assertEqual($data->getArg2(), 'bar'); 249 $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockInstanceArgs Persistable'), array('foo'));249 $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockInstanceArgsEntity'), array('foo')); 250 250 $data = $finderResult->current(); 251 251 $this->assertEqual($data->getArg1(), 'foo'); trunk/src/test/php/net/stubbles/rdbms/persistence/serializer/stubDatabaseSerializerTestCase.php
r482 r951 10 10 Mock::generate('stubDatabaseConnection'); 11 11 require_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'; 12 require_once dirname(__FILE__) . '/../MockNoEntityAnnotationEntity.php'; 13 require_once dirname(__FILE__) . '/../MockSinglePrimaryKeyEntity.php'; 15 14 /** 16 15 * Test for net.stubbles.rdbms.persistence.serializer.stubDatabaseSerializer … … 99 98 100 99 /** 101 * check that a class that is missing the DBTable annotationthrows an exception100 * check that a non-object throws an exception 102 101 */ 103 public function test ClassWithoutDBTableAnnotation()102 public function testSerializeNonObject() 104 103 { 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()); 107 115 } 108 116 … … 112 120 public function testInsertWithSinglePrimaryKey() 113 121 { 114 $singlePrimaryKey Persistable = new MockSinglePrimaryKeyPersistable();122 $singlePrimaryKeyEntity = new MockSinglePrimaryKeyEntity(); 115 123 $this->mockConnection->setReturnValue('getLastInsertId', 'mockId'); 116 $this->mockConnection->expectCallcount('exec', 2);117 $this->mockQueryBuilder->setInsertQueries(array('foo' => 'foo' , 'baz' => 'baz'));118 $this->dbSerializer->serialize($singlePrimaryKey Persistable);124 $this->mockConnection->expectCallcount('exec', 1); 125 $this->mockQueryBuilder->setInsertQueries(array('foo' => 'foo')); 126 $this->dbSerializer->serialize($singlePrimaryKeyEntity); 119 127 $this->assertEqual($this->mockQueryBuilder->getCallCount('createInsert'), 1); 120 128 $this->assertEqual($this->mockQueryBuilder->getCallCount('createUpdate'), 0); 121 129 $tableRows = $this->mockQueryBuilder->getInsertTableRows(); 122 $this->assertEqual(count($tableRows), 2);130 $this->assertEqual(count($tableRows), 1); 123 131 $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'); 127 133 $this->assertEqual($tableRows['foo']->getColumns(), array('bar' => 'this is bar', 'default' => 'example')); 128 $this->assertEqual($tableRows['blub']->getColumns(), array('baz' => 'this is baz'));129 134 $this->assertFalse($tableRows['foo']->hasCriterion()); 130 135 } … … 135 140 public function testUpdateWithSinglePrimaryKey() 136 141 { 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'); 141 145 $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')); 
