PageRenderTime 95ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/bayrock/gw2spidy
PHP | 313 lines | 256 code | 35 blank | 22 comment | 0 complexity | d9cca6a329058575eaae1950d41056ca MD5 | raw file
Possible License(s): BSD-3-Clause, BSD-2-Clause
  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. $getter = 'get' . ucfirst($property);
  54. $setter = 'set' . ucfirst($property);
  55. $this->assertNull($tmap->$getter(), "A new relation has no $property");
  56. $tmap->$setter('foo_value');
  57. $this->assertEquals('foo_value', $tmap->$getter(), "The $property is set by setType()");
  58. }
  59. }
  60. public function testHasColumn()
  61. {
  62. $this->assertFalse($this->tmap->hasColumn('BAR'), 'hascolumn() returns false when the column is not in the table map');
  63. $column = $this->tmap->addColumn('BAR', 'Bar', 'INTEGER');
  64. $this->assertTrue($this->tmap->hasColumn('BAR'), 'hascolumn() returns true when the column is in the table map');
  65. $this->assertTrue($this->tmap->hasColumn('foo.bar'), 'hascolumn() accepts a denormalized column name');
  66. $this->assertFalse($this->tmap->hasColumn('foo.bar', false), 'hascolumn() accepts a $normalize parameter to skip name normalization');
  67. $this->assertTrue($this->tmap->hasColumn('BAR', false), 'hascolumn() accepts a $normalize parameter to skip name normalization');
  68. $this->assertTrue($this->tmap->hasColumn($column), 'hascolumn() accepts a ColumnMap object as parameter');
  69. }
  70. public function testGetColumn()
  71. {
  72. $column = $this->tmap->addColumn('BAR', 'Bar', 'INTEGER');
  73. $this->assertEquals($column, $this->tmap->getColumn('BAR'), 'getColumn returns a ColumnMap according to a column name');
  74. try {
  75. $this->tmap->getColumn('FOO');
  76. $this->fail('getColumn throws an exception when called on an inexistent column');
  77. } catch (PropelException $e) {}
  78. $this->assertEquals($column, $this->tmap->getColumn('foo.bar'), 'getColumn accepts a denormalized column name');
  79. try {
  80. $this->tmap->getColumn('foo.bar', false);
  81. $this->fail('getColumn accepts a $normalize parameter to skip name normalization');
  82. } catch (PropelException $e) {}
  83. }
  84. public function testGetColumnByPhpName()
  85. {
  86. $column = $this->tmap->addColumn('BAR_BAZ', 'BarBaz', 'INTEGER');
  87. $this->assertEquals($column, $this->tmap->getColumnByPhpName('BarBaz'), 'getColumnByPhpName() returns a ColumnMap according to a column phpName');
  88. try {
  89. $this->tmap->getColumn('Foo');
  90. $this->fail('getColumnByPhpName() throws an exception when called on an inexistent column');
  91. } catch (PropelException $e) {}
  92. }
  93. public function testGetColumns()
  94. {
  95. $this->assertEquals(array(), $this->tmap->getColumns(), 'getColumns returns an empty array when no columns were added');
  96. $column1 = $this->tmap->addColumn('BAR', 'Bar', 'INTEGER');
  97. $column2 = $this->tmap->addColumn('BAZ', 'Baz', 'INTEGER');
  98. $this->assertEquals(array('BAR' => $column1, 'BAZ' => $column2), $this->tmap->getColumns(), 'getColumns returns the columns indexed by name');
  99. }
  100. public function testAddPrimaryKey()
  101. {
  102. $column1 = $this->tmap->addPrimaryKey('BAR', 'Bar', 'INTEGER');
  103. $this->assertTrue($column1->isPrimaryKey(), 'Columns added by way of addPrimaryKey() are primary keys');
  104. $column2 = $this->tmap->addColumn('BAZ', 'Baz', 'INTEGER');
  105. $this->assertFalse($column2->isPrimaryKey(), 'Columns added by way of addColumn() are not primary keys by default');
  106. $column3 = $this->tmap->addColumn('BAZZ', 'Bazz', 'INTEGER', null, null, null, true);
  107. $this->assertTrue($column3->isPrimaryKey(), 'Columns added by way of addColumn() can be defined as primary keys');
  108. $column4 = $this->tmap->addForeignKey('BAZZZ', 'Bazzz', 'INTEGER', 'Table1', 'column1');
  109. $this->assertFalse($column4->isPrimaryKey(), 'Columns added by way of addForeignKey() are not primary keys');
  110. $column5 = $this->tmap->addForeignPrimaryKey('BAZZZZ', 'Bazzzz', 'INTEGER', 'table1', 'column1');
  111. $this->assertTrue($column5->isPrimaryKey(), 'Columns added by way of addForeignPrimaryKey() are primary keys');
  112. }
  113. public function testGetPrimaryKeyColumns()
  114. {
  115. $this->assertEquals(array(), $this->tmap->getPrimaryKeyColumns(), 'getPrimaryKeyColumns() returns an empty array by default');
  116. $column1 = $this->tmap->addPrimaryKey('BAR', 'Bar', 'INTEGER');
  117. $column3 = $this->tmap->addColumn('BAZZ', 'Bazz', 'INTEGER', null, null, null, true);
  118. $expected = array($column1, $column3);
  119. $this->assertEquals($expected, $this->tmap->getPrimaryKeyColumns(), 'getPrimaryKeyColumns() returns an array of the table primary keys');
  120. }
  121. public function testGetPrimaryKeys()
  122. {
  123. $this->assertEquals(array(), $this->tmap->getPrimaryKeys(), 'getPrimaryKeys() returns an empty array by default');
  124. $column1 = $this->tmap->addPrimaryKey('BAR', 'Bar', 'INTEGER');
  125. $column3 = $this->tmap->addColumn('BAZZ', 'Bazz', 'INTEGER', null, null, null, true);
  126. $expected = array('BAR' => $column1, 'BAZZ' => $column3);
  127. $this->assertEquals($expected, $this->tmap->getPrimaryKeys(), 'getPrimaryKeys() returns an array of the table primary keys');
  128. }
  129. public function testAddForeignKey()
  130. {
  131. $column1 = $this->tmap->addForeignKey('BAR', 'Bar', 'INTEGER', 'Table1', 'column1');
  132. $this->assertTrue($column1->isForeignKey(), 'Columns added by way of addForeignKey() are foreign keys');
  133. $column2 = $this->tmap->addColumn('BAZ', 'Baz', 'INTEGER');
  134. $this->assertFalse($column2->isForeignKey(), 'Columns added by way of addColumn() are not foreign keys by default');
  135. $column3 = $this->tmap->addColumn('BAZZ', 'Bazz', 'INTEGER', null, null, null, false, 'Table1', 'column1');
  136. $this->assertTrue($column3->isForeignKey(), 'Columns added by way of addColumn() can be defined as foreign keys');
  137. $column4 = $this->tmap->addPrimaryKey('BAZZZ', 'Bazzz', 'INTEGER');
  138. $this->assertFalse($column4->isForeignKey(), 'Columns added by way of addPrimaryKey() are not foreign keys');
  139. $column5 = $this->tmap->addForeignPrimaryKey('BAZZZZ', 'Bazzzz', 'INTEGER', 'table1', 'column1');
  140. $this->assertTrue($column5->isForeignKey(), 'Columns added by way of addForeignPrimaryKey() are foreign keys');
  141. }
  142. public function testGetForeignKeys()
  143. {
  144. $this->assertEquals(array(), $this->tmap->getForeignKeys(), 'getForeignKeys() returns an empty array by default');
  145. $column1 = $this->tmap->addForeignKey('BAR', 'Bar', 'INTEGER', 'Table1', 'column1');
  146. $column3 = $this->tmap->addColumn('BAZZ', 'Bazz', 'INTEGER', null, null, null, false, 'Table1', 'column1');
  147. $expected = array('BAR' => $column1, 'BAZZ' => $column3);
  148. $this->assertEquals($expected, $this->tmap->getForeignKeys(), 'getForeignKeys() returns an array of the table foreign keys');
  149. }
  150. /**
  151. * @expectedException PropelException
  152. */
  153. public function testLoadWrongRelations()
  154. {
  155. $this->tmap->getRelation('Bar');
  156. }
  157. public function testLazyLoadRelations()
  158. {
  159. $foreigntmap = new BarTableMap();
  160. $this->databaseMap->addTableObject($foreigntmap);
  161. $localtmap = new FooTableMap();
  162. $this->databaseMap->addTableObject($localtmap);
  163. $rmap = $localtmap->getRelation('Bar');
  164. $this->assertEquals($rmap, $localtmap->rmap, 'getRelation() returns the relations lazy loaded by buildRelations()');
  165. }
  166. public function testAddRelation()
  167. {
  168. $foreigntmap1 = new TableMap('bar');
  169. $foreigntmap1->setClassname('Bar');
  170. $this->databaseMap->addTableObject($foreigntmap1);
  171. $foreigntmap2 = new TableMap('baz');
  172. $foreigntmap2->setClassname('Baz');
  173. $this->databaseMap->addTableObject($foreigntmap2);
  174. $this->rmap1 = $this->tmap->addRelation('Bar', 'Bar', RelationMap::MANY_TO_ONE);
  175. $this->rmap2 = $this->tmap->addRelation('Bazz', 'Baz', RelationMap::ONE_TO_MANY);
  176. $this->tmap->getRelations();
  177. // now on to the test
  178. $this->assertEquals($this->rmap1->getLocalTable(), $this->tmap, 'adding a relation with HAS_ONE sets the local table to the current table');
  179. $this->assertEquals($this->rmap1->getForeignTable(), $foreigntmap1, 'adding a relation with HAS_ONE sets the foreign table according to the name given');
  180. $this->assertEquals(RelationMap::MANY_TO_ONE, $this->rmap1->getType(), 'adding a relation with HAS_ONE sets the foreign table type accordingly');
  181. $this->assertEquals($this->rmap2->getForeignTable(), $this->tmap, 'adding a relation with HAS_MANY sets the foreign table to the current table');
  182. $this->assertEquals($this->rmap2->getLocalTable(), $foreigntmap2, 'adding a relation with HAS_MANY sets the local table according to the name given');
  183. $this->assertEquals(RelationMap::ONE_TO_MANY, $this->rmap2->getType(), 'adding a relation with HAS_MANY sets the foreign table type accordingly');
  184. $expectedRelations = array('Bar' => $this->rmap1, 'Bazz' => $this->rmap2);
  185. $this->assertEquals($expectedRelations, $this->tmap->getRelations(), 'getRelations() returns an associative array of all the relations');
  186. }
  187. public function testPrimaryStringAddColumn()
  188. {
  189. $this->assertFalse($this->tmap->hasPrimaryStringColumn(), 'hasPrimaryStringColumn() returns false while none set.');
  190. $this->assertNull($this->tmap->getPrimaryStringColumn(), 'getPrimaryStringColumn() returns null while none set.');
  191. $column = $this->tmap->addColumn('FOO', 'Foo', 'VARCHAR');
  192. $this->assertFalse($this->tmap->hasPrimaryStringColumn(), 'hasPrimaryStringColumn() returns false when no pkStr column is set.');
  193. $this->assertNull($this->tmap->getPrimaryStringColumn(), 'getPrimaryStringColumn() returns null when no pkStr column is set.');
  194. $column = $this->tmap->addColumn('PKSTR', 'pkStr', 'VARCHAR');
  195. $column->setPrimaryString(true);
  196. $this->assertTrue($this->tmap->hasPrimaryStringColumn(), 'hasPrimaryStringColumn() returns true after adding pkStr column.');
  197. $this->assertEquals($column, $this->tmap->getPrimaryStringColumn(), 'getPrimaryStringColumn() returns correct column.');
  198. }
  199. public function testPrimaryStringAddConfiguredColumn()
  200. {
  201. $this->assertFalse($this->tmap->hasPrimaryStringColumn(), 'hasPrimaryStringColumn() returns false while none set.');
  202. $column = new ColumnMap('BAR', $this->tmap);
  203. $column->setPhpName('Bar');
  204. $column->setType('VARCHAR');
  205. $column->setPrimaryString(true);
  206. $this->tmap->addConfiguredColumn($column);
  207. $this->assertTrue($this->tmap->hasPrimaryStringColumn(), 'hasPrimaryStringColumn() returns true after adding pkStr column.');
  208. $this->assertEquals($column, $this->tmap->getPrimaryStringColumn(), 'getPrimaryStringColumn() returns correct column.');
  209. }
  210. // deprecated method
  211. public function testNormalizeColName()
  212. {
  213. $tmap = new TestableTableMap();
  214. $this->assertEquals('', $tmap->normalizeColName(''), 'normalizeColName returns an empty string when passed an empty string');
  215. $this->assertEquals('BAR', $tmap->normalizeColName('bar'), 'normalizeColName uppercases the input');
  216. $this->assertEquals('BAR_BAZ', $tmap->normalizeColName('bar_baz'), 'normalizeColName does not mind underscores');
  217. $this->assertEquals('BAR', $tmap->normalizeColName('FOO.BAR'), 'normalizeColName removes table prefix');
  218. $this->assertEquals('BAR', $tmap->normalizeColName('BAR'), 'normalizeColName leaves normalized column names unchanged');
  219. $this->assertEquals('BAR_BAZ', $tmap->normalizeColName('foo.bar_baz'), 'normalizeColName can do all the above at the same time');
  220. }
  221. // deprecated method
  222. public function testContainsColumn()
  223. {
  224. $this->assertFalse($this->tmap->containsColumn('BAR'), 'containsColumn returns false when the column is not in the table map');
  225. $column = $this->tmap->addColumn('BAR', 'Bar', 'INTEGER');
  226. $this->assertTrue($this->tmap->containsColumn('BAR'), 'containsColumn returns true when the column is in the table map');
  227. $this->assertTrue($this->tmap->containsColumn('foo.bar'), 'containsColumn accepts a denormalized column name');
  228. $this->assertFalse($this->tmap->containsColumn('foo.bar', false), 'containsColumn accepts a $normalize parameter to skip name normalization');
  229. $this->assertTrue($this->tmap->containsColumn('BAR', false), 'containsColumn accepts a $normalize parameter to skip name normalization');
  230. $this->assertTrue($this->tmap->containsColumn($column), 'containsColumn accepts a ColumnMap object as parameter');
  231. }
  232. // deprecated methods
  233. public function testPrefix()
  234. {
  235. $tmap = new TestableTableMap();
  236. $this->assertNull($tmap->getPrefix(), 'prefix is empty until set');
  237. $this->assertFalse($tmap->hasPrefix('barbaz'), 'hasPrefix returns false when prefix is not set');
  238. $tmap->setPrefix('bar');
  239. $this->assertEquals('bar', $tmap->getPrefix(), 'prefix is set by setPrefix()');
  240. $this->assertTrue($tmap->hasPrefix('barbaz'), 'hasPrefix returns true when prefix is set and found in string');
  241. $this->assertFalse($tmap->hasPrefix('baz'), 'hasPrefix returns false when prefix is set and not found in string');
  242. $this->assertFalse($tmap->hasPrefix('bazbar'), 'hasPrefix returns false when prefix is set and not found anywhere in string');
  243. $this->assertEquals('baz', $tmap->removePrefix('barbaz'), 'removePrefix returns string without prefix if found at the beginning');
  244. $this->assertEquals('bazbaz', $tmap->removePrefix('bazbaz'), 'removePrefix returns original string when prefix is not found');
  245. $this->assertEquals('bazbar', $tmap->removePrefix('bazbar'), 'removePrefix returns original string when prefix is not found at the beginning');
  246. }
  247. }
  248. class TestableTableMap extends TableMap
  249. {
  250. public function hasPrefix($data)
  251. {
  252. return parent::hasPrefix($data);
  253. }
  254. public function removePrefix($data)
  255. {
  256. return parent::removePrefix($data);
  257. }
  258. public function normalizeColName($name)
  259. {
  260. return parent::normalizeColName($name);
  261. }
  262. }
  263. class FooTableMap extends TableMap
  264. {
  265. public $rmap;
  266. public function buildRelations()
  267. {
  268. $this->rmap = $this->addRelation('Bar', 'Bar', RelationMap::MANY_TO_ONE);
  269. }
  270. }
  271. class BarTableMap extends TableMap
  272. {
  273. public function initialize()
  274. {
  275. $this->setName('bar');
  276. $this->setClassName('Bar');
  277. }
  278. }