Skip to content

Commit d6a9777

Browse files
committed
Adding missing tests for tryFrom* methods
1 parent 3d044f5 commit d6a9777

File tree

6 files changed

+45
-0
lines changed

6 files changed

+45
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
77
### Added
88

99
- Support for the `DisplayString` type
10+
- `ByteSequence::tryFromEncoded`
11+
- `Token::tryFromString`
1012

1113
### Fixed
1214

src/DisplayString.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Stringable;
88

9+
use Throwable;
10+
911
use function preg_match;
1012
use function preg_replace_callback;
1113
use function rawurldecode;
@@ -21,6 +23,15 @@ private function __construct(
2123
) {
2224
}
2325

26+
public static function tryFromEncoded(Stringable|string $encodedValue): ?self
27+
{
28+
try {
29+
return self::fromEncoded($encodedValue);
30+
} catch (Throwable) {
31+
return null;
32+
}
33+
}
34+
2435
/**
2536
* Returns a new instance from a Base64 encoded string.
2637
*/

src/Token.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Stringable;
88

9+
use Throwable;
10+
911
use function preg_match;
1012

1113
/**
@@ -25,6 +27,15 @@ public function toString(): string
2527
return $this->value;
2628
}
2729

30+
public static function tryFromString(Stringable|string $value): ?self
31+
{
32+
try {
33+
return self::fromString($value);
34+
} catch (Throwable) {
35+
return null;
36+
}
37+
}
38+
2839
public static function fromString(Stringable|string $value): self
2940
{
3041
return new self((string) $value);

tests/ByteSequenceTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ public function it_will_fail_on_invalid_decoded_string_with_inner_space(): void
2121
ByteSequence::fromEncoded('a a');
2222
}
2323

24+
#[Test]
25+
public function it_will_return_null_on_invalid_encoded_string(): void
26+
{
27+
self::assertNull(ByteSequence::tryFromEncoded('a a'));
28+
self::assertNull(ByteSequence::tryFromEncoded('aaaaa'));
29+
}
30+
2431
#[Test]
2532
public function it_will_fail_on_invalid_decoded_string(): void
2633
{

tests/DisplayStringTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ public function it_will_fail_on_invalid_decoded_string(): void
2929
DisplayString::fromEncoded('%c3%28"');
3030
}
3131

32+
#[Test]
33+
public function it_will_return_null_on_invalid_encoded_string(): void
34+
{
35+
self::assertNull(DisplayString::tryFromEncoded('a %a'));
36+
self::assertNull(DisplayString::tryFromEncoded('%c3%28"'));
37+
}
38+
3239
#[Test]
3340
public function it_can_decode_base64_field(): void
3441
{

tests/TokenTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ public function it_will_fail_on_invalid_token_string(string $httpValue): void
2424
Token::fromString($httpValue);
2525
}
2626

27+
#[Test]
28+
#[DataProvider('invalidTokenString')]
29+
public function it_will_return_null_on_invalid_encoded_string(string $httpValue): void
30+
{
31+
self::assertNull(Token::tryFromString($httpValue));
32+
}
33+
2734
/**
2835
* @return array<array{0:string}>
2936
*/

0 commit comments

Comments
 (0)