Skip to content

Commit

Permalink
Library is updated to use PHPUnit 10
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyingDR committed Nov 4, 2023
1 parent 0011795 commit d71443d
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 91 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor/
/composer.lock
/.phpunit.result.cache
/.phpunit.cache/
33 changes: 16 additions & 17 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
colors="true"
bootstrap="vendor/autoload.php"
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
colors="true"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
>
<php>
<ini name="display_errors" value="1" />
<ini name="error_reporting" value="-1" />
<ini name="display_errors" value="1"/>
<ini name="error_reporting" value="-1"/>
</php>

<testsuites>
<testsuite name="Tests">
<directory>tests</directory>
</testsuite>
</testsuites>

<coverage processUncoveredFiles="true">
<coverage/>
<source>
<include>
<directory suffix=".php">src</directory>
<directory>src</directory>
</include>
<exclude>
<directory suffix=".php">src/PHPUnit</directory>
<directory>src/PHPUnit</directory>
</exclude>
</coverage>

<listeners>
<listener class="Flying\Date\PHPUnit\Listener\AdjustableDateListener"/>
</listeners>
</source>
<extensions>
<bootstrap class="Flying\Date\PHPUnit\Extension\DateExtension"/>
</extensions>
</phpunit>
23 changes: 23 additions & 0 deletions src/PHPUnit/Extension/DateExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php /** @noinspection DevelopmentDependenciesUsageInspection */

declare(strict_types=1);

namespace Flying\Date\PHPUnit\Extension;

use Flying\Date\PHPUnit\Extension\Subscriber\ReleaseDateAdjustment;
use Flying\Date\PHPUnit\Extension\Subscriber\SetupDateAdjustment;
use PHPUnit\Runner\Extension\Extension;
use PHPUnit\Runner\Extension\Facade;
use PHPUnit\Runner\Extension\ParameterCollection;
use PHPUnit\TextUI\Configuration\Configuration;

class DateExtension implements Extension
{
public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void
{
$facade->registerSubscribers(
new SetupDateAdjustment(),
new ReleaseDateAdjustment(),
);
}
}
40 changes: 40 additions & 0 deletions src/PHPUnit/Extension/Subscriber/ReleaseDateAdjustment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php /** @noinspection DevelopmentDependenciesUsageInspection */

declare(strict_types=1);

namespace Flying\Date\PHPUnit\Extension\Subscriber;

use Flying\Date\Date;
use PHPUnit\Event\Test\Errored;
use PHPUnit\Event\Test\ErroredSubscriber;
use PHPUnit\Event\Test\Failed;
use PHPUnit\Event\Test\FailedSubscriber;
use PHPUnit\Event\Test\MarkedIncomplete;
use PHPUnit\Event\Test\MarkedIncompleteSubscriber;
use PHPUnit\Event\Test\Passed;
use PHPUnit\Event\Test\PassedSubscriber;
use PHPUnit\Event\Test\PreparationFailed;
use PHPUnit\Event\Test\PreparationFailedSubscriber;
use PHPUnit\Event\Test\Skipped;
use PHPUnit\Event\Test\SkippedSubscriber;

class ReleaseDateAdjustment implements
PassedSubscriber,
MarkedIncompleteSubscriber,
SkippedSubscriber,
PreparationFailedSubscriber,
FailedSubscriber,
ErroredSubscriber
{
public function notify(Passed|MarkedIncomplete|Skipped|PreparationFailed|Failed|Errored $event): void
{
$this->release();
}

private function release(): void
{
Date::allowAdjustment(false);
Date::setTimezone();
Date::adjust();
}
}
48 changes: 48 additions & 0 deletions src/PHPUnit/Extension/Subscriber/SetupDateAdjustment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php /** @noinspection DevelopmentDependenciesUsageInspection */

declare(strict_types=1);

namespace Flying\Date\PHPUnit\Extension\Subscriber;

use Flying\Date\Date;
use Flying\Date\PHPUnit\Attribute\AdjustableDate;
use PHPUnit\Event\Code\TestMethod;
use PHPUnit\Event\Test\Prepared;
use PHPUnit\Event\Test\PreparedSubscriber;

