From aac56588fb3fa4f5abc07234bf1f3ccf67af991a Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Wed, 14 Aug 2024 21:41:21 +0800 Subject: [PATCH 1/2] wip Signed-off-by: Mior Muhammad Zaki --- bin/sync | 2 + composer.json | 1 + src/CanvasServiceProvider.php | 1 + src/Console/DuskMakeCommand.php | 149 ++++++++++++++++++++++++++++++++ 4 files changed, 153 insertions(+) create mode 100644 src/Console/DuskMakeCommand.php diff --git a/bin/sync b/bin/sync index 0e9ac86..6fb9a51 100755 --- a/bin/sync +++ b/bin/sync @@ -12,6 +12,8 @@ $version = ($input->hasParameterOption('--dev') && $input->hasParameterOption('- Illuminate\Support\Collection::make([ 'factory.stub' => 'Database/Console/Factories/stubs/factory.stub', + 'dusk.pest.stub' => 'Foundation/Console/stubs/pest.stub', + 'dusk.test.stub' => 'Foundation/Console/stubs/dusk.stub', 'pest.stub' => 'Foundation/Console/stubs/pest.stub', 'pest.unit.stub' => 'Foundation/Console/stubs/pest.unit.stub', 'test.stub' => 'Foundation/Console/stubs/test.stub', diff --git a/composer.json b/composer.json index f384c1e..878452c 100644 --- a/composer.json +++ b/composer.json @@ -41,6 +41,7 @@ "symfony/yaml": "^7.0" }, "require-dev": { + "laravel/dusk": "^8.0", "laravel/framework": "^11.21", "laravel/pint": "^1.17", "mockery/mockery": "^1.6", diff --git a/src/CanvasServiceProvider.php b/src/CanvasServiceProvider.php index 1b9418c..7345e2e 100644 --- a/src/CanvasServiceProvider.php +++ b/src/CanvasServiceProvider.php @@ -37,6 +37,7 @@ public function register(): void Arr::set($config, 'testing.extends', [ 'unit' => 'PHPUnit\Framework\TestCase', 'feature' => 'Tests\TestCase', + 'browser' => 'Tests\DuskTestCase', ]); $config['namespace'] = rescue(fn () => rtrim($this->app->getNamespace(), '\\'), null, false); diff --git a/src/Console/DuskMakeCommand.php b/src/Console/DuskMakeCommand.php new file mode 100644 index 0000000..75453f5 --- /dev/null +++ b/src/Console/DuskMakeCommand.php @@ -0,0 +1,149 @@ +addGeneratorPresetOptions(); + } + + /** + * Execute the console command. + * + * @return bool|null + * + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException + */ + #[\Override] + public function handle() + { + /** @phpstan-ignore return.type */ + return $this->generateCode() ? self::SUCCESS : self::FAILURE; + } + + /** + * Replace the class name for the given stub. + * + * @param string $stub + * @param string $name + * @return string + */ + protected function generatingCode($stub, $name) + { + $preset = $this->generatorPreset(); + + if (! $preset instanceof GeneratorPreset) { + return $stub; + } + + $testCase = $preset->canvas()->config( + 'testing.extends.browser', + $preset->canvas()->is('laravel') ? 'Tests\DuskTestCase' : 'Orchestra\Testbench\Dusk\TestCase' + ); + + return $this->replaceTestCase($stub, $testCase); + } + + /** + * Replace the model for the given stub. + */ + protected function replaceTestCase(string $stub, string $testCase): string + { + $namespaceTestCase = $testCase = str_replace('/', '\\', $testCase); + + if (Str::startsWith($testCase, '\\')) { + $stub = str_replace('DummyNamespace', trim($testCase, '\\'), $stub); + } else { + $stub = str_replace('DummyNamespace', $namespaceTestCase, $stub); + } + + $stub = str_replace( + "use {$namespaceTestCase};\nuse {$namespaceTestCase};", "use {$namespaceTestCase};", $stub + ); + + $testCase = class_basename(trim($testCase, '\\')); + + return str_replace('DummyClass', $testCase, $stub); + } + + /** + * Resolve the fully-qualified path to the stub. + * + * @param string $stub + * @return string + */ + protected function resolveStubPath($stub) + { + $preset = $this->generatorPreset(); + + if (! $preset instanceof GeneratorPreset) { + return parent::resolveStubPath($stub); + } + + return $preset->hasCustomStubPath() && file_exists($customPath = join_paths($preset->basePath(), $stub)) + ? $customPath + : $this->resolveDefaultStubPath($stub); + } + + /** + * Resolve the default fully-qualified path to the stub. + * + * @param string $stub + * @return string + */ + protected function resolveDefaultStubPath($stub) + { + return join_paths(__DIR__, $stub); + } + + /** + * Get the generator preset source path. + */ + protected function getGeneratorSourcePath(): string + { + return $this->generatorPreset()->testingPath(); + } + + /** + * Get the destination class path. + * + * @param string $name + * @return string + */ + protected function getPath($name) + { + return $this->getPathUsingCanvas($name); + } + + /** + * Get the root namespace for the class. + * + * @return string + */ + protected function rootNamespace() + { + return $this->generatorPreset()->testingNamespace().'Browser\\'; + } +} From 9cc8e06649eb43ca9222d4a4a4372ad676bd158c Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Tue, 19 Nov 2024 12:03:24 +0800 Subject: [PATCH 2/2] Update composer.json --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index 2af2b67..dc17623 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,6 @@ }, "require-dev": { "laravel/dusk": "^8.0", - "laravel/framework": "^11.21", "laravel/framework": "^11.31", "laravel/pint": "^1.17", "mockery/mockery": "^1.6.10",