Skip to content
Open
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
56 changes: 54 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
{
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions config/laravel-console-dusk.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion src/ConsoleBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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, ' ')));
Expand Down
2 changes: 1 addition & 1 deletion src/ConsoleBrowserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/ConsoleBrowserFactoryContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

interface ConsoleBrowserFactoryContract
{
public function make(Command $command, DriverContract $driver): ConsoleBrowserContract;
public function make(Command|null $command, DriverContract $driver): ConsoleBrowserContract;
}
2 changes: 1 addition & 1 deletion src/Contracts/ManagerContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

interface ManagerContract
{
public function browse(Command $command, Closure $callback): void;
public function browse(Command|null $command, Closure $callback): void;
}
2 changes: 1 addition & 1 deletion src/LaravelConsoleDuskServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
9 changes: 7 additions & 2 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -46,8 +46,13 @@ public function browse(Command $command, Closure $callback): void
}
}

public function browseWithoutCommand(Closure $callback): void
{
$this->browse(null, $callback);
}

/** @return Collection<int, ConsoleBrowserContract> */
protected function createBrowsers(Command $command, Closure $callback): Collection
protected function createBrowsers(Command|null $command, Closure $callback): Collection
{
$browsers = collect();

Expand Down