3
votes

I currently have PhpStorm running Dusk Test successfully however, I would like it to use the testing database I have set up. Per other threads and resources online, I have created the .env.dusk.local and phpunit.dusk.xml that points to the testing database I have created. When I run the dusk tests in PhpStorm the application that is rendered in Chromium doesn't use the testing database that is described in these files but when I run them using php artisan dusk in the terminal it uses the correct databases.

It seems like I need make phpstorm aware of what env file to use when running the tests. Any clues on how to make this work.

5
there are no similar feature requests out there (for phpstorm). Looks like this won't work out of the box. I would suggest to submit this as a feature request at youtrack.jetbrains.com/issues/WI#newissue=yes if you're interested in implementing this in future.Dmitrii

5 Answers

1
votes

If you're running the tests using artisan dusk, make sure that the APP_ENV setting you are running Dusk in matches the .env.dusk.[environment] setting.

The Dusk browser instance always use the current .env file so...

From the Laravel Dusk docs:

When running tests, Dusk will back-up your .env file and rename your Dusk environment to .env. Once the tests have completed, your .env file will be restored.

If you're not running the artisan dusk command to run your Dusk tests, I suspect that you would have to do something similar to this code before and after running the test suite: https://github.com/laravel/dusk/blob/2.0/src/Console/DuskCommand.php#L136

If you get this working I'd be very interested in how you did it.

1
votes

I found this article that works well and describes what the issue is.

https://harings.be/running-laravel-dusk-tests-from-phpstorm-atf2v

tests/DuskTestCase.php

tests/DuskTestCase.php

<?php

namespace Tests;

use Dotenv\Dotenv;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Laravel\Dusk\TestCase as BaseTestCase;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;

abstract class DuskTestCase extends BaseTestCase
{
      use CreatesApplication;
    use DatabaseMigrations;

        public static function basePath($path = '')
    {
        return __DIR__ . '/../' . ($path ? DIRECTORY_SEPARATOR . $path : $path);
    }

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

    public static function setUpBeforeClass()
    {
        copy(self::basePath('.env'), self::basePath('.env.backup'));
        copy(self::basePath('.env.dusk.local'), self::basePath('.env'));
        (new Dotenv(self::basePath()))->overload();

        parent::setUpBeforeClass();
    }

    public static function tearDownAfterClass(): void
    {
        copy(self::basePath('.env.backup'), self::basePath('.env'));
        unlink(self::basePath('.env.backup'));
        (new Dotenv(self::basePath()))->overload();

        parent::tearDownAfterClass();
    }

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

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

You need to add to DuskTestCase.php something like this:

 /**
 * @beforeClass
 * @return void
 */
public static function prepare()
{
    //static::startChromeDriver();
    copy(base_path('.env'), base_path('.env.backup'));
    copy(base_path('.env.dusk.local'), base_path('.env'));
    (new Dotenv(base_path()))->overload();
}

 /**
 * @afterClass
 * @return void
 */
public static function finish()
{
    copy(base_path('.env.backup'), base_path('.env'));
    unlink(base_path('.env.backup'));
    (new Dotenv(base_path()))->overload();
}
0
votes

Thx Andriy, I improved your code, this works for me :

use Dotenv\Dotenv;

public static function basePath($path = '') {
  return __DIR__. '/../' . ($path ? DIRECTORY_SEPARATOR.$path : $path);
}

/**
 * Prepare for Dusk test execution.
 *
 * @beforeClass
 * @return void
 */
public static function prepare()
{
  copy(DuskTestCase::basePath('.env'), DuskTestCase::basePath('.env.backup'));
  copy(DuskTestCase::basePath('.env.dusk.local'), DuskTestCase::basePath('.env'));
  (new Dotenv(DuskTestCase::basePath()))->overload();

  static::startChromeDriver();
}

public static function closeAll()
{
  copy(DuskTestCase::basePath('.env.backup'), DuskTestCase::basePath('.env'));
  unlink(DuskTestCase::basePath('.env.backup'));
  (new Dotenv(DuskTestCase::basePath()))->overload();

  return parent::closeAll();
}

..since base_path() and finish() weren't working in this DuskTestCase class

0
votes

This worked for me

<?php

namespace Tests;

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

abstract class DuskTestCase extends BaseTestCase
{
    use CreatesApplication;

    public static function basePath($path = '') {
        return __DIR__. '/../' . ($path ? DIRECTORY_SEPARATOR.$path : $path);
    }

    // [ ... ]

    public static function setUpBeforeClass(): void
    {
        if (file_get_contents(self::basePath('.env')) !== file_get_contents(self::basePath('.env.dusk.local'))) {
            copy(self::basePath('.env'), self::basePath('.env.backup'));
        }
        copy(self::basePath('.env.dusk.local'), self::basePath('.env'));
        Dotenv::createMutable(self::basePath())->load();

        parent::setUpBeforeClass();
    }

    public static function tearDownAfterClass(): void
    {
        copy(self::basePath('.env.backup'), self::basePath('.env'));
        unlink(self::basePath('.env.backup'));
        Dotenv::createMutable(self::basePath())->load();

        parent::tearDownAfterClass();
    }

    // [ ... ] 
}

Found it at https://github.com/laravel/dusk/issues/883