ENHANCEMENT Adding SapphireTest::set_up_once() and SapphireTest::tear_down_once() for better test performance with state that just needs to be initialized once per test case (not per test method). Added new SapphireTestSuite to support this through PHPUnit.

ENHANCEMENT Using set_up_once() in TranslatableTest and TranslatableSearchFormTest for better test run performance

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@74941 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-04-22 03:22:09 +00:00
parent 515f7b1587
commit 228b976ce1
5 changed files with 72 additions and 31 deletions

View File

@ -84,7 +84,18 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
}
/**
* Array of
* Called once per test case ({@link SapphireTest} subclass).
* 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()}
* for tearing down the state again.
*/
static function set_up_once() {
}
/**
* Array
*/
protected $fixtureDictionary;
@ -151,6 +162,9 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
$this->originalIsRunningTest = null;
}
static function tear_down_once() {
}
/**
* Clear the log of emails sent
*/

32
dev/SapphireTestSuite.php Normal file
View File

@ -0,0 +1,32 @@
<?php
/**
* Light wrapper around {@link PHPUnit_Framework_TestSuite}
* which allows to have {@link setUp()} and {@link tearDown()}
* methods which are called just once per suite, not once per
* test method in each suite/case.
*
* @package sapphire
* @subpackage testing
*/
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();");
}
}
}
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();");
}
}
}
}
?>

View File

@ -153,7 +153,7 @@ class TestRunner extends Controller {
foreach($classList as $className) {
// Ensure that the autoloader pulls in the test class, as PHPUnit won't know how to do this.
class_exists($className);
$suite->addTest(new PHPUnit_Framework_TestSuite($className));
$suite->addTest(new SapphireTestSuite($className));
}
// Remove the error handler so that PHPUnit can add its own

View File

@ -9,26 +9,24 @@ class TranslatableTest extends FunctionalTest {
static $fixture_file = 'sapphire/tests/model/TranslatableTest.yml';
protected $recreateTempDb = true;
/**
* @todo Necessary because of monolithic Translatable design
*/
protected $origTranslatableSettings = array();
static protected $origTranslatableSettings = array();
function setUp() {
static function set_up_once() {
// needs to recreate the database schema with language properties
self::kill_temp_db();
// store old defaults
$this->origTranslatableSettings['has_extension'] = singleton('SiteTree')->hasExtension('Translatable');
$this->origTranslatableSettings['default_locale'] = Translatable::default_locale();
self::$origTranslatableSettings['has_extension'] = singleton('SiteTree')->hasExtension('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(!$this->origTranslatableSettings['has_extension']) Object::add_extension('SiteTree', 'Translatable');
if(!self::$origTranslatableSettings['has_extension']) Object::add_extension('SiteTree', 'Translatable');
Object::add_extension('TranslatableTest_DataObject', 'Translatable');
// clear singletons, they're caching old extension info which is used in DatabaseAdmin->doBuild()
@ -50,18 +48,18 @@ class TranslatableTest extends FunctionalTest {
$dbname = self::create_temp_db();
DB::set_alternative_database_name($dbname);
parent::setUp();
parent::set_up_once();
}
function tearDown() {
if(!$this->origTranslatableSettings['has_extension']) Object::remove_extension('SiteTree', 'Translatable');
static function tear_down_once() {
if(!self::$origTranslatableSettings['has_extension']) Object::remove_extension('SiteTree', 'Translatable');
Translatable::set_default_locale($this->origTranslatableSettings['default_locale']);
Translatable::set_default_locale(self::$origTranslatableSettings['default_locale']);
self::kill_temp_db();
self::create_temp_db();
parent::tearDown();
parent::tear_down_once();
}
function testTranslationGroups() {
@ -652,7 +650,6 @@ class TranslatableTest extends FunctionalTest {
Translatable::set_reading_locale('en_US');
$_SERVER['HTTP_HOST'] = $_originalHost;
}
}
class TranslatableTest_DataObject extends DataObject implements TestOnly {

View File

@ -9,26 +9,24 @@ class TranslatableSearchFormTest extends FunctionalTest {
protected $mockController;
protected $recreateTempDb = true;
/**
* @todo Necessary because of monolithic Translatable design
*/
protected $origTranslatableSettings = array();
static protected $origTranslatableSettings = array();
function setUp() {
static function set_up_once() {
// needs to recreate the database schema with language properties
self::kill_temp_db();
// store old defaults
$this->origTranslatableSettings['has_extension'] = singleton('SiteTree')->hasExtension('Translatable');
$this->origTranslatableSettings['default_locale'] = Translatable::default_locale();
self::$origTranslatableSettings['has_extension'] = singleton('SiteTree')->hasExtension('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(!$this->origTranslatableSettings['has_extension']) Object::add_extension('SiteTree', 'Translatable');
if(!self::$origTranslatableSettings['has_extension']) Object::add_extension('SiteTree', 'Translatable');
Object::add_extension('TranslatableTest_DataObject', 'Translatable');
// clear singletons, they're caching old extension info which is used in DatabaseAdmin->doBuild()
@ -50,21 +48,21 @@ class TranslatableSearchFormTest extends FunctionalTest {
$dbname = self::create_temp_db();
DB::set_alternative_database_name($dbname);
parent::setUp();
parent::set_up_once();
$holderPage = $this->objFromFixture('SiteTree', 'searchformholder');
$this->mockController = new ContentController($holderPage);
}
function tearDown() {
if(!$this->origTranslatableSettings['has_extension']) Object::remove_extension('SiteTree', 'Translatable');
static function tear_down_once() {
if(!self::$origTranslatableSettings['has_extension']) Object::remove_extension('SiteTree', 'Translatable');
Translatable::set_default_locale($this->origTranslatableSettings['default_locale']);
Translatable::set_default_locale(self::$origTranslatableSettings['default_locale']);
self::kill_temp_db();
self::create_temp_db();
parent::tearDown();
parent::tear_down_once();
}
function testPublishedPagesMatchedByTitleInDefaultLanguage() {