Skip to content

Commit e37dab0

Browse files
committed
Improve DisplayString test coverage
1 parent 408300c commit e37dab0

File tree

6 files changed

+23
-11
lines changed

6 files changed

+23
-11
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ jobs:
4848

4949
- name: Run static analysis
5050
run: composer phpstan
51-
if: ${{ matrix.php == '8.2' && matrix.stability == 'prefer-stable'}}
51+
if: ${{ matrix.php == '8.3' && matrix.stability == 'prefer-stable'}}
5252

5353
- name: Run Coding style rules
5454
run: composer phpcs:fix
55-
if: ${{ matrix.php == '8.2' && matrix.stability == 'prefer-stable'}}
55+
if: ${{ matrix.php == '8.3' && matrix.stability == 'prefer-stable'}}

src/DisplayString.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ public static function fromEncoded(Stringable|string $encodedValue): self
4646
return new self($value);
4747
}
4848

49-
if (1 === preg_match('/%(?![0-9a-fA-F]{2})/', $value)) {
49+
if (1 === preg_match('/%(?![0-9a-f]{2})/', $value)) {
5050
throw new SyntaxError('The string '.$value.' contains invalid utf-8 encoded sequence.');
5151
}
5252

5353
$value = (string) preg_replace_callback(
54-
',%[A-Fa-f0-9]{2},',
54+
',%[a-f0-9]{2},',
5555
fn (array $matches): string => rawurldecode($matches[0]),
5656
$value
5757
);

src/Parser.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,12 +447,11 @@ private static function extractDisplayString(string $httpValue): array
447447

448448
$octet = substr($remainder, 0, 2);
449449
$offset += 2;
450-
$remainder = substr($remainder, 2);
451-
452-
if (2 !== strlen($octet) || $octet !== strtolower($octet)) {
450+
if (1 === preg_match('/^[0-9a-f]]{2}$/', $octet)) {
453451
throw new SyntaxError("The HTTP textual representation '$httpValue' for a DisplayString contains uppercased percent encoding sequence.");
454452
}
455453

454+
$remainder = substr($remainder, 2);
456455
$output .= $char.$octet;
457456
}
458457

tests/ItemTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ final class ItemTest extends StructuredFieldTestCase
2424
'number-generated.json',
2525
'string.json',
2626
'string-generated.json',
27-
'token.json',
28-
'token-generated.json',
2927
'item.json',
3028
'date.json',
3129
];

tests/ParserTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function it_fails_to_parse_an_invalid_http_field_2(): void
143143

144144
#[Test]
145145
#[DataProvider('provideHttpValueForDataType')]
146-
public function it_parses_basic_data_type(string $httpValue, ByteSequence|Token|DateTimeImmutable|string|int|float|bool $expected): void
146+
public function it_parses_basic_data_type(string $httpValue, ByteSequence|Token|DisplayString|DateTimeImmutable|string|int|float|bool $expected): void
147147
{
148148
$field = $this->parser->parseValue($httpValue);
149149
if (is_scalar($expected)) {
@@ -197,6 +197,11 @@ public static function provideHttpValueForDataType(): iterable
197197
'httpValue' => '?0',
198198
'expected' => false,
199199
];
200+
201+
yield 'it parses a display string' => [
202+
'httpValue' => '%"b%c3%a9b%c3%a9"',
203+
'expected' => DisplayString::fromDecoded('bébé'),
204+
];
200205
}
201206

202207
#[Test]
@@ -219,6 +224,9 @@ public static function provideInvalidHttpValueForDataType(): array
219224
['@1_000_000_000_000.0'],
220225
['-1_000_000_000_000.0'],
221226
[' '],
227+
['%"b%c3%a9b%c3%a9'],
228+
['%b%c3%a9b%c3%a9"'],
229+
['%"b%C3%A9b%C3%A9"'],
222230
];
223231
}
224232
}

tests/RecordAggregate.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@
1515

1616
/**
1717
* @implements IteratorAggregate<string, Record>
18-
* @phpstan-import-type RecordData from Record
18+
* @phpstan-type RecordData array{
19+
* name: string,
20+
* header_type: 'dictionary'|'list'|'item',
21+
* raw: array<string>,
22+
* canonical?: array<string>,
23+
* must_fail?: bool,
24+
* can_fail?: bool
25+
* }
1926
*/
2027
final class RecordAggregate implements IteratorAggregate
2128
{

0 commit comments

Comments
 (0)