class SetupDateAdjustment implements PreparedSubscriber
{
/** @var AdjustableDate[] */
private array $cache = [];

/**
* @throws \ReflectionException
*/
public function notify(Prepared $event): void
{
$test = $event->test();
if (!$test instanceof TestMethod) {
return;
}
$reflection = null;
$className = $test->className();
if (!array_key_exists($className, $this->cache)) {
$reflection ??= new \ReflectionClass($className);
$ar = $reflection->getAttributes(AdjustableDate::class)[0] ?? null;
$this->cache[$className] = $ar?->newInstance();
}
$method = $test->methodName();
$methodKey = $className . '::' . $method;
if (!array_key_exists($methodKey, $this->cache)) {
$reflection ??= new \ReflectionClass($className);
$ar = null;
if ($reflection->hasMethod($method)) {
$ar = $reflection->getMethod($method)->getAttributes(AdjustableDate::class)[0] ?? null;
}
$this->cache[$methodKey] = $ar?->newInstance();
}
Date::allowAdjustment($this->cache[$methodKey]?->isEnabled() ?? $this->cache[$className]?->isEnabled() ?? false);
Date::setTimezone($this->cache[$methodKey]?->getTimezone() ?? $this->cache[$className]?->getTimezone() ?? null);
Date::adjust();
}
}
50 changes: 0 additions & 50 deletions src/PHPUnit/Listener/AdjustableDateListener.php

This file was deleted.

48 changes: 24 additions & 24 deletions tests/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function testDefaultTimezoneIsUsedIfNotDefinedExplicitly(): void
$default = date_default_timezone_get();
self::assertEquals($default, Date::getTimezone()->getName());

$timezone = $this->getNonDefaultTimezone();
$timezone = self::getNonDefaultTimezone();
Date::setTimezone($timezone);
self::assertNotEquals($default, Date::getTimezone()->getName());
self::assertEquals($timezone, Date::getTimezone());
Expand All @@ -36,12 +36,12 @@ public function testNowReturnsCurrentDate(): void
{
$now = Date::now();
self::assertDateEquals(new \DateTimeImmutable(), $now);
self::assertEquals($this->getDefaultTimezone(), $now->getTimezone());
self::assertEquals(self::getDefaultTimezone(), $now->getTimezone());

Date::setTimezone($this->getNonDefaultTimezone());
Date::setTimezone(self::getNonDefaultTimezone());
$now = Date::now();
self::assertDateEquals(new \DateTimeImmutable(), $now);
self::assertEquals($this->getNonDefaultTimezone(), $now->getTimezone());
self::assertEquals(self::getNonDefaultTimezone(), $now->getTimezone());
}

