Skip to content

Instantly share code, notes, and snippets.

@creativecoder
Last active August 29, 2015 14:26
Show Gist options
  • Save creativecoder/954fc1167bb8ea321e6c to your computer and use it in GitHub Desktop.
Save creativecoder/954fc1167bb8ea321e6c to your computer and use it in GitHub Desktop.
Laravel FeatureContext for testing with Behat (use this to extend your FeatureContext class instead of Mink)
<?php
use Illuminate\Foundation\Testing\ApplicationTrait;
use Illuminate\Foundation\Testing\AssertionsTrait;
use Illuminate\Foundation\Testing\CrawlerTrait;
class LaravelContext extends PHPUnit_Framework_TestCase
{
use ApplicationTrait, AssertionsTrait, CrawlerTrait;
/**
* The base URL to use while testing the application.
*
* @var string
*/
protected $baseUrl = 'http://localhost';
/**
* The callbacks that should be run before the application is destroyed.
*
* @var array
*/
protected $beforeApplicationDestroyedCallbacks = [];
/**
* @BeforeScenario
*/
public function setUp()
{
if (!$this->app) {
$this->refreshApplication();
}
}
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
// APP_ENV=testing set by ApplicationTrait
putenv('DB_CONNECTION=test');
putenv('CACHE_DRIVER=array');
putenv('SESSION_DRIVER=array');
putenv('QUEUE_DRIVER=sync');
$app = require __DIR__ . '/../../bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
return $app;
}
/**
* @AfterScenario
*/
public function tearDown()
{
if (class_exists('Mockery')) {
Mockery::close();
}
if ($this->app) {
foreach ($this->beforeApplicationDestroyedCallbacks as $callback) {
call_user_func($callback);
}
$this->app->flush();
$this->app = null;
}
}
/**
* Register a callback to be run before the application is destroyed.
*
* @param callable $callback
* @return void
*/
protected function beforeApplicationDestroyed(callable $callback)
{
$this->beforeApplicationDestroyedCallbacks[] = $callback;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment