jeudi 23 août 2018

Enable or disable env on the fly when using Laravel Dusk

CONTEXT

I am using Laravel Dusk for testing if my web page properly display a warning message in case accessing a forbidden route.

I have the following arborescence for my tests:

app
...
tests/
  Browser/
    ...
    AuthorizationTest.php
  ...
  DuskTestCase.php

DuskTestCase.php

<?php

namespace Tests;

use Laravel\Dusk\TestCase as BaseTestCase;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Chrome\ChromeOptions;

abstract class DuskTestCase extends BaseTestCase
{
    use CreatesApplication;

    /**
     * Prepare for Dusk test execution.
     *
     * @beforeClass
     * @return void
     */
    public static function prepare()
    {
        static::startChromeDriver();
    }

    /**
     * Create the RemoteWebDriver instance.
     *
     * @return \Facebook\WebDriver\Remote\RemoteWebDriver
     */
    protected function driver()
    {
        $options = (new ChromeOptions)->addArguments([
            '--headless'
        ]);

        return RemoteWebDriver::create(
            'http://localhost:9515', 
            DesiredCapabilities::chrome()->setCapability(
                ChromeOptions::CAPABILITY,
                $options
            ), 
            5000, 
            10000 
        );
    }

    /**
     * Temporal solution for cleaning up session
     */
    protected function setUp()
    {
        parent::setUp();

        foreach (static::$browsers as $browser) {
            $browser->driver->manage()->deleteAllCookies();
        }
    }
}

AuthorizationTest.php

<?php

namespace Tests\Browser;

use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Config;

class AuthorizationTest extends DuskTestCase
{
    public function testNonAdminUserCannotAccessUserList()
    {
        Config::set('app.debug', false); // This does not make any effect

        $this->browse(function(Browser $browser) {
            $browser->visit('http://localhost:8000/loggin')
                ->type('#login', 'simple-user')
                ->type('#password', 'simple-password')
                ->press('loggin')
                ->waitForLocation('/')
                ->visit('http://localhost:8000/user')
                ->waitForLocation('/user')
                ->assertSee('Unsuficient rights to access this resource.');
        });
    }
}

PROBLEM

As the line Config::set('app.debug', true); does not take any effect, and as I use dd($exception); in my App/Exceptions/Handler.php, I have the following page popping up after running the test:

enter image description here

Which is nice because I know my authorization works, but I would like to simulate my production environnement with the APP_DEBUG=true configuration on the fly when running this test.

QUESTION

How can I safely change my environnement on the fly, in order to get it back in its original state after the test ran?

Aucun commentaire:

Enregistrer un commentaire