/test/testsuite/generator/util/PropelDotGeneratorTest.php

http://github.com/propelorm/Propel · PHP · 197 lines · 152 code · 34 blank · 11 comment · 0 complexity · 21abcb400e21f5e848130a39f7d759fd 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__) . '/../../../../generator/lib/util/PropelDotGenerator.php';
  10. require_once dirname(__FILE__) . '/../../../../generator/lib/model/AppData.php';
  11. /**
  12. *
  13. * @package generator.util
  14. */
  15. class PropelDotGeneratorTest extends PHPUnit_Framework_TestCase
  16. {
  17. public function testEmptyDatabase()
  18. {
  19. $db = new Database();
  20. $db->setName('Empty');
  21. $expected = implode("\n", array(
  22. 'digraph G {',
  23. '}',
  24. '',
  25. ));
  26. $this->assertEquals($expected, PropelDotGenerator::create($db));
  27. }
  28. public function testSingleTableWithoutName()
  29. {
  30. $column = new Column();
  31. $column->setName('id');
  32. $column->setPrimaryKey(true);
  33. $column->setAutoIncrement(true);
  34. $column->setType('integer');
  35. $table = new Table();
  36. $table->setCommonName("t");
  37. $table->addColumn($column);
  38. $db = new Database();
  39. $db->setName('SingleTable');
  40. $db->addTable($table);
  41. $expected = implode("\n", array(
  42. 'digraph G {',
  43. '"t" [label="{<table>t|<cols>id (integer) [PK]\l}", shape=record];',
  44. '}',
  45. '',
  46. ));
  47. $this->assertEquals($expected, PropelDotGenerator::create($db));
  48. }
  49. public function testMultipleTablesWithoutForeignKey()
  50. {
  51. $column = new Column();
  52. $column->setName('id');
  53. $column->setPrimaryKey(true);
  54. $column->setAutoIncrement(true);
  55. $column->setType('integer');
  56. $table = new Table();
  57. $table->setCommonName('table_one');
  58. $table->addColumn($column);
  59. $db = new Database();
  60. $db->setName('MultipleTables');
  61. $db->addTable($table);
  62. $column = new Column();
  63. $column->setName('id');
  64. $column->setPrimaryKey(true);
  65. $column->setAutoIncrement(true);
  66. $column->setType('integer');
  67. $table = new Table();
  68. $table->setCommonName('table_two');
  69. $table->addColumn($column);
  70. $db->addTable($table);
  71. $expected = implode("\n", array(
  72. 'digraph G {',
  73. '"table_one" [label="{<table>table_one|<cols>id (integer) [PK]\l}", shape=record];',
  74. '"table_two" [label="{<table>table_two|<cols>id (integer) [PK]\l}", shape=record];',
  75. '}',
  76. '',
  77. ));
  78. $this->assertEquals($expected, PropelDotGenerator::create($db));
  79. }
  80. public function testMultipleTablesWithFK()
  81. {
  82. $column = new Column();
  83. $column->setName('id');
  84. $column->setPrimaryKey(true);
  85. $column->setAutoIncrement(true);
  86. $column->setType('integer');
  87. $table = new Table();
  88. $table->setCommonName('table_one');
  89. $table->addColumn($column);
  90. $db = new Database();
  91. $db->setName('MultipleTables');
  92. $db->addTable($table);
  93. $column = new Column();
  94. $column->setName('id');
  95. $column->setPrimaryKey(true);
  96. $column->setAutoIncrement(true);
  97. $column->setType('integer');
  98. $c2 = new Column();
  99. $c2->setName('foreign_id');
  100. $c2->setType('integer');
  101. $table = new Table();
  102. $table->setCommonName('table_two');
  103. $table->addColumn($column);
  104. $table->addColumn($c2);
  105. $fk = new ForeignKey();
  106. $fk->setName('FK_1');
  107. $fk->addReference('foreign_id', 'id');
  108. $fk->setForeignTableCommonName('table_one');
  109. $table->addForeignKey($fk);
  110. $db->addTable($table);
  111. $expected = implode("\n", array(
  112. 'digraph G {',
  113. '"table_one" [label="{<table>table_one|<cols>id (integer) [PK]\l}", shape=record];',
  114. '"table_two" [label="{<table>table_two|<cols>id (integer) [PK]\lforeign_id (integer) [FK]\l}", shape=record];',
  115. '"table_two":cols -> "table_one":table [label="foreign_id=id"];',
  116. '}',
  117. '',
  118. ));
  119. $this->assertEquals($expected, PropelDotGenerator::create($db));
  120. }
  121. public function testColumnIsFKAndPK()
  122. {
  123. $column = new Column();
  124. $column->setName('id');
  125. $column->setPrimaryKey(true);
  126. $column->setAutoIncrement(true);
  127. $column->setType('integer');
  128. $table = new Table();
  129. $table->setCommonName('table_one');
  130. $table->addColumn($column);
  131. $db = new Database();
  132. $db->setName('MultipleTables');
  133. $db->addTable($table);
  134. $column = new Column();
  135. $column->setName('id');
  136. $column->setPrimaryKey(true);
  137. $column->setAutoIncrement(true);
  138. $column->setType('integer');
  139. $c2 = new Column();
  140. $c2->setPrimaryKey(true);
  141. $c2->setName('foreign_id');
  142. $c2->setType('integer');
  143. $table = new Table();
  144. $table->setCommonName('table_two');
  145. $table->addColumn($column);
  146. $table->addColumn($c2);
  147. $fk = new ForeignKey();
  148. $fk->setName('FK_1');
  149. $fk->addReference('foreign_id', 'id');
  150. $fk->setForeignTableCommonName('table_one');
  151. $table->addForeignKey($fk);
  152. $db->addTable($table);
  153. $expected = implode("\n", array(
  154. 'digraph G {',
  155. '"table_one" [label="{<table>table_one|<cols>id (integer) [PK]\l}", shape=record];',
  156. '"table_two" [label="{<table>table_two|<cols>id (integer) [PK]\lforeign_id (integer) [FK] [PK]\l}", shape=record];',
  157. '"table_two":cols -> "table_one":table [label="foreign_id=id"];',
  158. '}',
  159. '',
  160. ));
  161. $this->assertEquals($expected, PropelDotGenerator::create($db));
  162. }
  163. }