Last active
March 18, 2024 17:43
-
-
Save ziadoz/5013f1f0d5f429e251db7f2df2fc828f to your computer and use it in GitHub Desktop.
Laravel Dusk Events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Laravel\Dusk\Browser; | |
use Exception; | |
class BrowsingFailed | |
{ | |
/** | |
* @param array $browsers array<\Laravel\Dusk\Browse> | |
*/ | |
public function __construct(public array $browsers, public string $caller, public Exception $exception) | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Laravel\Dusk\Browser; | |
class BrowsingFinished | |
{ | |
/** | |
* @param array $browsers array<\Laravel\Dusk\Browse> | |
*/ | |
public function __construct(public array $browsers, public string $caller) | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Laravel\Dusk\Browser; | |
class BrowsingStarted | |
{ | |
/** | |
* @param array $browsers array<\Laravel\Dusk\Browse> | |
*/ | |
public function __construct(public array $browsers, public string $caller) | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Closure; | |
use Laravel\Dusk\Concerns\ProvidesBrowser; | |
use Laravel\Dusk\Browser; | |
use Throwable; | |
trait ProvidesEvents | |
{ | |
use ProvidesBrowser { | |
ProvidesBrowser::browse as duskBrowse; | |
} | |
/** | |
* Create a new browser instance and fire events. | |
* | |
* @param Closure $callback | |
* @return void | |
*/ | |
public function browse(Closure $callback) | |
{ | |
$this->duskBrowse(function (Browser ...$browsers) use ($callback) { | |
try { | |
event(new BrowsingStarted($browsers, $this->getCaller())); | |
$callback($browsers); | |
} catch (Throwable $exception) { | |
event(new BrowsingFailed($browsers, $this->getCaller(), $exception)); | |
throw $exception; | |
} finally { | |
event(new BrowsingFinished($browsers, $this->getCaller())); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment