API CHANGE: Renamed SapphireTest::set_up_once/tear_down_once to setUpOnce/tearDownOnce, and made them instance methods.

API CHANGE: Added SapphireTest::$illegalExtensions and SapphireTest::$requiredExtensions for making tests depending on particular extension sets (from r89958)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@96727 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2010-01-12 23:03:27 +00:00
parent 3419d87476
commit 5296f59d23
4 changed files with 127 additions and 93 deletions

View File

@ -31,6 +31,27 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
protected static $is_running_test = false;
/**
* A list of extensions that can't be applied during the execution of this run. If they are
* applied, they will be temporarily removed and a database migration called.
*
* The keys of the are the classes that the extensions can't be applied the extensions to, and
* the values are an array of illegal extensions on that class.
*/
protected $illegalExtensions = array(
);
/**
* A list of extensions that must be applied during the execution of this run. If they are
* not applied, they will be temporarily added and a database migration called.
*
* The keys of the are the classes to apply the extensions to, and the values are an array
* of illegal required extensions on that class.
*/
protected $requiredExtensions = array(
);
/**
* We need to disabling backing up of globals to avoid overriding
* the few globals SilverStripe relies on, like $lang for the i18n subsystem.
@ -38,6 +59,11 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
* @see http://sebastian-bergmann.de/archives/797-Global-Variables-and-PHPUnit.html
*/
protected $backupGlobals = FALSE;
/**
* Helper arrays for illegalExtensions/requiredExtensions code
*/
private $extensionsToReapply = array(), $extensionsToRemove = array();
public static function is_running_test() {
return self::$is_running_test;
@ -110,16 +136,81 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
* This is different to {@link setUp()}, which gets called once
* per method. Useful to initialize expensive operations which
* don't change state for any called method inside the test,
* e.g. dynamically adding an extension. See {@link tear_down_once()}
* e.g. dynamically adding an extension. See {@link tearDownOnce()}
* for tearing down the state again.
*/
static function set_up_once() {
function setUpOnce() {
// Remove any illegal extensions that are present
foreach($this->illegalExtensions as $class => $extensions) {
foreach($extensions as $extension) {
if (Object::has_extension($class, $extension)) {
if(!isset($this->extensionsToReapply[$class])) $this->extensionsToReapply[$class] = array();
$this->extensionsToReapply[$class][] = $extension;
Object::remove_extension($class, $extension);
$isAltered = true;
}
}
}
// Add any required extensions that aren't present
foreach($this->requiredExtensions as $class => $extensions) {
$this->extensionsToRemove[$class] = array();
foreach($extensions as $extension) {
if(!Object::has_extension($class, $extension)) {
if(!isset($this->extensionsToRemove[$class])) $this->extensionsToReapply[$class] = array();
$this->extensionsToRemove[$class][] = $extension;
Object::add_extension($class, $extension);
$isAltered = true;
}
}
}
// If we have made changes to the extensions present, then migrate the database schema.
if($this->extensionsToReapply || $this->extensionsToRemove) {
// clear singletons, they're caching old extension info which is used in DatabaseAdmin->doBuild()
global $_SINGLETONS;
$_SINGLETONS = array();
// rebuild the db schema
$dbadmin = new DatabaseAdmin();
$dbadmin->doBuild(true, false, true);
}
// clear singletons, they're caching old extension info
// which is used in DatabaseAdmin->doBuild()
global $_SINGLETONS;
$_SINGLETONS = array();
}
/**
* tearDown method that's called once per test class rather once per test method.
*/
function tearDownOnce() {
// If we have made changes to the extensions present, then migrate the database schema.
if($this->extensionsToReapply || $this->extensionsToRemove) {
// Remove extensions added for testing
foreach($this->extensionsToRemove as $class => $extensions) {
foreach($extensions as $extension) {
Object::remove_extension($class, $extension);
}
}
// Reapply ones removed
foreach($this->extensionsToReapply as $class => $extensions) {
foreach($extensions as $extension) {
Object::add_extension($class, $extension);
}
}
// clear singletons, they're caching old extension info which is used in DatabaseAdmin->doBuild()
global $_SINGLETONS;
$_SINGLETONS = array();
// rebuild the db schema
$dbadmin = new DatabaseAdmin();
$dbadmin->doBuild(true, false, true);
}
}
/**
* Array
*/
@ -242,14 +333,6 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
self::$is_running_test = $this->originalIsRunningTest;
$this->originalIsRunningTest = null;
}
static function tear_down_once() {
// clear singletons, they're caching old extension info
// which is used in DatabaseAdmin->doBuild()
global $_SINGLETONS;
$_SINGLETONS = array();
}
/**
* Clear the log of emails sent
*/

View File

@ -11,21 +11,13 @@
class SapphireTestSuite extends PHPUnit_Framework_TestSuite {
function setUp() {
foreach($this->groups as $group) {
// Assumption: All testcases in the group are the same, as defined in TestRunner->runTests()
$class = get_class($group[0]);
if(class_exists($class) && is_subclass_of($class, 'SapphireTest')) {
eval("$class::set_up_once();");
}
if($group[0] instanceof SapphireTest) $group[0]->setUpOnce();
}
}
function tearDown() {
foreach($this->groups as $group) {
$class = get_class($group[0]);
// Assumption: All testcases in the group are the same, as defined in TestRunner->runTests()
if(class_exists($class) && is_subclass_of($class, 'SapphireTest')) {
eval("$class::tear_down_once();");
}
if($group[0] instanceof SapphireTest) $group[0]->tearDownOnce();
}
}
}

View File

@ -9,53 +9,44 @@ class TranslatableTest extends FunctionalTest {
static $fixture_file = 'sapphire/tests/model/TranslatableTest.yml';
/**
* @todo Necessary because of monolithic Translatable design
*/
static protected $origTranslatableSettings = array();
protected $requiredExtensions = array(
'SiteTree' => array('Translatable'),
'TranslatableTest_DataObject' => array('Translatable'),
);
protected $illegalExtensions = array(
'SiteTree' => array(
'SiteTreeCMSThreeStepWorkflow',
'SiteTreeCMSWorkflow'
),
'WorkflowRequest' => array(
'WorkflowThreeStepRequest'
),
'LeftAndMain' => array(
'LeftAndMainCMSThreeStepWorkflow'
),
);
static function set_up_once() {
parent::set_up_once();
// needs to recreate the database schema with language properties
self::kill_temp_db();
private $origLocale;
// store old defaults
self::$origTranslatableSettings['has_extension'] = Object::has_extension('SiteTree', 'Translatable');
self::$origTranslatableSettings['default_locale'] = Translatable::default_locale();
// overwrite locale
Translatable::set_default_locale("en_US");
// refresh the decorated statics - different fields in $db with Translatable enabled
if(!self::$origTranslatableSettings['has_extension']) Object::add_extension('SiteTree', 'Translatable');
Object::add_extension('TranslatableTest_DataObject', 'Translatable');
// recreate database with new settings
$dbname = self::create_temp_db();
DB::set_alternative_database_name($dbname);
}
static function tear_down_once() {
if(!self::$origTranslatableSettings['has_extension']) Object::remove_extension('SiteTree', 'Translatable');
Translatable::set_default_locale(self::$origTranslatableSettings['default_locale']);
Translatable::set_current_locale(self::$origTranslatableSettings['default_locale']);
self::kill_temp_db();
self::create_temp_db();
parent::tear_down_once();
}
function setUp() {
parent::setUp();
// whenever a translation is created, canTranslate() is checked
$cmseditor = $this->objFromFixture('Member', 'cmseditor');
$cmseditor->logIn();
$this->origLocale = Translatable::default_locale();
Translatable::set_default_locale("en_US");
}
function tearDown() {
Translatable::set_default_locale($this->origLocale);
Translatable::set_current_locale($this->origLocale);
parent::tearDown();
}
function testTranslationGroups() {
// first in french
$frPage = new SiteTree();

View File

@ -8,33 +8,10 @@ class TranslatableSearchFormTest extends FunctionalTest {
static $fixture_file = 'sapphire/tests/search/TranslatableSearchFormTest.yml';
protected $mockController;
/**
* @todo Necessary because of monolithic Translatable design
*/
static protected $origTranslatableSettings = array();
static function set_up_once() {
parent::set_up_once();
// needs to recreate the database schema with language properties
self::kill_temp_db();
// store old defaults
self::$origTranslatableSettings['has_extension'] = Object::has_extension('SiteTree', 'Translatable');
self::$origTranslatableSettings['default_locale'] = Translatable::default_locale();
// overwrite locale
Translatable::set_default_locale("en_US");
// refresh the decorated statics - different fields in $db with Translatable enabled
if(!self::$origTranslatableSettings['has_extension']) Object::add_extension('SiteTree', 'Translatable');
Object::add_extension('TranslatableTest_DataObject', 'Translatable');
// recreate database with new settings
$dbname = self::create_temp_db();
DB::set_alternative_database_name($dbname);
}
protected $requiredExtensions = array(
'SiteTree' => array('Translatable'),
);
function setUp() {
parent::setUp();
@ -47,16 +24,7 @@ class TranslatableSearchFormTest extends FunctionalTest {
$admin->logIn();
}
static function tear_down_once() {
if(!self::$origTranslatableSettings['has_extension']) Object::remove_extension('SiteTree', 'Translatable');
Translatable::set_default_locale(self::$origTranslatableSettings['default_locale']);
self::kill_temp_db();
self::create_temp_db();
parent::tear_down_once();
}
function testPublishedPagesMatchedByTitleInDefaultLanguage() {
$sf = new SearchForm($this->mockController, 'SearchForm');