Move commented-out DateTime tests into their own class

This commit is contained in:
nathangray 2017-02-06 13:06:04 -07:00
parent 502caf4703
commit 4d6ba96115
2 changed files with 120 additions and 32 deletions

View File

@ -693,35 +693,3 @@ class DateTime extends \DateTime
}
}
DateTime::init();
/*
if (isset($_SERVER['SCRIPT_FILENAME']) && $_SERVER['SCRIPT_FILENAME'] == __FILE__) // some tests
{
// test timestamps/dates before 1970
foreach(array('19690811',-3600,'-119322000') as $ts)
{
try {
echo "<p>DateTime::to($ts,'Y-m-d H:i:s')=".DateTime::to($ts,'Y-m-d H:i:s')."</p>\n";
$et = new DateTime($ts);
echo "<p>DateTime($ts)->format('Y-m-d H:i:s')=".$et->format('Y-m-d H:i:s')."</p>\n";
$dt = new DateTime($ts);
echo "<p>DateTime($ts)->format('Y-m-d H:i:s')=".$dt->format('Y-m-d H:i:s')."</p>\n";
} catch(\Exception $e) {
echo "<p><b>Exception</b>: ".$e->getMessage()."</p>\n";
}
}
// user time is UTC
echo "<p>user timezone = ".($GLOBALS['egw_info']['user']['preferences']['common']['tz'] = 'UTC').", server timezone = ".date_default_timezone_get()."</p>\n";
$time = time();
echo "<p>time=$time=".date('Y-m-d H:i:s',$time)."(server) =".DateTime::server2user($time,'Y-m-d H:i:s')."(user) =".DateTime::server2user($time,'ts')."(user)=".date('Y-m-d H:i:s',DateTime::server2user($time,'ts'))."</p>\n";
echo "DateTime::to(array('full' => '20091020', 'hour' => 12, 'minute' => 0))='".DateTime::to(array('full' => '20091020', 'hour' => 12, 'minute' => 0))."'</p>\n";
$ts = DateTime::to(array('full' => '20091027', 'hour' => 10, 'minute' => 0),'ts');
echo "<p>2009-10-27 10h UTC timestamp=$ts --> server time = ".DateTime::user2server($ts,'')." --> user time = ".DateTime::server2user(DateTime::user2server($ts),'')."</p>\n";
$ts = DateTime::to(array('full' => '20090627', 'hour' => 10, 'minute' => 0),'ts');
echo "<p>2009-06-27 10h UTC timestamp=$ts --> server time = ".DateTime::user2server($ts,'')." --> user time = ".DateTime::server2user(DateTime::user2server($ts),'')."</p>\n";
}
*/

View File

@ -0,0 +1,120 @@
<?php
/**
* EGroupware Api: DateTime tests
*
* @link http://www.stylite.de
* @package api
* @author Nathan Gray
* @copyright (c) 2017 Nathan Gray
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
*/
namespace EGroupware\Api;
require_once realpath(__DIR__.'/../loader/common.php'); // autoloader & check_load_extension
use EGroupware\Api;
use PHPUnit_Framework_TestCase as TestCase;
/**
* Testing the Egroupware extension of DateTime
*
*/
class DateTimeTest extends TestCase {
static $usertime;
/**
* Work in server time, so tests match expectations
*/
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
static::$usertime = DateTime::$user_timezone;
// Set user time to server time for consistency
DateTime::setUserPrefs(date_default_timezone_get());
}
public static function tearDownAfterClass()
{
// Reset
DateTime::setUserPrefs(static::$usertime->getName());
}
/**
* Test that dates before 1970 are handled correctly
*/
public function testBefore1970()
{
$checklist = array(
// Use sub array instead of key/value pair to preserve type
// Timestamp Expected
array('19690811', '1969-08-11 00:00:00'),
array(-3600, '1969-12-31 23:00:00'),
array('-119322000', '1966-03-21 23:00:00')
);
foreach($checklist as $test)
{
list($ts, $expected) = $test;
$this->checkTimestamp($ts, $expected);
}
}
/**
* Check timezone conversion is sane - convert back and forth between same
* timezone
*/
public function testUserTimeEqualsServerTime()
{
$time = time();
$this->assertEquals($time, DateTime::server2user($time, 'ts'));
$this->assertEquals(date('Y-m-d H:i:s',$time), DateTime::server2user($time,'Y-m-d H:i:s'));
$this->assertEquals('2009-10-20, 12:00',DateTime::to(array('full' => '20091020', 'hour' => 12, 'minute' => 0)));
$ts = DateTime::to(array('full' => '20091027', 'hour' => 10, 'minute' => 0),'ts');
$this->assertEquals(DateTime::user2server($ts,''), DateTime::server2user(DateTime::user2server($ts),''));
$ts = DateTime::to(array('full' => '20090627', 'hour' => 10, 'minute' => 0),'ts');
$this->assertEquals(DateTime::user2server($ts,''), DateTime::server2user(DateTime::user2server($ts),''));
}
/**
* Test timezone conversion with actual changes
*/
public function testTimezoneConversion()
{
// Set server to UTC
$server_tz = date_default_timezone_get();
date_default_timezone_set('UTC');
// Set user to Berlin (UTC-1)
DateTime::setUserPrefs('Europe/Berlin');
$ts = DateTime::to(array('full' => '20091027', 'hour' => 10, 'minute' => 0),'ts');
$this->assertEquals('2009-10-27 09:00:00', DateTime::user2server($ts,'Y-m-d H:i:s'));
// Set user to Cape Verde (UTC+1)
DateTime::setUserPrefs('Atlantic/Cape_Verde');
$ts = DateTime::to(array('full' => '20091027', 'hour' => 10, 'minute' => 0),'ts');
$this->assertEquals('2009-10-27 11:00:00', DateTime::user2server($ts,'Y-m-d H:i:s'));
date_default_timezone_set($server_tz);
}
/**
* Check that a timestamp matches expectations
*
* @param string|int $ts Something that looks like a time
* @param string $expected Expected time, in Y-m-d H:i:s format
*/
protected function checkTimestamp($ts, $expected)
{
$fail_message = "Checking $ts = $expected";
$this->assertEquals($expected, DateTime::to($ts,'Y-m-d H:i:s'), $fail_message);
$dt = new DateTime($ts);
$this->assertEquals($expected, $dt->format('Y-m-d H:i:s'), $fail_message);
}
}