public function testCreatingDatesFromDifferentFormats(): void
Expand Down Expand Up @@ -75,8 +75,8 @@ public function testCreatingDatesFromDifferentFormats(): void
public function testTimezoneAppliesAtTheTimeOfDateCreation(): void
{
$date = '2022-08-01T12:23:34Z';
$tz1 = $this->getDefaultTimezone();
$tz2 = $this->getNonDefaultTimezone();
$tz1 = self::getDefaultTimezone();
$tz2 = self::getNonDefaultTimezone();

Date::setTimezone($tz1);
self::assertEquals(
Expand All @@ -96,25 +96,25 @@ public function testDefaultTimezoneIsUsedUnlessPassedExplicitly(): void
$reference = $this->getReferenceDate();

$date = Date::from($reference);
self::assertEquals($this->getDefaultTimezone(), $date->getTimezone());
self::assertEquals(self::getDefaultTimezone(), $date->getTimezone());

Date::setTimezone($this->getNonDefaultTimezone());
Date::setTimezone(self::getNonDefaultTimezone());
$date = Date::from($reference);
self::assertEquals($this->getNonDefaultTimezone(), $date->getTimezone());
self::assertEquals(self::getNonDefaultTimezone(), $date->getTimezone());

Date::setTimezone($this->getDefaultTimezone()->getName());
Date::setTimezone(self::getDefaultTimezone()->getName());
$date = Date::from($reference);
self::assertEquals($this->getDefaultTimezone(), $date->getTimezone());
self::assertEquals(self::getDefaultTimezone(), $date->getTimezone());

Date::setTimezone();
$date = Date::from($reference);
self::assertEquals($this->getDefaultTimezone(), $date->getTimezone());
self::assertEquals(self::getDefaultTimezone(), $date->getTimezone());

$timezone = $this->getNonDefaultTimezone();
$timezone = self::getNonDefaultTimezone();
$date = Date::from($reference, $timezone);
self::assertEquals($timezone, $date->getTimezone());

$timezone = $this->getNonDefaultTimezone();
$timezone = self::getNonDefaultTimezone();
$date = Date::from($reference, $timezone->getName());
self::assertEquals($timezone, $date->getTimezone());
}
Expand All @@ -128,7 +128,7 @@ public function testDatesFromFormatWithoutTimezone(string $format, string $datet
self::assertEquals($datetime, $created->format($format));
}

public function dpDatesFromFormatWithoutTimezone(): array
public static function dpDatesFromFormatWithoutTimezone(): array
{
return [
['Y-m-d', '2022-08-01'],
Expand All @@ -146,7 +146,7 @@ public function testDatesFromFormatWithEmbeddedTimezone(string $format, string $
self::assertEquals($timezone, $created->getTimezone());
}

public function dpDatesFromFormatWithEmbeddedTimezone(): array
public static function dpDatesFromFormatWithEmbeddedTimezone(): array
{
return [
[\DateTimeInterface::ATOM, '2022-08-01T12:23:34-05:00', new \DateTimeZone('-05:00')],
Expand All @@ -164,11 +164,11 @@ public function testDatesFromFormatWithExplicitlyGivenTimezone(string $format, s
self::assertEquals($timezone, $created->getTimezone());
}

public function dpDatesFromFormatWithExplicitlyGivenTimezone(): array
public static function dpDatesFromFormatWithExplicitlyGivenTimezone(): array
{
return [
['Y-m-d H:i:s', '2022-08-01 12:23:34', $this->getDefaultTimezone()],
['Y-m-d H:i:s', '2022-08-01 12:23:34', $this->getNonDefaultTimezone()],
['Y-m-d H:i:s', '2022-08-01 12:23:34', self::getDefaultTimezone()],
['Y-m-d H:i:s', '2022-08-01 12:23:34', self::getNonDefaultTimezone()],
[\DateTimeInterface::ATOM, '2022-08-01T12:23:34Z', new \DateTimeZone('America/New_York')],
[\DateTimeInterface::ATOM, '2022-08-01T12:23:34+03:00', new \DateTimeZone('Asia/Singapore')],
];
Expand Down Expand Up @@ -229,7 +229,7 @@ public function testInvalidIntervalFormatStringsForAdjustmentThrowsException(str
Date::adjust($adjustment);
}

public function dpInvalidIntervalFormatStringsForAdjustmentThrowsException(): array
public static function dpInvalidIntervalFormatStringsForAdjustmentThrowsException(): array
{
return [
['P1X2Y3Z'],
Expand Down Expand Up @@ -298,7 +298,7 @@ public function testDateAdjustmentsIsOnlyAllowedWhenExplicitlyEnabled(): void
public function testAdjustedDateAlwaysHaveZeroMicroseconds(): void
{
Date::adjust('1 minute');
// It is necessary to run lots of attempts because it is pretty rare case
// It is necessary to run lots of attempts because it is a pretty rare case
for ($i = 0; $i < 100; $i++) {
$now = Date::now();
self::assertEquals('000000', $now->format('u'));
Expand All @@ -308,15 +308,15 @@ public function testAdjustedDateAlwaysHaveZeroMicroseconds(): void

private function getReferenceDate(): \DateTimeImmutable
{
return \DateTimeImmutable::createFromFormat(\DateTimeInterface::ATOM, '2022-08-01T12:23:34Z', $this->getDefaultTimezone());
return \DateTimeImmutable::createFromFormat(\DateTimeInterface::ATOM, '2022-08-01T12:23:34Z', self::getDefaultTimezone());
}

private function getDefaultTimezone(): \DateTimeZone
private static function getDefaultTimezone(): \DateTimeZone
{
return new \DateTimeZone(date_default_timezone_get());
}

private function getNonDefaultTimezone(): \DateTimeZone
private static function getNonDefaultTimezone(): \DateTimeZone
{
$timezone = date_default_timezone_get() !== 'UTC' ? 'UTC' : 'Europe/Moscow';
return new \DateTimeZone($timezone);
Expand Down

0 comments on commit d71443d

Please sign in to comment.