PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/bayrock/gw2spidy
PHP | 152 lines | 116 code | 20 blank | 16 comment | 0 complexity | e3bba28402e86c2bb6d0fa4a73103ee8 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__) . '/../../../tools/helpers/bookstore/BookstoreTestBase.php';
  10. /**
  11. * Test class for TableMap.
  12. *
  13. * @author François Zaninotto
  14. * @version $Id$
  15. * @package runtime.map
  16. */
  17. class ColumnMapTest extends BookstoreTestBase
  18. {
  19. protected $databaseMap;
  20. protected function setUp()
  21. {
  22. parent::setUp();
  23. $this->dmap = new DatabaseMap('foodb');
  24. $this->tmap = new TableMap('foo', $this->dmap);
  25. $this->columnName = 'bar';
  26. $this->cmap = new ColumnMap($this->columnName, $this->tmap);
  27. }
  28. protected function tearDown()
  29. {
  30. // nothing to do for now
  31. parent::tearDown();
  32. }
  33. public function testConstructor()
  34. {
  35. $this->assertEquals($this->columnName, $this->cmap->getName(), 'constructor sets the column name');
  36. $this->assertEquals($this->tmap, $this->cmap->getTable(), 'Constructor sets the table map');
  37. $this->assertNull($this->cmap->getType(), 'A new column map has no type');
  38. }
  39. public function testPhpName()
  40. {
  41. $this->assertNull($this->cmap->getPhpName(), 'phpName is empty until set');
  42. $this->cmap->setPhpName('FooBar');
  43. $this->assertEquals('FooBar', $this->cmap->getPhpName(), 'phpName is set by setPhpName()');
  44. }
  45. public function testType()
  46. {
  47. $this->assertNull($this->cmap->getType(), 'type is empty until set');
  48. $this->cmap->setType('FooBar');
  49. $this->assertEquals('FooBar', $this->cmap->getType(), 'type is set by setType()');
  50. }
  51. public function tesSize()
  52. {
  53. $this->assertEquals(0, $this->cmap->getSize(), 'size is empty until set');
  54. $this->cmap->setSize(123);
  55. $this->assertEquals(123, $this->cmap->getSize(), 'size is set by setSize()');
  56. }
  57. public function testPrimaryKey()
  58. {
  59. $this->assertFalse($this->cmap->isPrimaryKey(), 'primaryKey is false by default');
  60. $this->cmap->setPrimaryKey(true);
  61. $this->assertTrue($this->cmap->isPrimaryKey(), 'primaryKey is set by setPrimaryKey()');
  62. }
  63. public function testNotNull()
  64. {
  65. $this->assertFalse($this->cmap->isNotNull(), 'notNull is false by default');
  66. $this->cmap->setNotNull(true);
  67. $this->assertTrue($this->cmap->isNotNull(), 'notNull is set by setPrimaryKey()');
  68. }
  69. public function testDefaultValue()
  70. {
  71. $this->assertNull($this->cmap->getDefaultValue(), 'defaultValue is empty until set');
  72. $this->cmap->setDefaultValue('FooBar');
  73. $this->assertEquals('FooBar', $this->cmap->getDefaultValue(), 'defaultValue is set by setDefaultValue()');
  74. }
  75. public function testGetForeignKey()
  76. {
  77. $this->assertFalse($this->cmap->isForeignKey(), 'foreignKey is false by default');
  78. try {
  79. $this->cmap->getRelatedTable();
  80. $this->fail('getRelatedTable throws an exception when called on a column with no foreign key');
  81. } catch (PropelException $e) {
  82. $this->assertTrue(true, 'getRelatedTable throws an exception when called on a column with no foreign key');
  83. }
  84. try {
  85. $this->cmap->getRelatedColumn();
  86. $this->fail('getRelatedColumn throws an exception when called on a column with no foreign key');
  87. } catch (PropelException $e) {
  88. $this->assertTrue(true, 'getRelatedColumn throws an exception when called on a column with no foreign key');
  89. }
  90. $relatedTmap = $this->dmap->addTable('foo2');
  91. // required to let the database map use the foreign TableMap
  92. $relatedCmap = $relatedTmap->addColumn('BAR2', 'Bar2', 'INTEGER');
  93. $this->cmap->setForeignKey('foo2', 'BAR2');
  94. $this->assertTrue($this->cmap->isForeignKey(), 'foreignKey is true after setting the foreign key via setForeignKey()');
  95. $this->assertEquals($relatedTmap, $this->cmap->getRelatedTable(), 'getRelatedTable returns the related TableMap object');
  96. $this->assertEquals($relatedCmap, $this->cmap->getRelatedColumn(), 'getRelatedColumn returns the related ColumnMap object');
  97. }
  98. public function testGetRelation()
  99. {
  100. $bookTable = BookPeer::getTableMap();
  101. $titleColumn = $bookTable->getColumn('TITLE');
  102. $this->assertNull($titleColumn->getRelation(), 'getRelation() returns null for non-foreign key columns');
  103. $publisherColumn = $bookTable->getColumn('PUBLISHER_ID');
  104. $this->assertEquals($publisherColumn->getRelation(), $bookTable->getRelation('Publisher'), 'getRelation() returns the RelationMap object for this foreign key');
  105. $bookstoreTable = BookstoreEmployeePeer::getTableMap();
  106. $supervisorColumn = $bookstoreTable->getColumn('SUPERVISOR_ID');
  107. $this->assertEquals($supervisorColumn->getRelation(), $supervisorColumn->getRelation('Supervisor'), 'getRelation() returns the RelationMap object even whit ha specific refPhpName');
  108. }
  109. public function testNormalizeName()
  110. {
  111. $this->assertEquals('', ColumnMap::normalizeName(''), 'normalizeColumnName() returns an empty string when passed an empty string');
  112. $this->assertEquals('BAR', ColumnMap::normalizeName('bar'), 'normalizeColumnName() uppercases the input');
  113. $this->assertEquals('BAR_BAZ', ColumnMap::normalizeName('bar_baz'), 'normalizeColumnName() does not mind underscores');
  114. $this->assertEquals('BAR', ColumnMap::normalizeName('FOO.BAR'), 'normalizeColumnName() removes table prefix');
  115. $this->assertEquals('BAR', ColumnMap::normalizeName('BAR'), 'normalizeColumnName() leaves normalized column names unchanged');
  116. $this->assertEquals('BAR_BAZ', ColumnMap::normalizeName('foo.bar_baz'), 'normalizeColumnName() can do all the above at the same time');
  117. }
  118. public function testIsPrimaryString()
  119. {
  120. $bookTable = BookPeer::getTableMap();
  121. $idColumn = $bookTable->getColumn('ID');
  122. $titleColumn = $bookTable->getColumn('TITLE');
  123. $isbnColumn = $bookTable->getColumn('ISBN');
  124. $this->assertFalse($idColumn->isPrimaryString(), 'isPrimaryString() returns false by default.');
  125. $this->assertTrue($titleColumn->isPrimaryString(), 'isPrimaryString() returns true if set in schema.');
  126. $this->assertFalse($isbnColumn->isPrimaryString(), 'isPrimaryString() returns false if not set in schema.');
  127. $titleColumn->setPrimaryString(false);
  128. $this->assertFalse($titleColumn->isPrimaryString(), 'isPrimaryString() returns false if unset.');
  129. $titleColumn->setPrimaryString(true);
  130. $this->assertTrue($titleColumn->isPrimaryString(), 'isPrimaryString() returns true if set.');
  131. }
  132. }