minor test enhancements

This commit is contained in:
Mike Macgirvin 2024-03-09 16:28:05 +11:00
parent 0e21c456e7
commit 4d1289ef15
5 changed files with 67 additions and 3 deletions

View file

@ -21,7 +21,7 @@ class JcsEddsa2022
$options = [
'type' => 'DataIntegrityProof',
'cryptosuite' => 'eddsa-jcs-2022',
'created' => datetime_convert(format: ATOM_TIME),
'created' => new Time(format: ATOM_TIME),
'verificationMethod' => Channel::url($channel) . '#' . $pubkey,
'proofPurpose' => 'assertionMethod',
];

60
Code/Lib/Time.php Normal file
View file

@ -0,0 +1,60 @@
<?php
namespace Code\Lib;
use DateTime;
use DateTimeZone;
use Exception;
class Time
{
protected $from = 'UTC';
protected $to = 'UTC';
protected $datetime;
protected $format;
public function __construct($from = 'UTC', $to = 'UTC', $datetime = 'now', $format = "Y-m-d H:i:s")
{
// Defaults to UTC if nothing is set, but throws an exception if set to empty string.
// Provide some sane defaults regardless.
if ($from === '') {
$from = 'UTC';
}
if ($to === '') {
$to = 'UTC';
}
if (($datetime === '') || (! is_string($datetime))) {
$datetime = 'now';
}
if (in_array($datetime, ['0000-00-00 00:00:00', '0001-01-01 00:00:00'])) {
$d = new DateTime('0001-01-01 00:00:00', new DateTimeZone('UTC'));
return $d->format($format);
}
try {
$from_obj = new DateTimeZone($from);
} catch (Exception $e) {
$from_obj = new DateTimeZone('UTC');
}
try {
$d = new DateTime($datetime, $from_obj);
} catch (Exception $e) {
logger('exception: ' . $e->getMessage());
$d = new DateTime('now', $from_obj);
}
try {
$to_obj = new DateTimeZone($to);
} catch (Exception $e) {
$to_obj = new DateTimeZone('UTC');
}
$d->setTimeZone($to_obj);
return($d->format($format));
}
}

View file

@ -91,7 +91,7 @@ function sys_boot() {
if (! App::$install) {
DBA::dba_factory($db_host, $db_port, $db_user, $db_pass, $db_data, $db_type, App::$install);
if (! DBA::$dba->connected) {
if (! DBA::$dba->connected && !defined('UNIT_TEST')) {
system_unavailable();
}

View file

@ -48,6 +48,10 @@ class DBA
self::$dba = null;
self::$dbtype = intval($dbtype);
if (defined('UNIT_TEST'))
return self::$dba;
}
if (self::$dbtype == DBTYPE_POSTGRES) {
if (!($port)) {
$port = 5432;

View file

@ -6,4 +6,4 @@ set_include_path(
. '../'
);
define('UNIT_TESTING', 1);
require_once('include/cli_startup.php');
require_once('boot.php');