Add LockTimeoutAnalyzer utility and corresponding tests

- Implemented `LockTimeoutAnalyzer` to parse timeout values from RFC-compliant strings and return `DateInterval` objects.
- Added `LockTimeoutAnalyzerTest` with data providers to validate handling of various timeout cases, including "Second" and "Infinite".
This commit is contained in:
2026-04-02 14:35:20 +02:00
parent ba2288de55
commit c9a632f3a9
2 changed files with 107 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocStoreBundle\Dav\Utils;
use DateInterval;
class LockTimeoutAnalyzer
{
/**
* Analyzes the timeout value from the provided content string, RFC2068 string
* and return a DateInterval object representing the timeout duration.
*
* @param string $content The input string containing timeout information, as described by RFC2518, section 4.2
*
* @return \DateInterval the calculated timeout as a DateInterval object
*
* @throws \Exception if the DateInterval creation fails
*/
public function analyzeTimeout(string $content): \DateInterval
{
$types = explode(',', $content);
$firstType = trim(reset($types));
if (str_starts_with($firstType, 'Second-')) {
$seconds = (int) substr($firstType, 7);
return new \DateInterval(sprintf('PT%dS', $seconds));
}
if ('Infinite' === $firstType) {
return new \DateInterval('PT24H');
}
return new \DateInterval('PT3600S');
}
}

View File

@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocStoreBundle\Tests\Dav\Utils;
use Chill\DocStoreBundle\Dav\Utils\LockTimeoutAnalyzer;
use PHPUnit\Framework\TestCase;
/**
* @internal
*
* @coversNothing
*/
class LockTimeoutAnalyzerTest extends TestCase
{
/**
* @dataProvider provideTimeoutData
*/
public function testAnalyzeTimeout(string $content, \DateInterval $expected): void
{
$analyzer = new LockTimeoutAnalyzer();
$result = $analyzer->analyzeTimeout($content);
self::assertEquals($expected, $result);
}
public function provideTimeoutData(): iterable
{
yield 'Second-1800' => [
'Second-1800',
new \DateInterval('PT1800S'),
];
yield 'Second-3600' => [
'Second-3600',
new \DateInterval('PT3600S'),
];
yield 'Infinite' => [
'Infinite',
new \DateInterval('PT24H'), // Typically "Infinite" is represented by a long duration or special handling; RFC says it should be long. Let's assume a long one for now or check common practices.
];
yield 'Multiple types, first is Second' => [
'Second-1800, Infinite',
new \DateInterval('PT1800S'),
];
yield 'Multiple types, first is Infinite' => [
'Infinite, Second-1800',
new \DateInterval('PT24H'),
];
}
}