Skip to content

Instantly share code, notes, and snippets.

@nateritter
Last active October 25, 2016 15:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nateritter/6b9bd60bf9a099dd5ab6b8ac94a68663 to your computer and use it in GitHub Desktop.
Save nateritter/6b9bd60bf9a099dd5ab6b8ac94a68663 to your computer and use it in GitHub Desktop.
SemaphoreCI setup for Laravel 5.3 + PHPUnit (unit, integration, API functional) + Codeception (acceptance) tests
/**
* NOTE: Earlier versions of this file used `php artisan serve`.
* However, Codeception and/or PhantomJS is not too fond of
* running that way for some reason. I ended up switching
* to Apache since that's what worked for all our devs
* locally. This script now includes how to get that
* setup properly. Please enjoy this responsibly.
*
* Assumptions:
* Using site5/phantoman to run phantomjs as the headless browser
* Using database/database.sqlite as the testing db.
* Using Facebook\WebDriver
* PhantomJS v2.1.1
* Codeception v2.2
* Laravel v5.3
*
* See http://www.chrisduell.com/blog/development/speeding-up-unit-tests-in-php/ for tips on speeding up tests.
*/
# Setup
sudo chmod +x setup-*tests.sh
sudo bash ./setup-tests.sh
# Job 1 (phpunit)
./setup-phpunit-tests.sh
./vendor/bin/phpunit
# Job 2 (Codecepion acceptance)
sudo bash ./setup-acceptance-tests.sh
./vendor/bin/codecept run acceptance -f -d
/**
* ------------------------------------------------------
* These files below are referenced in the setup scripts.
* BEWARE: Don't use these in a production environment as
* the chmod commands leave you very insecure.
*
* Also, replace the [your-whatever] with your whatever.
* ------------------------------------------------------
*/
#### setup-tests.sh
#!/bin/sh
sudo add-apt-repository -y ppa:ondrej/php
sudo apt-get update -qq
install-package php7.0 php7.0-curl php7.0-sqlite3 php7.0-mbstring php7.0-dom
sudo a2dismod php5
sudo a2enmod php7.0
sudo a2enmod rewrite
sudo a2enmod env
sudo sh -c "echo '<Directory /var/www/>' >> /etc/apache2/sites-enabled/000-default.conf"
sudo sh -c "echo ' Options Indexes FollowSymLinks' >> /etc/apache2/sites-enabled/000-default.conf"
sudo sh -c "echo ' AllowOverride All' >> /etc/apache2/sites-enabled/000-default.conf"
sudo sh -c "echo ' Require all granted' >> /etc/apache2/sites-enabled/000-default.conf"
sudo sh -c "echo '</Directory>' >> /etc/apache2/sites-enabled/000-default.conf"
source ~/.phpbrew/bashrc
sudo service apache2 restart
composer self-update -n
composer install --prefer-source -n
sudo sh -c "echo '127.0.0.1 [your-domain]' >> /etc/hosts"
#### setup-phpunit-tests.sh
#!/bin/sh
cp .env.testing .env
php artisan key:generate
php artisan config:cache
#### setup-acceptance-tests.sh
#!/bin/sh
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
export NVM_DIR="/home/runner/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
nvm install node
npm install -g yarn
npm install -g bower
npm install -g gulp
yarn
bower install --allow-root
gulp
sudo sh -c "cp .env.acceptance .env"
sudo sh -c "rm -rf /var/www/html"
sudo sh -c "ln -s /home/runner/[your-repo]/public /var/www/html"
sudo sh -c "chmod 777 -R database"
sudo sh -c "chmod -R 777 storage"
sudo sh -c "chmod -R +x vendor/bin"
#### .env.testing
APP_ENV=testing
APP_DEBUG=true
APP_KEY=key
DB_CONNECTION=sqlite
CACHE_DRIVER=array
SESSION_DRIVER=array
QUEUE_DRIVER=sync
MAIL_DRIVER=log
#### .env.acceptance
APP_ENV=acceptance
APP_DEBUG=true
APP_URL=http://localhost:8000
DB_CONNECTION=acceptance
CACHE_DRIVER=acceptance
SESSION_DRIVER=array
QUEUE_DRIVER=sync
MAIL_DRIVER=log
#### config/database.php (add this to force the usage of its own database connection)
'acceptance' => [
'driver' => 'sqlite',
'database' => database_path('database.sqlite'),
'prefix' => '',
],
#### config/cache.php (add this to give it's own location for cache files)
'acceptance' => [
'driver' => 'file',
'path' => storage_path('framework/cache/acceptance'),
],
#### tests/acceptance/_bootstrap.php
<?php
// Here you can initialize variables that will be available to your (Codeception) tests
require __DIR__. '/../../bootstrap/autoload.php';
$app = require __DIR__.'/../../bootstrap/app.php';
$app->loadEnvironmentFrom('.resbeat.testing.localhost.env');
$app->instance('request', new \Illuminate\Http\Request);
$app->make('Illuminate\Contracts\Http\Kernel')->bootstrap();
#### tests/acceptance.suite.yml
class_name: AcceptanceTester
modules:
enabled:
- WebDriver:
url: http://[your-testing-domain-goes-here]
browser: phantomjs
window_size: 1600x1000
restart: true
clear_cookies: true
- \Helper\Acceptance
- Cli
- Db:
dsn: 'sqlite:database/database.sqlite'
user: ''
password: ''
#### codeception.yml
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
settings:
bootstrap: _bootstrap.php
colors: true
memory_limit: 2048M
extensions:
enabled:
- Codeception\Extension\RunFailed
- Codeception\Extension\Recorder
- Codeception\Extension\Phantoman
config:
Codeception\Extension\Phantoman:
suites: ['acceptance']
ignoreSslErrors: true
modules:
config:
Db:
dsn: 'sqlite:database/database.sqlite'
user: ''
password: ''
Codeception\Extension\Recorder:
delete_successful: true
#### Inside your tests/TestCase.php (or tests/AcceptanceTestCase.php in my case) add the following functions
#### for Laravel to function properly and for you to run off the database.sqlite file instead of in memory
public function _before(AcceptanceTester $I)
{
$I->runShellCommand('cp database/database.sqlite database/backup.sqlite');
$this->faker = Faker::create();
}
public function _after(AcceptanceTester $I)
{
$I->runShellCommand('cp database/backup.sqlite database/database.sqlite');
$I->runShellCommand('rm database/backup.sqlite');
}
public function _failed(AcceptanceTester $I)
{
$I->runShellCommand('cp database/backup.sqlite database/database.sqlite');
$I->runShellCommand('rm database/backup.sqlite');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment