Last active
August 29, 2015 14:06
-
-
Save derekdowling/a796493c670ce286f0fa to your computer and use it in GitHub Desktop.
Non-PHPUnit based Symfony TestKernel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace PROJECT\Tests; | |
// PATH TO YOUR AUTOLOADER | |
require_once(__DIR__ . '/../../../../app/autoload.php'); | |
// PATH TO YOUR SYMFONY APP KERNEL | |
require_once(__DIR__ . '/../../../../app/AppKernel.php'); | |
use AppKernel; | |
/** | |
* Any test related loading should happen here. Since we are using an | |
* autoloader and decent framework, this file should hopefully remaing fairly | |
* concise. | |
*/ | |
class TestKernel | |
{ | |
protected static $kernel; | |
/** | |
* Because we'd like to avoid using actualy database connections as much | |
* as possible, use this call to get a mock Entity Manager which can then | |
* be passed into Mediator's constructor. | |
*/ | |
public static function getService($service_name) | |
{ | |
static::boot(); | |
return static::$kernel->getContainer()->get($service_name); | |
} | |
/** | |
* Retrieves a Doctrine Entity Manager with database connectivity from the | |
* Kernel. | |
*/ | |
public static function getEM() | |
{ | |
static::boot(); | |
return static::$kernel->getContainer->get('doctrine.orm.entity_manager'); | |
} | |
public static function getClient(array $options = array(), array $server = array()) | |
{ | |
static::boot($options); | |
$client = static::$kernel->getContainer()->get('test.client'); | |
$client->setServerParameters($server); | |
return $client; | |
} | |
/************** | |
* Kernel startup/shutdown related tasks. | |
**************/ | |
private static function boot(array $options = array()) | |
{ | |
static::ensureShutdown(); | |
static::$kernel = new AppKernel( | |
isset($options['environment']) ? $options['environment'] : 'test', | |
isset($options['debug']) ? $options['debug'] : true | |
); | |
static::$kernel->boot(); | |
} | |
/** | |
* Should be called at the "Tear-Down" stage within any tests that utilize | |
* a Client or Kernel. | |
*/ | |
public static function ensureShutdown() | |
{ | |
if (static::$kernel !== null) { | |
static::$kernel->shutdown(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment