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, biapar, Willr, Ingo, swaiba, simon_w

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


Reply


2 Posts   717 Views

Avatar
swaiba

Forum Moderator, 1808 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, 1808 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!