Skip to content

Commit cb3d59e

Browse files
committed
add column types to generated diagram
1 parent c815b72 commit cb3d59e

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

config/config.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
*/
1919
'use_db_schema' => true,
2020

21+
/*
22+
* This setting toggles weather the column types (VARCHAR, INT, TEXT, etc.)
23+
* should be visible on the generated diagram. This option requires
24+
* 'use_db_schema' to be set to true.
25+
*/
26+
'use_column_types' => true,
27+
2128
/*
2229
* These colors will be used in the table representation for each entity in
2330
* your graph.

src/GraphBuilder.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ protected function getModelLabel(string $model, string $label)
6060
if (config('erd-generator.use_db_schema')) {
6161
$columns = $this->getTableColumnsFromModel($model);
6262
foreach ($columns as $column) {
63-
$table .= '<tr width="100%"><td port="' . $column->getName() . '" align="left" width="100%" bgcolor="'.config('erd-generator.table.row_background_color').'"><font color="'.config('erd-generator.table.row_font_color').'" >' . $column->getName() . '</font></td></tr>' . PHP_EOL;
63+
$label = $column->getName();
64+
if (config('erd-generator.use_column_types')) {
65+
$label .= ' ('.$column->getType()->getName().')';
66+
}
67+
$table .= '<tr width="100%"><td port="' . $column->getName() . '" align="left" width="100%" bgcolor="'.config('erd-generator.table.row_background_color').'"><font color="'.config('erd-generator.table.row_font_color').'" >' . $label . '</font></td></tr>' . PHP_EOL;
6468
}
6569
}
6670

tests/GenerationTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,32 @@ class GenerationTest extends TestCase
1212
/** @test */
1313
public function it_generated_graphviz_for_test_models()
1414
{
15+
$this->app['config']->set('erd-generator.use_db_schema', false);
16+
$this->app['config']->set('erd-generator.directories', [__DIR__.'/Models']);
17+
18+
Artisan::call('generate:erd', [
19+
'--format' => 'text'
20+
]);
21+
22+
$this->assertMatchesSnapshot(Artisan::output());
23+
}
24+
25+
/** @test */
26+
public function it_generated_graphviz_for_test_models_with_db_columns_and_types()
27+
{
28+
$this->app['config']->set('erd-generator.directories', [__DIR__.'/Models']);
29+
30+
Artisan::call('generate:erd', [
31+
'--format' => 'text'
32+
]);
33+
34+
$this->assertMatchesSnapshot(Artisan::output());
35+
}
36+
37+
/** @test */
38+
public function it_generated_graphviz_for_test_models_with_db_columns()
39+
{
40+
$this->app['config']->set('erd-generator.use_column_types', false);
1541
$this->app['config']->set('erd-generator.directories', [__DIR__.'/Models']);
1642

1743
Artisan::call('generate:erd', [

tests/TestCase.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,64 @@
22

33
namespace BeyondCode\ErdGenerator\Tests;
44

5+
use Illuminate\Database\Schema\Blueprint;
56
use BeyondCode\ErdGenerator\ErdGeneratorServiceProvider;
67

78
abstract class TestCase extends \Orchestra\Testbench\TestCase
89
{
10+
public function setUp()
11+
{
12+
parent::setUp();
13+
14+
$this->setUpDatabase();
15+
}
916

1017
protected function getPackageProviders($app)
1118
{
1219
return [ErdGeneratorServiceProvider::class];
1320
}
1421

22+
protected function getEnvironmentSetUp($app)
23+
{
24+
$app['config']->set('database.default', 'sqlite');
25+
26+
$app['config']->set('database.connections.sqlite', [
27+
'driver' => 'sqlite',
28+
'database' => ':memory:',
29+
'prefix' => '',
30+
]);
31+
}
32+
33+
protected function setUpDatabase()
34+
{
35+
$this->app['db']->connection()->getSchemaBuilder()->create('avatars', function (Blueprint $table) {
36+
$table->increments('id');
37+
$table->string('url');
38+
$table->timestamps();
39+
});
40+
41+
$this->app['db']->connection()->getSchemaBuilder()->create('posts', function (Blueprint $table) {
42+
$table->increments('id');
43+
$table->unsignedInteger('user_id');
44+
$table->string('title');
45+
$table->text('body');
46+
$table->timestamps();
47+
});
48+
49+
$this->app['db']->connection()->getSchemaBuilder()->create('users', function (Blueprint $table) {
50+
$table->increments('id');
51+
$table->unsignedInteger('avatar_id');
52+
$table->string('name');
53+
$table->string('email');
54+
$table->timestamps();
55+
});
56+
57+
$this->app['db']->connection()->getSchemaBuilder()->create('comments', function (Blueprint $table) {
58+
$table->increments('id');
59+
$table->unsignedInteger('post_id');
60+
$table->string('body');
61+
$table->morphs('commentable');
62+
});
63+
}
64+
1565
}

0 commit comments

Comments
 (0)