mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-04-08 22:13:45 +00:00
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:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user