PageRenderTime 19ms CodeModel.GetById 13ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/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/**
  4 * This file is part of the Propel package.
  5 * For the full copyright and license information, please view the LICENSE
  6 * file that was distributed with this source code.
  7 *
  8 * @license    MIT License
  9 */
 10
 11require_once dirname(__FILE__) . '/../../../../generator/lib/util/PropelDotGenerator.php';
 12require_once dirname(__FILE__) . '/../../../../generator/lib/model/AppData.php';
 13
 14/**
 15 *
 16 * @package    generator.util
 17 */
 18class PropelDotGeneratorTest extends PHPUnit_Framework_TestCase
 19{
 20    public function testEmptyDatabase()
 21    {
 22        $db = new Database();
 23        $db->setName('Empty');
 24
 25        $expected = implode("\n", array(
 26            'digraph G {',
 27            '}',
 28            '',
 29        ));
 30        $this->assertEquals($expected, PropelDotGenerator::create($db));
 31    }
 32
 33    public function testSingleTableWithoutName()
 34    {
 35        $column = new Column();
 36        $column->setName('id');
 37        $column->setPrimaryKey(true);
 38        $column->setAutoIncrement(true);
 39        $column->setType('integer');
 40
 41        $table = new Table();
 42        $table->setCommonName("t");
 43        $table->addColumn($column);
 44
 45        $db = new Database();
 46        $db->setName('SingleTable');
 47        $db->addTable($table);
 48
 49        $expected = implode("\n", array(
 50            'digraph G {',
 51                '"t" [label="{<table>t|<cols>id (integer) [PK]\l}", shape=record];',
 52            '}',
 53            '',
 54        ));
 55        $this->assertEquals($expected, PropelDotGenerator::create($db));
 56    }
 57
 58    public function testMultipleTablesWithoutForeignKey()
 59    {
 60        $column = new Column();
 61        $column->setName('id');
 62        $column->setPrimaryKey(true);
 63        $column->setAutoIncrement(true);
 64        $column->setType('integer');
 65
 66        $table = new Table();
 67        $table->setCommonName('table_one');
 68        $table->addColumn($column);
 69
 70        $db = new Database();
 71        $db->setName('MultipleTables');
 72        $db->addTable($table);
 73
 74        $column = new Column();
 75        $column->setName('id');
 76        $column->setPrimaryKey(true);
 77        $column->setAutoIncrement(true);
 78        $column->setType('integer');
 79
 80        $table = new Table();
 81        $table->setCommonName('table_two');
 82        $table->addColumn($column);
 83        $db->addTable($table);
 84
 85        $expected = implode("\n", array(
 86            'digraph G {',
 87                '"table_one" [label="{<table>table_one|<cols>id (integer) [PK]\l}", shape=record];',
 88                '"table_two" [label="{<table>table_two|<cols>id (integer) [PK]\l}", shape=record];',
 89            '}',
 90            '',
 91        ));
 92        $this->assertEquals($expected, PropelDotGenerator::create($db));
 93    }
 94
 95    public function testMultipleTablesWithFK()
 96    {
 97        $column = new Column();
 98        $column->setName('id');
 99        $column->setPrimaryKey(true);
100        $column->setAutoIncrement(true);
101        $column->setType('integer');
102
103        $table = new Table();
104        $table->setCommonName('table_one');
105        $table->addColumn($column);
106
107        $db = new Database();
108        $db->setName('MultipleTables');
109        $db->addTable($table);
110
111        $column = new Column();
112        $column->setName('id');
113        $column->setPrimaryKey(true);
114        $column->setAutoIncrement(true);
115        $column->setType('integer');
116
117        $c2 = new Column();
118        $c2->setName('foreign_id');
119        $c2->setType('integer');
120
121        $table = new Table();
122        $table->setCommonName('table_two');
123        $table->addColumn($column);
124        $table->addColumn($c2);
125
126        $fk = new ForeignKey();
127        $fk->setName('FK_1');
128        $fk->addReference('foreign_id', 'id');
129        $fk->setForeignTableCommonName('table_one');
130
131        $table->addForeignKey($fk);
132
133        $db->addTable($table);
134
135        $expected = implode("\n", array(
136            'digraph G {',
137                '"table_one" [label="{<table>table_one|<cols>id (integer) [PK]\l}", shape=record];',
138                '"table_two" [label="{<table>table_two|<cols>id (integer) [PK]\lforeign_id (integer) [FK]\l}", shape=record];',
139                '"table_two":cols -> "table_one":table [label="foreign_id=id"];',
140            '}',
141            '',
142        ));
143        $this->assertEquals($expected, PropelDotGenerator::create($db));
144    }
145
146    public function testColumnIsFKAndPK()
147    {
148        $column = new Column();
149        $column->setName('id');
150        $column->setPrimaryKey(true);
151        $column->setAutoIncrement(true);
152        $column->setType('integer');
153
154        $table = new Table();
155        $table->setCommonName('table_one');
156        $table->addColumn($column);
157
158        $db = new Database();
159        $db->setName('MultipleTables');
160        $db->addTable($table);
161
162        $column = new Column();
163        $column->setName('id');
164        $column->setPrimaryKey(true);
165        $column->setAutoIncrement(true);
166        $column->setType('integer');
167
168        $c2 = new Column();
169        $c2->setPrimaryKey(true);
170        $c2->setName('foreign_id');
171        $c2->setType('integer');
172
173        $table = new Table();
174        $table->setCommonName('table_two');
175        $table->addColumn($column);
176        $table->addColumn($c2);
177
178        $fk = new ForeignKey();
179        $fk->setName('FK_1');
180        $fk->addReference('foreign_id', 'id');
181        $fk->setForeignTableCommonName('table_one');
182
183        $table->addForeignKey($fk);
184
185        $db->addTable($table);
186
187        $expected = implode("\n", array(
188            'digraph G {',
189                '"table_one" [label="{<table>table_one|<cols>id (integer) [PK]\l}", shape=record];',
190                '"table_two" [label="{<table>table_two|<cols>id (integer) [PK]\lforeign_id (integer) [FK] [PK]\l}", shape=record];',
191                '"table_two":cols -> "table_one":table [label="foreign_id=id"];',
192            '}',
193            '',
194        ));
195        $this->assertEquals($expected, PropelDotGenerator::create($db));
196    }
197}