Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
uses: ramsey/composer-install@v2

- name: Check coding style
run: vendor/bin/php-cs-fixer fix --dry-run
run: vendor/bin/php-cs-fixer fix --dry-run -v

type:
name: Type
Expand Down
27 changes: 20 additions & 7 deletions src/ValidatorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Osteel\OpenApi\Testing;

use cebe\openapi\Reader;
use InvalidArgumentException;
use League\OpenAPIValidation\PSR7\ValidatorBuilder as BaseValidatorBuilder;
use Osteel\OpenApi\Testing\Adapters\MessageAdapterInterface;
Expand Down Expand Up @@ -33,7 +34,7 @@ public function __construct(private BaseValidatorBuilder $validatorBuilder)
*/
public static function fromYaml(string $definition): ValidatorBuilderInterface
{
$method = is_file($definition) ? 'fromYamlFile' : 'fromYaml';
$method = self::isUrl($definition) || is_file($definition) ? 'readFromYamlFile' : 'readFromYaml';

return self::fromMethod($method, $definition);
}
Expand All @@ -45,19 +46,30 @@ public static function fromYaml(string $definition): ValidatorBuilderInterface
*/
public static function fromJson(string $definition): ValidatorBuilderInterface
{
$method = is_file($definition) ? 'fromJsonFile' : 'fromJson';
$method = self::isUrl($definition) || is_file($definition) ? 'readFromJsonFile' : 'readFromJson';

return self::fromMethod($method, $definition);
}

private static function isUrl(string $value): bool
{
if (! filter_var($value, FILTER_VALIDATE_URL)) {
return false;
}

$scheme = parse_url($value, PHP_URL_SCHEME);

return in_array($scheme, ['http', 'https'], true);
}

/**
* @inheritDoc
*
* @param string $definition the OpenAPI definition's file
*/
public static function fromYamlFile(string $definition): ValidatorBuilderInterface
{
return self::fromMethod('fromYamlFile', $definition);
return self::fromMethod('readFromYamlFile', $definition);
}

/**
Expand All @@ -67,7 +79,7 @@ public static function fromYamlFile(string $definition): ValidatorBuilderInterfa
*/
public static function fromJsonFile(string $definition): ValidatorBuilderInterface
{
return self::fromMethod('fromJsonFile', $definition);
return self::fromMethod('readFromJsonFile', $definition);
}

/**
Expand All @@ -77,7 +89,7 @@ public static function fromJsonFile(string $definition): ValidatorBuilderInterfa
*/
public static function fromYamlString(string $definition): ValidatorBuilderInterface
{
return self::fromMethod('fromYaml', $definition);
return self::fromMethod('readFromYaml', $definition);
}

/**
Expand All @@ -87,7 +99,7 @@ public static function fromYamlString(string $definition): ValidatorBuilderInter
*/
public static function fromJsonString(string $definition): ValidatorBuilderInterface
{
return self::fromMethod('fromJson', $definition);
return self::fromMethod('readFromJson', $definition);
}

/**
Expand All @@ -98,7 +110,8 @@ public static function fromJsonString(string $definition): ValidatorBuilderInter
*/
private static function fromMethod(string $method, string $definition): ValidatorBuilderInterface
{
$builder = (new BaseValidatorBuilder())->{$method}($definition);
$specObject = Reader::{$method}($definition);
$builder = (new BaseValidatorBuilder())->fromSchema($specObject);

return new ValidatorBuilder($builder);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/ValidatorBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ public function definitionProvider(): array
{
return [
['fromYaml', self::$yamlDefinition],
['fromYaml', 'https://raw.githubusercontent.com/osteel/openapi-httpfoundation-testing/refs/heads/main/tests/stubs/example.yaml'],
['fromYaml', file_get_contents(self::$yamlDefinition)],
['fromYamlFile', self::$yamlDefinition],
['fromYamlFile', 'https://raw.githubusercontent.com/osteel/openapi-httpfoundation-testing/refs/heads/main/tests/stubs/example.yaml'],
['fromYamlString', file_get_contents(self::$yamlDefinition)],
['fromJson', self::$jsonDefinition],
['fromJson', 'https://raw.githubusercontent.com/osteel/openapi-httpfoundation-testing/refs/heads/main/tests/stubs/example.json'],
['fromJson', file_get_contents(self::$jsonDefinition)],
['fromJsonFile', self::$jsonDefinition],
['fromJsonFile', 'https://raw.githubusercontent.com/osteel/openapi-httpfoundation-testing/refs/heads/main/tests/stubs/example.json'],
['fromJsonString', file_get_contents(self::$jsonDefinition)],
];
}
Expand Down