From 228b976ce1c38265ea0706d3fc508e6fd596a56c Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Wed, 22 Apr 2009 03:22:09 +0000 Subject: [PATCH] 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 --- dev/SapphireTest.php | 16 ++++++++++- dev/SapphireTestSuite.php | 32 +++++++++++++++++++++ dev/TestRunner.php | 2 +- tests/model/TranslatableTest.php | 31 +++++++++----------- tests/search/TranslatableSearchFormTest.php | 22 +++++++------- 5 files changed, 72 insertions(+), 31 deletions(-) create mode 100644 dev/SapphireTestSuite.php diff --git a/dev/SapphireTest.php b/dev/SapphireTest.php index e533648ee..6afeca74a 100644 --- a/dev/SapphireTest.php +++ b/dev/SapphireTest.php @@ -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 */ diff --git a/dev/SapphireTestSuite.php b/dev/SapphireTestSuite.php new file mode 100644 index 000000000..c9a17c7c0 --- /dev/null +++ b/dev/SapphireTestSuite.php @@ -0,0 +1,32 @@ +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();"); + } + } + } +} +?> \ No newline at end of file diff --git a/dev/TestRunner.php b/dev/TestRunner.php index f4fae3664..32b467368 100644 --- a/dev/TestRunner.php +++ b/dev/TestRunner.php @@ -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 diff --git a/tests/model/TranslatableTest.php b/tests/model/TranslatableTest.php index ce4fb3d7c..31a6a0727 100644 --- a/tests/model/TranslatableTest.php +++ b/tests/model/TranslatableTest.php @@ -9,32 +9,30 @@ 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() global $_SINGLETONS; $_SINGLETONS = array(); - + // @todo Hack to refresh statics on the newly decorated classes $newSiteTree = new SiteTree(); foreach($newSiteTree->getExtensionInstances() as $extInstance) { @@ -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 { diff --git a/tests/search/TranslatableSearchFormTest.php b/tests/search/TranslatableSearchFormTest.php index 85f497e59..3018ad3ac 100644 --- a/tests/search/TranslatableSearchFormTest.php +++ b/tests/search/TranslatableSearchFormTest.php @@ -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() {