From feb566dd9e87779b824f4eceab7e4e5cee3fbd2c Mon Sep 17 00:00:00 2001 From: atymic Date: Thu, 25 May 2023 09:58:50 +1000 Subject: [PATCH] fix: ics as file --- src/Generators/Ics.php | 6 ++++++ tests/Generators/IcsGeneratorTest.php | 8 ++++++++ .../IcsGeneratorTest__it_can_generate_a_file__1.txt | 13 +++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 tests/Generators/__snapshots__/IcsGeneratorTest__it_can_generate_a_file__1.txt diff --git a/src/Generators/Ics.php b/src/Generators/Ics.php index b738974..c48c8d4 100644 --- a/src/Generators/Ics.php +++ b/src/Generators/Ics.php @@ -10,6 +10,8 @@ */ class Ics implements Generator { + public const OPTION_AS_FILE = 'as_file'; + /** @var string {@see https://www.php.net/manual/en/function.date.php} */ protected $dateFormat = 'Ymd'; /** @var string */ @@ -64,6 +66,10 @@ public function generate(Link $link): string $url[] = 'END:VEVENT'; $url[] = 'END:VCALENDAR'; + if ($this->options[self::OPTION_AS_FILE] ?? false) { + return implode("\r\n", $url); + } + return $this->buildLink($url); } diff --git a/tests/Generators/IcsGeneratorTest.php b/tests/Generators/IcsGeneratorTest.php index 9c3b481..41a3a55 100644 --- a/tests/Generators/IcsGeneratorTest.php +++ b/tests/Generators/IcsGeneratorTest.php @@ -53,4 +53,12 @@ public function it_has_a_product_dtstamp(): void $this->generator(['DTSTAMP' => '20180201T090000Z'])->generate($this->createShortEventLink()) ); } + + /** @test */ + public function it_can_generate_a_file(): void + { + $this->assertMatchesSnapshot( + $this->generator([Ics::OPTION_AS_FILE => true])->generate($this->createShortEventLink()) + ); + } } diff --git a/tests/Generators/__snapshots__/IcsGeneratorTest__it_can_generate_a_file__1.txt b/tests/Generators/__snapshots__/IcsGeneratorTest__it_can_generate_a_file__1.txt new file mode 100644 index 0000000..e0bd621 --- /dev/null +++ b/tests/Generators/__snapshots__/IcsGeneratorTest__it_can_generate_a_file__1.txt @@ -0,0 +1,13 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:Spatie calendar-links +BEGIN:VEVENT +UID:94ab75add84a67c019eae57539658036 +SUMMARY:Birthday +DTSTAMP:20180201T090000Z +DTSTART:20180201T090000Z +DTEND:20180201T180000Z +DESCRIPTION:With balloons\, clowns and stuff\nBring a dog\, bring a frog +LOCATION:Party Lane 1A\, 1337 Funtown +END:VEVENT +END:VCALENDAR \ No newline at end of file