Skip to content

Commit

Permalink
[7.x] Ensure generated testbench.yaml is configured with App\\ an…
Browse files Browse the repository at this point in the history
…d `Database\\` instead of `Workbench\App\\` and `Workbench\Database\\` based on configured namespaces (#63)

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

---------

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone authored Dec 14, 2024
1 parent b8a5531 commit 9ca758c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 15 deletions.
17 changes: 10 additions & 7 deletions src/Console/DevToolCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class DevToolCommand extends Command
{
use Concerns\InteractsWithFiles;

/**
* Namespace prefix for Workbench environment.
*/
protected string $workbenchNamespacePrefix = 'Workbench\\';

/**
* Execute the console command.
*
Expand Down Expand Up @@ -235,16 +240,14 @@ protected function appendAutoloadDevToComposer(array $content, Filesystem $files
$content['autoload-dev']['psr-4'] = [];
}

$namespacePrefix = '';

if ($this->components->confirm('Prefix with `Workbench` namespace?', default: true)) {
$namespacePrefix = 'Workbench\\';
if ($this->components->confirm('Prefix with `Workbench` namespace?', default: false) === false) {
$this->workbenchNamespacePrefix = '';
}

$namespaces = [
'workbench/app/' => $namespacePrefix.'App\\',
'workbench/database/factories/' => $namespacePrefix.'Database\\Factories\\',
'workbench/database/seeders/' => $namespacePrefix.'Database\\Seeders\\',
'workbench/app/' => $this->workbenchNamespacePrefix.'App\\',
'workbench/database/factories/' => $this->workbenchNamespacePrefix.'Database\\Factories\\',
'workbench/database/seeders/' => $this->workbenchNamespacePrefix.'Database\\Seeders\\',
];

$autoloads = array_flip($content['autoload-dev']['psr-4']);
Expand Down
13 changes: 13 additions & 0 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ protected function copyTestbenchConfigurationFile(Filesystem $filesystem, string
components: $this->components,
force: (bool) $this->option('force'),
))->handle($from, $to);

$this->replaceInFile(
$filesystem,
[
'Workbench\App\Providers\WorkbenchServiceProvider',
'Workbench\Database\Seeders\DatabaseSeeder',
],
[
\sprintf('%sProviders\WorkbenchServiceProvider', Workbench::detectNamespace('app') ?? 'Workbench\App\\'),
\sprintf('%sDatabaseSeeder', Workbench::detectNamespace('database/seeders') ?? 'Workbench\Database\Seeders\\'),
],
$to
);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Console/stubs/testbench.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ providers:
migrations:
- workbench/database/migrations

seeders:
- Workbench\Database\Seeders\DatabaseSeeder

workbench:
start: '/'
install: true
Expand All @@ -21,7 +24,6 @@ workbench:
- db-wipe
- migrate-fresh:
--seed: true
--seeder: Workbench\Database\Seeders\DatabaseSeeder
assets:
- laravel-assets
sync: []
3 changes: 1 addition & 2 deletions tests/Console/CommandTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,13 @@ protected function assertCommandExecutedWithInstall(): void
$config = Config::loadFromYaml($workingPath);

$this->assertSame(default_skeleton_path(), $config['laravel']);
$this->assertFalse($config->seeders);
$this->assertSame([DatabaseSeeder::class], $config->seeders);
$this->assertSame([
'asset-publish',
'create-sqlite-db',
'db-wipe',
['migrate-fresh' => [
'--seed' => true,
'--seeder' => DatabaseSeeder::class,
]],
], $config->getWorkbenchAttributes()['build']);
$this->assertSame([
Expand Down
8 changes: 4 additions & 4 deletions tests/Console/DevToolCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class DevToolCommandTest extends CommandTestCase
public function it_can_run_devtool_command_with_installation(?string $answer, bool $createEnvironmentFile)
{
$this->artisan('workbench:devtool', ['--install' => true])
->expectsConfirmation('Prefix with `Workbench` namespace?', true)
->expectsConfirmation('Prefix with `Workbench` namespace?', 'yes')
->expectsChoice("Export '.env' file as?", $answer, [
'Skip exporting .env',
'.env',
Expand All @@ -33,7 +33,7 @@ public function it_can_run_devtool_command_with_installation(?string $answer, bo
public function it_can_run_devtool_command_with_basic_installation(?string $answer, bool $createEnvironmentFile)
{
$this->artisan('workbench:devtool', ['--install' => true, '--basic' => true])
->expectsConfirmation('Prefix with `Workbench` namespace?', true)
->expectsConfirmation('Prefix with `Workbench` namespace?', 'yes')
->expectsChoice("Export '.env' file as?", $answer, [
'Skip exporting .env',
'.env',
Expand All @@ -50,7 +50,7 @@ public function it_can_run_devtool_command_with_basic_installation(?string $answ
public function it_can_run_devtool_command_without_installation()
{
$this->artisan('workbench:devtool', ['--no-install' => true])
->expectsConfirmation('Prefix with `Workbench` namespace?', true)
->expectsConfirmation('Prefix with `Workbench` namespace?', 'yes')
->assertSuccessful();

$this->assertCommandExecutedWithDevTool();
Expand All @@ -61,7 +61,7 @@ public function it_can_run_devtool_command_without_installation()
public function it_can_be_installed_with_no_interaction_options()
{
$this->artisan('workbench:devtool', ['--no-install' => true, '--no-interaction' => true])
->expectsConfirmation('Prefix with `Workbench` namespace?', true)
->expectsConfirmation('Prefix with `Workbench` namespace?', 'yes')
->assertSuccessful();

$this->assertCommandExecutedWithDevTool();
Expand Down
2 changes: 1 addition & 1 deletion tests/Console/InstallCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class InstallCommandTest extends CommandTestCase
public function it_can_run_installation_command_with_devtool(?string $answer, bool $createEnvironmentFile)
{
$this->artisan('workbench:install', ['--devtool' => true])
->expectsConfirmation('Prefix with `Workbench` namespace?', true)
->expectsConfirmation('Prefix with `Workbench` namespace?', 'yes')
->expectsChoice("Export '.env' file as?", $answer, [
'Skip exporting .env',
'.env',
Expand Down

0 comments on commit 9ca758c

Please sign in to comment.