Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Laravel Dusk Integration #41

Draft
wants to merge 4 commits into
base: 9.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bin/sync
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"symfony/yaml": "^7.0.3"
},
"require-dev": {
"laravel/dusk": "^8.0",
"laravel/framework": "^11.42",
"laravel/pint": "^1.17",
"mockery/mockery": "^1.6.10",
Expand Down
1 change: 1 addition & 0 deletions src/CanvasServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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($app->getNamespace(), '\\'), null, false);
Expand Down
149 changes: 149 additions & 0 deletions src/Console/DuskMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

namespace Orchestra\Canvas\Console;

use Laravel\Dusk\Console\MakeCommand;
use Orchestra\Canvas\Core\Concerns\CodeGenerator;
use Orchestra\Canvas\Core\Concerns\UsesGeneratorOverrides;
use Orchestra\Canvas\GeneratorPreset;

/**
* @see https://github.com/laravel/framework/blob/11.x/src/Illuminate/Routing/Console/ControllerMakeCommand.php
*/
#[AsCommand(name: 'make:dusk', description: 'Create a new Dusk test class')]

Check failure on line 13 in src/Console/DuskMakeCommand.php

View workflow job for this annotation

GitHub Actions / PHP:8.3 Code Analysis

Attribute class Orchestra\Canvas\Console\AsCommand does not exist.

Check failure on line 13 in src/Console/DuskMakeCommand.php

View workflow job for this annotation

GitHub Actions / PHP:8.3 Code Analysis

Attribute class Orchestra\Canvas\Console\AsCommand does not exist.
class DuskMakeCommand extends MakeCommand
{
use CodeGenerator;
use UsesGeneratorOverrides;

/**
* Configures the current command.
*
* @return void
*/
#[\Override]
protected function configure()
{
parent::configure();

$this->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, '\\')) {

Check failure on line 76 in src/Console/DuskMakeCommand.php

View workflow job for this annotation

GitHub Actions / PHP:8.3 Code Analysis

Call to static method startsWith() on an unknown class Orchestra\Canvas\Console\Str.

Check failure on line 76 in src/Console/DuskMakeCommand.php

View workflow job for this annotation

GitHub Actions / PHP:8.3 Code Analysis

Call to static method startsWith() on an unknown class Orchestra\Canvas\Console\Str.
$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);

Check failure on line 102 in src/Console/DuskMakeCommand.php

View workflow job for this annotation

GitHub Actions / PHP:8.3 Code Analysis

Call to an undefined static method Laravel\Dusk\Console\MakeCommand::resolveStubPath().

Check failure on line 102 in src/Console/DuskMakeCommand.php

View workflow job for this annotation

GitHub Actions / PHP:8.3 Code Analysis

Call to an undefined static method Laravel\Dusk\Console\MakeCommand::resolveStubPath().
}

return $preset->hasCustomStubPath() && file_exists($customPath = join_paths($preset->basePath(), $stub))

Check failure on line 105 in src/Console/DuskMakeCommand.php

View workflow job for this annotation

GitHub Actions / PHP:8.3 Code Analysis

Function join_paths not found.

Check failure on line 105 in src/Console/DuskMakeCommand.php

View workflow job for this annotation

GitHub Actions / PHP:8.3 Code Analysis

Function join_paths not found.
? $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);

Check failure on line 118 in src/Console/DuskMakeCommand.php

View workflow job for this annotation

GitHub Actions / PHP:8.3 Code Analysis

Function join_paths not found.

Check failure on line 118 in src/Console/DuskMakeCommand.php

View workflow job for this annotation

GitHub Actions / PHP:8.3 Code Analysis

Function join_paths not found.
}

/**
* 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\\';
}
}
Loading