Issue
With CakePHP 3, I'm using the built in class Cake\I18n\Time
to calculate the difference in time from an API response and the current time. The results are getting stored for a unit test, but I don't know how to set what the current time is, so the expected results keep changing.
Here's the unit I'm trying to test:
$apiTime = Time::createFromTimestamp($apiTimestamp);
// Get the time the prediction was generated
$currentTime = Time::now();
return (int)(($apiTime->getTimestamp() - $currentTime->getTimestamp()) / 60);
So, is there any way that I can set the current time? That way I would be able to actually know what the expected results are.
Solution
(from http://book.cakephp.org/3.0/en/core-libraries/time.html#creating-time-instances)
In test cases you can mock out now()
using setTestNow()
:
// Fixate time.
$now = new Time('2014-04-12 12:22:30');
Time::setTestNow($now);
// Returns '2014-04-12 12:22:30'
$now = Time::now();
// Returns '2014-04-12 12:22:30'
$now = Time::parse('now');
Answered By - Ethan
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.