Skip to content

Commit 8763f84

Browse files
committed
Use JsonSchemaTestSuiteSchemaPool for function tests to avoid real HTTP requests on test suite schemas
1 parent 706d9de commit 8763f84

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

src/SchemaPool/SchemaPool.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function fetchRemoteSchema(string $uri): \stdClass
3737
$streamContext = stream_context_create(
3838
[
3939
'http' => [
40-
'user_agent' => 'ropi-json-schema-evaluator'
40+
'user_agent' => 'ropi/json-schema-evaluator'
4141
],
4242
'ssl' => [
4343
'verify_peer' => false,

tests/Functional/Draft/Draft202012ShortCircuitTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Ropi\JsonSchemaEvaluator\Draft\Draft202012;
77
use Ropi\JsonSchemaEvaluator\EvaluationConfig\StaticEvaluationConfig;
88
use Ropi\JsonSchemaEvaluator\Tests\Functional\AbstractJsonSchemaTestSuite;
9+
use Ropi\JsonSchemaEvaluator\Tests\Functional\JsonSchemaTestSuiteSchemaPool;
910

1011
class Draft202012ShortCircuitTest extends AbstractJsonSchemaTestSuite
1112
{
@@ -49,7 +50,8 @@ public function test(\stdClass $testCollection): void
4950
defaultDraft: $this->draft,
5051
supportedDrafts: [
5152
$metaSchemaNoValidation
52-
]
53+
],
54+
schemaPool: new JsonSchemaTestSuiteSchemaPool()
5355
));
5456
}
5557
}

tests/Functional/Draft/Draft202012Test.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Ropi\JsonSchemaEvaluator\Draft\Draft202012;
77
use Ropi\JsonSchemaEvaluator\EvaluationConfig\StaticEvaluationConfig;
88
use Ropi\JsonSchemaEvaluator\Tests\Functional\AbstractJsonSchemaTestSuite;
9+
use Ropi\JsonSchemaEvaluator\Tests\Functional\JsonSchemaTestSuiteSchemaPool;
910

1011
class Draft202012Test extends AbstractJsonSchemaTestSuite
1112
{
@@ -43,7 +44,8 @@ public function test(\stdClass $testCollection): void
4344
defaultDraft: $this->draft,
4445
supportedDrafts: [
4546
$metaSchemaNoValidation
46-
]
47+
],
48+
schemaPool: new JsonSchemaTestSuiteSchemaPool()
4749
));
4850
}
4951
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Ropi\JsonSchemaEvaluator\Tests\Functional;
5+
6+
use Ropi\JsonSchemaEvaluator\SchemaPool\Exception\RemoteSchemaParseException;
7+
use Ropi\JsonSchemaEvaluator\SchemaPool\Exception\RemoteSchemaRequestException;
8+
use Ropi\JsonSchemaEvaluator\SchemaPool\SchemaPool;
9+
10+
class JsonSchemaTestSuiteSchemaPool extends SchemaPool
11+
{
12+
/**
13+
* @throws RemoteSchemaParseException
14+
* @throws RemoteSchemaRequestException
15+
*/
16+
public function fetchRemoteSchema(string $uri): \stdClass
17+
{
18+
if (str_starts_with($uri, 'http://localhost:1234')) {
19+
$relativeFilePath = substr($uri, strlen('http://localhost:1234'));
20+
$absoluteFilePath = dirname(__DIR__) . '/Resources/json-schema-test-suite/remotes' . $relativeFilePath;
21+
22+
$content = file_get_contents($absoluteFilePath);
23+
if (!is_string($content)) {
24+
throw new RemoteSchemaRequestException(
25+
'Failed to open json schema test suite remote schema with URI \''
26+
. $uri
27+
. '\'',
28+
1703195612
29+
);
30+
}
31+
32+
$schema = json_decode($content);
33+
if (!$schema instanceof \stdClass) {
34+
throw new RemoteSchemaParseException(
35+
'The json schema test suite remote schema URI \''
36+
. $uri
37+
. '\' does not contain a valid JSON Schema object',
38+
1703195642
39+
);
40+
}
41+
42+
return $schema;
43+
}
44+
45+
return parent::fetchRemoteSchema($uri);
46+
}
47+
}

0 commit comments

Comments
 (0)