2012-08-15 16:50:19 +02:00
|
|
|
<?php
|
|
|
|
|
2012-10-16 22:01:16 +02:00
|
|
|
namespace SilverStripe\BehatExtension\Context\Initializer;
|
2012-08-15 16:50:19 +02:00
|
|
|
|
2016-08-10 03:35:13 +02:00
|
|
|
use Behat\Behat\Context\Initializer\InitializerInterface;
|
|
|
|
use Behat\Behat\Context\ContextInterface;
|
2012-10-16 22:01:16 +02:00
|
|
|
use SilverStripe\BehatExtension\Context\SilverStripeAwareContextInterface;
|
2016-09-01 06:22:47 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2012-08-15 16:50:19 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Behat/SilverStripeExtension
|
|
|
|
*
|
|
|
|
* (c) Michał Ochman <ochman.d.michal@gmail.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SilverStripe aware contexts initializer.
|
|
|
|
* Sets SilverStripe instance to the SilverStripeAware contexts.
|
|
|
|
*
|
|
|
|
* @author Michał Ochman <ochman.d.michal@gmail.com>
|
|
|
|
*/
|
|
|
|
class SilverStripeAwareInitializer implements InitializerInterface
|
|
|
|
{
|
2016-09-01 06:22:47 +02:00
|
|
|
|
2014-10-08 22:55:00 +02:00
|
|
|
private $databaseName;
|
2016-09-01 06:22:47 +02:00
|
|
|
|
2012-11-14 00:29:20 +01:00
|
|
|
/**
|
2016-09-01 06:22:47 +02:00
|
|
|
* @var array
|
2012-11-14 00:29:20 +01:00
|
|
|
*/
|
|
|
|
protected $ajaxSteps;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Int Timeout in milliseconds
|
|
|
|
*/
|
|
|
|
protected $ajaxTimeout;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var String {@link see SilverStripeContext}
|
|
|
|
*/
|
|
|
|
protected $adminUrl;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var String {@link see SilverStripeContext}
|
|
|
|
*/
|
|
|
|
protected $loginUrl;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var String {@link see SilverStripeContext}
|
|
|
|
*/
|
|
|
|
protected $screenshotPath;
|
2012-08-15 16:50:19 +02:00
|
|
|
|
2016-08-10 03:35:13 +02:00
|
|
|
/**
|
|
|
|
* @var object {@link TestSessionEnvironment}
|
|
|
|
*/
|
|
|
|
protected $testSessionEnvironment;
|
2014-02-04 23:57:46 +01:00
|
|
|
|
2016-09-13 08:03:12 +02:00
|
|
|
/**
|
|
|
|
* @var string PHP file to included before loading Core.php
|
|
|
|
*/
|
|
|
|
protected $bootstrapFile;
|
|
|
|
|
2014-10-08 22:55:00 +02:00
|
|
|
/**
|
|
|
|
* Initializes initializer.
|
2016-09-01 06:22:47 +02:00
|
|
|
*
|
|
|
|
* @param string $frameworkPath
|
2014-10-08 22:55:00 +02:00
|
|
|
*/
|
2016-09-13 08:03:12 +02:00
|
|
|
public function __construct($frameworkPath, $bootstrapFile = null)
|
2014-10-08 22:55:00 +02:00
|
|
|
{
|
2016-09-13 08:03:12 +02:00
|
|
|
$this->bootstrap($frameworkPath, $bootstrapFile);
|
2014-02-04 23:57:46 +01:00
|
|
|
|
2016-08-10 03:35:13 +02:00
|
|
|
file_put_contents('php://stdout', "Creating test session environment" . PHP_EOL);
|
2014-02-04 23:57:46 +01:00
|
|
|
|
2016-09-01 06:22:47 +02:00
|
|
|
$testEnv = Injector::inst()->get('TestSessionEnvironment');
|
2016-08-10 03:35:13 +02:00
|
|
|
$testEnv->startTestSession(array(
|
|
|
|
'createDatabase' => true
|
|
|
|
));
|
2014-02-04 23:57:46 +01:00
|
|
|
|
2016-08-10 03:35:13 +02:00
|
|
|
$state = $testEnv->getState();
|
2014-02-04 23:57:46 +01:00
|
|
|
|
2016-08-10 03:35:13 +02:00
|
|
|
$this->databaseName = $state->database;
|
|
|
|
$this->testSessionEnvironment = $testEnv;
|
2014-02-04 23:57:46 +01:00
|
|
|
|
2016-08-10 03:35:13 +02:00
|
|
|
file_put_contents('php://stdout', "Temp Database: $this->databaseName" . PHP_EOL . PHP_EOL);
|
2014-02-09 06:56:40 +01:00
|
|
|
|
2014-10-08 22:55:00 +02:00
|
|
|
register_shutdown_function(array($this, '__destruct'));
|
|
|
|
}
|
2012-08-15 16:50:19 +02:00
|
|
|
|
2014-10-08 22:55:00 +02:00
|
|
|
public function __destruct()
|
|
|
|
{
|
2015-12-18 03:42:48 +01:00
|
|
|
// Add condition here as register_shutdown_function() also calls this in __construct()
|
2016-08-10 03:35:13 +02:00
|
|
|
if ($this->testSessionEnvironment) {
|
2015-12-18 03:42:48 +01:00
|
|
|
file_put_contents('php://stdout', "Killing test session environment...");
|
2014-10-08 22:55:00 +02:00
|
|
|
$this->testSessionEnvironment->endTestSession();
|
2015-12-18 03:42:48 +01:00
|
|
|
$this->testSessionEnvironment = null;
|
|
|
|
file_put_contents('php://stdout', " done!" . PHP_EOL);
|
2014-10-08 22:55:00 +02:00
|
|
|
}
|
|
|
|
}
|
2012-08-15 16:50:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if initializer supports provided context.
|
|
|
|
*
|
|
|
|
* @param ContextInterface $context
|
|
|
|
*
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
public function supports(ContextInterface $context)
|
|
|
|
{
|
|
|
|
return $context instanceof SilverStripeAwareContextInterface;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes provided context.
|
|
|
|
*
|
|
|
|
* @param ContextInterface $context
|
|
|
|
*/
|
|
|
|
public function initialize(ContextInterface $context)
|
|
|
|
{
|
2012-11-15 18:12:21 +01:00
|
|
|
$context->setDatabase($this->databaseName);
|
2012-11-14 00:29:20 +01:00
|
|
|
$context->setAjaxSteps($this->ajaxSteps);
|
|
|
|
$context->setAjaxTimeout($this->ajaxTimeout);
|
|
|
|
$context->setScreenshotPath($this->screenshotPath);
|
2014-03-19 02:53:00 +01:00
|
|
|
$context->setRegionMap($this->regionMap);
|
2012-11-14 00:29:20 +01:00
|
|
|
$context->setAdminUrl($this->adminUrl);
|
|
|
|
$context->setLoginUrl($this->loginUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setAjaxSteps($ajaxSteps)
|
|
|
|
{
|
2016-08-10 03:35:13 +02:00
|
|
|
if ($ajaxSteps) {
|
|
|
|
$this->ajaxSteps = $ajaxSteps;
|
|
|
|
}
|
2012-11-14 00:29:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getAjaxSteps()
|
|
|
|
{
|
|
|
|
return $this->ajaxSteps;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setAjaxTimeout($ajaxTimeout)
|
|
|
|
{
|
|
|
|
$this->ajaxTimeout = $ajaxTimeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAjaxTimeout()
|
|
|
|
{
|
|
|
|
return $this->ajaxTimeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setAdminUrl($adminUrl)
|
|
|
|
{
|
|
|
|
$this->adminUrl = $adminUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAdminUrl()
|
|
|
|
{
|
|
|
|
return $this->adminUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setLoginUrl($loginUrl)
|
|
|
|
{
|
|
|
|
$this->loginUrl = $loginUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLoginUrl()
|
|
|
|
{
|
|
|
|
return $this->loginUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setScreenshotPath($screenshotPath)
|
|
|
|
{
|
|
|
|
$this->screenshotPath = $screenshotPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getScreenshotPath()
|
|
|
|
{
|
|
|
|
return $this->screenshotPath;
|
2012-08-15 16:50:19 +02:00
|
|
|
}
|
|
|
|
|
2016-08-10 03:35:13 +02:00
|
|
|
public function getRegionMap()
|
|
|
|
{
|
2014-03-19 02:53:00 +01:00
|
|
|
return $this->regionMap;
|
|
|
|
}
|
|
|
|
|
2016-08-10 03:35:13 +02:00
|
|
|
public function setRegionMap($regionMap)
|
|
|
|
{
|
2014-03-19 02:53:00 +01:00
|
|
|
$this->regionMap = $regionMap;
|
|
|
|
}
|
|
|
|
|
2013-05-09 16:26:24 +02:00
|
|
|
/**
|
2016-09-01 06:22:47 +02:00
|
|
|
* @param string $frameworkPath Absolute path to 'framework' module
|
2013-05-09 16:26:24 +02:00
|
|
|
*/
|
2016-09-13 08:03:12 +02:00
|
|
|
protected function bootstrap($frameworkPath, $bootstrapFile = null)
|
2012-08-15 16:50:19 +02:00
|
|
|
{
|
2013-08-13 13:03:29 +02:00
|
|
|
file_put_contents('php://stdout', 'Bootstrapping' . PHP_EOL);
|
2012-08-15 16:50:19 +02:00
|
|
|
|
2016-09-13 08:03:12 +02:00
|
|
|
// Require a bootstrap file, if provided
|
|
|
|
if ($bootstrapFile) {
|
|
|
|
require_once($bootstrapFile);
|
|
|
|
}
|
|
|
|
|
2012-11-09 15:46:33 +01:00
|
|
|
// Connect to database and build manifest
|
|
|
|
$_GET['flush'] = 1;
|
2016-09-08 06:31:48 +02:00
|
|
|
require_once $frameworkPath . '/Core/Core.php';
|
2012-11-09 15:46:33 +01:00
|
|
|
unset($_GET['flush']);
|
2012-08-15 16:50:19 +02:00
|
|
|
|
|
|
|
// Remove the error handler so that PHPUnit can add its own
|
|
|
|
restore_error_handler();
|
|
|
|
}
|
2016-08-10 03:35:13 +02:00
|
|
|
}
|