Skip to content

Commit

Permalink
Implement TimecopDateTimeImmutable::__construct(),
Browse files Browse the repository at this point in the history
timecop_date_create_immutable(),
timecop_date_create_immutable_from_format()
/ Now timecop_date_create_from_format() returns DateTime instance
  • Loading branch information
hnw committed Jan 4, 2017
1 parent 3fece0b commit f5bcffb
Show file tree
Hide file tree
Showing 83 changed files with 1,510 additions and 607 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ var_dump($new_time == time()); // bool(false)

## Timecop Scale

`timecop_scale($scaling_factor)` make time move at an accelerated pace. It is convenient because you can reduce execution time of time-dependent unit test.
`timecop_scale($scaling_factor)` make time move at an accelerated pace. With this function, you can emulate long-span integration test and reduce execution time of time-dependent unit test.

```php
<?php
Expand All @@ -98,6 +98,10 @@ var_dump((new DateTime())->format("c")); // string(25) "2017-01-01T00:00:05+00:0

## CHANGELOG

### version 1.2.2(alpha), 2017/1/4
- Implement Implement `TimecopDateTimeImmutable` class and `timecop_date_create_immutable()`, `timecop_date_create_immutable_from_format()` functions.
- Now `timecop_date_create_from_format()` returns DateTime instance

### version 1.2.1(alpha), 2016/12/30
- Fix the year 2038 problem for PHP 7.x on 64bit Windows.

Expand Down
15 changes: 13 additions & 2 deletions php_timecop.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#ifndef PHP_TIMECOP_H
#define PHP_TIMECOP_H

#define PHP_TIMECOP_VERSION "1.2.1"
#define PHP_TIMECOP_VERSION "1.2.2"

extern zend_module_entry timecop_module_entry;
#define phpext_timecop_ptr &timecop_module_entry
Expand Down Expand Up @@ -64,10 +64,19 @@ PHP_FUNCTION(timecop_gettimeofday);
PHP_FUNCTION(timecop_unixtojd);
PHP_FUNCTION(timecop_date_create);
PHP_FUNCTION(timecop_date_create_from_format);
#if PHP_VERSION_ID >= 50500
PHP_FUNCTION(timecop_date_create_immutable);
PHP_FUNCTION(timecop_date_create_immutable_from_format);
#endif

PHP_METHOD(TimecopDateTime, __construct);
PHP_METHOD(TimecopOrigDateTime, __construct);

#if PHP_VERSION_ID >= 50500
PHP_METHOD(TimecopDateTimeImmutable, __construct);
PHP_METHOD(TimecopOrigDateTimeImmutable, __construct);
#endif

PHP_METHOD(Timecop, freeze);
PHP_METHOD(Timecop, travel);

Expand Down Expand Up @@ -95,9 +104,11 @@ ZEND_BEGIN_MODULE_GLOBALS(timecop)
tc_timeval travel_origin;
tc_timeval travel_offset;
tc_timeval_long scaling_factor;
zend_class_entry *ce_DateTime;
zend_class_entry *ce_DateTimeInterface;
zend_class_entry *ce_DateTime;
zend_class_entry *ce_TimecopDateTime;
zend_class_entry *ce_DateTimeImmutable;
zend_class_entry *ce_TimecopDateTimeImmutable;
ZEND_END_MODULE_GLOBALS(timecop)

#if ZEND_DEBUG
Expand Down
29 changes: 29 additions & 0 deletions tests-skipcheck.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
extension_loaded('timecop') or die('skip timecop module not available');
if (isset($required_version)) {
if (version_compare(PHP_VERSION, $required_version, "<")) {
die("skip PHP ${required_version}+ required for this test");
}
}
if (isset($required_func)) {
foreach ($required_func as $func_name) {
if (!function_exists($func_name)) {
die("skip $func_name() function is not available.");
}
}
}
if (isset($required_class)) {
foreach ($required_class as $class_name) {
if (!class_exists($class_name)) {
die("skip $class_name class is not available.");
}
}
}
if (isset($required_method)) {
foreach ($required_method as $v) {
list($class_name, $method_name) = $v;
if (!method_exists($class_name, $method_name)) {
die("skip $class_name::$method_name() method is not available.");
}
}
}
21 changes: 0 additions & 21 deletions tests/001.phpt

This file was deleted.

19 changes: 0 additions & 19 deletions tests/002.phpt

This file was deleted.

21 changes: 0 additions & 21 deletions tests/003.phpt

This file was deleted.

22 changes: 0 additions & 22 deletions tests/004.phpt

This file was deleted.

22 changes: 0 additions & 22 deletions tests/005.phpt

This file was deleted.

26 changes: 0 additions & 26 deletions tests/011.phpt

This file was deleted.

26 changes: 0 additions & 26 deletions tests/019.phpt

This file was deleted.

54 changes: 54 additions & 0 deletions tests/core_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
--TEST--
Check for timecop_freeze()
--SKIPIF--
<?php
$required_func = array("timecop_freeze");
$required_class = array("TimecopDateTime");
include(__DIR__."/../tests-skipcheck.inc.php");
--INI--
date.timezone=America/Los_Angeles
timecop.func_override=0
--FILE--
<?php
$dt1 = new DateTime("1970-01-01 00:00:00.000 GMT");
timecop_freeze($dt1);
$dt2 =new TimecopDateTime();

if (class_exists("DateTimeImmutable")) {
$dt3 = new DateTimeImmutable("1970-01-01 09:00:00.000 GMT");
timecop_freeze($dt3);
$dt4 =new TimecopDateTime();
} else {
$dt3 = new DateTime("1970-01-01 01:00:00.000");
$dt4 = new DateTime("1970-01-01 01:00:00.000");
}

$dt5 = new TimecopDateTime("2040-01-01 00:00:00.000 GMT");
timecop_freeze($dt5);

if (PHP_INT_SIZE === 8) {
$dt6 = new TimecopDateTime();
} else {
// force pass the test on 32bit environment
$dt6 = $dt5;
$dt6->setTimeZone(new DateTimezone("America/Los_Angeles"));
}

timecop_freeze(1);
$dt7 =new TimecopDateTime();

var_dump($dt2->format("Y-m-d H:i:s.u"));
var_dump($dt4->format("Y-m-d H:i:s.u"));
var_dump($dt6->format("Y-m-d H:i:s.u"));
var_dump($dt1 == $dt2);
var_dump($dt3 == $dt4);
var_dump($dt5 == $dt6);
var_dump($dt7->format("Y-m-d H:i:s.u"));
--EXPECT--
string(26) "1969-12-31 16:00:00.000000"
string(26) "1970-01-01 01:00:00.000000"
string(26) "2039-12-31 16:00:00.000000"
bool(true)
bool(true)
bool(true)
string(26) "1969-12-31 16:00:01.000000"
59 changes: 59 additions & 0 deletions tests/core_002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
--TEST--
Check for timecop_travel()
--SKIPIF--
<?php
$required_func = array("timecop_travel");
$required_class = array("DateTime", "TimecopDateTime");
$required_method = array();
include(__DIR__."/../tests-skipcheck.inc.php");
--INI--
date.timezone=America/Los_Angeles
timecop.func_override=0
--FILE--
<?php
$dt1 = new DateTime("1970-01-01 00:00:00.900 GMT");
timecop_travel($dt1);
usleep(100000);
$dt2 =new TimecopDateTime();

if (class_exists("DateTimeImmutable")) {
$dt3 = new DateTimeImmutable("1970-01-01 09:00:00.900 GMT");
timecop_travel($dt3);
usleep(100000);
$dt4 =new TimecopDateTime();
} else {
$dt3 = new DateTime("1970-01-01 01:00:00.900");
$dt4 = new DateTime("1970-01-01 01:00:01.050");
}

$dt5 = new TimecopDateTime("2040-01-01 00:00:00.900 GMT");
timecop_travel($dt5);
usleep(100000);
if (PHP_INT_SIZE === 8) {
$dt6 =new TimecopDateTime();
} else {
// always pass the test on 32bit environment
$dt6 = clone $dt5;
$dt6->modify("+1second");
$dt6->setTimeZone(new DateTimezone("America/Los_Angeles"));
}

timecop_travel(1);
sleep(1);
$dt7 =new TimecopDateTime();

var_dump($dt2->format("Y-m-d H:i:s"));
var_dump($dt4->format("Y-m-d H:i:s"));
var_dump($dt6->format("Y-m-d H:i:s"));
var_dump($dt1 < $dt2);
var_dump($dt3 < $dt4);
var_dump($dt5 < $dt6);
var_dump($dt7->format("Y-m-d H:i:s"));
--EXPECT--
string(19) "1969-12-31 16:00:01"
string(19) "1970-01-01 01:00:01"
string(19) "2039-12-31 16:00:01"
bool(true)
bool(true)
bool(true)
string(19) "1969-12-31 16:00:02"
26 changes: 26 additions & 0 deletions tests/core_003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Check for timecop_scale()
--SKIPIF--
<?php
$required_func = array("timecop_travel", "timecop_scale");
$required_class = array("TimecopDateTime");
$required_method = array();
include(__DIR__."/../tests-skipcheck.inc.php");
--INI--
date.timezone=America/Los_Angeles
timecop.func_override=0
--FILE--
<?php
$dt1 = new TimecopDateTime("1970-01-01 00:00:00.900 GMT");
timecop_travel($dt1);
timecop_scale(10);
usleep(110000);
$dt2 =new TimecopDateTime();
timecop_scale(20);
usleep(100000);
$dt3 =new TimecopDateTime();
var_dump($dt2->format("Y-m-d H:i:s"));
var_dump($dt3->format("Y-m-d H:i:s"));
--EXPECT--
string(19) "1969-12-31 16:00:02"
string(19) "1969-12-31 16:00:04"
Loading

0 comments on commit f5bcffb

Please sign in to comment.