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() {