File: D:/HostingSpaces/SBogers10/vangogh.komma.pro/app/Komma/Shop/Tests/DuskTestCase.php
<?php
namespace App\Komma\Shop\Tests;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Foundation\Exceptions\Handler;
use Laravel\Dusk\Browser;
use Laravel\Dusk\TestCase as BaseTestCase;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use PHPUnit\Framework\Exception;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Output\ConsoleOutput;
/**
* Class DuskTestCase
*
* Note: There is some kind of bug in phpunit / laravel that causes this error:
* Uncaught Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable
* See: https://github.com/laravel/framework/issues/10808#issuecomment-392444043
* When you get that error it means the test failed at a certain condition, an exception was thrown. But could not be handled.
* Surround the suspected code with try catch to analyse it (see catchExceptions method).
*
* @package App\Komma\Shop\Tests
*/
abstract class DuskTestCase extends BaseTestCase
{
use CreatesApplication;
/**
* Prepare for Dusk test execution.
*
* @beforeClass
* @return void
*/
public static function prepare()
{
static::startChromeDriver();
}
/**
* Make sure all cookies (and thus session) are cleared after each test
*/
public function tearDown(): void
{
foreach (static::$browsers as $browser) {
$browser->driver->manage()->deleteAllCookies();
}
parent::tearDown();
}
/**
* Register the base URL with Dusk.
*
* @preserveGlobalState disabled
* @return void
* @throws \Throwable
*/
protected function setUp(): void
{
parent::setUp();
$this->browse(function (Browser $browser) {
$browser->resize(1920, 1080);
});
$browserDir = __DIR__.DIRECTORY_SEPARATOR.'Browser'.DIRECTORY_SEPARATOR;
Browser::$storeScreenshotsAt = $browserDir.'screenshots'.DIRECTORY_SEPARATOR;
Browser::$storeConsoleLogAt = $browserDir.'console'.DIRECTORY_SEPARATOR;
}
/**
* Create the RemoteWebDriver instance.
*
* @return \Facebook\WebDriver\Remote\RemoteWebDriver
*/
protected function driver()
{
$options = (new ChromeOptions)->addArguments([
'--disable-gpu',
// '--headless'
]);
return RemoteWebDriver::create(
'http://localhost:9515', DesiredCapabilities::chrome()->setCapability(
ChromeOptions::CAPABILITY, $options
));
}
/**
* Manual exception handling for when for example Illuminate\Contracts\Container\BindingResolutionException
* occurs, and you need to find out whats going on.
*
* @param \Closure $testCodeClosure
* @return $this|void
*/
protected function catchExceptions(\Closure $testCodeClosure) {
$formatter = new OutputFormatter(true, self::createAdditionalStyles());
$output = new ConsoleOutput(ConsoleOutput::VERBOSITY_VERY_VERBOSE, null, $formatter);
try {
$testCodeClosure->call($this);
}
catch (Exception $exception)
{
$output->writeln('Catched exception manually: ');
$output->writeln((string) $exception);
}
}
protected static function createAdditionalStyles()
{
return array(
'highlight' => new OutputFormatterStyle('red'),
'warning' => new OutputFormatterStyle('black', 'yellow'),
);
}
}