PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/test/testsuite/runtime/map/TableMapTest.php

https://github.com/1989gaurav/Propel
PHP | 317 lines | 260 code | 35 blank | 22 comment | 0 complexity | 3f9b2e317acf96d46fd4f183dc0ce699 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. require_once dirname(__FILE__) . '/../../../../runtime/lib/map/ColumnMap.php';
  10. require_once dirname(__FILE__) . '/../../../../runtime/lib/map/RelationMap.php';
  11. require_once dirname(__FILE__) . '/../../../../runtime/lib/map/TableMap.php';
  12. require_once dirname(__FILE__) . '/../../../../runtime/lib/map/DatabaseMap.php';
  13. require_once dirname(__FILE__) . '/../../../../runtime/lib/exception/PropelException.php';
  14. /**
  15. * Test class for TableMap.
  16. *
  17. * @author François Zaninotto
  18. * @version $Id$
  19. * @package runtime.map
  20. */
  21. class TableMapTest extends PHPUnit_Framework_TestCase
  22. {
  23. protected $databaseMap;
  24. protected function setUp()
  25. {
  26. parent::setUp();
  27. $this->databaseMap = new DatabaseMap('foodb');
  28. $this->tableName = 'foo';
  29. $this->tmap = new TableMap($this->tableName, $this->databaseMap);
  30. }
  31. protected function tearDown()
  32. {
  33. // nothing to do for now
  34. parent::tearDown();
  35. }
  36. public function testConstructor()
  37. {
  38. $this->assertEquals(array(), $this->tmap->getColumns(), 'A new table map has no columns');
  39. $this->assertEquals($this->tableName, $this->tmap->getName(), 'constructor can set the table name');
  40. $this->assertEquals($this->databaseMap, $this->tmap->getDatabaseMap(), 'Constructor can set the database map');
  41. try {
  42. $tmap = new TableMap();
  43. $this->assertTrue(true, 'A table map can be instanciated with no parameters');
  44. } catch (Exception $e) {
  45. $this->fail('A table map can be instanciated with no parameters');
  46. }
  47. }
  48. public function testProperties()
  49. {
  50. $tmap = new TableMap();
  51. $properties = array('name', 'phpName', 'className', 'package');
  52. foreach ($properties as $property)
  53. {
  54. $getter = 'get' . ucfirst($property);
  55. $setter = 'set' . ucfirst($property);
  56. $this->assertNull($tmap->$getter(), "A new relation has no $property");
  57. $tmap->$setter('foo_value');
  58. $this->assertEquals('foo_value', $tmap->$getter(), "The $property is set by setType()");
  59. }
  60. }
  61. public function testHasColumn()
  62. {
  63. $this->assertFalse($this->tmap->hasColumn('BAR'), 'hascolumn() returns false when the column is not in the table map');
  64. $column = $this->tmap->addColumn('BAR', 'Bar', 'INTEGER');
  65. $this->assertTrue($this->tmap->hasColumn('BAR'), 'hascolumn() returns true when the column is in the table map');
  66. $this->assertTrue($this->tmap->hasColumn('foo.bar'), 'hascolumn() accepts a denormalized column name');
  67. $this->assertFalse($this->tmap->hasColumn('foo.bar', false), 'hascolumn() accepts a $normalize parameter to skip name normalization');
  68. $this->assertTrue($this->tmap->hasColumn('BAR', false), 'hascolumn() accepts a $normalize parameter to skip name normalization');
  69. $this->assertTrue($this->tmap->hasColumn($column), 'hascolumn() accepts a ColumnMap object as parameter');
  70. }
  71. public function testGetColumn()
  72. {
  73. $column = $this->tmap->addColumn('BAR', 'Bar', 'INTEGER');
  74. $this->assertEquals($column, $this->tmap->getColumn('BAR'), 'getColumn returns a ColumnMap according to a column name');
  75. try
  76. {
  77. $this->tmap->getColumn('FOO');
  78. $this->fail('getColumn throws an exception when called on an inexistent column');
  79. } catch(PropelException $e) {}
  80. $this->assertEquals($column, $this->tmap->getColumn('foo.bar'), 'getColumn accepts a denormalized column name');
  81. try
  82. {
  83. $this->tmap->getColumn('foo.bar', false);
  84. $this->fail('getColumn accepts a $normalize parameter to skip name normalization');
  85. } catch(PropelException $e) {}
  86. }
  87. public function testGetColumnByPhpName()
  88. {
  89. $column = $this->tmap->addColumn('BAR_BAZ', 'BarBaz', 'INTEGER');
  90. $this->assertEquals($column, $this->tmap->getColumnByPhpName('BarBaz'), 'getColumnByPhpName() returns a ColumnMap according to a column phpName');
  91. try
  92. {
  93. $this->tmap->getColumn('Foo');
  94. $this->fail('getColumnByPhpName() throws an exception when called on an inexistent column');
  95. } catch(PropelException $e) {}
  96. }
  97. public function testGetColumns()
  98. {
  99. $this->assertEquals(array(), $this->tmap->getColumns(), 'getColumns returns an empty array when no columns were added');
  100. $column1 = $this->tmap->addColumn('BAR', 'Bar', 'INTEGER');
  101. $column2 = $this->tmap->addColumn('BAZ', 'Baz', 'INTEGER');
  102. $this->assertEquals(array('BAR' => $column1, 'BAZ' => $column2), $this->tmap->getColumns(), 'getColumns returns the columns indexed by name');
  103. }
  104. public function testAddPrimaryKey()
  105. {
  106. $column1 = $this->tmap->addPrimaryKey('BAR', 'Bar', 'INTEGER');
  107. $this->assertTrue($column1->isPrimaryKey(), 'Columns added by way of addPrimaryKey() are primary keys');
  108. $column2 = $this->tmap->addColumn('BAZ', 'Baz', 'INTEGER');
  109. $this->assertFalse($column2->isPrimaryKey(), 'Columns added by way of addColumn() are not primary keys by default');
  110. $column3 = $this->tmap->addColumn('BAZZ', 'Bazz', 'INTEGER', null, null, null, true);
  111. $this->assertTrue($column3->isPrimaryKey(), 'Columns added by way of addColumn() can be defined as primary keys');
  112. $column4 = $this->tmap->addForeignKey('BAZZZ', 'Bazzz', 'INTEGER', 'Table1', 'column1');
  113. $this->assertFalse($column4->isPrimaryKey(), 'Columns added by way of addForeignKey() are not primary keys');
  114. $column5 = $this->tmap->addForeignPrimaryKey('BAZZZZ', 'Bazzzz', 'INTEGER', 'table1', 'column1');
  115. $this->assertTrue($column5->isPrimaryKey(), 'Columns added by way of addForeignPrimaryKey() are primary keys');
  116. }
  117. public function testGetPrimaryKeyColumns()
  118. {
  119. $this->assertEquals(array(), $this->tmap->getPrimaryKeyColumns(), 'getPrimaryKeyColumns() returns an empty array by default');
  120. $column1 = $this->tmap->addPrimaryKey('BAR', 'Bar', 'INTEGER');
  121. $column3 = $this->tmap->addColumn('BAZZ', 'Bazz', 'INTEGER', null, null, null, true);
  122. $expected = array($column1, $column3);
  123. $this->assertEquals($expected, $this->tmap->getPrimaryKeyColumns(), 'getPrimaryKeyColumns() returns an array of the table primary keys');
  124. }
  125. public function testGetPrimaryKeys()
  126. {
  127. $this->assertEquals(array(), $this->tmap->getPrimaryKeys(), 'getPrimaryKeys() returns an empty array by default');
  128. $column1 = $this->tmap->addPrimaryKey('BAR', 'Bar', 'INTEGER');
  129. $column3 = $this->tmap->addColumn('BAZZ', 'Bazz', 'INTEGER', null, null, null, true);
  130. $expected = array('BAR' => $column1, 'BAZZ' => $column3);
  131. $this->assertEquals($expected, $this->tmap->getPrimaryKeys(), 'getPrimaryKeys() returns an array of the table primary keys');
  132. }
  133. public function testAddForeignKey()
  134. {
  135. $column1 = $this->tmap->addForeignKey('BAR', 'Bar', 'INTEGER', 'Table1', 'column1');
  136. $this->assertTrue($column1->isForeignKey(), 'Columns added by way of addForeignKey() are foreign keys');
  137. $column2 = $this->tmap->addColumn('BAZ', 'Baz', 'INTEGER');
  138. $this->assertFalse($column2->isForeignKey(), 'Columns added by way of addColumn() are not foreign keys by default');
  139. $column3 = $this->tmap->addColumn('BAZZ', 'Bazz', 'INTEGER', null, null, null, false, 'Table1', 'column1');
  140. $this->assertTrue($column3->isForeignKey(), 'Columns added by way of addColumn() can be defined as foreign keys');
  141. $column4 = $this->tmap->addPrimaryKey('BAZZZ', 'Bazzz', 'INTEGER');
  142. $this->assertFalse($column4->isForeignKey(), 'Columns added by way of addPrimaryKey() are not foreign keys');
  143. $column5 = $this->tmap->addForeignPrimaryKey('BAZZZZ', 'Bazzzz', 'INTEGER', 'table1', 'column1');
  144. $this->assertTrue($column5->isForeignKey(), 'Columns added by way of addForeignPrimaryKey() are foreign keys');
  145. }
  146. public function testGetForeignKeys()
  147. {
  148. $this->assertEquals(array(), $this->tmap->getForeignKeys(), 'getForeignKeys() returns an empty array by default');
  149. $column1 = $this->tmap->addForeignKey('BAR', 'Bar', 'INTEGER', 'Table1', 'column1');
  150. $column3 = $this->tmap->addColumn('BAZZ', 'Bazz', 'INTEGER', null, null, null, false, 'Table1', 'column1');
  151. $expected = array('BAR' => $column1, 'BAZZ' => $column3);
  152. $this->assertEquals($expected, $this->tmap->getForeignKeys(), 'getForeignKeys() returns an array of the table foreign keys');
  153. }
  154. /**
  155. * @expectedException PropelException
  156. */
  157. public function testLoadWrongRelations()
  158. {
  159. $this->tmap->getRelation('Bar');
  160. }
  161. public function testLazyLoadRelations()
  162. {
  163. $foreigntmap = new BarTableMap();
  164. $this->databaseMap->addTableObject($foreigntmap);
  165. $localtmap = new FooTableMap();
  166. $this->databaseMap->addTableObject($localtmap);
  167. $rmap = $localtmap->getRelation('Bar');
  168. $this->assertEquals($rmap, $localtmap->rmap, 'getRelation() returns the relations lazy loaded by buildRelations()');
  169. }
  170. public function testAddRelation()
  171. {
  172. $foreigntmap1 = new TableMap('bar');
  173. $foreigntmap1->setClassname('Bar');
  174. $this->databaseMap->addTableObject($foreigntmap1);
  175. $foreigntmap2 = new TableMap('baz');
  176. $foreigntmap2->setClassname('Baz');
  177. $this->databaseMap->addTableObject($foreigntmap2);
  178. $this->rmap1 = $this->tmap->addRelation('Bar', 'Bar', RelationMap::MANY_TO_ONE);
  179. $this->rmap2 = $this->tmap->addRelation('Bazz', 'Baz', RelationMap::ONE_TO_MANY);
  180. $this->tmap->getRelations();
  181. // now on to the test
  182. $this->assertEquals($this->rmap1->getLocalTable(), $this->tmap, 'adding a relation with HAS_ONE sets the local table to the current table');
  183. $this->assertEquals($this->rmap1->getForeignTable(), $foreigntmap1, 'adding a relation with HAS_ONE sets the foreign table according to the name given');
  184. $this->assertEquals(RelationMap::MANY_TO_ONE, $this->rmap1->getType(), 'adding a relation with HAS_ONE sets the foreign table type accordingly');
  185. $this->assertEquals($this->rmap2->getForeignTable(), $this->tmap, 'adding a relation with HAS_MANY sets the foreign table to the current table');
  186. $this->assertEquals($this->rmap2->getLocalTable(), $foreigntmap2, 'adding a relation with HAS_MANY sets the local table according to the name given');
  187. $this->assertEquals(RelationMap::ONE_TO_MANY, $this->rmap2->getType(), 'adding a relation with HAS_MANY sets the foreign table type accordingly');
  188. $expectedRelations = array('Bar' => $this->rmap1, 'Bazz' => $this->rmap2);
  189. $this->assertEquals($expectedRelations, $this->tmap->getRelations(), 'getRelations() returns an associative array of all the relations');
  190. }
  191. public function testPrimaryStringAddColumn()
  192. {
  193. $this->assertFalse($this->tmap->hasPrimaryStringColumn(), 'hasPrimaryStringColumn() returns false while none set.');
  194. $this->assertNull($this->tmap->getPrimaryStringColumn(), 'getPrimaryStringColumn() returns null while none set.');
  195. $column = $this->tmap->addColumn('FOO', 'Foo', 'VARCHAR');
  196. $this->assertFalse($this->tmap->hasPrimaryStringColumn(), 'hasPrimaryStringColumn() returns false when no pkStr column is set.');
  197. $this->assertNull($this->tmap->getPrimaryStringColumn(), 'getPrimaryStringColumn() returns null when no pkStr column is set.');
  198. $column = $this->tmap->addColumn('PKSTR', 'pkStr', 'VARCHAR');
  199. $column->setPrimaryString(true);
  200. $this->assertTrue($this->tmap->hasPrimaryStringColumn(), 'hasPrimaryStringColumn() returns true after adding pkStr column.');
  201. $this->assertEquals($column, $this->tmap->getPrimaryStringColumn(), 'getPrimaryStringColumn() returns correct column.');
  202. }
  203. public function testPrimaryStringAddConfiguredColumn()
  204. {
  205. $this->assertFalse($this->tmap->hasPrimaryStringColumn(), 'hasPrimaryStringColumn() returns false while none set.');
  206. $column = new ColumnMap('BAR', $this->tmap);
  207. $column->setPhpName('Bar');
  208. $column->setType('VARCHAR');
  209. $column->setPrimaryString(true);
  210. $this->tmap->addConfiguredColumn($column);
  211. $this->assertTrue($this->tmap->hasPrimaryStringColumn(), 'hasPrimaryStringColumn() returns true after adding pkStr column.');
  212. $this->assertEquals($column, $this->tmap->getPrimaryStringColumn(), 'getPrimaryStringColumn() returns correct column.');
  213. }
  214. // deprecated method
  215. public function testNormalizeColName()
  216. {
  217. $tmap = new TestableTableMap();
  218. $this->assertEquals('', $tmap->normalizeColName(''), 'normalizeColName returns an empty string when passed an empty string');
  219. $this->assertEquals('BAR', $tmap->normalizeColName('bar'), 'normalizeColName uppercases the input');
  220. $this->assertEquals('BAR_BAZ', $tmap->normalizeColName('bar_baz'), 'normalizeColName does not mind underscores');
  221. $this->assertEquals('BAR', $tmap->normalizeColName('FOO.BAR'), 'normalizeColName removes table prefix');
  222. $this->assertEquals('BAR', $tmap->normalizeColName('BAR'), 'normalizeColName leaves normalized column names unchanged');
  223. $this->assertEquals('BAR_BAZ', $tmap->normalizeColName('foo.bar_baz'), 'normalizeColName can do all the above at the same time');
  224. }
  225. // deprecated method
  226. public function testContainsColumn()
  227. {
  228. $this->assertFalse($this->tmap->containsColumn('BAR'), 'containsColumn returns false when the column is not in the table map');
  229. $column = $this->tmap->addColumn('BAR', 'Bar', 'INTEGER');
  230. $this->assertTrue($this->tmap->containsColumn('BAR'), 'containsColumn returns true when the column is in the table map');
  231. $this->assertTrue($this->tmap->containsColumn('foo.bar'), 'containsColumn accepts a denormalized column name');
  232. $this->assertFalse($this->tmap->containsColumn('foo.bar', false), 'containsColumn accepts a $normalize parameter to skip name normalization');
  233. $this->assertTrue($this->tmap->containsColumn('BAR', false), 'containsColumn accepts a $normalize parameter to skip name normalization');
  234. $this->assertTrue($this->tmap->containsColumn($column), 'containsColumn accepts a ColumnMap object as parameter');
  235. }
  236. // deprecated methods
  237. public function testPrefix()
  238. {
  239. $tmap = new TestableTableMap();
  240. $this->assertNull($tmap->getPrefix(), 'prefix is empty until set');
  241. $this->assertFalse($tmap->hasPrefix('barbaz'), 'hasPrefix returns false when prefix is not set');
  242. $tmap->setPrefix('bar');
  243. $this->assertEquals('bar', $tmap->getPrefix(), 'prefix is set by setPrefix()');
  244. $this->assertTrue($tmap->hasPrefix('barbaz'), 'hasPrefix returns true when prefix is set and found in string');
  245. $this->assertFalse($tmap->hasPrefix('baz'), 'hasPrefix returns false when prefix is set and not found in string');
  246. $this->assertFalse($tmap->hasPrefix('bazbar'), 'hasPrefix returns false when prefix is set and not found anywhere in string');
  247. $this->assertEquals('baz', $tmap->removePrefix('barbaz'), 'removePrefix returns string without prefix if found at the beginning');
  248. $this->assertEquals('bazbaz', $tmap->removePrefix('bazbaz'), 'removePrefix returns original string when prefix is not found');
  249. $this->assertEquals('bazbar', $tmap->removePrefix('bazbar'), 'removePrefix returns original string when prefix is not found at the beginning');
  250. }
  251. }
  252. class TestableTableMap extends TableMap
  253. {
  254. public function hasPrefix($data)
  255. {
  256. return parent::hasPrefix($data);
  257. }
  258. public function removePrefix($data)
  259. {
  260. return parent::removePrefix($data);
  261. }
  262. public function normalizeColName($name)
  263. {
  264. return parent::normalizeColName($name);
  265. }
  266. }
  267. class FooTableMap extends TableMap
  268. {
  269. public $rmap;
  270. public function buildRelations()
  271. {
  272. $this->rmap = $this->addRelation('Bar', 'Bar', RelationMap::MANY_TO_ONE);
  273. }
  274. }
  275. class BarTableMap extends TableMap
  276. {
  277. public function initialize()
  278. {
  279. $this->setName('bar');
  280. $this->setClassName('Bar');
  281. }
  282. }