Skip to content

Commit

Permalink
db:get-name command
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony BOTALLA committed May 3, 2023
1 parent 2ec4a68 commit 886964f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/ArtisanCommandsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function boot()
$this->commands([
Console\Database\Configure::class,
Console\Database\Create::class,
Console\Database\GetName::class,
]);
}
}
Expand Down
46 changes: 46 additions & 0 deletions src/Console/Database/GetName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Novius\ArtisanCommands\Console\Database;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class GetName extends Command
{
protected $signature = 'db:get-name {--connection=}';

protected $description = 'Get database name for current or given connection name.';

protected ?string $connectionName;

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$this->connectionName = $this->option('connection');
$dbName = DB::getDatabaseName();

if ($this->connectionName !== null) {
if (! config()->has($this->databaseConfigName())) {
return Command::FAILURE;
}

$dbName = config($this->databaseConfigName());
}

$this->line($dbName);

return Command::SUCCESS;
}

/**
* Return complete database connection config name, using $this->connectionName.
*/
protected function databaseConfigName(): string
{
return 'database.connections.'.$this->connectionName.'.database';
}
}
1 change: 1 addition & 0 deletions tests/Console/Database/ConfigureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ protected function getPackageProviders($app)
public function testConfigureDBCommandExists()
{
$commands = Artisan::all();

$this->assertArrayHasKey('db:configure', $commands);
}
}
27 changes: 27 additions & 0 deletions tests/Console/Database/GetNameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Novius\ArtisanCommands\Tests\Console\Database;

use Illuminate\Support\Facades\Artisan;
use Novius\ArtisanCommands\ArtisanCommandsServiceProvider;
use Orchestra\Testbench\TestCase;

class GetNameTest extends TestCase
{
protected function getPackageProviders($app)
{
return [
ArtisanCommandsServiceProvider::class,
];
}

/**
* Does the new command db:create exist ? This doesn't check if this command actually work.
*/
public function testConfigureDBCommandExists()
{
$commands = Artisan::all();

$this->assertArrayHasKey('db:get-name', $commands);
}
}

0 comments on commit 886964f

Please sign in to comment.