function testInfectedClass() { TimeMachine::infectNamespace("TestNamespace"); TimeMachine::freeze(); TimeMachine::setNow("2009-03-07 14:00:00"); require_once __DIR__ . "/SUT.php"; $sut = new \TestNamespace\SUT(); $this->assertEquals("2009-03-07 14:00:00", \date("Y-m-d H:i:s", $sut->constructedAt())); }
function testAppendAndFlushing() { $flushContainer = new \stdClass(); $flushContainer->data = array(); $buffer = new \IO\Util\Buffer(); $buffer->registerConsumer(function ($_data) use($flushContainer) { $flushContainer->data = $_data; }); $buffer->append("TimeMachine"); TimeMachine::fastForward(4); //fast forward time by 4 seconds $buffer->append("rocks"); TimeMachine::fastForward(7); //fast forward time by 7 seconds $buffer->append("the world!"); //since we forwarded time more than 10 seconds our consumer should now have been called, //lets check this $this->assertEquals(3, count($flushContainer->data)); $this->assertEquals("TimeMachine", $flushContainer->data[0]); $this->assertEquals("rocks", $flushContainer->data[1]); $this->assertEquals("the world!", $flushContainer->data[2]); }
* Replacement for php's native microtime() method. * Should work exaclty as php's microtime() but be constrained to the time-machine. * * @param bool $_asFloat * @return float|null|string */ static function microtime($_asFloat = false) { if ($_asFloat == true) { if (self::$timeAnchor) { //calculate relative time $timePassed = \microtime(true) - self::$timeAnchor; return self::$now + $timePassed; } else { $mt = (double) \TimeDilation\TimeMachine::now(); return $mt; } } else { //we have to return the microtime as string $mt = explode(".", self::microtime(true)); if (!isset($mt[1])) { return "0.0 {$mt['0']}"; } $fraction = str_pad("0." . $mt[1], 10, "0"); return $fraction . " " . $mt[0]; } } } //now infect our own namespace so that time(), date() and strotime() behave properly TimeMachine::infectNamespace("TimeDilation");
function testBasics() { $now = "2028-08-29 17:28:49"; TimeMachine::setNow($now); $this->assertEquals($now, date("Y-m-d H:i:s")); sleep(1); $this->assertEquals("2028-08-29 17:28:50", date("Y-m-d H:i:s")); TimeMachine::freeze(); sleep(1); $this->assertEquals("2028-08-29 17:28:50", date("Y-m-d H:i:s")); TimeMachine::fastForward(10); $this->assertEquals("2028-08-29 17:29:00", date("Y-m-d H:i:s")); }