Skip to content

Commit 5f8bcf1

Browse files
committed
Improve Loading test files
1 parent 99b05a7 commit 5f8bcf1

8 files changed

+49
-68
lines changed

src/ByteSequenceTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
final class ByteSequenceTest extends StructuredFieldTestCase
1010
{
1111
/** @var array<string> */
12-
protected static array $paths = [
13-
'/binary.json',
12+
protected static array $httpWgTestFilenames = [
13+
'binary.json',
1414
];
1515

1616
#[Test]

src/DictionaryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
final class DictionaryTest extends StructuredFieldTestCase
1111
{
1212
/** @var array<string> */
13-
protected static array $paths = [
14-
'/dictionary.json',
13+
protected static array $httpWgTestFilenames = [
14+
'dictionary.json',
1515
];
1616

1717
#[Test]

src/ItemTest.php

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414
final class ItemTest extends StructuredFieldTestCase
1515
{
1616
/** @var array<string> */
17-
protected static array $paths = [
18-
'/boolean.json',
19-
'/number.json',
20-
'/number-generated.json',
21-
'/string.json',
22-
'/string-generated.json',
23-
'/token.json',
24-
'/token-generated.json',
25-
'/item.json',
26-
'/date.json',
17+
protected static array $httpWgTestFilenames = [
18+
'boolean.json',
19+
'number.json',
20+
'number-generated.json',
21+
'string.json',
22+
'string-generated.json',
23+
'token.json',
24+
'token-generated.json',
25+
'item.json',
26+
'date.json',
2727
];
2828

2929
#[Test]
@@ -147,7 +147,7 @@ public function it_fails_to_instantiate_an_invalid_string(): void
147147
#[DataProvider('itemTypeProvider')]
148148
public function it_can_tell_the_item_type(Item $item, Type $expectedType): void
149149
{
150-
self::assertSame($expectedType, $item->type());
150+
self::assertTrue($item->type()->equals($expectedType));
151151
}
152152

153153
/**
@@ -189,37 +189,34 @@ public function __toString(): string
189189
}),
190190
'expectedType' => Type::String,
191191
],
192-
'date' => [
192+
'date-immutable' => [
193193
'item' => Item::from(new DateTimeImmutable('2020-07-12 13:37:00')),
194194
'expectedType' => Type::Date,
195195
],
196+
'date-interface' => [
197+
'item' => Item::from(new DateTime('2020-07-12 13:37:00')),
198+
'expectedType' => Type::Date,
199+
],
196200
];
197201
}
198202

199203
#[Test]
200204
public function test_in_can_be_instantiated_using_bare_items(): void
201205
{
202-
$item1 = Item::from('/terms', [
206+
$parameters = [
203207
'string' => '42',
204208
'integer' => 42,
205209
'float' => 4.2,
206210
'boolean' => true,
207211
'token' => Token::fromString('forty-two'),
208212
'byte-sequence' => ByteSequence::fromDecoded('a42'),
209213
'date' => new DateTimeImmutable('2020-12-01 11:43:17'),
210-
]);
211-
212-
$item2 = Item::from('/terms', new ArrayObject([
213-
'string' => Item::from('42'),
214-
'integer' => Item::from(42),
215-
'float' => Item::from(4.2),
216-
'boolean' => Item::from(true),
217-
'token' => Item::from(Token::fromString('forty-two')),
218-
'byte-sequence' => Item::from(ByteSequence::fromDecoded('a42')),
219-
'date' => Item::from(new DateTimeImmutable('2020-12-01 11:43:17')),
220-
]));
221-
222-
self::assertEquals($item2, $item1);
214+
];
215+
216+
self::assertEquals(
217+
Item::from('/terms', $parameters),
218+
Item::from('/terms', new ArrayObject($parameters))
219+
);
223220
}
224221

225222
#[Test]
@@ -236,6 +233,7 @@ public function it_can_access_its_parameter_values(): void
236233
$instance = Item::fromHttpValue('1; a; b=?0');
237234

238235
self::assertTrue($instance->parameters()->get('a')->value());
236+
self::assertFalse($instance->parameters()->get('b')->value());
239237
}
240238

241239
#[Test]

src/OuterListTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
final class OuterListTest extends StructuredFieldTestCase
1111
{
1212
/** @var array<string> */
13-
protected static array $paths = [
14-
'/list.json',
15-
'/listlist.json',
13+
protected static array $httpWgTestFilenames = [
14+
'list.json',
15+
'listlist.json',
1616
];
1717

1818
#[Test]

src/ParametersTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
final class ParametersTest extends StructuredFieldTestCase
1111
{
1212
/** @var array<string> */
13-
protected static array $paths = [
14-
'/param-dict.json',
15-
'/param-list.json',
16-
'/param-listlist.json',
13+
protected static array $httpWgTestFilenames = [
14+
'param-dict.json',
15+
'param-list.json',
16+
'param-listlist.json',
1717
];
1818

1919
#[Test]

src/ParserTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
final class ParserTest extends StructuredFieldTestCase
1111
{
1212
/** @var array<string> */
13-
protected static array $paths = [
14-
'/examples.json',
15-
'/key-generated.json',
16-
'/large-generated.json',
13+
protected static array $httpWgTestFilenames = [
14+
'examples.json',
15+
'key-generated.json',
16+
'large-generated.json',
1717
];
1818

1919
#[Test]

src/StructuredFieldTestCase.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
use PHPUnit\Framework\Attributes\Test;
1010
use PHPUnit\Framework\TestCase;
1111
use function implode;
12+
use function ltrim;
1213

1314
abstract class StructuredFieldTestCase extends TestCase
1415
{
1516
protected static string $rootPath = __DIR__.'/../vendor/httpwg/structured-field-tests';
1617

1718
/** @var array<string> */
18-
protected static array $paths;
19+
protected static array $httpWgTestFilenames;
1920

2021
#[Test]
2122
#[DataProvider('httpWgDataProvider')]
@@ -38,8 +39,8 @@ public function it_can_pass_http_wg_tests(TestRecord $test): void
3839
*/
3940
public static function httpWgDataProvider(): iterable
4041
{
41-
foreach (static::$paths as $path) {
42-
foreach (TestRecordCollection::fromPath(static::$rootPath.'/'.$path) as $test) {
42+
foreach (static::$httpWgTestFilenames as $path) {
43+
foreach (TestRecordCollection::fromPath(static::$rootPath.'/'.ltrim($path, '/')) as $test) {
4344
yield $test->name => [$test];
4445
}
4546
}

src/TestRecordCollection.php

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,17 @@
99
use JsonException;
1010
use RuntimeException;
1111
use function basename;
12-
use function fclose;
13-
use function fopen;
14-
use function is_resource;
12+
use function file_get_contents;
1513
use function json_decode;
16-
use function stream_get_contents;
1714

1815
/**
1916
* @implements IteratorAggregate<string, TestRecord>
2017
* @phpstan-import-type RecordData from TestRecord
2118
*/
2219
final class TestRecordCollection implements IteratorAggregate
2320
{
24-
/** @var array<string, TestRecord> */
25-
private array $elements = [];
26-
27-
private function __construct()
21+
/** @param array<string, TestRecord> $elements */
22+
private function __construct(private array $elements = [])
2823
{
2924
}
3025

@@ -38,30 +33,17 @@ public function add(TestRecord $test): void
3833
}
3934

4035
/**
41-
* @param resource|null $context
42-
*
4336
* @throws JsonException|RuntimeException
4437
*/
45-
public static function fromPath(string $path, $context = null): self
38+
public static function fromPath(string $path): self
4639
{
47-
$args = [$path, 'r'];
48-
if (null !== $context) {
49-
$args[] = false;
50-
$args[] = $context;
51-
}
52-
53-
$resource = @fopen(...$args);
54-
if (!is_resource($resource)) {
40+
if (false === $content = file_get_contents($path)) {
5541
throw new RuntimeException("unable to connect to the path `$path`.");
5642
}
5743

58-
/** @var string $content */
59-
$content = stream_get_contents($resource);
60-
fclose($resource);
61-
44+
$suite = new self();
6245
/** @var array<RecordData> $records */
6346
$records = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
64-
$suite = new self();
6547
foreach ($records as $offset => $record) {
6648
$record['name'] = basename($path).' #'.($offset + 1).': '.$record['name'];
6749
$suite->add(TestRecord::fromDecoded($record));

0 commit comments

Comments
 (0)