Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Dates, Created, Now() and automated testing....

Go to End

2 Posts   926 Views


Forum Moderator, 1899 Posts

17 December 2010 at 12:27pm

Edited: 17/12/2010 12:28pm

I am doing work on automated testing at the moment and there is large areas of the code that rely on the 'Created' field in tables. This, as far as I can tell, is always going to use now() in mysql to write it, how can I go about changing it so that it returns my "fake date" that I am using elsewhere.

To note I am currently using a very simple date class to get now (time()) and then overriding the object to allow setting a var and then returning that instead. Am I to do the same thing for MySQLDatabase? overide it (TestMySQLDatabase, function now())?

Are there other areas to do this for? Is there anyinformation on the how to do this for automated testing?


Forum Moderator, 1899 Posts

17 December 2010 at 10:50pm

ok so I have my own ...

class TestMySQLDatabase extends MySQLDatabase {

	function now(){
		global $dm;

		//return 'NOW()';

		return DateManager::mysqlDateQuoted($dm->getCurrentDateTime());


and I'm using it this way...

global $databaseConfig;
$oldConfig = $databaseConfig;

$databaseConfig = array(
	"type" => 'TestMySQLDatabase',
	"server" => 'localhost',
	"username" => $oldConfig['username'],
	"password" => $oldConfig['password'],
	"database" => $oldConfig['database'],
	"path" => '',


$do = new SomeDataObject();
$do->SomeField = $somevalue;

$databaseConfig = $oldConfig;

But this is the one I was looking for...

$mockDate = $this->Date.' 00:00:00';

DataObjects now with created & lastedited times under my control... Silverstripe again streets ahead!