Changeset 928
- Timestamp:
- 09/18/07 23:21:03 (1 year ago)
- Files:
-
- trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinder.php (modified) (3 diffs)
- trunk/src/main/php/net/stubbles/rdbms/persistence/stubPersistenceHelper.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) (3 diffs)
- trunk/src/test/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinderTestCase.php (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinder.php
r913 r928 1 1 <?php 2 2 /** 3 * Class for finding the data of a persistableobject within a database.3 * Class for finding the data of an entity object within a database. 4 4 * 5 5 * @author Frank Kleine <mikey@stubbles.net> … … 15 15 'net.stubbles.rdbms.persistence.finder.stubDatabaseFinderResult', 16 16 'net.stubbles.rdbms.querybuilder.stubDatabaseQueryBuilderFactory', 17 'net.stubbles.rdbms.querybuilder.stubDatabaseSelect', 18 'net.stubbles.reflection.reflection' 17 'net.stubbles.rdbms.querybuilder.stubDatabaseSelect' 19 18 ); 20 19 /** 21 * Class for finding the data of a persistableobject within a database.20 * Class for finding the data of an entity object within a database. 22 21 * 23 22 * @package stubbles … … 168 167 } 169 168 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())); 172 173 } 173 174 trunk/src/main/php/net/stubbles/rdbms/persistence/stubPersistenceHelper.php
r911 r928 78 78 } else { 79 79 $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())); 82 81 $returnType = $method->getReturnType(); 83 82 if (null === $returnType) { … … 122 121 return $column; 123 122 } 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 } 124 135 } 125 136 ?> trunk/src/test/php/net/stubbles/rdbms/RDBMSTestSuite.php
r925 r928 46 46 $this->addTestFile($dir . '/persistence/creator/stubDatabaseCreatorTestCase.php'); 47 47 $this->addTestFile($dir . '/persistence/eraser/stubDatabaseEraserTestCase.php'); 48 #$this->addTestFile($dir . '/persistence/finder/stubDatabaseFinderTestCase.php');48 $this->addTestFile($dir . '/persistence/finder/stubDatabaseFinderTestCase.php'); 49 49 # $this->addTestFile($dir . '/persistence/serializer/stubDatabaseSerializerTestCase.php'); 50 50 trunk/src/test/php/net/stubbles/rdbms/persistence/MockNoTableAnnotationPersistable.php
r925 r928 100 100 101 101 /** 102 * setter method to prevent exceptions 103 * 104 * @param int $intValue 105 */ 106 public function setIntValue($intValue) 107 { 108 // ignore this 109 } 110 111 /** 102 112 * method that has a no annotation 103 113 * … … 110 120 111 121 /** 122 * setter method to prevent exceptions 123 * 124 * @param bool $boolValue 125 */ 126 public function setBoolValue($boolValue) 127 { 128 // ignore this 129 } 130 131 /** 112 132 * method that has a no annotation 113 133 * … … 117 137 { 118 138 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 119 149 } 120 150 trunk/src/test/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinderTestCase.php
r881 r928 1 1 <?php 2 2 /** 3 * Test for net.stubbles.rdbms.persistence.finder.stubDatabaseFinder 3 * Test for net.stubbles.rdbms.persistence.finder.stubDatabaseFinder. 4 4 * 5 5 * @author Frank Kleine <mikey@stubbles.net> … … 14 14 require_once dirname(__FILE__) . '/../MockNoTableAnnotationPersistable.php'; 15 15 require_once dirname(__FILE__) . '/../MockSinglePrimaryKeyPersistable.php'; 16 require_once dirname(__FILE__) . '/../ TestNonPersistable.php';16 require_once dirname(__FILE__) . '/../MockNoEntityAnnotationPersistable.php'; 17 17 /** 18 18 * This is a persistable that requires arguments in the constructor. 19 19 * 20 * @Entity 20 21 * @DBTable(name='bar') 21 22 */ 22 class MockInstanceArgsPersistable extends stub AbstractPersistable23 class MockInstanceArgsPersistable extends stubBaseObject 23 24 { 24 25 protected $arg1; … … 31 32 } 32 33 34 /** 35 * returns the first argument 36 * 37 * @return string 38 * @Transient 39 */ 33 40 public function getArg1() 34 41 { 35 42 return $this->arg1; 36 43 } 37 44 45 /** 46 * returns the second argument 47 * 48 * @return string 49 * @Transient 50 */ 38 51 public function getArg2() 39 52 { … … 42 55 } 43 56 /** 44 * Test for net.stubbles.rdbms.persistence.finder.stubDatabaseFinder 57 * Test for net.stubbles.rdbms.persistence.finder.stubDatabaseFinder. 45 58 * 46 59 * @package stubbles … … 127 140 128 141 /** 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()); 144 148 } 145 149 … … 149 153 public function testByPrimaryKeys() 150 154 { 151 $singlePrimaryKey = new MockSinglePrimaryKeyPersistable();152 $singlePrimaryKey->setId('mock');153 155 $mockResult = new MockstubDatabaseResult(); 154 156 $this->mockConnection->setReturnValue('query', $mockResult); 155 157 $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'); 163 162 $this->assertEqual($singlePrimaryKey->withAnnotation(), 'Here is bar.'); 164 163 $this->assertEqual($singlePrimaryKey->withDefaultValue(), 'And this is default.'); 165 164 $select = $this->mockQueryBuilder->getSelect(); 166 165 $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() 176 190 { 177 191 $mockCriterion = new MockstubCriterion(); … … 179 193 $mockResult = new MockstubDatabaseResult(); 180 194 $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); 181 222 $mockResult->setReturnValueAt(0, 'fetchAll', false); 182 223 $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')); 184 225 $this->assertEqual($finderResult->count(), 0); 185 $finderResult = $this->dbFinder->findByCriterion($mockCriterion, 'MockSinglePrimaryKeyPersistable');226 $finderResult = $this->dbFinder->findByCriterion($mockCriterion, new stubReflectionClass('MockSinglePrimaryKeyPersistable')); 186 227 $this->assertEqual($finderResult->count(), 1); 187 228 $data = $finderResult->current(); 188 $this->assertTrue($data->isPersistent());189 229 $this->assertEqual($data->withAnnotation(), 'Here is bar.'); 190 230 $this->assertEqual($data->withDefaultValue(), 'And this is default.'); 191 231 $select = $this->mockQueryBuilder->getSelect(); 192 232 $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 exception200 */201 public function testByCriterionNonPersistableClass()202 {203 $this->expectException('stubDatabaseFinderException');204 $this->dbFinder->findByCriterion(new MockstubCriterion(), 'TestNonPersistable');205 233 } 206 234 … … 215 243 $this->mockConnection->setReturnValue('query', $mockResult); 216 244 $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')); 218 246 $data = $finderResult->current(); 219 247 $this->assertEqual($data->getArg1(), 'foo'); 220 248 $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')); 222 250 $data = $finderResult->current(); 223 251 $this->assertEqual($data->getArg1(), 'foo');
