Last active
February 22, 2023 16:15
-
-
Save wrabit/e01df16858505c395b7b0d271112a023 to your computer and use it in GitHub Desktop.
Override Laravel config during Dusk tests
This file contains hidden or 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 | |
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); | |
} | |
} |
This file contains hidden or 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
// override newBrowser to return an instance of your Browser. | |
protected function newBrowser($driver) | |
{ | |
return new Browser($driver); | |
} |
This file contains hidden or 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 | |
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 | |
// .. | |
], | |
// .. | |
} |
This file contains hidden or 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 | |
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); | |
} | |
} |
This file contains hidden or 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 | |
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') | |
// ... | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
public function withConfig($config = []) { $this->modifiedConfig = $config; }
should return $this;
`public function withConfig($config = [])
{
$this->modifiedConfig = $config;
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); } }