Skip to content

Commit

Permalink
DateTimeImmutable support added
Browse files Browse the repository at this point in the history
  • Loading branch information
tuscanicz committed Apr 3, 2018
1 parent 89a295c commit 95ea015
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use DateInterval;
use DateTime as DateTimePhp;
use DateTimeImmutable;
use DateTimeZone;
use Tuscanicz\DateTimeBundle\Date\Date;
use Tuscanicz\DateTimeBundle\Time\Time;
Expand Down Expand Up @@ -53,6 +54,11 @@ public function toDateTime(): DateTimePhp
return new DateTimePhp($this->toFormat('r'));
}

public function toDateTimeImmutable(): DateTimeImmutable
{
return DateTimeImmutable::createFromMutable($this->toDateTime());
}

public function isBetween(DateTime $start, DateTime $end): bool
{
$thisDateTime = $this->toDateTime();
Expand Down
7 changes: 4 additions & 3 deletions src/DateTimeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tuscanicz\DateTimeBundle;

use DateTime as DateTimePhp;
use DateTimeInterface;
use DateTimeZone;
use Tuscanicz\DateTimeBundle\Date\Date;
use Tuscanicz\DateTimeBundle\Time\Time;
Expand All @@ -20,7 +21,7 @@ public function now(string $timezone = self::TIMEZONE_GMT): DateTime
$timezonePhp = new DateTimeZone($timezone);
$datetimePhp = new DateTimePhp('now', $timezonePhp);

return $this->fromDateTimePhp($datetimePhp, $timezone);
return $this->fromDateTimeInterface($datetimePhp, $timezone);
}

public function fromTimestamp(int $timestamp, string $timezone = self::TIMEZONE_GMT): DateTime
Expand All @@ -29,10 +30,10 @@ public function fromTimestamp(int $timestamp, string $timezone = self::TIMEZONE_
$datetimePhp = new DateTimePhp('now', $timezonePhp);
$datetimePhp->setTimestamp($timestamp);

return $this->fromDateTimePhp($datetimePhp, $timezone);
return $this->fromDateTimeInterface($datetimePhp, $timezone);
}

private function fromDateTimePhp(DateTimePhp $dateTimePhp, string $timezone = DateTimeFactory::TIMEZONE_GMT): DateTime
public function fromDateTimeInterface(DateTimeInterface $dateTimePhp, string $timezone = DateTimeFactory::TIMEZONE_GMT): DateTime
{
return new DateTime(
new Date(
Expand Down
24 changes: 24 additions & 0 deletions tests/DateTimeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Tuscanicz\DateTimeBundle;

use DateTime as DateTimePhp;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use Tuscanicz\DateTimeBundle\Date\Date;
use Tuscanicz\DateTimeBundle\Time\Time;
Expand Down Expand Up @@ -36,4 +38,26 @@ public function testFromTimestamp(): void

self::assertEquals($expectedDateTime, $dateTime);
}

public function testFromDateTimeInterfaceWithDateTime(): void
{
$expectedDateTime = new DateTime(new Date(2009, 2, 13), new Time(23, 31, 30));
$dateTime = $this->dateTimeFactory->fromDateTimeInterface(
new DateTimePhp('2009-02-13 23:31:30'),
self::TIMEZONE_GMT
);

self::assertEquals($expectedDateTime, $dateTime);
}

public function testFromDateTimeInterfaceWithDateTimeImmutable(): void
{
$expectedDateTime = new DateTime(new Date(2009, 2, 13), new Time(23, 31, 30));
$dateTime = $this->dateTimeFactory->fromDateTimeInterface(
new DateTimeImmutable('2009-02-13 23:31:30'),
self::TIMEZONE_GMT
);

self::assertEquals($expectedDateTime, $dateTime);
}
}
32 changes: 30 additions & 2 deletions tests/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Tuscanicz\DateTimeBundle;

use DateTime as DateTimePhp;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use Tuscanicz\DateTimeBundle\Date\Date;
use Tuscanicz\DateTimeBundle\Time\Time;
Expand Down Expand Up @@ -85,7 +87,7 @@ public function testToTimestamp(): void
* @param bool $expectedResult
* @dataProvider isBetweenProvider
*/
public function testIsBetween(DateTime $now, DateTime $start, DateTime $end, bool $expectedResult)
public function testIsBetween(DateTime $now, DateTime $start, DateTime $end, bool $expectedResult): void
{
self::assertSame($expectedResult, $now->isBetween($start, $end));
}
Expand All @@ -104,7 +106,7 @@ public function isBetweenProvider(): array
* @param bool $expectedResult
* @dataProvider isSameAsDataProvider
*/
public function testIsSameAs(DateTime $oneDateTime, DateTime $secondDateTime, bool $expectedResult)
public function testIsSameAs(DateTime $oneDateTime, DateTime $secondDateTime, bool $expectedResult): void
{
self::assertSame($expectedResult, $oneDateTime->isSameAs($secondDateTime));
}
Expand Down Expand Up @@ -283,4 +285,30 @@ public function testAddMinutes(): void

self::assertEquals($expectedNewDateTime, $newDateTime);
}

public function testToDateTime(): void
{
$dateTime = new DateTime(
new Date(1987, 7, 31),
new Time(11, 19, 0)
);

$actualPhpDateTime = $dateTime->toDateTime();
$expectedNewDateTime = new DateTimePhp('1987-07-31 11:19:00');

self::assertEquals($expectedNewDateTime, $actualPhpDateTime);
}

public function testToDateTimeImmutable(): void
{
$dateTime = new DateTime(
new Date(1987, 7, 3),
new Time(11, 19, 3)
);

$actualPhpDateTime = $dateTime->toDateTimeImmutable();
$expectedNewDateTime = new DateTimeImmutable('1987-07-03 11:19:03');

self::assertEquals($expectedNewDateTime, $actualPhpDateTime);
}
}

0 comments on commit 95ea015

Please sign in to comment.