Jump to:

23456 Posts in 18905 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Dates, Created, Now() and automated testing....

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

Page: 1
Go to End
Author Topic: 654 Views
  • swaiba
    Avatar
    Forum Moderator
    1788 Posts

    Dates, Created, Now() and automated testing.... Link to this post

    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?

  • swaiba
    Avatar
    Forum Moderator
    1788 Posts

    Re: Dates, Created, Now() and automated testing.... Link to this post

    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!

    654 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.