Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Symfony Behat extension for automatic OpenAPI validation of HTTP requests/respon
## Features

- Automatic request/response validation against OpenAPI schemas
- Scan local directories for OpenAPI YAML files
- Load OpenAPI YAML files from local files or directories (directories are scanned recursively)
- Fetch OpenAPI specs from remote GitHub repositories (with token support for private repos)
- Separate control for request and response validation skipping
- Auto-skip request validation for 4xx responses (configurable)
Expand Down Expand Up @@ -59,7 +59,8 @@ when@test:
is_enabled: true
should_request_on_4xx_be_skipped: true
local_paths:
- '%kernel.project_dir%/docs/openapi'
- '%kernel.project_dir%/openapi.yaml' # a single spec file
- '%kernel.project_dir%/docs/openapi' # or a directory (scanned recursively)
github_sources:
- url: 'https://github.com/Owner/Repo/tree/main/docs/openapi'
token_env: 'GITHUB_TOKEN'
Expand Down
8 changes: 8 additions & 0 deletions src/SchemaLoader/LocalSchemaLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ public function loadSchemas(): array
$schemas = [];

foreach ($this->paths as $path) {
if (is_file($path)) {
if (preg_match('/\.ya?ml$/', $path) === 1) {
$schemas[(string) realpath($path)] = (string) file_get_contents($path);
}

continue;
}

if (is_dir($path) === false) {
continue;
}
Expand Down
37 changes: 37 additions & 0 deletions tests/Unit/SchemaLoader/LocalSchemaLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,43 @@ public function itLoadsYamlFilesFromDirectory(): void
}
}

#[Test]
public function itLoadsSingleYamlFile(): void
{
$tempDir = sys_get_temp_dir() . '/behat-openapi-test-' . uniqid();
mkdir($tempDir, 0o777, true);

$yamlContent = "openapi: '3.0.0'\ninfo:\n title: Test API\n version: '1.0.0'";
file_put_contents($tempDir . '/openapi.yaml', $yamlContent);
file_put_contents($tempDir . '/other.yaml', 'ignored');

try {
$loader = new LocalSchemaLoader([$tempDir . '/openapi.yaml']);
$schemas = $loader->loadSchemas();

self::assertSame([realpath($tempDir . '/openapi.yaml') => $yamlContent], $schemas);
} finally {
unlink($tempDir . '/openapi.yaml');
unlink($tempDir . '/other.yaml');
rmdir($tempDir);
}
}

#[Test]
public function itSkipsNonYamlFile(): void
{
$tempFile = sys_get_temp_dir() . '/behat-openapi-test-' . uniqid() . '.txt';
file_put_contents($tempFile, 'ignored');

try {
$loader = new LocalSchemaLoader([$tempFile]);

self::assertSame([], $loader->loadSchemas());
} finally {
unlink($tempFile);
}
}

#[Test]
public function itLoadsFromMultipleDirectories(): void
{
Expand Down
Loading