From 187f3bf1ffa8cc043ff4da431ed77242fa1a3e8c Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Sun, 8 Dec 2024 12:58:10 +0800 Subject: [PATCH] [7.x] `workbench:install` should suggest creating `.env.dusk` equivalent file when `orchestra/testbench-dusk` is installed (#57) * `workbench:install` should suggest creating `.env.dusk` equivalent file when `orchestra/testbench-dusk` is installed solved #53 Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki --------- Signed-off-by: Mior Muhammad Zaki --- .github/workflows/analyse.yaml | 2 + .github/workflows/audits.yaml | 2 + .github/workflows/coveralls.yaml | 2 + .github/workflows/tests.yaml | 2 + src/Actions/DumpComposerAutoloads.php | 23 +++++++ .../ModifyComposer.php} | 15 +++-- src/Console/DevToolCommand.php | 13 ++-- src/Console/InstallCommand.php | 48 ++++++++++---- src/WorkbenchServiceProvider.php | 3 + tests/Actions/DumpComposerAutoloadsTest.php | 33 ++++++++++ tests/Actions/ModifyComposerTest.php | 66 +++++++++++++++++++ tests/Actions/stubs/composer.json | 1 + tests/Actions/tmp/.gitignore | 2 + 13 files changed, 189 insertions(+), 23 deletions(-) create mode 100644 src/Actions/DumpComposerAutoloads.php rename src/{Composer.php => Actions/ModifyComposer.php} (72%) create mode 100644 tests/Actions/DumpComposerAutoloadsTest.php create mode 100644 tests/Actions/ModifyComposerTest.php create mode 100644 tests/Actions/stubs/composer.json create mode 100644 tests/Actions/tmp/.gitignore diff --git a/.github/workflows/analyse.yaml b/.github/workflows/analyse.yaml index 3105082..1f557f2 100644 --- a/.github/workflows/analyse.yaml +++ b/.github/workflows/analyse.yaml @@ -2,6 +2,8 @@ name: analyse on: push: + branches: + - '*.x' pull_request: workflow_dispatch: diff --git a/.github/workflows/audits.yaml b/.github/workflows/audits.yaml index bb81362..cbf8e27 100644 --- a/.github/workflows/audits.yaml +++ b/.github/workflows/audits.yaml @@ -2,6 +2,8 @@ name: audits on: push: + branches: + - '*.x' pull_request: workflow_dispatch: diff --git a/.github/workflows/coveralls.yaml b/.github/workflows/coveralls.yaml index 8c87a8f..f59a9a0 100644 --- a/.github/workflows/coveralls.yaml +++ b/.github/workflows/coveralls.yaml @@ -2,6 +2,8 @@ name: coveralls on: push: + branches: + - '*.x' pull_request: jobs: diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 6147454..e80ad86 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -2,6 +2,8 @@ name: tests on: push: + branches: + - '*.x' pull_request: workflow_dispatch: diff --git a/src/Actions/DumpComposerAutoloads.php b/src/Actions/DumpComposerAutoloads.php new file mode 100644 index 0000000..7fcb866 --- /dev/null +++ b/src/Actions/DumpComposerAutoloads.php @@ -0,0 +1,23 @@ +setWorkingPath($this->workingPath) + ->dumpAutoloads(); + } +} diff --git a/src/Composer.php b/src/Actions/ModifyComposer.php similarity index 72% rename from src/Composer.php rename to src/Actions/ModifyComposer.php index 3d01bf5..363f332 100644 --- a/src/Composer.php +++ b/src/Actions/ModifyComposer.php @@ -1,17 +1,24 @@ workingPath}/composer.json"; diff --git a/src/Console/DevToolCommand.php b/src/Console/DevToolCommand.php index 247e324..7199c76 100644 --- a/src/Console/DevToolCommand.php +++ b/src/Console/DevToolCommand.php @@ -9,7 +9,8 @@ use Illuminate\Support\Collection; use Orchestra\Testbench\Foundation\Console\Actions\EnsureDirectoryExists; use Orchestra\Testbench\Foundation\Console\Actions\GeneratesFile; -use Orchestra\Workbench\Composer; +use Orchestra\Workbench\Actions\DumpComposerAutoloads; +use Orchestra\Workbench\Actions\ModifyComposer; use Orchestra\Workbench\Events\InstallEnded; use Orchestra\Workbench\Events\InstallStarted; use Orchestra\Workbench\Workbench; @@ -49,12 +50,10 @@ public function handle(Filesystem $filesystem) ]); } - return tap(Command::SUCCESS, function ($exitCode) use ($filesystem, $workingPath) { + return tap(Command::SUCCESS, function ($exitCode) use ($workingPath) { event(new InstallEnded($this->input, $this->output, $this->components, $exitCode)); - (new Composer($filesystem)) - ->setWorkingPath($workingPath) - ->dumpAutoloads(); + (new DumpComposerAutoloads($workingPath))->handle(); }); } @@ -107,9 +106,9 @@ protected function prepareWorkbenchDirectories(Filesystem $filesystem, string $w */ protected function prepareWorkbenchNamespaces(Filesystem $filesystem, string $workingPath): void { - $composer = (new Composer($filesystem))->setWorkingPath($workingPath); + $action = new ModifyComposer($workingPath); - $composer->modify(function (array $content) use ($filesystem) { + $action->handle(function (array $content) use ($filesystem) { return $this->appendScriptsToComposer( $this->appendAutoloadDevToComposer($content, $filesystem), $filesystem ); diff --git a/src/Console/InstallCommand.php b/src/Console/InstallCommand.php index 05e68a3..289d053 100644 --- a/src/Console/InstallCommand.php +++ b/src/Console/InstallCommand.php @@ -9,7 +9,9 @@ use Orchestra\Testbench\Foundation\Console\Actions\GeneratesFile; use Orchestra\Workbench\Workbench; use Symfony\Component\Console\Attribute\AsCommand; +use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; use function Orchestra\Testbench\join_paths; use function Orchestra\Testbench\package_path; @@ -27,6 +29,20 @@ class InstallCommand extends Command */ public static ?string $configurationBaseFile = null; + /** + * Determine if Package also uses Testbench Dusk. + */ + protected ?bool $hasTestbenchDusk = null; + + /** {@inheritDoc} */ + #[\Override] + protected function initialize(InputInterface $input, OutputInterface $output) + { + $this->hasTestbenchDusk = InstalledVersions::isInstalled('orchestra/testbench-dusk'); + + parent::initialize($input, $output); + } + /** * Execute the console command. * @@ -54,9 +70,7 @@ public function handle(Filesystem $filesystem) $this->copyTestbenchConfigurationFile($filesystem, $workingPath); $this->copyTestbenchDotEnvFile($filesystem, $workingPath); - $hasTestbenchDusk = InstalledVersions::isInstalled('orchestra/testbench-dusk'); - - if ($hasTestbenchDusk) { + if ($this->hasTestbenchDusk) { $this->replaceInFile($filesystem, ["laravel: '@testbench'"], ["laravel: '@testbench-dusk'"], join_paths($workingPath, 'testbench.yaml')); } @@ -116,13 +130,27 @@ protected function copyTestbenchDotEnvFile(Filesystem $filesystem, string $worki return; } - $to = join_paths($workbenchWorkingPath, $choice); + if ($this->hasTestbenchDusk === true) { + if ($this->components->confirm('Create separate environment file for Testbench Dusk?', false)) { + (new GeneratesFile( + filesystem: $filesystem, + components: $this->components, + force: (bool) $this->option('force'), + ))->handle( + $from, + join_paths($workbenchWorkingPath, str_replace('.env', '.env.dusk', $choice)) + ); + } + } (new GeneratesFile( filesystem: $filesystem, components: $this->components, force: (bool) $this->option('force'), - ))->handle($from, $to); + ))->handle( + $from, + join_paths($workbenchWorkingPath, $choice) + ); (new GeneratesFile( filesystem: $filesystem, @@ -140,14 +168,10 @@ protected function copyTestbenchDotEnvFile(Filesystem $filesystem, string $worki */ protected function environmentFiles(): array { - $environmentFile = \defined('TESTBENCH_DUSK') && TESTBENCH_DUSK === true - ? '.env.dusk' - : '.env'; - return [ - $environmentFile, - "{$environmentFile}.example", - "{$environmentFile}.dist", + '.env', + '.env.example', + '.env.dist', ]; } diff --git a/src/WorkbenchServiceProvider.php b/src/WorkbenchServiceProvider.php index 5d06acc..688bac3 100644 --- a/src/WorkbenchServiceProvider.php +++ b/src/WorkbenchServiceProvider.php @@ -6,7 +6,9 @@ use Illuminate\Contracts\Events\Dispatcher as EventDispatcher; use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\Http\Kernel as HttpKernel; +use Illuminate\Filesystem\Filesystem; use Illuminate\Foundation\Console\AboutCommand; +use Illuminate\Support\Composer; use Illuminate\Support\ServiceProvider; use Orchestra\Canvas\Core\PresetManager; use Orchestra\Testbench\Foundation\Events\ServeCommandEnded; @@ -21,6 +23,7 @@ class WorkbenchServiceProvider extends ServiceProvider */ public function register(): void { + $this->app->bind('workbench.composer', static fn () => new Composer(new Filesystem)); $this->app->singleton(Contracts\RecipeManager::class, static fn (Application $app) => new RecipeManager($app)); $this->callAfterResolving(PresetManager::class, static function ($manager) { diff --git a/tests/Actions/DumpComposerAutoloadsTest.php b/tests/Actions/DumpComposerAutoloadsTest.php new file mode 100644 index 0000000..22de2e8 --- /dev/null +++ b/tests/Actions/DumpComposerAutoloadsTest.php @@ -0,0 +1,33 @@ +instance('workbench.composer', $composer = m::mock(Composer::class, ['files' => $filesystem])); + + $composer->shouldReceive('setWorkingPath')->once()->with($workingPath)->andReturnSelf(); + $composer->shouldReceive('dumpAutoloads')->once()->andReturnNull(); + + $action = new DumpComposerAutoloads($workingPath); + + $action->handle(); + } +} diff --git a/tests/Actions/ModifyComposerTest.php b/tests/Actions/ModifyComposerTest.php new file mode 100644 index 0000000..1d9f505 --- /dev/null +++ b/tests/Actions/ModifyComposerTest.php @@ -0,0 +1,66 @@ +afterApplicationCreated(function () { + copy(join_paths(__DIR__, 'stubs', 'composer.json'), join_paths(__DIR__, 'tmp', 'composer.json')); + }); + + $this->beforeApplicationDestroyed(function () { + @unlink(join_paths(__DIR__, 'tmp', 'composer.json')); + }); + + parent::setUp(); + } + + /** @test */ + public function it_can_modify_composer_file() + { + $workingPath = join_paths(__DIR__, 'tmp'); + + $action = new ModifyComposer($workingPath); + + $this->assertTrue(is_file(join_paths($workingPath, 'composer.json'))); + $this->assertSame('{}'.PHP_EOL, file_get_contents(join_paths($workingPath, 'composer.json'))); + + $action->handle(function (array $content) { + $content['$schema'] = 'https://getcomposer.org/schema.json'; + + return $content; + }); + + $this->assertTrue(is_file(join_paths($workingPath, 'composer.json'))); + $this->assertSame('{ + "$schema": "https://getcomposer.org/schema.json" +}', file_get_contents(join_paths($workingPath, 'composer.json'))); + } + + /** @test */ + public function it_throws_exception_when_composer_file_does_not_exists() + { + $workingPath = __DIR__; + + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage(\sprintf('Unable to locate `composer.json` file at [%s]', $workingPath)); + + $action = new ModifyComposer($workingPath); + $action->handle(static fn (array $content) => $content); + } +} diff --git a/tests/Actions/stubs/composer.json b/tests/Actions/stubs/composer.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/tests/Actions/stubs/composer.json @@ -0,0 +1 @@ +{} diff --git a/tests/Actions/tmp/.gitignore b/tests/Actions/tmp/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/tests/Actions/tmp/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore