Skip to content

Time testing library inspired by ruby timecop gem (A project working with latest runkit7)

License

Notifications You must be signed in to change notification settings

runkit7/Timecop-PHP

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Timecop-PHP

Build Status

Supports php 7.0-7.2.

Time testing php library inspired by ruby timecop gem.

Timecop-PHP provides wrappers around date/time functions:

  • time (Timecop::time())
  • date (Timecop::date())
  • getdate (Timecop::getdate())
  • gmdate (Timecop::gmdate())
  • gmmktime (Timecop::gmmktime())
  • gmstrftime (Timecop::gmstrftime())
  • idate (Timecop::idate())
  • localtime (Timecop::localtime())
  • mktime (Timecop::mktime())
  • strftime (Timecop::strftime())
  • strptime (Timecop::strptime())
  • strtotime (Timecop::strtotime())

You can override built-in functions in cases where it's not possible to replace parts of code using PHP's built-in functions like time() or date() with Timecop wrappers. To do so runkit pecl extension (or runkit7/runkit7 fork) must be installed and working. See installation section for more details.

Usage

// override internal PHP functions
Timecop::warpTime();

// time travel
$presentTime = time();
Timecop::travel(time() - 3600); // one hour back

$presentTime > time(); // TRUE

Timecop::travel(time() + 7200); // one hour forward
$presentTime < time(); // TRUE

// freeze time
Timecop::freeze();
$frozenTime = time();
sleep(2);

$frozenTime == time(); // TRUE

// restore time - unfreezes time and returns to present
Timecop::restore();

$frozenTime == time(); // FALSE
$frozenTime > time(); // TRUE

// restore original PHP functionality
Timecop::unwarpTime();

Installation and Configuration

For php >= 7.0, a different fork must be used. (the runkit pecl does not support php 5)

$ git clone https://github.com/runkit7/runkit7
$ cd ./runkit
$ phpize
$ ./configure && make
$ make test
$ make install

For php 5.x (TODO: Restore $is_dst param)

$ git clone https://github.com/zenovich/runkit
$ cd ./runkit
$ phpize
$ ./configure && make
$ make test
$ make install

You need to enable runkit extension and override flag in php.ini:

extension=runkit.so
runkit.internal_override = 1

Examples

See tests/ReadmeTest.php

Running tests

php -d runkit.internal_override=1 vendor/bin/phpunit

About

Time testing library inspired by ruby timecop gem (A project working with latest runkit7)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 96.2%
  • Shell 3.8%