mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
parent
3419d87476
commit
5296f59d23
@ -31,6 +31,27 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
|
|||||||
|
|
||||||
protected static $is_running_test = false;
|
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
|
* We need to disabling backing up of globals to avoid overriding
|
||||||
* the few globals SilverStripe relies on, like $lang for the i18n subsystem.
|
* 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
|
* @see http://sebastian-bergmann.de/archives/797-Global-Variables-and-PHPUnit.html
|
||||||
*/
|
*/
|
||||||
protected $backupGlobals = FALSE;
|
protected $backupGlobals = FALSE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper arrays for illegalExtensions/requiredExtensions code
|
||||||
|
*/
|
||||||
|
private $extensionsToReapply = array(), $extensionsToRemove = array();
|
||||||
|
|
||||||
public static function is_running_test() {
|
public static function is_running_test() {
|
||||||
return self::$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
|
* This is different to {@link setUp()}, which gets called once
|
||||||
* per method. Useful to initialize expensive operations which
|
* per method. Useful to initialize expensive operations which
|
||||||
* don't change state for any called method inside the test,
|
* 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.
|
* 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
|
// clear singletons, they're caching old extension info
|
||||||
// which is used in DatabaseAdmin->doBuild()
|
// which is used in DatabaseAdmin->doBuild()
|
||||||
global $_SINGLETONS;
|
global $_SINGLETONS;
|
||||||
$_SINGLETONS = array();
|
$_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
|
* Array
|
||||||
*/
|
*/
|
||||||
@ -242,14 +333,6 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
|
|||||||
self::$is_running_test = $this->originalIsRunningTest;
|
self::$is_running_test = $this->originalIsRunningTest;
|
||||||
$this->originalIsRunningTest = null;
|
$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
|
* Clear the log of emails sent
|
||||||
*/
|
*/
|
||||||
|
@ -11,21 +11,13 @@
|
|||||||
class SapphireTestSuite extends PHPUnit_Framework_TestSuite {
|
class SapphireTestSuite extends PHPUnit_Framework_TestSuite {
|
||||||
function setUp() {
|
function setUp() {
|
||||||
foreach($this->groups as $group) {
|
foreach($this->groups as $group) {
|
||||||
// Assumption: All testcases in the group are the same, as defined in TestRunner->runTests()
|
if($group[0] instanceof SapphireTest) $group[0]->setUpOnce();
|
||||||
$class = get_class($group[0]);
|
|
||||||
if(class_exists($class) && is_subclass_of($class, 'SapphireTest')) {
|
|
||||||
eval("$class::set_up_once();");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function tearDown() {
|
function tearDown() {
|
||||||
foreach($this->groups as $group) {
|
foreach($this->groups as $group) {
|
||||||
$class = get_class($group[0]);
|
if($group[0] instanceof SapphireTest) $group[0]->tearDownOnce();
|
||||||
// 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();");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,53 +9,44 @@ class TranslatableTest extends FunctionalTest {
|
|||||||
|
|
||||||
static $fixture_file = 'sapphire/tests/model/TranslatableTest.yml';
|
static $fixture_file = 'sapphire/tests/model/TranslatableTest.yml';
|
||||||
|
|
||||||
/**
|
protected $requiredExtensions = array(
|
||||||
* @todo Necessary because of monolithic Translatable design
|
'SiteTree' => array('Translatable'),
|
||||||
*/
|
'TranslatableTest_DataObject' => array('Translatable'),
|
||||||
static protected $origTranslatableSettings = array();
|
);
|
||||||
|
|
||||||
|
protected $illegalExtensions = array(
|
||||||
|
'SiteTree' => array(
|
||||||
|
'SiteTreeCMSThreeStepWorkflow',
|
||||||
|
'SiteTreeCMSWorkflow'
|
||||||
|
),
|
||||||
|
'WorkflowRequest' => array(
|
||||||
|
'WorkflowThreeStepRequest'
|
||||||
|
),
|
||||||
|
'LeftAndMain' => array(
|
||||||
|
'LeftAndMainCMSThreeStepWorkflow'
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
static function set_up_once() {
|
private $origLocale;
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
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() {
|
function setUp() {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
// whenever a translation is created, canTranslate() is checked
|
// whenever a translation is created, canTranslate() is checked
|
||||||
$cmseditor = $this->objFromFixture('Member', 'cmseditor');
|
$cmseditor = $this->objFromFixture('Member', 'cmseditor');
|
||||||
$cmseditor->logIn();
|
$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() {
|
function testTranslationGroups() {
|
||||||
// first in french
|
// first in french
|
||||||
$frPage = new SiteTree();
|
$frPage = new SiteTree();
|
||||||
|
@ -8,33 +8,10 @@ class TranslatableSearchFormTest extends FunctionalTest {
|
|||||||
static $fixture_file = 'sapphire/tests/search/TranslatableSearchFormTest.yml';
|
static $fixture_file = 'sapphire/tests/search/TranslatableSearchFormTest.yml';
|
||||||
|
|
||||||
protected $mockController;
|
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
|
protected $requiredExtensions = array(
|
||||||
if(!self::$origTranslatableSettings['has_extension']) Object::add_extension('SiteTree', 'Translatable');
|
'SiteTree' => array('Translatable'),
|
||||||
Object::add_extension('TranslatableTest_DataObject', 'Translatable');
|
);
|
||||||
|
|
||||||
// recreate database with new settings
|
|
||||||
$dbname = self::create_temp_db();
|
|
||||||
DB::set_alternative_database_name($dbname);
|
|
||||||
}
|
|
||||||
|
|
||||||
function setUp() {
|
function setUp() {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
@ -47,16 +24,7 @@ class TranslatableSearchFormTest extends FunctionalTest {
|
|||||||
$admin->logIn();
|
$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() {
|
function testPublishedPagesMatchedByTitleInDefaultLanguage() {
|
||||||
$sf = new SearchForm($this->mockController, 'SearchForm');
|
$sf = new SearchForm($this->mockController, 'SearchForm');
|
||||||
|
Loading…
Reference in New Issue
Block a user