diff --git a/src/OpenApiV3Validator.php b/src/OpenApiV3Validator.php index 7358be5..5cd8f43 100644 --- a/src/OpenApiV3Validator.php +++ b/src/OpenApiV3Validator.php @@ -14,10 +14,6 @@ class OpenApiV3Validator implements OpenApiValidatorInterface { - /** - * @var OpenApiV3ToJsonSchemaConverter - */ - private $converter; /** * @var JsonSchemaValidator @@ -55,31 +51,20 @@ class OpenApiV3Validator implements OpenApiValidatorInterface private $openApiSchemaFileName; /** - * @param string $openApiSchemaFileName + * @param string $openApiSchemaFileName * @param OpenApiV3ToJsonSchemaConverter $converter - * @param SchemaStorage $schemaStorage + * @param SchemaStorage $schemaStorage */ public function __construct( string $openApiSchemaFileName, - OpenApiV3ToJsonSchemaConverter $converter, SchemaStorage $schemaStorage - ) { - $this->converter = $converter; + ) + { $this->openApiSchemaFileName = $openApiSchemaFileName; $this->schemaStorage = $schemaStorage; - $this->setupSchema(); $this->jsonSchemaValidator = new JsonSchemaValidator(new Factory($this->schemaStorage)); } - /** - * Converts OpenApi v3 schema to json schema draft 4, adds it to storage. - */ - private function setupSchema() - { - $openApiSchema = json_decode(file_get_contents($this->openApiSchemaFileName)); - $this->schemaStorage->addSchema($this->openApiSchemaFileName, $this->converter->convertDocument($openApiSchema)); - } - /** * Validate a response against the OpenApi schema. * @@ -91,11 +76,12 @@ public function validateResponse( string $method, int $responseCode, string $contentType = 'application/json' - ): bool { + ): bool + { if (!$this->emptyResponseExpected($responseCode)) { $responseSchemaPath = $this->getResponseSchemaPath(preg_replace('/\?.*/', '', $pathName), $method, $responseCode, $contentType); $responseJson = json_decode($response->getBody()); - $this->jsonSchemaValidator->validate($responseJson, (object) ['$ref' => $responseSchemaPath]); + $this->jsonSchemaValidator->validate($responseJson, (object)['$ref' => $responseSchemaPath]); } if (!$this->jsonSchemaValidator->isValid()) { @@ -108,7 +94,7 @@ public function validateResponse( /** * @param string $pathName * @param string $method - * @param int $responseCode + * @param int $responseCode * @param string $contentType * * @return string @@ -252,4 +238,5 @@ private function emptyResponseExpected($responseCode): bool { return 204 === $responseCode; } + } diff --git a/src/OpenApiValidatorFactory.php b/src/OpenApiValidatorFactory.php index b1289b7..e6286bb 100644 --- a/src/OpenApiValidatorFactory.php +++ b/src/OpenApiValidatorFactory.php @@ -15,6 +15,25 @@ class OpenApiValidatorFactory */ public function v3Validator(string $openApiSchemaFileName): OpenApiValidatorInterface { - return new OpenApiV3Validator($openApiSchemaFileName, new OpenApiV3ToJsonSchemaConverter(), new SchemaStorage()); + $converter = new OpenApiV3ToJsonSchemaConverter(); + $schemaStorage = new SchemaStorage(); + $openApiSchema = json_decode(file_get_contents($openApiSchemaFileName)); + $schemaStorage->addSchema($openApiSchemaFileName, $converter->convertDocument($openApiSchema)); + + return new OpenApiV3Validator($openApiSchemaFileName, $schemaStorage); + } + + /** + * @param object $openApiSchema + * @param string $id + * @return OpenApiValidatorInterface + */ + public static function v3ValidatorFromSchema($openApiSchema, string $id = 'file://openapi'): OpenApiValidatorInterface + { + $converter = new OpenApiV3ToJsonSchemaConverter(); + $schemaStorage = new SchemaStorage(); + $schemaStorage->addSchema($id, $converter->convertDocument($openApiSchema)); + + return new OpenApiV3Validator($id, $schemaStorage); } } diff --git a/tests/unit/OpenApiV3ValidatorTest.php b/tests/unit/OpenApiV3ValidatorTest.php index ca91a12..46adc65 100644 --- a/tests/unit/OpenApiV3ValidatorTest.php +++ b/tests/unit/OpenApiV3ValidatorTest.php @@ -26,7 +26,6 @@ public function setUp() $this->validator = new OpenApiV3Validator( 'file://'.dirname(__DIR__).'/fixtures/openapiv3-schema.json', - new OpenApiV3ToJsonSchemaConverter(), new SchemaStorage() ); } diff --git a/tests/unit/OpenApiValidatorFactoryTest.php b/tests/unit/OpenApiValidatorFactoryTest.php index 02dc598..6f79ffa 100644 --- a/tests/unit/OpenApiValidatorFactoryTest.php +++ b/tests/unit/OpenApiValidatorFactoryTest.php @@ -8,6 +8,12 @@ class OpenApiValidatorFactoryTest extends TestCase { + + /** + * @var OpenApiValidatorFactory + */ + private $factory; + public function setUp() { parent::setUp(); @@ -19,7 +25,7 @@ public function testV3ValidatorSuccess() { $this->assertInstanceOf( OpenApiV3Validator::class, - $this->factory->v3Validator('file://'.dirname(__DIR__).'/fixtures/openapiv3-schema.json') + $this->factory->v3Validator('file://' . dirname(__DIR__) . '/fixtures/openapiv3-schema.json') ); } @@ -30,7 +36,15 @@ public function testV3ValidatorFileNotFound() { $this->assertInstanceOf( OpenApiV3Validator::class, - $this->factory->v3Validator('file://'.dirname(__DIR__).'/fixtures/doesnt-exist.json') + $this->factory->v3Validator('file://' . dirname(__DIR__) . '/fixtures/doesnt-exist.json') + ); + } + + public function testV3ValidatorFromSchemaSuccess() + { + $this->assertInstanceOf( + OpenApiV3Validator::class, + OpenApiValidatorFactory::v3ValidatorFromSchema((object)[]) ); } }