diff --git a/README.md b/README.md index 4f580fc..0552e38 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ ## About Laravel Console Dusk -Laravel Console Dusk was created by, and is maintained by [Nuno Maduro](https://github.com/nunomaduro), and allows the usage of [Laravel Dusk](https://github.com/laravel/dusk) in Laravel/Laravel Zero artisan commands. +Laravel Console Dusk was created by, and is maintained by [Nuno Maduro](https://github.com/nunomaduro), and allows the usage of [Laravel Dusk](https://github.com/laravel/dusk) in Laravel/Laravel Zero artisan commands, as well as in queued jobs. ## Installation @@ -25,6 +25,7 @@ composer require nunomaduro/laravel-console-dusk The package provide a config file that allows you to configure some options. ```php return [ + /* |-------------------------------------------------------------------------- | Laravel Console Dusk Paths @@ -34,9 +35,20 @@ return [ */ 'paths' => [ 'screenshots' => storage_path('laravel-console-dusk/screenshots'), - 'log' => storage_path('laravel-console-dusk/log'), + 'log' => storage_path('laravel-console-dusk/log'), + 'source' => storage_path('laravel-console-dusk/source'), ], + /* + | -------------------------------------------------------------------------- + | Always Available Mode + | -------------------------------------------------------------------------- + | + | Make Laravel Console Dusk available even when not running in the context + | of an Artisan command (e.g. from a queue worker). + */ + 'always_boot' => env('LCD_ALWAYS_BOOT', false), + /* | -------------------------------------------------------------------------- | Headless Mode @@ -75,6 +87,10 @@ php artisan vendor:publish --provider="NunoMaduro\LaravelConsoleDusk\LaravelCons ## Usage +Check how use [Laravel Dusk here](https://github.com/laravel/dusk). + +### Usage in an Artisan command + ```php class VisitLaravelZeroCommand extends Command { @@ -93,6 +109,42 @@ class VisitLaravelZeroCommand extends Command } ``` +### Usage in a job + +Ensure that the `laravel-console-dusk.always_register` configuration setting is set to `true` (either +in your config file, or through the `LCD_ALWAYS_REGISTER` environment variable). Then, +in your job (or wherever else you wish to use Laravel-Console-Dusk), you can resolve the +manager and execute as follows: + +```php +namespace App\Jobs; + +use Illuminate\Bus\Queueable; +use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Foundation\Bus\Dispatchable; +use Illuminate\Queue\InteractsWithQueue; +use Illuminate\Queue\SerializesModels; +use NunoMaduro\LaravelConsoleDusk\ConsoleBrowser; +use NunoMaduro\LaravelConsoleDusk\Contracts\ManagerContract; + +class MyDemoJob implements ShouldQueue +{ + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; + + public function __construct() + { + } + + public function handle(): void + { + app(ManagerContract::class)->browseWithoutCommand(function (ConsoleBrowser $browser) { + $browser->visit('http://laravel-zero.com') + ->assertSee('100% Open Source'); + }); + } +} +``` + Check how use [Laravel Dusk here](https://github.com/laravel/dusk). ## Contributing diff --git a/config/laravel-console-dusk.php b/config/laravel-console-dusk.php index 4d5ff24..acb4721 100644 --- a/config/laravel-console-dusk.php +++ b/config/laravel-console-dusk.php @@ -15,6 +15,16 @@ 'source' => storage_path('laravel-console-dusk/source'), ], + /* + | -------------------------------------------------------------------------- + | Always Available Mode + | -------------------------------------------------------------------------- + | + | Make Laravel Console Dusk available even when not running in the context + | of an Artisan command (e.g. from a queue worker). + */ + 'always_boot' => env('LCD_ALWAYS_BOOT', false), + /* | -------------------------------------------------------------------------- | Headless Mode diff --git a/src/ConsoleBrowser.php b/src/ConsoleBrowser.php index dbf8c89..678cfca 100644 --- a/src/ConsoleBrowser.php +++ b/src/ConsoleBrowser.php @@ -17,7 +17,7 @@ class ConsoleBrowser implements ConsoleBrowserContract protected $inSecret = false; - public function __construct(Command $command, Browser $browser) + public function __construct(Command|null $command, Browser $browser) { $this->command = $command; $this->browser = $browser; @@ -39,6 +39,15 @@ public function __call(string $name, array $arguments) { $description = $this->getHumanReadableMethodDescription($name, $arguments); + if ($this->command) { + return $this->executeInConsole($description, $name, $arguments); + } else { + return $this->executeNoConsole($name, $arguments); + } + } + + protected function executeInConsole($description, $name, $arguments) + { $exception = null; $result = null; @@ -59,6 +68,13 @@ public function __call(string $name, array $arguments) return $result instanceof Browser ? $this : $result; } + protected function executeNoConsole($name, $arguments) + { + $result = call_user_func_array([$this->browser, $name], $arguments); + + return $result instanceof Browser ? $this : $result; + } + protected function getHumanReadableMethodDescription(string $methodName, array $arguments): string { $description = Str::ucfirst(Str::lower(Str::snake($methodName, ' '))); diff --git a/src/ConsoleBrowserFactory.php b/src/ConsoleBrowserFactory.php index c3dcc3b..1172922 100644 --- a/src/ConsoleBrowserFactory.php +++ b/src/ConsoleBrowserFactory.php @@ -17,7 +17,7 @@ class ConsoleBrowserFactory implements ConsoleBrowserFactoryContract protected $driver; - public function make(Command $command, DriverContract $driver): ConsoleBrowserContract + public function make(Command|null $command, DriverContract $driver): ConsoleBrowserContract { $this->driver = $driver; diff --git a/src/Contracts/ConsoleBrowserFactoryContract.php b/src/Contracts/ConsoleBrowserFactoryContract.php index b742dc9..9930041 100644 --- a/src/Contracts/ConsoleBrowserFactoryContract.php +++ b/src/Contracts/ConsoleBrowserFactoryContract.php @@ -9,5 +9,5 @@ interface ConsoleBrowserFactoryContract { - public function make(Command $command, DriverContract $driver): ConsoleBrowserContract; + public function make(Command|null $command, DriverContract $driver): ConsoleBrowserContract; } diff --git a/src/Contracts/ManagerContract.php b/src/Contracts/ManagerContract.php index bb54c23..0605ffc 100644 --- a/src/Contracts/ManagerContract.php +++ b/src/Contracts/ManagerContract.php @@ -9,5 +9,5 @@ interface ManagerContract { - public function browse(Command $command, Closure $callback): void; + public function browse(Command|null $command, Closure $callback): void; } diff --git a/src/LaravelConsoleDuskServiceProvider.php b/src/LaravelConsoleDuskServiceProvider.php index c90c671..5253840 100644 --- a/src/LaravelConsoleDuskServiceProvider.php +++ b/src/LaravelConsoleDuskServiceProvider.php @@ -16,7 +16,7 @@ class LaravelConsoleDuskServiceProvider extends ServiceProvider implements Defer { public function boot(): void { - if ($this->app->runningInConsole()) { + if ($this->app->runningInConsole() || config('laravel-console-dusk.always_boot')) { $this->publishes([ __DIR__.'/../config/laravel-console-dusk.php' => config_path('laravel-console-dusk.php'), ], 'config'); diff --git a/src/Manager.php b/src/Manager.php index 1f113fe..5e11ee6 100644 --- a/src/Manager.php +++ b/src/Manager.php @@ -26,7 +26,7 @@ public function __construct(?DriverContract $driver = null, ?ConsoleBrowserFacto $this->browserFactory = $browserFactory ?: new ConsoleBrowserFactory(); } - public function browse(Command $command, Closure $callback): void + public function browse(Command|null $command, Closure $callback): void { $this->driver->open(); @@ -46,8 +46,13 @@ public function browse(Command $command, Closure $callback): void } } + public function browseWithoutCommand(Closure $callback): void + { + $this->browse(null, $callback); + } + /** @return Collection */ - protected function createBrowsers(Command $command, Closure $callback): Collection + protected function createBrowsers(Command|null $command, Closure $callback): Collection { $browsers = collect();