Add LockTokenParser::parseIfCondition method and corresponding tests

- Implemented the `parseIfCondition` method in `LockTokenParser` to extract lock tokens from `if` headers.
- Added `LockTokenParserTest` with multiple test cases using data providers to validate parsing logic, including scenarios with no headers, resource URIs, and "not" conditions.
This commit is contained in:
2026-04-03 18:01:04 +02:00
parent ff9e4f2709
commit a1d72cefff
2 changed files with 85 additions and 0 deletions

View File

@@ -40,4 +40,19 @@ final readonly class LockTokenParser
return $token;
}
public function parseIfCondition(Request $request): ?string
{
$if = $request->headers->get('if');
if (null === $if) {
return null;
}
if (preg_match('/\((?:not\s+)?<([^>]+)>/i', $if, $matches)) {
return $matches[1];
}
return null;
}
}

View File

@@ -0,0 +1,70 @@
<?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\LockTokenParser;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
/**
* @internal
*
* @coversNothing
*/
class LockTokenParserTest extends TestCase
{
private LockTokenParser $parser;
protected function setUp(): void
{
$this->parser = new LockTokenParser();
}
/**
* @dataProvider provideIfConditions
*/
public function testParseIfCondition(string $ifHeader, ?string $expectedToken): void
{
$request = new Request();
$request->headers->set('if', $ifHeader);
$this->assertSame($expectedToken, $this->parser->parseIfCondition($request));
}
public function provideIfConditions(): array
{
return [
'standard lock token' => [
'(<opaquelocktoken:f81d4fae-7dec-11d0-a765-00a0c91e6bf6>)',
'opaquelocktoken:f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
],
'with resource uri' => [
'<http://www.ics.uci.edu/users/f/fielding/index.html> (<opaquelocktoken:f81d4fae-7dec-11d0-a765-00a0c91e6bf6>)',
'opaquelocktoken:f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
],
'no match' => [
'some other value',
null,
],
'with NOT' => [
'(Not <opaquelocktoken:f81d4fae-7dec-11d0-a765-00a0c91e6bf6>)',
'opaquelocktoken:f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
],
];
}
public function testParseIfConditionWithNoHeader(): void
{
$request = new Request();
$this->assertNull($this->parser->parseIfCondition($request));
}
}