From e82d4438340c58f7765065cf17cf73ab9da9d6c8 Mon Sep 17 00:00:00 2001 From: Xavier D Hola Date: Thu, 7 Nov 2013 17:55:20 +0100 Subject: [PATCH] Added support resolution of for external files --- src/Sanpi/Behatch/Context/JsonContext.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Sanpi/Behatch/Context/JsonContext.php b/src/Sanpi/Behatch/Context/JsonContext.php index f0ad4fda..898f743b 100644 --- a/src/Sanpi/Behatch/Context/JsonContext.php +++ b/src/Sanpi/Behatch/Context/JsonContext.php @@ -142,7 +142,7 @@ public function theJsonShouldBeValidAccordingToTheSchema($filename) { if (is_file($filename)) { $schema = file_get_contents($filename); - $this->validate($schema); + $this->validate($schema, array('uri' => 'file://' . getcwd() . '/' . dirname($filename) . '/')); } else { throw new \RuntimeException( @@ -194,7 +194,7 @@ private function evaluateJson($json, $expression) return $result; } - private function validate($schema) + private function validate($schema, $context = null) { try { $jsonSchema = $this->decode($schema); @@ -203,8 +203,22 @@ private function validate($schema) throw new \Exception('The schema is not a valid JSON'); } + $uri = null; + if (null !== $context) { + if (is_string($context)) { + $uri = $context; + } elseif (is_array($context) && array_key_exists('uri', $context)) { + $uri = $context['uri']; + } + } + + $retriever = new \JsonSchema\Uri\UriRetriever; + $refResolver = new \JsonSchema\RefResolver($retriever); + $refResolver->resolve($jsonSchema, $uri); + $validator = new \JsonSchema\Validator(); $validator->check($this->getJson(), $jsonSchema); + if (!$validator->isValid()) { $msg = "JSON does not validate. Violations:\n"; foreach ($validator->getErrors() as $error) {