Skip to content

Commit

Permalink
Merge pull request #16 from recca0120/feat/connections
Browse files Browse the repository at this point in the history
connections
  • Loading branch information
recca0120 authored Nov 4, 2024
2 parents 06796d7 + ecf0d33 commit 7af5679
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,33 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup MySQL
uses: shogo82148/actions-setup-mysql@v1
with:
mysql-version: ${{ matrix.mysql }}
root-password: ''
my-cnf: |
innodb_log_file_size=256MB
innodb_buffer_pool_size=512MB
max_allowed_packet=16MB
max_connections=50
local_infile=1
log-output=NONE
slow-query-log=0
innodb_flush_log_at_trx_commit=0
innodb_log_buffer_size=3M
innodb_buffer_pool_size=180M
innodb_doublewrite=0
datadir=/dev/shm/mysql
- name: Show MySQL Variables
run: mysql -uroot -h127.0.0.1 -e 'show variables'

- name: Create Database
run: |
mysql -uroot -h127.0.0.1 -e 'create database testbench;'
mysql -uroot -h127.0.0.1 -e 'create database test;'
- name: Setup cache environment
id: extcache
uses: shivammathur/cache-extensions@v1
Expand Down
1 change: 1 addition & 0 deletions config/laravel-erd.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
'erd-go' => env('LARAVEL_ERD_GO', '/usr/local/bin/erd-go'),
'dot' => env('LARAVEL_ERD_DOT', '/usr/local/bin/dot'),
],
'connections' => [],
];
22 changes: 18 additions & 4 deletions src/Console/Commands/GenerateErd.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ public function handle(Factory $factory, TemplateFactory $templateFactory): int

private function runMigrate(?string $database): int
{
$default = config('database.default');
$arguments = array_filter([
'--database' => $database,
'--database' => $default === $database ? null : $database,
'--path' => $this->option('path'),
]);

Expand All @@ -84,11 +85,15 @@ private function runMigrate(?string $database): int

private function setupFakeDatabase(?string $database): void
{
$default = config('database.default');
$database = $database ?? $default;
$connections = config('laravel-erd.connections');

$this->backup['cache.default'] = config('cache.default');
$this->backup['database.connections'] = config('database.connections');

config(['cache.default' => 'array']);
config(Arr::dot(array_map(static fn (array $config) => [
config(Arr::dot(array_map(static fn (array $config) => $connections[$database] ?? [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => $config['prefix'] ?? '',
Expand All @@ -98,12 +103,21 @@ private function setupFakeDatabase(?string $database): void
'synchronous' => null,
], $this->backup['database.connections']), 'database.connections.'));

DB::purge($database ?? config('database.default'));
DB::purge($database);
}

private function restoreDatabase(?string $database): void
{
DB::purge($database ?? config('database.default'));
$default = config('database.default');
$arguments = array_filter([
'--database' => $default === $database ? null : $database,
'--path' => $this->option('path'),
]);

$output = new BufferedOutput;
$this->runCommand('migrate:rollback', $arguments, $output);

DB::purge($database);

config(['cache.default' => $this->backup['cache.default']]);
config(['database.connections' => $this->backup['database.connections']]);
Expand Down
34 changes: 31 additions & 3 deletions tests/Console/Commands/GenerateErdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ class GenerateErdTest extends TestCase
{
private string $storagePath;

private string $file = 'actual_artisan.svg';

protected function setUp(): void
{
parent::setUp();
Expand All @@ -27,6 +25,7 @@ protected function setUp(): void
'telescope.storage.database.connection' => 'testbench',
]);

$this->app['config']->set('database.default', 'testbench');
$this->app['config']->set('database.connections.testbench', [
'driver' => 'mysql',
'url' => '',
Expand Down Expand Up @@ -74,10 +73,39 @@ protected function tearDown(): void
parent::tearDown();
}

public function test_generate_default_svg(): void
public function test_generate_testbench_svg(): void
{
$file = 'testbench.svg';

$parameters = $this->givenParameters(['--file' => $file]);
$this->artisan('erd:generate', $parameters)->execute();

$contents = file_get_contents($this->storagePath.'/'.$file);
self::assertStringContainsString('<!-- cars -->', $contents);
self::assertStringContainsString('<!-- phones&#45;&#45;users -->', $contents);
}

public function test_customize_fake_database(): void
{
$file = 'testbench.svg';

$this->app['config']->set('laravel-erd.connections.testbench', [
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => '3306',
'database' => 'test',
'username' => 'root',
'password' => '',
'unix_socket' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => false,
'engine' => null,
'options' => [],
]);

$parameters = $this->givenParameters(['--file' => $file]);
$this->artisan('erd:generate', $parameters)->execute();

Expand Down

0 comments on commit 7af5679

Please sign in to comment.