Changeset 1611
- Timestamp:
- 05/30/08 11:12:34 (4 months ago)
- Files:
-
- trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinder.php (modified) (6 diffs)
- trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinderResult.php (modified) (3 diffs)
- trunk/src/test/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinderTestCase.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinder.php
r1311 r1611 81 81 * get an entity from database by its primary keys 82 82 * 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) 86 85 * @return object 87 86 * @throws stubPersistenceException 88 87 */ 89 public function findByPrimaryKeys(stubBaseReflectionClass $entityClass, array $primaryKeys , array $arguments = null)88 public function findByPrimaryKeys(stubBaseReflectionClass $entityClass, array $primaryKeys) 90 89 { 91 90 if ($entityClass->hasAnnotation('Entity') === false) { … … 99 98 } 100 99 101 $entity = ((null === $arguments) ? ($entityClass->newInstance()) : ($entityClass->newInstanceArgs($arguments)));100 $entity = $entityClass->newInstance(); 102 101 $setterMethodHelper->applySetterMethods($entity, $data); 103 102 return $entity; … … 107 106 * finds all instances of $entityClass by given criterion 108 107 * 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 113 111 * @throws stubPersistenceException 114 112 */ 115 public function findByCriterion(stubCriterion $criterion, stubBaseReflectionClass $entityClass , array $arguments = null)113 public function findByCriterion(stubCriterion $criterion, stubBaseReflectionClass $entityClass) 116 114 { 117 115 if ($entityClass->hasAnnotation('Entity') === false) { … … 121 119 $setterMethodHelper = new stubSetterMethodHelper($entityClass); 122 120 $data = $this->fetchData($entityClass, $setterMethodHelper, $criterion); 123 $finderResult = new stubDatabaseFinderResult($entityClass, $data, $setterMethodHelper , $arguments);121 $finderResult = new stubDatabaseFinderResult($entityClass, $data, $setterMethodHelper); 124 122 return $finderResult; 125 123 } … … 128 126 * finds all instances of $entityClass 129 127 * 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 133 130 * @throws stubPersistenceException 134 131 */ 135 public function findAll(stubBaseReflectionClass $entityClass , array $arguments = null)132 public function findAll(stubBaseReflectionClass $entityClass) 136 133 { 137 134 if ($entityClass->hasAnnotation('Entity') === false) { … … 141 138 $setterMethodHelper = new stubSetterMethodHelper($entityClass); 142 139 $data = $this->fetchData($entityClass, $setterMethodHelper); 143 $finderResult = new stubDatabaseFinderResult($entityClass, $data, $setterMethodHelper , $arguments);140 $finderResult = new stubDatabaseFinderResult($entityClass, $data, $setterMethodHelper); 144 141 return $finderResult; 145 142 } trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinderResult.php
r1311 r1611 36 36 */ 37 37 protected $setterMethodHelper; 38 /**39 * list of arguments for entity constructor40 *41 * @var array<mixed>42 */43 protected $arguments;44 38 45 39 /** … … 49 43 * @param array $result result set from the database query 50 44 * @param stubSetterMethodHelper $setterMethodHelper list of setter methods for entity 51 * @param array $arguments optional list of arguments for entity constructor52 45 */ 53 public function __construct(stubBaseReflectionClass $entityClass, array $result, stubSetterMethodHelper $setterMethodHelper , array $arguments = null)46 public function __construct(stubBaseReflectionClass $entityClass, array $result, stubSetterMethodHelper $setterMethodHelper) 54 47 { 55 48 $this->entityClass = $entityClass; 56 49 $this->resultIterator = new ArrayIterator($result); 57 50 $this->setterMethodHelper = $setterMethodHelper; 58 $this->arguments = $arguments;59 51 } 60 52 … … 88 80 { 89 81 try { 90 $entity = ((null === $this->arguments) ? ($this->entityClass->newInstance()) : ($this->entityClass->newInstanceArgs($this->arguments)));82 $entity = $this->entityClass->newInstance(); 91 83 } catch (ReflectionException $re) { 92 84 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 15 15 require_once dirname(__FILE__) . '/../MockNoEntityAnnotationEntity.php'; 16 16 /** 17 * This is an entity that requires arguments in the constructor.18 *19 * @Entity20 * @DBTable(name='bar')21 */22 class MockInstanceArgsEntity extends stubBaseObject23 {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 argument35 *36 * @return string37 * @Transient38 */39 public function getArg1()40 {41 return $this->arg1;42 }43 44 /**45 * returns the second argument46 *47 * @return string48 * @Transient49 */50 public function getArg2()51 {52 return $this->arg2;53 }54 }55 /**56 17 * Test for net::stubbles::rdbms::persistence::finder::stubDatabaseFinder. 57 18 * … … 195 156 $this->assertEquals('And this is default.', $entity->getDefaultValue()); 196 157 $this->assertEquals('MockNoTableAnnotationEntitys', $this->mockQueryBuilder->getSelect()->getBaseTableName()); 197 }198 199 /**200 * test that instance args are transmitted correct201 *202 * @test203 */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());219 158 } 220 159 … … 257 196 258 197 /** 259 * test that instance args are transmitted correct260 *261 * @test262 */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 /**286 198 * test that finding data for all instances of an object works as expected 287 199 * … … 306 218 $this->assertFalse($select->hasCriterion()); 307 219 } 308 309 /**310 * test that instance args are transmitted correct311 *312 * @test313 */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 }333 220 } 334 221 ?>
