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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

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   988 Views

Avatar
swaiba

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?

Avatar
swaiba

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" => '',
);

DB::connect($databaseConfig);

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

$databaseConfig = $oldConfig;
DB::connect($databaseConfig);

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

$mockDate = $this->Date.' 00:00:00';
SS_Datetime::set_mock_now($mockDate);

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