Skip to content

Commit fde2ae6

Browse files
authored
[8.x] Allow to dynamically add Tests\Browsers\Page\Page or Laravel\Dusk\Page via dusk:page command (#1148)
Improvements based on #1145 Signed-off-by: Mior Muhammad Zaki <[email protected]>
1 parent 3cebce5 commit fde2ae6

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/Console/PageCommand.php

+35
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,41 @@ class PageCommand extends GeneratorCommand
3030
*/
3131
protected $type = 'Page';
3232

33+
/**
34+
* Build the class with the given name.
35+
*
36+
* @param string $name
37+
* @return string
38+
*/
39+
protected function buildClass($name)
40+
{
41+
$result = parent::buildClass($name);
42+
43+
$pageName = $this->argument('name');
44+
45+
$baseClass = 'Tests\Browser\Pages\Page';
46+
47+
if (! Str::contains($pageName, '/') && class_exists($baseClass)) {
48+
return $result;
49+
} elseif (! class_exists($baseClass)) {
50+
$baseClass = 'Laravel\Dusk\Page';
51+
}
52+
53+
$lineEndingCount = [
54+
"\r\n" => substr_count($result, "\r\n"),
55+
"\r" => substr_count($result, "\r"),
56+
"\n" => substr_count($result, "\n"),
57+
];
58+
59+
$eol = array_keys($lineEndingCount, max($lineEndingCount))[0];
60+
61+
return str_replace(
62+
'use Laravel\Dusk\Browser;'.$eol,
63+
'use Laravel\Dusk\Browser;'.$eol."use {$baseClass};".$eol,
64+
$result
65+
);
66+
}
67+
3368
/**
3469
* Get the stub file for the generator.
3570
*

0 commit comments

Comments
 (0)