Skip to content

Commit 5bf53fc

Browse files
committed
Improve Item test suite
1 parent 2924e86 commit 5bf53fc

File tree

6 files changed

+53
-51
lines changed

6 files changed

+53
-51
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@
5555
"test": [
5656
"@phpunit",
5757
"@phpstan",
58-
"@phpcs",
59-
"@benchmark"
58+
"@phpcs"
6059
]
6160
},
6261
"scripts-descriptions": {
6362
"phpstan": "Runs complete codebase static analysis",
6463
"phpunit": "Runs unit and functional testing",
6564
"phpcs": "Runs coding style testing",
6665
"phpcs:fix": "Fix coding style issues",
66+
"benchmark": "Runs parser benchmark",
6767
"test": "Runs all tests"
6868
},
6969
"repositories": [

src/Dictionary.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private function __construct(iterable $members = [])
3535
{
3636
$filteredMembers = [];
3737
foreach ($members as $key => $member) {
38-
$filteredMembers[MapKey::fromString($key)->value] = self::filterMember($member);
38+
$filteredMembers[MapKey::from($key)->value] = self::filterMember($member);
3939
}
4040

4141
$this->members = $filteredMembers;

src/ItemTest.php

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use PHPUnit\Framework\Attributes\Test;
1313
use Stringable;
1414

15+
/**
16+
* @phpstan-import-type DataType from Value
17+
*/
1518
final class ItemTest extends StructuredFieldTestCase
1619
{
1720
/** @var array<string> */
@@ -28,19 +31,46 @@ final class ItemTest extends StructuredFieldTestCase
2831
];
2932

3033
#[Test]
31-
public function it_fails_to_instantiate_a_decimal_too_big(): void
34+
#[DataProvider('provideInvalidArguments')]
35+
public function it_fails_to_instantiate_an_item(mixed $value): void
3236
{
3337
$this->expectException(SyntaxError::class);
3438

35-
Item::from(1_000_000_000_000.0);
39+
Item::from($value);
3640
}
3741

38-
#[Test]
39-
public function it_fails_to_instantiate_a_decimal_too_small(): void
42+
/**
43+
* @return iterable<string, array{value:mixed}>
44+
*/
45+
public static function provideInvalidArguments(): iterable
4046
{
41-
$this->expectException(SyntaxError::class);
47+
yield 'if the decimal is too big' => [
48+
'value' => 1_000_000_000_000.0,
49+
];
50+
51+
yield 'if the decimal is too small' => [
52+
'value' => -1_000_000_000_000.0,
53+
];
54+
55+
yield 'if the integer is too big' => [
56+
'value' => 1_000_000_000_000_000,
57+
];
4258

43-
Item::from(-1_000_000_000_000.0);
59+
yield 'if the integer is too small' => [
60+
'value' => -1_000_000_000_000_000,
61+
];
62+
63+
yield 'if the date is too much in the future' => [
64+
'value' => new DateTime('@'. 1_000_000_000_000_000),
65+
];
66+
67+
yield 'if the date is too much in the past' => [
68+
'value' => new DateTime('@'.-1_000_000_000_000_000),
69+
];
70+
71+
yield 'if the string contains invalud characters' => [
72+
'value' => "\0foobar",
73+
];
4474
}
4575

4676
#[Test]
@@ -91,22 +121,6 @@ public function __toString(): string
91121
];
92122
}
93123

94-
#[Test]
95-
public function it_fails_to_instantiate_a_integer_too_big(): void
96-
{
97-
$this->expectException(SyntaxError::class);
98-
99-
Item::from(1_000_000_000_000_000);
100-
}
101-
102-
#[Test]
103-
public function it_fails_to_instantiate_a_integer_too_small(): void
104-
{
105-
$this->expectException(SyntaxError::class);
106-
107-
Item::from(-1_000_000_000_000_000);
108-
}
109-
110124
#[Test]
111125
public function it_instantiates_a_token(): void
112126
{
@@ -133,14 +147,6 @@ public function it_fails_to_instantiate_an_invalid_date_format(): void
133147
Item::fromHttpValue('@112345.678');
134148
}
135149

136-
#[Test]
137-
public function it_fails_to_instantiate_an_out_of_range_date_in_the_future(): void
138-
{
139-
$this->expectException(SyntaxError::class);
140-
141-
Item::from(new DateTime('@'. 1_000_000_000_000_000));
142-
}
143-
144150
#[Test]
145151
public function it_fails_to_instantiate_an_out_of_range_timestamp_in_the_future(): void
146152
{
@@ -149,14 +155,6 @@ public function it_fails_to_instantiate_an_out_of_range_timestamp_in_the_future(
149155
Item::fromTimestamp(1_000_000_000_000_000);
150156
}
151157

152-
#[Test]
153-
public function it_fails_to_instantiate_an_out_of_range_date_in_the_past(): void
154-
{
155-
$this->expectException(SyntaxError::class);
156-
157-
Item::from(new DateTime('@'.-1_000_000_000_000_000));
158-
}
159-
160158
#[Test]
161159
public function it_fails_to_instantiate_an_out_of_range_timestamp_in_the_past(): void
162160
{
@@ -205,14 +203,6 @@ public function it_instantiates_a_string(): void
205203
self::assertSame('"foobar"', Item::from('foobar')->toHttpValue());
206204
}
207205

208-
#[Test]
209-
public function it_fails_to_instantiate_an_invalid_string(): void
210-
{
211-
$this->expectException(SyntaxError::class);
212-
213-
Item::from("\0foobar");
214-
}
215-
216206
#[Test]
217207
#[DataProvider('itemTypeProvider')]
218208
public function it_can_tell_the_item_type(Item $item, Type $expectedType): void

src/MapKey.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ private function __construct(
1818
/**
1919
* @throws SyntaxError If the string is not a valid HTTP value field key
2020
*/
21-
public static function fromString(string $httpValue): self
21+
public static function from(string|int $httpValue): self
2222
{
23+
if (!is_string($httpValue)) {
24+
throw new SyntaxError('The key must be a string; '.gettype($httpValue).' received.');
25+
}
26+
2327
$instance = self::fromStringBeginning($httpValue);
2428
if ($instance->value !== $httpValue) {
2529
throw new SyntaxError('No valid http value key could be extracted from "'.$httpValue.'".');

src/Parameters.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private function __construct(iterable $members = [])
3333
{
3434
$filteredMembers = [];
3535
foreach ($members as $key => $member) {
36-
$filteredMembers[MapKey::fromString($key)->value] = self::filterMember($member);
36+
$filteredMembers[MapKey::from($key)->value] = self::filterMember($member);
3737
}
3838

3939
$this->members = $filteredMembers;

src/ParametersTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ public function test_it_can_be_instantiated_with_key_value_pairs(): void
4949
);
5050
}
5151

52+
#[Test]
53+
public function it_fails_to_instantiate_a_pair_with_the_associative_constructor(): void
54+
{
55+
$this->expectException(SyntaxError::class);
56+
57+
Parameters::fromAssociative([['a', 'b']]); // @phpstan-ignore-line
58+
}
59+
5260
#[Test]
5361
public function it_fails_to_instantiate_with_an_item_containing_already_parameters(): void
5462
{

0 commit comments

Comments
 (0)