activitypub-php/test/TestUtils/TestActivityPubObject.php

67 lines
1.7 KiB
PHP
Raw Normal View History

2019-02-14 03:27:47 +00:00
<?php /** @noinspection PhpUnhandledExceptionInspection */
namespace ActivityPub\Test\TestUtils;
use ActivityPub\Entities\ActivityPubObject;
use ActivityPub\Entities\Field;
use DateTime;
/**
* Like an ActivityPubObject, but with fixed timestamps for testing
*/
class TestActivityPubObject extends ActivityPubObject
{
private $fixedTime;
public function __construct( DateTime $time = null )
{
2019-02-16 17:51:24 +00:00
if ( !$time ) {
$time = self::getDefaultTime();
}
$this->fixedTime = $time;
parent::__construct( $time );
}
2019-02-16 17:51:24 +00:00
public static function getDefaultTime()
{
2019-02-16 17:51:24 +00:00
return DateTime::createFromFormat(
DateTime::RFC2822, 'Sun, 05 Jan 2014 21:31:40 GMT'
);
}
public static function fromArray( array $arr, DateTime $time = null )
{
2019-02-16 17:51:24 +00:00
if ( !$time ) {
$time = self::getDefaultTime();
}
$object = new TestActivityPubObject( $time );
foreach ( $arr as $name => $value ) {
if ( is_array( $value ) ) {
$child = self::fromArray( $value, $time );
TestField::withObject( $object, $name, $child, $time );
} else {
TestField::withValue( $object, $name, $value, $time );
}
}
return $object;
}
2019-02-16 17:51:24 +00:00
public function addField( Field $field, DateTime $time = null )
{
parent::addField( $field, $time );
$this->lastUpdated = $this->fixedTime;
}
public function removeField( Field $field, DateTime $time = null )
{
parent::removeField( $field, $time );
$this->lastUpdated = $this->fixedTime;
}
public function setLastUpdated( $lastUpdated )
{
// do not change lastUpdated
}
}