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()));
 }
Beispiel #2
0
     * 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");
Beispiel #3
0
<?php

/**
 * TimeDilation/TimeMachine - mocking time() for PHPUnit
 *
 * @copyright Daniel Haas 2013
 * @author Daniel Haas <*****@*****.**>
 *
 * This shows how the example BufferTest class could be tested with the
 * powers of TimeMachine.
 */
namespace TimeDilation;

require_once __DIR__ . "/../../../src/TimeDilation/TimeMachine.php";
TimeMachine::infectNamespace("IO\\Util");
//infect the namespace so that we can control time
require_once __DIR__ . "/Buffer.php";
class BufferTest extends \PHPUnit_Framework_TestCase
{
    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");