Skip to content

Commit dde4f4c

Browse files
committed
feat(api tester): handle new baseline for response schema validation
1 parent 1a022c9 commit dde4f4c

5 files changed

Lines changed: 98 additions & 3 deletions

File tree

src/Config/Filters.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
final class Filters
1212
{
13+
public string $schemaValidationBaseline = 'api-tester.schema-baseline.yaml';
14+
1315
/**
1416
* @var array<array<string, string>>
1517
*/
@@ -26,11 +28,16 @@ final class Filters
2628
* @param array<array<string, string>> $include
2729
* @param array<array<string, string>> $exclude
2830
*/
29-
public function __construct(?array $include = null, ?array $exclude = null, string $baseline = null)
30-
{
31+
public function __construct(
32+
?array $include = null,
33+
?array $exclude = null,
34+
string $baseline = null,
35+
string $schemaValidationBaseline = null
36+
) {
3137
$this->include = $include ?? [];
3238
$this->exclude = $exclude ?? [];
3339
$this->baseline = $baseline ?? $this->baseline;
40+
$this->schemaValidationBaseline = $schemaValidationBaseline ?? $this->schemaValidationBaseline;
3441
}
3542

3643
/**
@@ -68,6 +75,26 @@ public function getBaseLineExclude(): array
6875
return $baseline['exclude'];
6976
}
7077

78+
/**
79+
* @return array<string>
80+
*/
81+
public function getSchemaValidationBaseline(): array
82+
{
83+
if (!file_exists($this->schemaValidationBaseline)) {
84+
return [];
85+
}
86+
87+
/** @var array{'exclude': ?array<string>} */
88+
$baseline = Yaml::parseFile($this->schemaValidationBaseline);
89+
90+
if (!isset($baseline['exclude'])) {
91+
return [];
92+
}
93+
94+
/** @var array<string> */
95+
return $baseline['exclude'];
96+
}
97+
7198
public function getBaseline(): string
7299
{
73100
return $this->baseline;

src/Preparator/TestCasesPreparator.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,17 @@ abstract class TestCasesPreparator
3030

3131
protected LoggerInterface $logger;
3232

33+
/**
34+
* @var array<string>
35+
*/
36+
protected array $schemaValidationBaseline;
37+
3338
public function __construct()
3439
{
3540
$this->tokens = new Tokens();
3641
$this->config = $this->newConfigInstance(static::getConfigFQCN());
3742
$this->logger = new NullLogger();
43+
$this->schemaValidationBaseline = [];
3844
}
3945

4046
/**
@@ -85,7 +91,10 @@ final public function doPrepare(Operations $operations): iterable
8591
$testCases = $this->prepare($operations);
8692
foreach ($testCases as $testCase) {
8793
$testCase->addExcludedFields($this->config->excludedFields);
88-
$testCase->setSchemaValidation($this->config->schemaValidation);
94+
if (!$this->config->schemaValidation
95+
|| in_array($testCase->getOperation(), $this->schemaValidationBaseline, true)) {
96+
$testCase->setSchemaValidation(false);
97+
}
8998
}
9099

91100
return $testCases;
@@ -130,6 +139,14 @@ final public function setLogger(LoggerInterface $logger): void
130139
$this->logger = $logger;
131140
}
132141

142+
/**
143+
* @param array<string> $schemaValidationBaseline
144+
*/
145+
final public function setSchemaValidationBaseline(array $schemaValidationBaseline): void
146+
{
147+
$this->schemaValidationBaseline = $schemaValidationBaseline;
148+
}
149+
133150
/**
134151
* @return class-string<PreparatorConfig>
135152
*/

src/Test/Suite.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ private function prepareTestCases(): void
148148
$allTests = collect();
149149
foreach ($this->preparators as $preparator) {
150150
$preparator->setLogger($this->logger);
151+
$preparator->setSchemaValidationBaseline($this->filters->getSchemaValidationBaseline());
151152
$operations = $this->api->getOperations()
152153
->map(
153154
static fn (Operation $op) => $op->setPreparator($preparator::getName())
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exclude:
2+
- 'operationIdInBaseline'

tests/Preparator/ExamplesPreparatorTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,54 @@ public function getExpectedTestSuites(): iterable
441441
];
442442
}
443443

444+
public function testSchemaValidationDisabledForBaselineOperation(): void
445+
{
446+
$preparator = new ExamplesPreparator();
447+
448+
$preparator->configure([
449+
'schemaValidation' => true,
450+
]);
451+
452+
$api = Api::create()->addOperation(
453+
Operation::create('operationIdInBaseline', '/test')
454+
->addQueryParameter(
455+
Parameter::create('foo')
456+
->setSchema(
457+
new Schema([
458+
'type' => 'string',
459+
])
460+
)
461+
)
462+
->addResponse(DefinitionResponse::create(200))
463+
->addExample(
464+
OperationExample::create('200.default')
465+
->setQueryParameters([
466+
'foo' => 'bar',
467+
])
468+
->setResponse(new ResponseExample())
469+
)
470+
);
471+
$filters = new Filters(
472+
schemaValidationBaseline: __DIR__ . '/../../tests/Fixtures/Config/schema-validation-baseline.yaml'
473+
);
474+
$preparator->setSchemaValidationBaseline($filters->getSchemaValidationBaseline());
475+
476+
Assert::objectsEqual(
477+
[
478+
new TestCase(
479+
ExamplesPreparator::getName() . ' - operationIdInBaseline - 200.default',
480+
OperationExample::create('operationIdInBaseline')
481+
->setPath('/test')
482+
->setQueryParameter('foo', 'bar')
483+
->setResponse(ResponseExample::create('200')),
484+
schemaValidation: false
485+
),
486+
],
487+
$preparator->doPrepare($api->getOperations()),
488+
['parent']
489+
);
490+
}
491+
444492
private function addTokens(ExamplesPreparator $preparator): void
445493
{
446494
$preparator->addToken(

0 commit comments

Comments
 (0)