diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index b8c579c..e70f0e7 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2'] + php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3'] steps: - name: Checkout uses: actions/checkout@v2 diff --git a/.gitignore b/.gitignore index 0bf65d1..33acdc8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.* composer.lock phpcs.xml phpunit.xml diff --git a/composer.json b/composer.json index d30408a..820380b 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "mongodb/mongodb": "^1.0" }, "require-dev": { - "phpunit/phpunit": ">=6.5", + "phpunit/phpunit": "^6.5||^9.6", "squizlabs/php_codesniffer": "^3.7" }, "config": { diff --git a/src/Utilities/UTCDateTimeUtil.php b/src/Utilities/UTCDateTimeUtil.php index d530b16..97e4bb6 100644 --- a/src/Utilities/UTCDateTimeUtil.php +++ b/src/Utilities/UTCDateTimeUtil.php @@ -2,6 +2,7 @@ namespace SubjectivePHP\MongoDB\Utilities; +use DateTimeInterface; use MongoDB\BSON\UTCDateTime; abstract class UTCDateTimeUtil @@ -25,4 +26,24 @@ final public static function fromSeconds(int $seconds) : UTCDateTime $microSeconds = $seconds * 1000; return new UTCDateTime($microSeconds); } + + /** + * @param DateTimeInterface $dateTime The DateTime object from which a UTCDateTime object will be constructed. + * + * @return UTCDateTime + */ + final public static function fromDateTime(DateTimeInterface $dateTime): UTCDateTime + { + return self::fromSeconds($dateTime->getTimestamp()); + } + + /** + * @param string $dateTime A date/time string. + * + * @return UTCDateTime + */ + final public static function fromDateTimeString(string $dateTime): UTCDateTime + { + return self::fromSeconds(strtotime($dateTime)); + } } diff --git a/tests/Utilities/UTCDateTimeUtilTest.php b/tests/Utilities/UTCDateTimeUtilTest.php index f8e6b11..a27c20f 100644 --- a/tests/Utilities/UTCDateTimeUtilTest.php +++ b/tests/Utilities/UTCDateTimeUtilTest.php @@ -2,6 +2,7 @@ namespace SubjectivePHPTest\MongoDB\Utilities; +use DateTime; use PHPUnit\Framework\TestCase; use SubjectivePHP\MongoDB\Utilities\UTCDateTimeUtil; @@ -25,10 +26,32 @@ public function nowReturnsCurrentDateTime() * @test * @covers ::fromSeconds */ - public function fromSecondsReturnsExcpectedDateTime() + public function fromSecondsReturnsExpectedDateTime() { $expectedTimestamp = time(); $actual = UTCDateTimeUtil::fromSeconds($expectedTimestamp)->toDateTime()->getTimestamp(); $this->assertLessThanOrEqual($expectedTimestamp, $actual); } + + /** + * @test + * @covers ::fromDateTime + */ + public function fromDateTimeReturnsExpectedDateTime() + { + $dateTime = new DateTime(); + $actual = UTCDateTimeUtil::fromDateTime($dateTime)->toDateTime()->getTimestamp(); + $this->assertSame($dateTime->getTimestamp(), $actual); + } + + /** + * @test + * @covers ::fromDateTimeString + */ + public function fromDateTimeStringReturnsExpectedDateTime() + { + $dateTimeString = '2025-04-11 12:00:00'; + $actual = UTCDateTimeUtil::fromDateTimeString($dateTimeString)->toDateTime()->getTimestamp(); + $this->assertSame(strtotime($dateTimeString), $actual); + } }