mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
parent
515f7b1587
commit
228b976ce1
@ -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;
|
protected $fixtureDictionary;
|
||||||
|
|
||||||
@ -151,6 +162,9 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
|
|||||||
$this->originalIsRunningTest = null;
|
$this->originalIsRunningTest = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static function tear_down_once() {
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear the log of emails sent
|
* Clear the log of emails sent
|
||||||
*/
|
*/
|
||||||
|
32
dev/SapphireTestSuite.php
Normal file
32
dev/SapphireTestSuite.php
Normal 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();");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
@ -153,7 +153,7 @@ class TestRunner extends Controller {
|
|||||||
foreach($classList as $className) {
|
foreach($classList as $className) {
|
||||||
// Ensure that the autoloader pulls in the test class, as PHPUnit won't know how to do this.
|
// Ensure that the autoloader pulls in the test class, as PHPUnit won't know how to do this.
|
||||||
class_exists($className);
|
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
|
// Remove the error handler so that PHPUnit can add its own
|
||||||
|
@ -9,32 +9,30 @@ class TranslatableTest extends FunctionalTest {
|
|||||||
|
|
||||||
static $fixture_file = 'sapphire/tests/model/TranslatableTest.yml';
|
static $fixture_file = 'sapphire/tests/model/TranslatableTest.yml';
|
||||||
|
|
||||||
protected $recreateTempDb = true;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @todo Necessary because of monolithic Translatable design
|
* @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
|
// needs to recreate the database schema with language properties
|
||||||
self::kill_temp_db();
|
self::kill_temp_db();
|
||||||
|
|
||||||
// store old defaults
|
// store old defaults
|
||||||
$this->origTranslatableSettings['has_extension'] = singleton('SiteTree')->hasExtension('Translatable');
|
self::$origTranslatableSettings['has_extension'] = singleton('SiteTree')->hasExtension('Translatable');
|
||||||
$this->origTranslatableSettings['default_locale'] = Translatable::default_locale();
|
self::$origTranslatableSettings['default_locale'] = Translatable::default_locale();
|
||||||
|
|
||||||
// overwrite locale
|
// overwrite locale
|
||||||
Translatable::set_default_locale("en_US");
|
Translatable::set_default_locale("en_US");
|
||||||
|
|
||||||
// refresh the decorated statics - different fields in $db with Translatable enabled
|
// 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');
|
Object::add_extension('TranslatableTest_DataObject', 'Translatable');
|
||||||
|
|
||||||
// clear singletons, they're caching old extension info which is used in DatabaseAdmin->doBuild()
|
// clear singletons, they're caching old extension info which is used in DatabaseAdmin->doBuild()
|
||||||
global $_SINGLETONS;
|
global $_SINGLETONS;
|
||||||
$_SINGLETONS = array();
|
$_SINGLETONS = array();
|
||||||
|
|
||||||
// @todo Hack to refresh statics on the newly decorated classes
|
// @todo Hack to refresh statics on the newly decorated classes
|
||||||
$newSiteTree = new SiteTree();
|
$newSiteTree = new SiteTree();
|
||||||
foreach($newSiteTree->getExtensionInstances() as $extInstance) {
|
foreach($newSiteTree->getExtensionInstances() as $extInstance) {
|
||||||
@ -50,18 +48,18 @@ class TranslatableTest extends FunctionalTest {
|
|||||||
$dbname = self::create_temp_db();
|
$dbname = self::create_temp_db();
|
||||||
DB::set_alternative_database_name($dbname);
|
DB::set_alternative_database_name($dbname);
|
||||||
|
|
||||||
parent::setUp();
|
parent::set_up_once();
|
||||||
}
|
}
|
||||||
|
|
||||||
function tearDown() {
|
static function tear_down_once() {
|
||||||
if(!$this->origTranslatableSettings['has_extension']) Object::remove_extension('SiteTree', 'Translatable');
|
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::kill_temp_db();
|
||||||
self::create_temp_db();
|
self::create_temp_db();
|
||||||
|
|
||||||
parent::tearDown();
|
parent::tear_down_once();
|
||||||
}
|
}
|
||||||
|
|
||||||
function testTranslationGroups() {
|
function testTranslationGroups() {
|
||||||
@ -652,7 +650,6 @@ class TranslatableTest extends FunctionalTest {
|
|||||||
Translatable::set_reading_locale('en_US');
|
Translatable::set_reading_locale('en_US');
|
||||||
$_SERVER['HTTP_HOST'] = $_originalHost;
|
$_SERVER['HTTP_HOST'] = $_originalHost;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class TranslatableTest_DataObject extends DataObject implements TestOnly {
|
class TranslatableTest_DataObject extends DataObject implements TestOnly {
|
||||||
|
@ -9,26 +9,24 @@ class TranslatableSearchFormTest extends FunctionalTest {
|
|||||||
|
|
||||||
protected $mockController;
|
protected $mockController;
|
||||||
|
|
||||||
protected $recreateTempDb = true;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @todo Necessary because of monolithic Translatable design
|
* @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
|
// needs to recreate the database schema with language properties
|
||||||
self::kill_temp_db();
|
self::kill_temp_db();
|
||||||
|
|
||||||
// store old defaults
|
// store old defaults
|
||||||
$this->origTranslatableSettings['has_extension'] = singleton('SiteTree')->hasExtension('Translatable');
|
self::$origTranslatableSettings['has_extension'] = singleton('SiteTree')->hasExtension('Translatable');
|
||||||
$this->origTranslatableSettings['default_locale'] = Translatable::default_locale();
|
self::$origTranslatableSettings['default_locale'] = Translatable::default_locale();
|
||||||
|
|
||||||
// overwrite locale
|
// overwrite locale
|
||||||
Translatable::set_default_locale("en_US");
|
Translatable::set_default_locale("en_US");
|
||||||
|
|
||||||
// refresh the decorated statics - different fields in $db with Translatable enabled
|
// 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');
|
Object::add_extension('TranslatableTest_DataObject', 'Translatable');
|
||||||
|
|
||||||
// clear singletons, they're caching old extension info which is used in DatabaseAdmin->doBuild()
|
// 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();
|
$dbname = self::create_temp_db();
|
||||||
DB::set_alternative_database_name($dbname);
|
DB::set_alternative_database_name($dbname);
|
||||||
|
|
||||||
parent::setUp();
|
parent::set_up_once();
|
||||||
|
|
||||||
$holderPage = $this->objFromFixture('SiteTree', 'searchformholder');
|
$holderPage = $this->objFromFixture('SiteTree', 'searchformholder');
|
||||||
$this->mockController = new ContentController($holderPage);
|
$this->mockController = new ContentController($holderPage);
|
||||||
}
|
}
|
||||||
|
|
||||||
function tearDown() {
|
static function tear_down_once() {
|
||||||
if(!$this->origTranslatableSettings['has_extension']) Object::remove_extension('SiteTree', 'Translatable');
|
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::kill_temp_db();
|
||||||
self::create_temp_db();
|
self::create_temp_db();
|
||||||
|
|
||||||
parent::tearDown();
|
parent::tear_down_once();
|
||||||
}
|
}
|
||||||
|
|
||||||
function testPublishedPagesMatchedByTitleInDefaultLanguage() {
|
function testPublishedPagesMatchedByTitleInDefaultLanguage() {
|
||||||
|
Loading…
Reference in New Issue
Block a user