Skip to content

Instantly share code, notes, and snippets.

@wrabit
Last active February 22, 2023 16:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wrabit/e01df16858505c395b7b0d271112a023 to your computer and use it in GitHub Desktop.
Save wrabit/e01df16858505c395b7b0d271112a023 to your computer and use it in GitHub Desktop.
Override Laravel config during Dusk tests
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class DuskConfigOverride
{
/**
* Set config, serverside on dusk tests
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
//Only handle the DuskRuntimeConfig when debug=TRUE and environment=testing
if (!config('app.debug') || !app()->environment() == 'testing') {
return $next($request);
}
if ($request->has('duskconfig') || $request->session()->has('duskconfig')) {
$configJson = $request->get('duskconfig') ?? $request->session()->get('duskconfig');
$configValues = json_decode($configJson, true);
if (sizeof($configValues) > 0) {
foreach ($configValues as $key => $value) {
config([$key => $value]);
}
}
\Session::put('duskconfig', $configJson);
\Session::save();
}
return $next($request);
}
}
// override newBrowser to return an instance of your Browser.
protected function newBrowser($driver)
{
return new Browser($driver);
}
<?php
namespace App\Http;
use App\Http\Middleware\DuskConfigOverride;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
use Illuminate\Routing\Router;
class Kernel extends HttpKernel
{
// ..
protected $middlewareGroups = [
'web' => [
// ..
\Illuminate\Session\Middleware\StartSession::class,
DuskConfigOverride::class, // after StartSession
// ..
],
// ..
}
<?php
namespace Tests\Browser;
class Browser extends \Laravel\Dusk\Browser
{
protected $modifiedConfig = [];
public function withConfig($config = [])
{
$this->modifiedConfig = $config;
}
public function visit($url)
{
if (!empty($this->modifiedConfig))
$url .= (parse_url($url, PHP_URL_QUERY) ? '&' : '?') . 'duskconfig=' . json_encode($this->modifiedConfig);
return parent::visit($url);
}
}
<?php
namespace Tests\Browser;
use App\Components\Currency\Currencies\GBP;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\DuskTestCase;
class SomeDuskTest extends DuskTestCase
public function test_something()
{
$this->browse(function(Browser $browser) {
$browser->withConfig(['currency.default' => 'GBP'])
->visit('/login')
->waitFor('@login.email')
// ...
});
}
@mcisback
Copy link

public function withConfig($config = []) { $this->modifiedConfig = $config; }

should return $this;

`public function withConfig($config = [])
{
$this->modifiedConfig = $config;

    return $this;
}`

Also is better to register the middleware using AppServiceProvider boot method, like this:
public function boot(Kernel $kernel) { if (app()->environment() == 'testing' || app()->environment() == 'local') { $kernel->appendMiddlewareToGroup('web', DuskConfigOverrideMiddleware::class); } }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment