Skip to content

Commit

Permalink
Update database connection when changing database.sqlite using `wor…
Browse files Browse the repository at this point in the history
…kbench:build` (#13)

* Update database connection when changing `database.sqlite` using
`workbench:build`

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

---------

Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone authored Aug 18, 2023
1 parent 7e45401 commit b2a509e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ protected function appendScriptsToComposer(array $content, Filesystem $filesyste
$content['scripts']['clear'] = '@php vendor/bin/testbench package:purge-skeleton --ansi';
$content['scripts']['prepare'] = '@php vendor/bin/testbench package:discover --ansi';
$content['scripts']['build'] = '@php vendor/bin/testbench workbench:build';
$content['scripts']['serve'] = '@php vendor/bin/testbench serve';
$content['scripts']['serve'] = [
'@build',
'@php vendor/bin/testbench serve',
];

if (! \array_key_exists('lint', $content['scripts'])) {
$lintScripts = [];
Expand Down
12 changes: 10 additions & 2 deletions src/RecipeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ public function createAssetPublishDriver(): Contracts\Recipe
*/
public function createCreateSqliteDbDriver(): Contracts\Recipe
{
return new Recipes\Command('workbench:create-sqlite-db');
return new Recipes\Command('workbench:create-sqlite-db', callback: function () {
if (config('database.default') === 'testing') {
config(['database.default' => 'sqlite']);
}
});
}

/**
Expand All @@ -34,7 +38,11 @@ public function createCreateSqliteDbDriver(): Contracts\Recipe
*/
public function createDropSqliteDbDriver(): Contracts\Recipe
{
return new Recipes\Command('workbench:drop-sqlite-db');
return new Recipes\Command('workbench:drop-sqlite-db', callback: function () {
if (config('database.default') === 'sqlite') {
config(['database.default' => 'testing']);
}
});
}

/**
Expand Down
17 changes: 15 additions & 2 deletions src/Recipes/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,25 @@

class Command implements Recipe
{
/**
* After completion callback.
*
* @var (callable(\Symfony\Component\Console\Output\OutputInterface):(void))|null
*/
public $callback;

/**
* Construct a new recipe.
*
* @param array<string, mixed> $options
* @param (callable(\Symfony\Component\Console\Output\OutputInterface):(void))|null $callback
*/
public function __construct(
public string $command,
public array $options = []
public array $options = [],
?callable $callback = null
) {
//
$this->callback = $callback;
}

/**
Expand All @@ -30,6 +39,10 @@ public function handle(ConsoleKernel $kernel, OutputInterface $output)
$kernel->call(
$this->commandName(), $this->commandOptions(), $output
);

if (\is_callable($this->callback)) {
\call_user_func($this->callback, $output);
}
}

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/Recipes/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,27 @@ public function it_can_resolve_correct_command()

$command->handle($kernel, $output);
}

/** @test */
public function it_can_resolve_correct_command_with_callback()
{
$kernel = m::mock(ConsoleKernel::class);
$output = m::mock(OutputInterface::class);

$command = new Command(
'vendor:publish',
['--tag' => ['laravel-assets']],
fn ($o) => $this->assertSame($o, $output)
);

$kernel->shouldReceive('call')
->once()
->with(
'vendor:publish',
['--tag' => ['laravel-assets']],
m::type(OutputInterface::class)
)->andReturnNull();

$command->handle($kernel, $output);
}
}

0 comments on commit b2a509e

Please sign in to comment.