Skip to content

Commit

Permalink
[8.x] Allow to dynamically add Tests\Browsers\Page\Page or `Laravel…
Browse files Browse the repository at this point in the history
…\Dusk\Page` via `dusk:page` command (#1148)

Improvements based on #1145

Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone authored Oct 22, 2024
1 parent 3cebce5 commit fde2ae6
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/Console/PageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,41 @@ class PageCommand extends GeneratorCommand
*/
protected $type = 'Page';

/**
* Build the class with the given name.
*
* @param string $name
* @return string
*/
protected function buildClass($name)
{
$result = parent::buildClass($name);

$pageName = $this->argument('name');

$baseClass = 'Tests\Browser\Pages\Page';

if (! Str::contains($pageName, '/') && class_exists($baseClass)) {
return $result;
} elseif (! class_exists($baseClass)) {
$baseClass = 'Laravel\Dusk\Page';
}

$lineEndingCount = [
"\r\n" => substr_count($result, "\r\n"),
"\r" => substr_count($result, "\r"),
"\n" => substr_count($result, "\n"),
];

$eol = array_keys($lineEndingCount, max($lineEndingCount))[0];

return str_replace(
'use Laravel\Dusk\Browser;'.$eol,
'use Laravel\Dusk\Browser;'.$eol."use {$baseClass};".$eol,
$result
);
}

/**
* Get the stub file for the generator.
*
Expand Down

0 comments on commit fde2ae6

Please sign in to comment.