PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/propel/test/testsuite/generator/model/ColumnTest.php

https://bitbucket.org/bayrock/gw2spidy
PHP | 211 lines | 175 code | 18 blank | 18 comment | 2 complexity | ba5fa8d7adb960cf214b7ce344744938 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__) . '/../../../../generator/lib/model/Column.php';
  10. require_once dirname(__FILE__) . '/../../../../generator/lib/builder/util/XmlToAppData.php';
  11. require_once dirname(__FILE__) . '/../../../../generator/lib/platform/DefaultPlatform.php';
  12. require_once dirname(__FILE__) . '/../../../../generator/lib/behavior/AutoAddPkBehavior.php';
  13. /**
  14. * Tests for package handling.
  15. *
  16. * @author <a href="mailto:mpoeschl@marmot.at>Martin Poeschl</a>
  17. * @version $Revision$
  18. * @package generator.model
  19. */
  20. class ColumnTest extends PHPUnit_Framework_TestCase
  21. {
  22. /**
  23. * Tests static Column::makeList() method.
  24. * @deprecated - Column::makeList() is deprecated and set to be removed in 1.3
  25. */
  26. public function testMakeList()
  27. {
  28. $expected = 'Column0, Column1, Column2, Column3, Column4';
  29. $objArray = array();
  30. for ($i=0; $i<5; $i++) {
  31. $c = new Column();
  32. $c->setName("Column" . $i);
  33. $objArray[] = $c;
  34. }
  35. $list = Column::makeList($objArray, new DefaultPlatform());
  36. $this->assertEquals($expected, $list, sprintf("Expected '%s' match, got '%s' ", var_export($expected, true), var_export($list,true)));
  37. $strArray = array();
  38. for ($i=0; $i<5; $i++) {
  39. $strArray[] = "Column" . $i;
  40. }
  41. $list = Column::makeList($strArray, new DefaultPlatform());
  42. $this->assertEquals($expected, $list, sprintf("Expected '%s' match, got '%s' ", var_export($expected, true), var_export($list,true)));
  43. }
  44. public function testPhpNamingMethod()
  45. {
  46. $xmlToAppData = new XmlToAppData(new DefaultPlatform());
  47. $schema = <<<EOF
  48. <database name="test1">
  49. <behavior name="auto_add_pk" />
  50. <table name="table1">
  51. <column name="id" type="INTEGER" primaryKey="true" />
  52. <column name="author_id" type="INTEGER" />
  53. <column name="editor_id" type="INTEGER" phpNamingMethod="nochange" />
  54. </table>
  55. </database>
  56. EOF;
  57. $appData = $xmlToAppData->parseString($schema);
  58. $column = $appData->getDatabase('test1')->getTable('table1')->getColumn('author_id');
  59. $this->assertEquals('AuthorId', $column->getPhpName(), 'setPhpName() uses the default phpNamingMethod');
  60. $column = $appData->getDatabase('test1')->getTable('table1')->getColumn('editor_id');
  61. $this->assertEquals('editor_id', $column->getPhpName(), 'setPhpName() uses the column phpNamingMethod if given');
  62. }
  63. public function testDefaultPhpNamingMethod()
  64. {
  65. $xmlToAppData = new XmlToAppData(new DefaultPlatform());
  66. $schema = <<<EOF
  67. <database name="test2" defaultPhpNamingMethod="nochange">
  68. <behavior name="auto_add_pk" />
  69. <table name="table1">
  70. <column name="id" primaryKey="true" />
  71. <column name="author_id" type="VARCHAR" />
  72. </table>
  73. </database>
  74. EOF;
  75. $appData = $xmlToAppData->parseString($schema);
  76. $column = $appData->getDatabase('test2')->getTable('table1')->getColumn('author_id');
  77. $this->assertEquals('author_id', $column->getPhpName(), 'setPhpName() uses the database defaultPhpNamingMethod if given');
  78. }
  79. public function testGetConstantName()
  80. {
  81. $xmlToAppData = new XmlToAppData(new DefaultPlatform());
  82. $schema = <<<EOF
  83. <database name="test">
  84. <table name="table1">
  85. <column name="id" primaryKey="true" />
  86. <column name="title" type="VARCHAR" />
  87. </table>
  88. </database>
  89. EOF;
  90. $appData = $xmlToAppData->parseString($schema);
  91. $column = $appData->getDatabase('test')->getTable('table1')->getColumn('title');
  92. $this->assertEquals('Table1Peer::TITLE', $column->getConstantName(), 'getConstantName() returns the complete constant name by default');
  93. }
  94. public function testIsLocalColumnsRequired()
  95. {
  96. $xmlToAppData = new XmlToAppData(new DefaultPlatform());
  97. $schema = <<<EOF
  98. <database name="test">
  99. <table name="table1">
  100. <column name="id" primaryKey="true" />
  101. <column name="table2_foo" type="VARCHAR" />
  102. <foreign-key foreignTable="table2">
  103. <reference local="table2_foo" foreign="foo"/>
  104. </foreign-key>
  105. <column name="table2_bar" required="true" type="VARCHAR" />
  106. <foreign-key foreignTable="table2">
  107. <reference local="table2_bar" foreign="bar"/>
  108. </foreign-key>
  109. </table>
  110. <table name="table2">
  111. <column name="id" primaryKey="true" />
  112. <column name="foo" type="VARCHAR" />
  113. <column name="bar" type="VARCHAR" />
  114. </table>
  115. </database>
  116. EOF;
  117. $appData = $xmlToAppData->parseString($schema);
  118. $fk = $appData->getDatabase('test')->getTable('table1')->getColumnForeignKeys('table2_foo');
  119. $this->assertFalse($fk[0]->isLocalColumnsRequired());
  120. $fk = $appData->getDatabase('test')->getTable('table1')->getColumnForeignKeys('table2_bar');
  121. $this->assertTrue($fk[0]->isLocalColumnsRequired());
  122. }
  123. public function testIsNamePlural()
  124. {
  125. $column = new Column('foo');
  126. $this->assertFalse($column->isNamePlural());
  127. $column = new Column('foos');
  128. $this->assertTrue($column->isNamePlural());
  129. $column = new Column('foso');
  130. $this->assertFalse($column->isNamePlural());
  131. }
  132. public function testGetSingularName()
  133. {
  134. $column = new Column('foo');
  135. $this->assertEquals('foo', $column->getSingularName());
  136. $column = new Column('foos');
  137. $this->assertEquals('foo', $column->getSingularName());
  138. $column = new Column('foso');
  139. $this->assertEquals('foso', $column->getSingularName());
  140. }
  141. public function testGetValidator()
  142. {
  143. $xmlToAppData = new XmlToAppData(new DefaultPlatform());
  144. $schema = <<<EOF
  145. <database name="test">
  146. <table name="table1">
  147. <column name="id" primaryKey="true" />
  148. <column name="title1" type="VARCHAR" />
  149. <validator column="title1">
  150. <rule name="minLength" value="4" message="Username must be at least 4 characters !" />
  151. </validator>
  152. <column name="title2" type="VARCHAR" />
  153. <validator column="title2">
  154. <rule name="minLength" value="4" message="Username must be at least 4 characters !" />
  155. <rule name="maxLength" value="10" message="Username must be at most 10 characters !" />
  156. </validator>
  157. </table>
  158. </database>
  159. EOF;
  160. $appData = $xmlToAppData->parseString($schema);
  161. $table1 = $appData->getDatabase('test')->getTable('table1');
  162. $this->assertNull($table1->getColumn('id')->getValidator());
  163. $title1Column = $table1->getColumn('title1');
  164. $title1Validator = $title1Column->getValidator();
  165. $this->assertInstanceOf('Validator', $title1Validator);
  166. $this->assertEquals(1, count($title1Validator->getRules()));
  167. $title2Column = $table1->getColumn('title2');
  168. $title2Validator = $title2Column->getValidator();
  169. $this->assertInstanceOf('Validator', $title2Validator);
  170. $this->assertEquals(2, count($title2Validator->getRules()));
  171. }
  172. public function testHasPlatform()
  173. {
  174. $column = new Column();
  175. $this->assertFalse($column->hasPlatform());
  176. $table = new Table();
  177. $table->addColumn($column);
  178. $this->assertFalse($column->hasPlatform());
  179. $database = new Database();
  180. $database->addTable($table);
  181. $this->assertFalse($column->hasPlatform());
  182. $platform = new DefaultPlatform();
  183. $database->setPlatform($platform);
  184. $this->assertTrue($column->hasPlatform());
  185. }
  186. public function testIsPhpArrayType()
  187. {
  188. $column = new Column();
  189. $this->assertFalse($column->isPhpArrayType());
  190. $column->setType(PropelTypes::PHP_ARRAY);
  191. $this->assertTrue($column->isPhpArrayType());
  192. }
  193. }