From 1b544a46a326f98877a41f182a0db2b60cbb54c6 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Sun, 4 Dec 2011 13:35:06 +0100 Subject: [PATCH] MINOR Backported bootstrap.php changes from CI setup (creates a FakeController to avoid empty controller stacks, fake empty session global) --- tests/bootstrap.php | 55 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 1381ad6c3..da81eb645 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -2,11 +2,17 @@ // Simulate an execution from sapphire/cli-script.php, Core.php has too many // hardcoded assumptions about folder depth of the executing script. -// Overrides paths relative to this file (in sapphire/tests/FullTestSuite.php) -$_SERVER['SCRIPT_FILENAME'] = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'cli-script.php'; +// Make sure display_errors is on +ini_set('display_errors', 1); + +// Fake the script name and base +global $_SERVER; +if (!$_SERVER) $_SERVER = array(); + +$_SERVER['SCRIPT_FILENAME'] = getcwd() . DIRECTORY_SEPARATOR . 'sapphire' . DIRECTORY_SEPARATOR . 'cli-script.php'; $_SERVER['SCRIPT_NAME'] = '.' . DIRECTORY_SEPARATOR . 'sapphire' . DIRECTORY_SEPARATOR . 'cli-script.php'; -define('BASE_PATH', rtrim(dirname(dirname(dirname(__FILE__)))), DIRECTORY_SEPARATOR); +define('BASE_PATH', getcwd()); // Copied from cli-script.php, to enable same behaviour through phpunit runner. if(isset($_SERVER['argv'][2])) { @@ -24,14 +30,47 @@ if(isset($_SERVER['argv'][2])) { $_REQUEST = $_GET; } -if(!class_exists('Object')) { - require_once("sapphire/core/Core.php"); - global $databaseConfig; - DB::connect($databaseConfig); -} +// Connect to database +require_once(getcwd()."/sapphire/core/Core.php"); +global $databaseConfig; +DB::connect($databaseConfig); +// Now set a fake REQUEST_URI $_SERVER['REQUEST_URI'] = BASE_URL . '/dev'; +// Fake a session +$_SESSION = null; + +// Fake a current controller. Way harder than it should be +class FakeController extends Controller { + + function __construct() { + parent::__construct(); + + $session = new Session(isset($_SESSION) ? $_SESSION : null); + $this->setSession($session); + + $this->pushCurrent(); + + $this->request = new SS_HTTPRequest( + (isset($_SERVER['X-HTTP-Method-Override'])) ? $_SERVER['X-HTTP-Method-Override'] : $_SERVER['REQUEST_METHOD'], + '/' + ); + + $this->response = new SS_HTTPResponse(); + + $this->init(); + } +} +global $_ALL_CLASSES; +if(isset($_ALL_CLASSES)) { + $_ALL_CLASSES['parents']['FakeController'] = array_merge($_ALL_CLASSES['parents']['Controller'], array('Controller' => 'Controller')); +} + +// Prepare manifest autoloader +$controller = new FakeController(); + +// Get test manifest TestRunner::use_test_manifest(); // Remove the error handler so that PHPUnit can add its own