Skip to content
Snippets Groups Projects
Commit b92ef16b authored by Patrick Kenny's avatar Patrick Kenny
Browse files

Merge branch 'rector' into '8.x-1.x'

Require PHP 8.3

See merge request !26
parents 555d7a33 9776bbe4
No related branches found
No related tags found
No related merge requests found
Pipeline #321514 failed
......@@ -41,6 +41,7 @@ variables:
OPT_IN_TEST_NEXT_MINOR: '1'
OPT_IN_TEST_NEXT_MAJOR: '1'
OPT_IN_TEST_PREVIOUS_MAJOR: '1'
OPT_IN_TEST_PREVIOUS_MINOR: '1'
# _CURL_TEMPLATES_REF: 'main'
composer (previous major):
variables:
PHP_VERSION: '8.3'
......@@ -5,6 +5,9 @@
"license": "GPL-2.0+",
"minimum-stability": "dev",
"homepage": "https://www.drupal.org/project/datetime_testing",
"require": {
"php": "^8.3"
},
"require-dev": {
"drupal/drupal-extension": "^5.0",
"drush/drush": "^11.1 || ^12 || ^13"
......
......@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Drupal\datetime_testing\Drush\Commands;
use Drupal\Component\Datetime\DateTimePlus;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Datetime\DrupalDateTime;
use Drush\Commands\DrushCommands;
......@@ -14,26 +15,14 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*/
class TestTimeCommands extends DrushCommands {
/**
* Mock request time manager service.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected TimeInterface $testTime;
/**
* MockRequestTimeCommands constructor.
*
* @param \Drupal\Component\Datetime\TimeInterface $test_time
* Test time service.
*/
public function __construct(TimeInterface $test_time) {
public function __construct(
protected TimeInterface $testTime,
) {
parent::__construct();
$this->testTime = $test_time;
}
/**
* {@inheritdoc}
* DI for the Drush command.
*/
public static function create(ContainerInterface $container): self {
return new static(
......@@ -54,7 +43,7 @@ class TestTimeCommands extends DrushCommands {
* @command datetime-testing:set
*/
public function set(string $time, ?string $timezone = NULL): void {
$drupal_datetime = DrupalDateTime::createFromFormat(DrupalDateTime::FORMAT, $time, $timezone);
$drupal_datetime = DrupalDateTime::createFromFormat(DateTimePlus::FORMAT, $time, $timezone);
$timestamp = $drupal_datetime->getTimestamp();
$this->testTime->setTime($timestamp);
$logged_timezone = $drupal_datetime->getTimezone()->getName();
......@@ -71,7 +60,7 @@ class TestTimeCommands extends DrushCommands {
public function get(): void {
$time = $this->testTime->getCurrentTime();
$drupal_datetime = DrupalDateTime::createFromTimestamp($time);
$date = $drupal_datetime->format(DrupalDateTime::FORMAT);
$date = $drupal_datetime->format(DateTimePlus::FORMAT);
$logged_timezone = $drupal_datetime->getTimezone()->getName();
$this->logger()->success("Current time value is $date, timestamp: $time, timezone: $logged_timezone.");
}
......
......@@ -82,6 +82,7 @@ class TestDateTime extends DrupalDatetime {
/**
* {@inheritdoc}
*/
#[\Override]
protected function prepareTime($time): string {
// Break the string into an array using the standard php parsing logic.
$parsedDate = date_parse($time);
......@@ -184,13 +185,8 @@ class TestDateTime extends DrupalDatetime {
// If a time part is not supplied, fill in from the current time only if
// the day is also not supplied.
if ($filled[$part] === FALSE) {
if ($parsedDate['day'] === FALSE) {
$filled[$part] = $current->format($format);
}
else {
// Treat missing times to midnight, or exactly on the hour or minute.
$filled[$part] = '0';
}
// If FALSE, treat missing times to midnight, or exactly on the hour or minute.
$filled[$part] = $parsedDate['day'] === FALSE ? $current->format($format) : '0';
}
// PHP date formats require a leading zero for minute and second, but
// date_parse doesn't provide one.
......@@ -217,7 +213,7 @@ class TestDateTime extends DrupalDatetime {
// Build a string describing a time interval, in the format
// required by DateInterval:createFromString.
$intervalString = '';
foreach ($parts as $part => $format) {
foreach (array_keys($parts) as $part) {
// Treat empty increments as zero increments.
$increment = 0;
if (isset($relative[$part]) && $relative[$part]) {
......
......@@ -16,13 +16,6 @@ class TestTime implements TestTimeInterface {
use DependencySerializationTrait;
/**
* The normal time service being decorated.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected TimeInterface $realTime;
/**
* The key value service for this module.
*
......@@ -32,22 +25,17 @@ class TestTime implements TestTimeInterface {
const DATETIME_TESTING_STORE = 'datetime_testing';
/**
* Construct a TestTime object.
*
* @param \Drupal\Component\Datetime\TimeInterface $real_time
* A real (non-test) time object.
* @param \Drupal\Core\KeyValueStore\KeyValueFactory $key_value_factory
* The key value factory.
*/
public function __construct(TimeInterface $real_time, KeyValueFactoryInterface $key_value_factory) {
$this->realTime = $real_time;
public function __construct(
protected TimeInterface $realTime,
KeyValueFactoryInterface $key_value_factory,
) {
$this->keyValueStore = $key_value_factory->get(self::DATETIME_TESTING_STORE);
}
/**
* {@inheritdoc}
*/
#[\Override]
public function setTime(int|float|string $time): void {
if (!is_string($time) && !is_int($time) && !is_float($time)) {
throw new \Exception('Time to be set must be passed as a string, integer or float.');
......@@ -68,6 +56,7 @@ class TestTime implements TestTimeInterface {
/**
* {@inheritdoc}
*/
#[\Override]
public function resetTime(): void {
$this->keyValueStore->set('datetime_testing.specified_time', NULL);
$this->keyValueStore->set('datetime_testing.time_started', NULL);
......@@ -77,6 +66,7 @@ class TestTime implements TestTimeInterface {
/**
* {@inheritdoc}
*/
#[\Override]
public function freezeTime(): void {
// Do nothing if time already frozen.
if ($this->keyValueStore->get('datetime_testing.time_passing') !== FALSE) {
......@@ -88,6 +78,7 @@ class TestTime implements TestTimeInterface {
/**
* {@inheritdoc}
*/
#[\Override]
public function unfreezeTime(): void {
// Do nothing if time not frozen.
// If time_passing is null, don't set a start time.
......@@ -100,9 +91,10 @@ class TestTime implements TestTimeInterface {
/**
* {@inheritdoc}
*/
#[\Override]
public function getCurrentMicroTime(): float {
$specifiedTime = $this->keyValueStore->get('datetime_testing.specified_time');
$baseTime = !empty($specifiedTime) ? $specifiedTime : $this->realTime->getCurrentMicroTime();
$baseTime = empty($specifiedTime) ? $this->realTime->getCurrentMicroTime() : $specifiedTime;
$passed = $this->getMicroTimePassed();
$timeNow = $baseTime + $passed;
return $timeNow;
......@@ -111,6 +103,7 @@ class TestTime implements TestTimeInterface {
/**
* {@inheritdoc}
*/
#[\Override]
public function getCurrentTime(): int {
return (int) $this->getCurrentMicroTime();
}
......@@ -118,6 +111,7 @@ class TestTime implements TestTimeInterface {
/**
* {@inheritdoc}
*/
#[\Override]
public function getRequestMicroTime(): float {
$requestLag = $this->realTime->getCurrentMicroTime() - $this->realTime->getRequestMicroTime();
return $this->getCurrentMicroTime() - $requestLag;
......@@ -126,6 +120,7 @@ class TestTime implements TestTimeInterface {
/**
* {@inheritdoc}
*/
#[\Override]
public function getRequestTime(): int {
// In theory, we should be able to use getRequestMicroTime to get the same
// result but getting micro time on kernel tests is not possible at the
......
......@@ -91,10 +91,11 @@ class PinnedTimeTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
#[\Override]
protected function setUp(): void {
parent::setUp();
$this->requestStack = $this->createMock('Symfony\Component\HttpFoundation\RequestStack');
$this->requestStack = $this->createMock(RequestStack::class);
$this->normalTime = new Time($this->requestStack);
$this->testTime = new TestTime($this->normalTime, \Drupal::service('keyvalue'));
......@@ -113,6 +114,7 @@ class PinnedTimeTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
#[\Override]
protected function tearDown(): void {
$this->testTime->resetTime();
parent::tearDown();
......@@ -123,7 +125,7 @@ class PinnedTimeTest extends KernelTestBase {
*/
public function testDecoration(): void {
$service = \Drupal::time();
$this->assertEquals('Drupal\\datetime_testing\\TestTime', get_class($service));
$this->assertEquals(TestTime::class, $service::class);
}
/**
......
......@@ -13,28 +13,22 @@ use Drupal\datetime_testing\TestDateTime;
*/
class TestTestDateTime extends TestDateTime {
/**
* The current time.
*
* @var string
*/
protected string $fallbackTimezone;
/**
* {@inheritdoc}
*/
public function __construct(string $time = 'now', ?string $timezone = NULL, array $settings = [], string $fallbackTimezone = 'UTC') {
$this->fallbackTimezone = $fallbackTimezone;
// Instantiate the parent class.
public function __construct(
string $time = 'now',
?string $timezone = NULL,
array $settings = [],
protected string $fallbackTimezone = 'UTC',
) {
parent::__construct($time, $timezone, $settings);
}
/**
* {@inheritdoc}
*/
#[\Override]
protected function prepareTimezone(mixed $timezone): \DateTimeZone {
// Override the default timezone for testing purposes.
if (empty($timezone) && !empty($this->fallbackTimezone)) {
if (empty($timezone) && ($this->fallbackTimezone !== '' && $this->fallbackTimezone !== '0')) {
return new \DateTimeZone($this->fallbackTimezone);
}
else {
......
......@@ -46,6 +46,7 @@ class UnpinnedTimeTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
#[\Override]
protected function setUp(): void {
parent::setUp();
......@@ -57,6 +58,7 @@ class UnpinnedTimeTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
#[\Override]
protected function tearDown(): void {
$this->testTime->resetTime();
parent::tearDown();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment