Skip to content

Instantly share code, notes, and snippets.

@mapsi
Created July 29, 2017 11:41
Show Gist options
  • Save mapsi/602c915c7d1b2417c8de2e0039566113 to your computer and use it in GitHub Desktop.
Save mapsi/602c915c7d1b2417c8de2e0039566113 to your computer and use it in GitHub Desktop.
Laravel Dusk (PHPUNIT) Test for Gitlab-ci with docker chromdriver , headless chrome, vnc, screenshots, pipeline, artifacts
APP_ENV=testing
APP_KEY=base64:DimZ0aVNA8ZWtWxTB4fDxFc6lL1wM2C7evETA4QK3+c=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost:8081
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=secca_testing
DB_USERNAME=root
DB_PASSWORD=secret
GEOIP_SERVICES=ipapi
BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=log
#!/bin/bash
set -xe
composer install
npm install
# Copy over testing configuration.
cp .env.gitlab .env
npm run dev
php artisan key:generate
php artisan config:cache
php artisan migrate
php artisan db:seed
image: cerw/docker-laravel
services:
- mysql:5.7
cache:
untracked: true
key: "secca-laravel"
paths:
- node_modules
- vendor
- /root/.composer
- /root/.npm
variables:
MYSQL_DATABASE: secca_testing
MYSQL_ROOT_PASSWORD: secret
stages:
- build
- tests
- deploy
build:
stage: build
script:
- mkdir -vp `pwd`/vendor
- mkdir -vp `pwd`/node_modules
- bash .gitlab-ci.sh
features:
stage: tests
before_script:
- cp .env.gitlab .env
script:
- php vendor/bin/phpunit --coverage-text --colors=never ./tests/Feature
browser:
stage: tests
before_script:
- cp .env.gitlab .env
script:
- /usr/bin/supervisord
- sleep 3
- php vendor/bin/phpunit --coverage-text --colors=never ./tests/Browser
artifacts:
name: "${CI_JOB_NAME}_${CI_COMMIT_REF_NAME}"
paths:
- tests/Browser/screenshots/
expire_in: 4 weeks
when: always
deploy_staging:
stage: deploy
script:
- curl -X POST https://deploy.nano.rocks/XXX-d "reason=$CI_COMMIT_REF_SLUG"
environment:
name: staging
url: https://secca.nano.rocks
only:
- master
deploy_production:
stage: deploy
script:
- curl -X POST https://deploy.nano.rocks/XXX -d "reason=$CI_COMMIT_REF_SLUG"
environment:
name: production
url: http://app.secca.org.au
only:
- production
<?php
namespace Tests;
use Laravel\Dusk\TestCase as BaseTestCase;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Facades\Artisan;
use App\User;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\ProcessBuilder;
use Msurguy\Honeypot\HoneypotFacade;
use Illuminate\Foundation\Testing\DatabaseTransactions;
abstract class DuskTestCase extends BaseTestCase
{
use CreatesApplication;// DatabaseTransactions;
/**
* Prepare for Dusk test execution.
*
* @beforeClass
* @return void
*/
public static function prepare()
{
static::startChromeDriver();
}
/**
* Default preparation for each test
*/
public function setUp()
{
parent::setUp(); // Don't forget this!
Artisan::call('migrate');
if (User::count() == 0) {
Artisan::call('db:seed', ['--class' => 'TestSeeder']);
}
exec('rm -f ' . storage_path('framework/sessions/*'));
}
/**
* Create the RemoteWebDriver instance.
*
* @return \Facebook\WebDriver\Remote\RemoteWebDriver
*/
protected function driver()
{
$chromeOptions = new ChromeOptions();
$chromeOptions->addArguments(['--window-size=1466,1224']);
return RemoteWebDriver::create(
env('CHROMEDRIVER', 'http://localhost:9515'),
$chromeOptions->toCapabilities()
);
}
public static function runServer()
{
if (PHP_OS == 'Darwin') {
// we are on mac
$env = 'testing';
} else {
// linux
$env = 'phpci';
}
$command = (new ProcessBuilder())
->setTimeout(null)
->setEnv('APP_ENV', $env)
->add('exec')
->add(PHP_BINARY)
->add('artisan')
->add('serve')
->add('--port=8081')
->add('--env=' . $env)
->getProcess()
->getCommandLine();
$webserver = new Process($command . ' &');
$webserver->disableOutput();
$webserver->run();
sleep(1);
}
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();
}
public function loginAsUser()
{
// see https://github.com/laravel/dusk/issues/100
$user = User::first();
$this->browse(function ($browser) use ($user) {
$browser->logout();
$browser->loginAs($user->id);
});
}
}
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="bootstrap/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Browser Test Suite">
<directory suffix="Test.php">./tests/Browser</directory>
</testsuite>
<testsuite name="Feature Tests">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="Unit Tests">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="APP_KEY" value="base64:DimZ0aVNA8ZWtWxTB4fDxFc6lL1wM2C7evETA4QK3+c="/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="file"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="MAIL_DRIVER" value="log"/>
<env name="APP_URL" value="http://localhost:8081"/>
</php>
</phpunit>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment