Skip to content

Instantly share code, notes, and snippets.

@cerw
Created August 14, 2019 08:49
Show Gist options
  • Save cerw/7530d1fe5ff36315dd4323600c659f1c to your computer and use it in GitHub Desktop.
Save cerw/7530d1fe5ff36315dd4323600c659f1c to your computer and use it in GitHub Desktop.
DuskTestCase.php
<?php
namespace Tests;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Symfony\Component\Process\Process;
use Nano\Docs\NanoDocsTestCase;
abstract class DuskTestCase extends NanoDocsTestCase
{
use CreatesApplication;
/**
* Prepare for Dusk test execution.
*
* @beforeClass
* @return void
*/
public static function prepare()
{
if (PHP_OS == 'Darwin') {
// we are on mac
static::startChromeDriver();
} else {
// linux
// we have one running in Docker
}
}
/**
* Create the RemoteWebDriver instance.
*
* @return \Facebook\WebDriver\Remote\RemoteWebDriver
*/
protected function driver()
{
$resolution = env('RESOLUTION', '2560,1600');
if (env('HEADLESS') === "0") {
$options = (new ChromeOptions)->addArguments([
'--window-size='.$resolution
]);
} else {
$options = (new ChromeOptions)->addArguments([
'--disable-gpu',
'--headless',
'--window-size='.$resolution
]);
}
return RemoteWebDriver::create(
'http://localhost:9515',
DesiredCapabilities::chrome()->setCapability(
ChromeOptions::CAPABILITY,
$options
)
);
}
public static function runServer()
{
$env = 'testing';
$webserver = new Process(array(PHP_BINARY, 'artisan', 'serve', '--port=8081', '--env=' . $env));
$webserver->disableOutput();
$webserver->start();
// executes after the command finishes
// if (! $webserver->isSuccessful()) {
// echo $webserver->getOutput();
// throw new ProcessFailedException($webserver);
// } else {
// //echo "all good";
// }
if (PHP_OS == 'Darwin') {
sleep(2);
} else {
sleep(4);
}
}
public static function setUpBeforeClass()
{
self::runServer();
}
public static function tearDownAfterClass()
{
$webserverProcess = new Process("ps ax|grep 8081|grep -v grep|awk '{print $1}'|xargs kill");
$webserverProcess->disableOutput();
$webserverProcess->run();
}
/**
* Assert that a given where condition matches a soft deleted record
*
* @param string $table
* @param array $data
* @param string $connection
*
* @return $this
*/
protected function seeIsSoftDeletedInDatabase($table, array $data, $connection = null)
{
$database = $this->app->make('db');
$connection = $connection ?: $database->getDefaultConnection();
$count = $database->connection($connection)
->table($table)
->where($data)
->whereNotNull('deleted_at')
->count();
$this->assertGreaterThan(0, $count, sprintf(
'Found unexpected records in database table [%s] that matched attributes [%s].',
$table,
json_encode($data)
));
return $this;
}
}
@chanaksha
Copy link

chanaksha commented Aug 14, 2019

        $webserver = new Process(array(PHP_BINARY, 'artisan', 'serve', '--port=8081', '--env=' . $env));
        $webserver->disableOutput();
        $webserver->start();

You are still running this as different forked process. Not too different than keeping the server running in separate console.

@cerw
Copy link
Author

cerw commented Aug 14, 2019

That is true, but at least its pure php and not via nginx. I guess I would have to collect all coverage and combine them>

@chanaksha
Copy link

Yes, I don't see any other good alternative.

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