107 lines
3.4 KiB
PHP
107 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\App\Services;
|
|
|
|
use App\Dto\IcsContentDto;
|
|
use App\Services\IcsGeneratorService;
|
|
use Carbon\Carbon;
|
|
use Carbon\CarbonImmutable;
|
|
|
|
covers(IcsGeneratorService::class);
|
|
|
|
afterEach(function (): void {
|
|
Carbon::setTestNow();
|
|
CarbonImmutable::setTestNow();
|
|
});
|
|
|
|
describe('IcsGeneratorService', function (): void {
|
|
it('throws exception if app name is empty', function (): void {
|
|
// GIVEN
|
|
$appName = '';
|
|
|
|
// WHEN
|
|
$action = fn (): IcsGeneratorService => new IcsGeneratorService($appName);
|
|
|
|
// THEN
|
|
expect($action)->toThrow(\InvalidArgumentException::class, 'App name must be provided for ICS generation.');
|
|
});
|
|
|
|
it('generates ics format without url', function (): void {
|
|
// GIVEN
|
|
$service = new IcsGeneratorService('MyApp');
|
|
$dto = new IcsContentDto(
|
|
uid: 'uid-123',
|
|
summary: 'My Event, with comma',
|
|
description: "Line 1\nLine 2;",
|
|
startsAt: Carbon::parse('2026-06-11 10:00:00'),
|
|
endsAt: Carbon::parse('2026-06-11 11:00:00'),
|
|
url: null,
|
|
);
|
|
|
|
Carbon::setTestNow('2026-06-11 09:00:00');
|
|
|
|
// WHEN
|
|
$ics = $service->generate($dto);
|
|
|
|
// THEN
|
|
expect($ics->value)
|
|
->toContain('BEGIN:VCALENDAR')
|
|
->toContain('PRODID:-//MyApp//NONSGML v1.0//EN')
|
|
->toContain('UID:uid-123')
|
|
->toContain('DTSTAMP:20260611T090000Z')
|
|
->toContain('DTSTART:20260611T100000Z')
|
|
->toContain('DTEND:20260611T110000Z')
|
|
->toContain('SUMMARY:My Event\, with comma')
|
|
->toContain('DESCRIPTION:Line 1\nLine 2\;')
|
|
->and($ics->value)->not->toContain('URL:');
|
|
});
|
|
|
|
it('generates ics format with url', function (): void {
|
|
// GIVEN
|
|
$service = new IcsGeneratorService('MyApp');
|
|
$dto = new IcsContentDto(
|
|
uid: 'uid-123',
|
|
summary: 'Event',
|
|
description: 'Desc',
|
|
startsAt: Carbon::parse('2026-06-11 10:00:00'),
|
|
endsAt: Carbon::parse('2026-06-11 11:00:00'),
|
|
url: 'http://example.com',
|
|
);
|
|
|
|
// WHEN
|
|
$ics = $service->generate($dto);
|
|
|
|
// THEN
|
|
expect($ics->value)->toContain('URL:http://example.com');
|
|
});
|
|
|
|
it('converts Europe/Paris winter timestamps to UTC correctly', function (): void {
|
|
// GIVEN
|
|
$service = new IcsGeneratorService('La Boutique de Cocotte');
|
|
CarbonImmutable::setTestNow(CarbonImmutable::parse('2024-01-01 10:00:00', 'UTC'));
|
|
|
|
$dto = new IcsContentDto(
|
|
uid: 'test-uid@laboutiquedecocotte.fr',
|
|
summary: 'Test Event',
|
|
description: 'Winter timezone check',
|
|
startsAt: CarbonImmutable::parse('2024-01-01 12:00:00', 'Europe/Paris'),
|
|
endsAt: CarbonImmutable::parse('2024-01-01 12:15:00', 'Europe/Paris'),
|
|
url: 'https://laboutiquedecocotte.fr',
|
|
);
|
|
|
|
// WHEN
|
|
$ics = $service->generate($dto);
|
|
|
|
// THEN
|
|
// 12:00 Europe/Paris in winter (UTC+1) is 11:00 UTC
|
|
expect($ics->value)->toContain('VERSION:2.0')
|
|
->toContain('BEGIN:VEVENT')
|
|
->toContain('DTSTAMP:20240101T100000Z')
|
|
->toContain('DTSTART:20240101T110000Z')
|
|
->toContain('DTEND:20240101T111500Z')
|
|
->toContain('END:VEVENT')
|
|
->toContain('END:VCALENDAR');
|
|
});
|
|
});
|