From a79be5e6976c2f8c1864b67f7121b331ae283058 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 10 Dec 2022 22:39:09 -0500 Subject: [PATCH] Add Clock classes to deal with "now" times in tests - [Composer] Add psr/clock dependency --- composer.json | 3 ++- composer.lock | 46 +++++++++++++++++++++++++++++++- src/Util/Clock/FrozenClock.php | 47 +++++++++++++++++++++++++++++++++ src/Util/Clock/SystemClock.php | 48 ++++++++++++++++++++++++++++++++++ static/dependencies.config.php | 3 +++ 5 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 src/Util/Clock/FrozenClock.php create mode 100644 src/Util/Clock/SystemClock.php diff --git a/composer.json b/composer.json index 0e3e42c721..c6746c1231 100644 --- a/composer.json +++ b/composer.json @@ -71,7 +71,8 @@ "npm-asset/perfect-scrollbar": "0.6.16", "npm-asset/textcomplete": "^0.18.2", "npm-asset/typeahead.js": "^0.11.1", - "kornrunner/blurhash": "^1.2" + "kornrunner/blurhash": "^1.2", + "psr/clock": "^1.0" }, "repositories": [ { diff --git a/composer.lock b/composer.lock index 9a4854f15c..63f8a0d5b0 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f8e7baec685d20e6aee56978c275d64c", + "content-hash": "5af9ac9003f4653f3aa1860dd5a4d821", "packages": [ { "name": "asika/simple-console", @@ -3329,6 +3329,50 @@ ], "time": "2016-08-06T20:24:11+00:00" }, + { + "name": "psr/clock", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/clock.git", + "reference": "e41a24703d4560fd0acb709162f73b8adfc3aa0d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/clock/zipball/e41a24703d4560fd0acb709162f73b8adfc3aa0d", + "reference": "e41a24703d4560fd0acb709162f73b8adfc3aa0d", + "shasum": "" + }, + "require": { + "php": "^7.0 || ^8.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Psr\\Clock\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common interface for reading the clock.", + "homepage": "https://github.com/php-fig/clock", + "keywords": [ + "clock", + "now", + "psr", + "psr-20", + "time" + ], + "time": "2022-11-25T14:36:26+00:00" + }, { "name": "psr/container", "version": "1.1.1", diff --git a/src/Util/Clock/FrozenClock.php b/src/Util/Clock/FrozenClock.php new file mode 100644 index 0000000000..aa9b30142d --- /dev/null +++ b/src/Util/Clock/FrozenClock.php @@ -0,0 +1,47 @@ +. + * + */ + +namespace Friendica\Util\Clock; + +use DateTimeImmutable; + +/** + * Inspired by lcobucci/clock + * @see https://github.com/lcobucci/clock + */ +final class FrozenClock implements \Psr\Clock\ClockInterface +{ + /** @var DateTimeImmutable */ + private $now; + + public function __construct(DateTimeImmutable $now = null) + { + $this->now = $now ?? new DateTimeImmutable('now', new \DateTimeZone('UTC')); + } + + /** + * @inheritDoc + */ + public function now(): DateTimeImmutable + { + return $this->now; + } +} diff --git a/src/Util/Clock/SystemClock.php b/src/Util/Clock/SystemClock.php new file mode 100644 index 0000000000..900013c60c --- /dev/null +++ b/src/Util/Clock/SystemClock.php @@ -0,0 +1,48 @@ +. + * + */ + +namespace Friendica\Util\Clock; + +use DateTimeImmutable; +use DateTimeZone; + +/** + * Inspired by lcobucci/clock + * @see https://github.com/lcobucci/clock + */ +final class SystemClock implements \Psr\Clock\ClockInterface +{ + /** @var DateTimeZone */ + private $timezone; + + public function __construct(DateTimeZone $timezone = null) + { + $this->timezone = $timezone ?? new DateTimeZone('UTC'); + } + + /** + * @inheritDoc + */ + public function now(): DateTimeImmutable + { + return new DateTimeImmutable('now', $this->timezone); + } +} diff --git a/static/dependencies.config.php b/static/dependencies.config.php index f7f98bb676..a7de89d614 100644 --- a/static/dependencies.config.php +++ b/static/dependencies.config.php @@ -264,5 +264,8 @@ return [ 'constructParams' => [ $_SERVER ], + ], + \Psr\Clock\ClockInterface::class => [ + 'instanceOf' => Util\Clock\SystemClock::class ] ];