From 3e4130cad155ac2262455ff0ecd89a18990d6ad0 Mon Sep 17 00:00:00 2001 From: Markus Poerschke Date: Sun, 27 Sep 2020 18:23:21 +0200 Subject: [PATCH] Avoid using named constructors (#181) --- README.md | 6 ++-- docs/components/event.md | 26 ++++++++-------- docs/index.md | 6 ++-- examples/example1.php | 10 ++---- examples/example3.php | 2 +- src/Domain/Entity/Event.php | 8 ++--- src/Domain/ValueObject/Date.php | 22 ------------- src/Domain/ValueObject/GeographicPosition.php | 7 +---- src/Domain/ValueObject/MultiDay.php | 5 --- src/Domain/ValueObject/PointInTime.php | 10 ++++-- src/Domain/ValueObject/SingleDay.php | 7 +---- src/Domain/ValueObject/TimeSpan.php | 2 +- src/Domain/ValueObject/Timestamp.php | 22 ------------- src/Domain/ValueObject/UniqueIdentifier.php | 5 --- src/Presentation/Component.php | 6 ++-- .../Property/Value/DateTimeValue.php | 30 ++++++++++++++---- .../Component/Property/Value/DateValue.php | 9 ++---- .../Component/Property/Value/TextValue.php | 5 --- src/Presentation/ContentLine.php | 7 +---- src/Presentation/Factory/EventFactory.php | 12 +++---- src/Util/DateTimeImmutableFactory.php | 31 +++++++++++++++++++ tests/Integration/EventsGeneratorTest.php | 6 ++-- .../ValueObject/GeographicPositionTest.php | 2 +- tests/Unit/Presentation/ContentLineTest.php | 4 +-- .../Factory/CalendarFactoryTest.php | 6 ++-- .../Presentation/Factory/EventFactoryTest.php | 20 ++++++------ .../Util/DateTimeImmutableFactoryTest.php | 28 +++++++++++++++++ 27 files changed, 149 insertions(+), 155 deletions(-) create mode 100644 src/Util/DateTimeImmutableFactory.php create mode 100644 tests/Unit/Util/DateTimeImmutableFactoryTest.php diff --git a/README.md b/README.md index d2c8cac1..f5192e7b 100644 --- a/README.md +++ b/README.md @@ -100,15 +100,15 @@ $event = (new Eluceo\iCal\Domain\Entity\Event()) ->setSummary('Christmas Eve') ->setDescription('Lorem Ipsum Dolor...') ->setOccurrence( - Eluceo\iCal\Domain\ValueObject\SingleDay::fromDate( - Eluceo\iCal\Domain\ValueObject\Date::fromDateTimeInterface( + new Eluceo\iCal\Domain\ValueObject\SingleDay( + new Eluceo\iCal\Domain\ValueObject\Date( \DateTimeImmutable::createFromFormat('Y-m-d', '2030-12-24') ) ) ); // 2. Create Calendar domain entity -$calendar = Eluceo\iCal\Domain\Entity\Calendar::create([$event]); +$calendar = new Eluceo\iCal\Domain\Entity\Calendar([$event]); // 3. Transform domain entity into an iCalendar component $componentFactory = new Eluceo\iCal\Presentation\Factory\CalendarFactory(); diff --git a/docs/components/event.md b/docs/components/event.md index 13c2f235..f9d9725e 100644 --- a/docs/components/event.md +++ b/docs/components/event.md @@ -29,7 +29,7 @@ use Eluceo\iCal\Domain\ValueObject\SingleDay; $event = (new Event()) ->setSummary('Lunch Meeting') ->setDescription('Lorem Ipsum...') - ->setOccurrence(SingleDay::fromDate(Date::fromCurrentDay())); + ->setOccurrence(new SingleDay(new Date())); ``` ## Properties @@ -74,7 +74,7 @@ use Eluceo\iCal\Domain\ValueObject\Timestamp; use Eluceo\iCal\Domain\Entity\Event; $event = new Event(); -$event->touch(Timestamp::fromCurrentTime()); +$event->touch(new Timestamp()); ``` A timestamp object can be also created from an object that implements `\DateTimeInterface` like this: @@ -85,7 +85,7 @@ use Eluceo\iCal\Domain\ValueObject\Timestamp; $event = new Event(); $dateTime = DateTimeImmutable::createFromFormat('Y-m-d', '2019-12-24'); -$timestamp = Timestamp::fromDateTimeInterface($dateTime); +$timestamp = new Timestamp($dateTime); $event->touch($timestamp); ``` @@ -131,8 +131,8 @@ use Eluceo\iCal\Domain\ValueObject\SingleDay; use Eluceo\iCal\Domain\ValueObject\Date; use Eluceo\iCal\Domain\Entity\Event; -$date = Date::fromDateTimeInterface(DateTimeImmutable::createFromFormat('Y-m-d', '2019-12-24')); -$occurrence = SingleDay::fromDate($date); +$date = new Date(DateTimeImmutable::createFromFormat('Y-m-d', '2019-12-24')); +$occurrence = new SingleDay($date); $event = new Event(); $event->setOccurrence($occurrence); @@ -143,7 +143,7 @@ $event->setOccurrence($occurrence); A multi day event will take place on more than one consecutive day. The multi day occurrence defines a span of days. -The named constructor `MultiDay::fromDates()` accepts two dates: +The constructor `MultiDay($firstDay, $lastDay)` accepts two dates: - The `$firstDay` attribute defines the first inclusive day, the event will take place. - The `$lastDay` attribute defines the last inclusive day, the event will take place. @@ -155,9 +155,9 @@ use Eluceo\iCal\Domain\ValueObject\MultiDay; use Eluceo\iCal\Domain\ValueObject\Date; use Eluceo\iCal\Domain\Entity\Event; -$firstDay = Date::fromDateTimeInterface(DateTimeImmutable::createFromFormat('Y-m-d', '2019-12-24')); -$lastDay = Date::fromDateTimeInterface(DateTimeImmutable::createFromFormat('Y-m-d', '2019-12-26')); -$occurrence = MultiDay::fromDates($firstDay, $lastDay); +$firstDay = new Date(DateTimeImmutable::createFromFormat('Y-m-d', '2019-12-24')); +$lastDay = new Date(DateTimeImmutable::createFromFormat('Y-m-d', '2019-12-26')); +$occurrence = new MultiDay($firstDay, $lastDay); $event = new Event(); $event->setOccurrence($occurrence); @@ -178,9 +178,9 @@ use Eluceo\iCal\Domain\ValueObject\TimeSpan; use Eluceo\iCal\Domain\ValueObject\DateTime; use Eluceo\iCal\Domain\Entity\Event; -$start = DateTime::fromDateTimeInterface(DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-03 13:00:00')); -$end = DateTime::fromDateTimeInterface(DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-03 14:00:00')); -$occurrence = TimeSpan::create($start, $end); +$start = new DateTime(DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-03 13:00:00')); +$end = new DateTime(DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-03 14:00:00')); +$occurrence = new TimeSpan($start, $end); $event = new Event(); $event->setOccurrence($occurrence); @@ -202,7 +202,7 @@ use Eluceo\iCal\Domain\ValueObject\GeographicPosition; $location = new Location('North Pole'); // optionally a location with a geographical position can be created -$location = $location->withGeographicPosition(GeographicPosition::fromLatitudeAndLongitude(64.751111,147.349444)); +$location = $location->withGeographicPosition(new GeographicPosition(64.751111, 147.349444)); $event = new Event(); ``` diff --git a/docs/index.md b/docs/index.md index 3e65f2e6..3c39ff71 100644 --- a/docs/index.md +++ b/docs/index.md @@ -27,10 +27,8 @@ $event = new Event(); $event ->setSummary('Christmas Eve') ->setOccurrence( - SingleDay::fromDate( - Date::fromDateTimeInterface( - DateTimeImmutable::createFromFormat('Y-m-d', '2030-12-24') - ) + new SingleDay( + new Date(DateTimeImmutable::createFromFormat('Y-m-d', '2030-12-24')) ) ); diff --git a/examples/example1.php b/examples/example1.php index e6a38fe1..caa07486 100644 --- a/examples/example1.php +++ b/examples/example1.php @@ -26,13 +26,9 @@ ->setSummary('Christmas Eve') ->setDescription('Lorem Ipsum Dolor...') ->setOccurrence( - TimeSpan::create( - DateTime::fromDateTimeInterface( - DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2030-12-24 13:30:00') - ), - DateTime::fromDateTimeInterface( - DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2030-12-24 14:30:00') - ) + new TimeSpan( + new DateTime(DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2030-12-24 13:30:00')), + new DateTime(DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2030-12-24 14:30:00')) ) ) ; diff --git a/examples/example3.php b/examples/example3.php index 4d56b6eb..3d2063f4 100644 --- a/examples/example3.php +++ b/examples/example3.php @@ -34,7 +34,7 @@ for ($i = 0; $i < 10; ++$i) { yield (new Event()) ->setSummary('Event ' . $i) - ->setOccurrence(SingleDay::fromDate(Date::fromDateTimeInterface($day))) + ->setOccurrence(new SingleDay(new Date($day))) ; $day = $day->add($dayInterval); } diff --git a/src/Domain/Entity/Event.php b/src/Domain/Entity/Event.php index d6d98532..9049be4e 100644 --- a/src/Domain/Entity/Event.php +++ b/src/Domain/Entity/Event.php @@ -28,7 +28,7 @@ class Event public function __construct(?UniqueIdentifier $uniqueIdentifier = null) { $this->uniqueIdentifier = $uniqueIdentifier ?? UniqueIdentifier::createRandom(); - $this->touchedAt = Timestamp::fromCurrentTime(); + $this->touchedAt = new Timestamp(); } public function getUniqueIdentifier(): ?UniqueIdentifier @@ -43,11 +43,7 @@ public function getTouchedAt(): Timestamp public function touch(?Timestamp $dateTime = null): self { - if ($dateTime === null) { - $dateTime = Timestamp::fromCurrentTime(); - } - - $this->touchedAt = $dateTime; + $this->touchedAt = $dateTime ?? new Timestamp(); return $this; } diff --git a/src/Domain/ValueObject/Date.php b/src/Domain/ValueObject/Date.php index 0ef71de2..5c12a7ba 100644 --- a/src/Domain/ValueObject/Date.php +++ b/src/Domain/ValueObject/Date.php @@ -12,32 +12,10 @@ namespace Eluceo\iCal\Domain\ValueObject; use DateInterval; -use DateTimeImmutable as PhpDateTimeImmutable; -use DateTimeInterface as PhpDateTimeInterface; use InvalidArgumentException; -use RuntimeException; final class Date extends PointInTime { - public static function fromDateTimeInterface(PhpDateTimeInterface $dateTime): self - { - $dateTime = PhpDateTimeImmutable::createFromFormat( - PhpDateTimeInterface::ATOM, - $dateTime->format(PhpDateTimeInterface::ATOM), $dateTime->getTimezone() - ); - - if ($dateTime === false) { - throw new RuntimeException('Unexpected date time value.'); - } - - return new self($dateTime); - } - - public static function fromCurrentDay(): self - { - return self::fromDateTimeInterface(new PhpDateTimeImmutable()); - } - public function add(DateInterval $interval): self { if ( diff --git a/src/Domain/ValueObject/GeographicPosition.php b/src/Domain/ValueObject/GeographicPosition.php index 6d2aae3b..7a25de18 100644 --- a/src/Domain/ValueObject/GeographicPosition.php +++ b/src/Domain/ValueObject/GeographicPosition.php @@ -18,7 +18,7 @@ final class GeographicPosition private float $latitude; private float $longitude; - private function __construct(float $latitude, float $longitude) + public function __construct(float $latitude, float $longitude) { $this->latitude = $latitude; $this->longitude = $longitude; @@ -32,11 +32,6 @@ private function __construct(float $latitude, float $longitude) } } - public static function fromLatitudeAndLongitude(float $latitude, float $longitude): self - { - return new static($latitude, $longitude); - } - public function getLatitude(): float { return $this->latitude; diff --git a/src/Domain/ValueObject/MultiDay.php b/src/Domain/ValueObject/MultiDay.php index 3c7beb51..f63ab6eb 100644 --- a/src/Domain/ValueObject/MultiDay.php +++ b/src/Domain/ValueObject/MultiDay.php @@ -22,11 +22,6 @@ public function __construct(Date $firstDay, Date $lastDay) $this->lastDay = $lastDay; } - public static function fromDates(Date $firstDay, Date $lastDay): self - { - return new static($firstDay, $lastDay); - } - public function getFirstDay(): Date { return $this->firstDay; diff --git a/src/Domain/ValueObject/PointInTime.php b/src/Domain/ValueObject/PointInTime.php index 8765c7c2..9a95eb4e 100644 --- a/src/Domain/ValueObject/PointInTime.php +++ b/src/Domain/ValueObject/PointInTime.php @@ -13,6 +13,8 @@ use DateInterval; use DateTimeImmutable as PhpDateTimeImmutable; +use DateTimeInterface as PhpDateTimeInterface; +use Eluceo\iCal\Util\DateTimeImmutableFactory; /** * @internal @@ -21,9 +23,13 @@ abstract class PointInTime { private PhpDateTimeImmutable $dateTime; - final protected function __construct(PhpDateTimeImmutable $dateTime) + public function __construct(PhpDateTimeInterface $dateTime = null) { - $this->dateTime = $dateTime; + if ($dateTime === null) { + $dateTime = new PhpDateTimeImmutable(); + } + + $this->dateTime = DateTimeImmutableFactory::createFromInterface($dateTime); } public function getDateTime(): PhpDateTimeImmutable diff --git a/src/Domain/ValueObject/SingleDay.php b/src/Domain/ValueObject/SingleDay.php index e5c4b22f..d2639c93 100644 --- a/src/Domain/ValueObject/SingleDay.php +++ b/src/Domain/ValueObject/SingleDay.php @@ -15,7 +15,7 @@ final class SingleDay extends Occurrence { private Date $date; - private function __construct(Date $date) + public function __construct(Date $date) { $this->date = $date; } @@ -24,9 +24,4 @@ public function getDate(): Date { return $this->date; } - - public static function fromDate(Date $date): self - { - return new static($date); - } } diff --git a/src/Domain/ValueObject/TimeSpan.php b/src/Domain/ValueObject/TimeSpan.php index 57bc1d19..1564e93d 100644 --- a/src/Domain/ValueObject/TimeSpan.php +++ b/src/Domain/ValueObject/TimeSpan.php @@ -16,7 +16,7 @@ final class TimeSpan extends Occurrence private DateTime $begin; private DateTime $end; - private function __construct(DateTime $begin, DateTime $end) + public function __construct(DateTime $begin, DateTime $end) { $this->begin = $begin; $this->end = $end; diff --git a/src/Domain/ValueObject/Timestamp.php b/src/Domain/ValueObject/Timestamp.php index 4ddd856a..3d297703 100644 --- a/src/Domain/ValueObject/Timestamp.php +++ b/src/Domain/ValueObject/Timestamp.php @@ -11,28 +11,6 @@ namespace Eluceo\iCal\Domain\ValueObject; -use DateTimeImmutable as PhpDateTimeImmutable; -use DateTimeInterface as PhpDateTimeInterface; -use RuntimeException; - class Timestamp extends PointInTime { - public static function fromDateTimeInterface(PhpDateTimeInterface $dateTime): self - { - $dateTime = PhpDateTimeImmutable::createFromFormat( - PhpDateTimeInterface::ATOM, - $dateTime->format(PhpDateTimeInterface::ATOM), $dateTime->getTimezone() - ); - - if ($dateTime === false) { - throw new RuntimeException('Unexpected date time value.'); - } - - return new static($dateTime); - } - - public static function fromCurrentTime(): self - { - return static::fromDateTimeInterface(new PhpDateTimeImmutable()); - } } diff --git a/src/Domain/ValueObject/UniqueIdentifier.php b/src/Domain/ValueObject/UniqueIdentifier.php index f9477c12..f3dad0e0 100644 --- a/src/Domain/ValueObject/UniqueIdentifier.php +++ b/src/Domain/ValueObject/UniqueIdentifier.php @@ -23,11 +23,6 @@ public function __construct(string $uid) $this->uid = $uid; } - public static function fromString(string $uid): self - { - return new static($uid); - } - public static function createRandom(): self { if (function_exists('uuid_create') && defined('UUID_TYPE_RANDOM')) { diff --git a/src/Presentation/Component.php b/src/Presentation/Component.php index 2b792d0c..7404b16b 100644 --- a/src/Presentation/Component.php +++ b/src/Presentation/Component.php @@ -57,15 +57,15 @@ public function getIterator() protected function getContentLines(): Generator { - yield ContentLine::fromString('BEGIN:' . $this->componentName); + yield new ContentLine('BEGIN:' . $this->componentName); yield from $this->getContentLinesGenerator(); - yield ContentLine::fromString('END:' . $this->componentName); + yield new ContentLine('END:' . $this->componentName); } protected function getContentLinesGenerator(): Generator { yield from array_map( - [ContentLine::class, 'fromString'], + fn (string $string) => new ContentLine($string), array_map('strval', $this->properties) ); } diff --git a/src/Presentation/Component/Property/Value/DateTimeValue.php b/src/Presentation/Component/Property/Value/DateTimeValue.php index de4ab09d..d8de6b7e 100644 --- a/src/Presentation/Component/Property/Value/DateTimeValue.php +++ b/src/Presentation/Component/Property/Value/DateTimeValue.php @@ -14,8 +14,10 @@ use BadMethodCallException; use DateTimeZone; use Eluceo\iCal\Domain\ValueObject\DateTime; +use Eluceo\iCal\Domain\ValueObject\PointInTime; use Eluceo\iCal\Domain\ValueObject\Timestamp; use Eluceo\iCal\Presentation\Component\Property\Value; +use InvalidArgumentException; final class DateTimeValue extends Value { @@ -23,19 +25,22 @@ final class DateTimeValue extends Value private const FORMAT_NO_TIMEZONE = 'Ymd\\THis'; private string $valueAsString; - private function __construct(string $valueAsString) + /** + * @param Timestamp|DateTime $pointInTime + */ + public function __construct(PointInTime $pointInTime) { - $this->valueAsString = $valueAsString; + $this->valueAsString = $this->convertPointInTime($pointInTime); } - public static function fromTimestamp(Timestamp $timestamp): self + private function convertTimestampToString(Timestamp $timestamp): string { $dateTime = $timestamp->getDateTime()->setTimezone(new DateTimeZone('UTC')); - return new self($dateTime->format(self::FORMAT_UTC_DATE_TIME)); + return $dateTime->format(self::FORMAT_UTC_DATE_TIME); } - public static function fromDateTime(DateTime $dateTime): self + private function convertDateTimeToString(DateTime $dateTime): string { $format = self::FORMAT_NO_TIMEZONE; @@ -43,11 +48,24 @@ public static function fromDateTime(DateTime $dateTime): self throw new BadMethodCallException('not implemented yet'); } - return new static($dateTime->getDateTime()->format($format)); + return $dateTime->getDateTime()->format($format); } public function __toString(): string { return $this->valueAsString; } + + private function convertPointInTime(PointInTime $pointInTime): string + { + if ($pointInTime instanceof DateTime) { + return $this->convertDateTimeToString($pointInTime); + } + + if ($pointInTime instanceof Timestamp) { + return $this->convertTimestampToString($pointInTime); + } + + throw new InvalidArgumentException('Cannot convert object of type ' . get_class($pointInTime) . ' to string'); + } } diff --git a/src/Presentation/Component/Property/Value/DateValue.php b/src/Presentation/Component/Property/Value/DateValue.php index 28874d9f..04a8edd5 100644 --- a/src/Presentation/Component/Property/Value/DateValue.php +++ b/src/Presentation/Component/Property/Value/DateValue.php @@ -19,14 +19,9 @@ final class DateValue extends Value private const FORMAT = 'Ymd'; private string $valueAsString; - private function __construct(string $valueAsString) + public function __construct(Date $date) { - $this->valueAsString = $valueAsString; - } - - public static function fromDate(Date $date): self - { - return new static($date->getDateTime()->format(self::FORMAT)); + $this->valueAsString = $date->getDateTime()->format(self::FORMAT); } public function __toString(): string diff --git a/src/Presentation/Component/Property/Value/TextValue.php b/src/Presentation/Component/Property/Value/TextValue.php index f9148f98..f02ff14d 100644 --- a/src/Presentation/Component/Property/Value/TextValue.php +++ b/src/Presentation/Component/Property/Value/TextValue.php @@ -82,9 +82,4 @@ public function __toString(): string return $value; } - - public static function fromString(string $value): self - { - return new static($value); - } } diff --git a/src/Presentation/ContentLine.php b/src/Presentation/ContentLine.php index 709da395..69438eef 100644 --- a/src/Presentation/ContentLine.php +++ b/src/Presentation/ContentLine.php @@ -17,16 +17,11 @@ final class ContentLine private const LINE_LENGTH = 75; private string $line; - private function __construct(string $line) + public function __construct(string $line) { $this->line = $line; } - public static function fromString(string $line): self - { - return new static($line); - } - public function __toString() { $string = $this->line; diff --git a/src/Presentation/Factory/EventFactory.php b/src/Presentation/Factory/EventFactory.php index 13c5f165..3df71299 100644 --- a/src/Presentation/Factory/EventFactory.php +++ b/src/Presentation/Factory/EventFactory.php @@ -52,7 +52,7 @@ public function createComponent(Event $event): Component private function getProperties(Event $event): Generator { yield new Property('UID', new TextValue((string) $event->getUniqueIdentifier())); - yield new Property('DTSTAMP', DateTimeValue::fromTimestamp($event->getTouchedAt())); + yield new Property('DTSTAMP', new DateTimeValue($event->getTouchedAt())); if ($event->hasSummary()) { yield new Property('SUMMARY', new TextValue($event->getSummary())); @@ -77,17 +77,17 @@ private function getProperties(Event $event): Generator private function getOccurrenceProperties(Occurrence $occurrence): Generator { if ($occurrence instanceof SingleDay) { - yield new Property('DTSTART', DateValue::fromDate($occurrence->getDate())); + yield new Property('DTSTART', new DateValue($occurrence->getDate())); } if ($occurrence instanceof MultiDay) { - yield new Property('DTSTART', DateValue::fromDate($occurrence->getFirstDay())); - yield new Property('DTEND', DateValue::fromDate($occurrence->getLastDay()->add(new DateInterval('P1D')))); + yield new Property('DTSTART', new DateValue($occurrence->getFirstDay())); + yield new Property('DTEND', new DateValue($occurrence->getLastDay()->add(new DateInterval('P1D')))); } if ($occurrence instanceof TimeSpan) { - yield new Property('DTSTART', DateTimeValue::fromDateTime($occurrence->getBegin())); - yield new Property('DTEND', DateTimeValue::fromDateTime($occurrence->getEnd())); + yield new Property('DTSTART', new DateTimeValue($occurrence->getBegin())); + yield new Property('DTEND', new DateTimeValue($occurrence->getEnd())); } } diff --git a/src/Util/DateTimeImmutableFactory.php b/src/Util/DateTimeImmutableFactory.php new file mode 100644 index 00000000..547f7b32 --- /dev/null +++ b/src/Util/DateTimeImmutableFactory.php @@ -0,0 +1,31 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace Eluceo\iCal\Util; + +use DateTimeImmutable; +use DateTimeInterface; + +/** + * @internal + */ +final class DateTimeImmutableFactory +{ + public static function createFromInterface(DateTimeInterface $dateTime): DateTimeImmutable + { + if ($dateTime instanceof DateTimeImmutable) { + return $dateTime; + } + + return (new DateTimeImmutable('now', $dateTime->getTimezone())) + ->setTimestamp($dateTime->getTimestamp()); + } +} diff --git a/tests/Integration/EventsGeneratorTest.php b/tests/Integration/EventsGeneratorTest.php index 8a0835f0..3bf871c0 100644 --- a/tests/Integration/EventsGeneratorTest.php +++ b/tests/Integration/EventsGeneratorTest.php @@ -29,13 +29,13 @@ public function testEventsGeneratorCreatesIcsContent(): void { $generator = function (): Generator { $day = new DateTimeImmutable('2020-01-01 15:00:00'); - $timestamp = Timestamp::fromDateTimeInterface($day); + $timestamp = new Timestamp($day); $dayInterval = new DateInterval('P1D'); for ($i = 0; $i < 3; ++$i) { - yield (new Event(UniqueIdentifier::fromString('event-' . $i))) + yield (new Event(new UniqueIdentifier('event-' . $i))) ->touch($timestamp) ->setSummary('Event ' . $i) - ->setOccurrence(SingleDay::fromDate(Date::fromDateTimeInterface($day))); + ->setOccurrence(new SingleDay(new Date($day))); $day = $day->add($dayInterval); } }; diff --git a/tests/Unit/Domain/ValueObject/GeographicPositionTest.php b/tests/Unit/Domain/ValueObject/GeographicPositionTest.php index b62f3502..bcffc926 100644 --- a/tests/Unit/Domain/ValueObject/GeographicPositionTest.php +++ b/tests/Unit/Domain/ValueObject/GeographicPositionTest.php @@ -24,7 +24,7 @@ class GeographicPositionTest extends TestCase public function testConstructorDoesNotAcceptInvalidArguments(float $latitude, float $longitude) { static::expectException(InvalidArgumentException::class); - GeographicPosition::fromLatitudeAndLongitude($latitude, $longitude); + new GeographicPosition($latitude, $longitude); } public function provideInvalidPositions(): Generator diff --git a/tests/Unit/Presentation/ContentLineTest.php b/tests/Unit/Presentation/ContentLineTest.php index aa563790..b61ba5d9 100644 --- a/tests/Unit/Presentation/ContentLineTest.php +++ b/tests/Unit/Presentation/ContentLineTest.php @@ -19,7 +19,7 @@ class ContentLineTest extends TestCase public function testShortLinesAreNotFolded() { $lineAsString = 'BEGIN:EVENT'; - $contentLine = ContentLine::fromString($lineAsString); + $contentLine = new ContentLine($lineAsString); self::assertSame($lineAsString . ContentLine::LINE_SEPARATOR, (string) $contentLine); } @@ -33,7 +33,7 @@ public function testLongLinesAreFolder() '', ]); - $contentLine = ContentLine::fromString($lineAsString); + $contentLine = new ContentLine($lineAsString); self::assertSame($expected, (string) $contentLine); } } diff --git a/tests/Unit/Presentation/Factory/CalendarFactoryTest.php b/tests/Unit/Presentation/Factory/CalendarFactoryTest.php index be03269f..7458b412 100644 --- a/tests/Unit/Presentation/Factory/CalendarFactoryTest.php +++ b/tests/Unit/Presentation/Factory/CalendarFactoryTest.php @@ -39,7 +39,7 @@ public function testRenderEmptyCalendar() public function testRenderWithEvents() { - $currentTime = Timestamp::fromDateTimeInterface( + $currentTime = new Timestamp( DateTimeImmutable::createFromFormat( 'Y-m-d H:i:s', '2019-11-10 11:22:33', @@ -48,8 +48,8 @@ public function testRenderWithEvents() ); $calendar = new Calendar( [ - (new Event(UniqueIdentifier::fromString('event1')))->touch($currentTime), - (new Event(UniqueIdentifier::fromString('event2')))->touch($currentTime), + (new Event(new UniqueIdentifier('event1')))->touch($currentTime), + (new Event(new UniqueIdentifier('event2')))->touch($currentTime), ] ); $calendar->setProductIdentifier('-//test/ical//2.0/EN'); diff --git a/tests/Unit/Presentation/Factory/EventFactoryTest.php b/tests/Unit/Presentation/Factory/EventFactoryTest.php index 71ef5d44..fe171371 100644 --- a/tests/Unit/Presentation/Factory/EventFactoryTest.php +++ b/tests/Unit/Presentation/Factory/EventFactoryTest.php @@ -31,7 +31,7 @@ class CalendarFactoryTest extends TestCase { public function testMinimalEvent() { - $currentTime = Timestamp::fromDateTimeInterface( + $currentTime = new Timestamp( DateTimeImmutable::createFromFormat( 'Y-m-d H:i:s', '2019-11-10 11:22:33', @@ -39,7 +39,7 @@ public function testMinimalEvent() ) ); - $event = (new Event(UniqueIdentifier::fromString('event1')))->touch($currentTime); + $event = (new Event(new UniqueIdentifier('event1')))->touch($currentTime); $expected = implode(ContentLine::LINE_SEPARATOR, [ 'BEGIN:VEVENT', @@ -66,7 +66,7 @@ public function testEventWithSummaryAndDescription() public function testEventWithLocation() { - $geographicalPosition = GeographicPosition::fromLatitudeAndLongitude(51.333333333333, 7.05); + $geographicalPosition = new GeographicPosition(51.333333333333, 7.05); $location = (new Location('Location Name'))->withGeographicPosition($geographicalPosition); $event = (new Event())->setLocation($location); @@ -81,7 +81,7 @@ public function testEventWithLocation() public function testSingleDayEvent() { - $event = (new Event())->setOccurrence(SingleDay::fromDate(Date::fromDateTimeInterface(DateTimeImmutable::createFromFormat('Y-m-d', '2030-12-24')))); + $event = (new Event())->setOccurrence(new SingleDay(new Date(DateTimeImmutable::createFromFormat('Y-m-d', '2030-12-24')))); self::assertEventRendersCorrect($event, [ 'DTSTART:20301224', @@ -90,9 +90,9 @@ public function testSingleDayEvent() public function testMultiDayEvent() { - $firstDay = Date::fromDateTimeInterface(DateTimeImmutable::createFromFormat('Y-m-d', '2030-12-24')); - $lastDay = Date::fromDateTimeInterface(DateTimeImmutable::createFromFormat('Y-m-d', '2030-12-26')); - $occurrence = MultiDay::fromDates($firstDay, $lastDay); + $firstDay = new Date(DateTimeImmutable::createFromFormat('Y-m-d', '2030-12-24')); + $lastDay = new Date(DateTimeImmutable::createFromFormat('Y-m-d', '2030-12-26')); + $occurrence = new MultiDay($firstDay, $lastDay); $event = (new Event())->setOccurrence($occurrence); self::assertEventRendersCorrect($event, [ @@ -103,9 +103,9 @@ public function testMultiDayEvent() public function testTimespanEvent() { - $begin = DateTime::fromDateTimeInterface(DateTimeImmutable::createFromFormat('Y-m-d H:i', '2030-12-24 12:15')); - $end = DateTime::fromDateTimeInterface(DateTimeImmutable::createFromFormat('Y-m-d H:i', '2030-12-24 13:45')); - $occurrence = TimeSpan::create($begin, $end); + $begin = new DateTime(DateTimeImmutable::createFromFormat('Y-m-d H:i', '2030-12-24 12:15')); + $end = new DateTime(DateTimeImmutable::createFromFormat('Y-m-d H:i', '2030-12-24 13:45')); + $occurrence = new TimeSpan($begin, $end); $event = (new Event())->setOccurrence($occurrence); self::assertEventRendersCorrect($event, [ diff --git a/tests/Unit/Util/DateTimeImmutableFactoryTest.php b/tests/Unit/Util/DateTimeImmutableFactoryTest.php new file mode 100644 index 00000000..7a9cf58d --- /dev/null +++ b/tests/Unit/Util/DateTimeImmutableFactoryTest.php @@ -0,0 +1,28 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace Eluceo\iCal\Util; + +use DateTime; +use DateTimeImmutable; +use PHPUnit\Framework\TestCase; + +class DateTimeImmutableFactoryTest extends TestCase +{ + public function testDateTimeIsConvertedToImmutableObject() + { + $input = new DateTime(); + $actual = DateTimeImmutableFactory::createFromInterface($input); + + static::assertInstanceOf(DateTimeImmutable::class, $actual); + static::assertSame($input->format(DATE_COOKIE), $actual->format(DATE_COOKIE)); + } +}