Updated behat-extension to use app object

This commit is contained in:
Damian Mooyman 2017-06-21 18:17:18 +12:00
parent 3d2e742709
commit f768c952dc
3 changed files with 57 additions and 13 deletions

View File

@ -2,7 +2,13 @@
namespace SilverStripe\BehatExtension\Compiler; namespace SilverStripe\BehatExtension\Compiler;
use SilverStripe\Control\CLIRequestBuilder;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPApplication;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\CoreKernel;
use SilverStripe\Core\Manifest\ModuleLoader; use SilverStripe\Core\Manifest\ModuleLoader;
use SilverStripe\ORM\DataObject;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
@ -18,12 +24,13 @@ class CoreInitializationPass implements CompilerPassInterface
*/ */
public function process(ContainerBuilder $container) public function process(ContainerBuilder $container)
{ {
// Note: Moved from SilverStripeAwareInitializer
file_put_contents('php://stdout', 'Bootstrapping' . PHP_EOL);
// Connect to database and build manifest // Connect to database and build manifest
$_GET['flush'] = 1;
if (!getenv('SS_ENVIRONMENT_TYPE')) { if (!getenv('SS_ENVIRONMENT_TYPE')) {
putenv('SS_ENVIRONMENT_TYPE=dev'); putenv('SS_ENVIRONMENT_TYPE=dev');
} }
require_once('Core/Core.php');
// Include bootstrap file // Include bootstrap file
$bootstrapFile = $container->getParameter('silverstripe_extension.bootstrap_file'); $bootstrapFile = $container->getParameter('silverstripe_extension.bootstrap_file');
@ -31,6 +38,25 @@ class CoreInitializationPass implements CompilerPassInterface
require_once $bootstrapFile; require_once $bootstrapFile;
} }
// Copied from SapphireTest
$request = CLIRequestBuilder::createFromEnvironment();
$kernel = new CoreKernel(BASE_PATH);
$app = new HTTPApplication($kernel);
$app->execute($request, function (HTTPRequest $request) {
// Start session and execute
$request->getSession()->init();
// Invalidate classname spec since the test manifest will now pull out new subclasses for each internal class
// (e.g. Member will now have various subclasses of DataObjects that implement TestOnly)
DataObject::reset();
// Set dummy controller;
$controller = Controller::create();
$controller->setRequest($request);
$controller->pushCurrent();
$controller->doInit();
}, true);
// Register all paths // Register all paths
foreach (ModuleLoader::inst()->getManifest()->getModules() as $module) { foreach (ModuleLoader::inst()->getManifest()->getModules() as $module) {
$container->setParameter('paths.modules.'.$module->getShortName(), $module->getPath()); $container->setParameter('paths.modules.'.$module->getShortName(), $module->getPath());
@ -41,8 +67,6 @@ class CoreInitializationPass implements CompilerPassInterface
} }
} }
unset($_GET['flush']);
// Remove the error handler so that PHPUnit can add its own // Remove the error handler so that PHPUnit can add its own
restore_error_handler(); restore_error_handler();
} }

View File

@ -18,6 +18,7 @@ use SilverStripe\Dev\FixtureBlueprint;
use SilverStripe\Dev\FixtureFactory; use SilverStripe\Dev\FixtureFactory;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\YamlFixture; use SilverStripe\Dev\YamlFixture;
use SilverStripe\ORM\Connect\TempDatabase;
use SilverStripe\ORM\DB; use SilverStripe\ORM\DB;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\Versioned\Versioned; use SilverStripe\Versioned\Versioned;
@ -46,6 +47,13 @@ class FixtureContext implements Context
*/ */
protected $filesPath; protected $filesPath;
/**
* Temp database helper
*
* @var TempDatabase
*/
protected $tempDatabase;
/** /**
* @var String Tracks all files and folders created from fixtures, for later cleanup. * @var String Tracks all files and folders created from fixtures, for later cleanup.
*/ */
@ -66,6 +74,7 @@ class FixtureContext implements Context
throw new InvalidArgumentException("filesPath is required"); throw new InvalidArgumentException("filesPath is required");
} }
$this->setFilesPath($filesPath); $this->setFilesPath($filesPath);
$this->setTempDatabase(new TempDatabase());
} }
/** /**
@ -125,6 +134,24 @@ class FixtureContext implements Context
return $this->filesPath; return $this->filesPath;
} }
/**
* @return TempDatabase
*/
public function getTempDatabase()
{
return $this->tempDatabase;
}
/**
* @param TempDatabase $database
* @return $this
*/
public function setTempDatabase(TempDatabase $database)
{
$this->tempDatabase = $database;
return $this;
}
/** /**
* @BeforeScenario @database-defaults * @BeforeScenario @database-defaults
* *
@ -132,7 +159,7 @@ class FixtureContext implements Context
*/ */
public function beforeDatabaseDefaults(BeforeScenarioScope $event) public function beforeDatabaseDefaults(BeforeScenarioScope $event)
{ {
SapphireTest::empty_temp_db(); $this->getTempDatabase()->clearAllData();
DB::get_conn()->quiet(); DB::get_conn()->quiet();
$dataClasses = ClassInfo::subclassesFor(DataObject::class); $dataClasses = ClassInfo::subclassesFor(DataObject::class);
array_shift($dataClasses); array_shift($dataClasses);
@ -147,7 +174,7 @@ class FixtureContext implements Context
*/ */
public function afterResetDatabase(AfterScenarioScope $event) public function afterResetDatabase(AfterScenarioScope $event)
{ {
SapphireTest::empty_temp_db(); $this->getTempDatabase()->clearAllData();
} }
/** /**

View File

@ -65,13 +65,6 @@ class SilverStripeAwareInitializer implements ContextInitializer
*/ */
public function __construct() public function __construct()
{ {
file_put_contents('php://stdout', 'Bootstrapping' . PHP_EOL);
SapphireTest::start();
// Remove the error handler so that PHPUnit can add its own
restore_error_handler();
file_put_contents('php://stdout', "Creating test session environment" . PHP_EOL); file_put_contents('php://stdout', "Creating test session environment" . PHP_EOL);
$testEnv = TestSessionEnvironment::singleton(); $testEnv = TestSessionEnvironment::singleton();