Skip to content

Commit

Permalink
BaseSchema: New - Add ability to manipulate UriRetriever.
Browse files Browse the repository at this point in the history
  • Loading branch information
iNewLegend committed May 3, 2021
1 parent e559f8c commit b22ddba
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions phpunit/base-schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,29 @@
use JsonSchema\Exception\ValidationException;
use JsonSchema\SchemaStorage;
use JsonSchema\Validator;
use JsonSchema\Uri\UriRetriever;
use JsonSchema\Constraints\Factory;

abstract class Base_Schema extends Elementor_Test_Base {
const HTTP_USER_AGENT = 'test-agent';

/**
* @var UriRetriever
*/
public $uriRetriever;

/**
* @var SchemaStorage
*/
public $refResolver;

public function __construct( $name = null, array $data = [], $dataName = '' ) {
parent::__construct( $name, $data, $dataName );

$this->uriRetriever = new UriRetriever();
$this->refResolver = new SchemaStorage( $this->uriRetriever );
}

public function setUp() {
parent::setUp();

Expand All @@ -21,9 +40,11 @@ protected function validate_against_schema( $data_for_validation, $schema_file )
// Since the usage system represents objects as array instead of stdClass.
$data_for_validation = json_decode( json_encode( $data_for_validation ) );

$schema = $this->refResolver->resolveRef( (object) [ '$ref' => 'file://' . $schema_file ] );

// Validate
$validator = new Validator;
$validator->validate( $data_for_validation, (object) [ '$ref' => 'file://' . $schema_file ] );
$validator = new Validator( new Factory( $this->refResolver ) );
$validator->validate( $data_for_validation, $schema );

if ( ! $validator->isValid() ) {
$error_message = 'JSON does not validate. Violations:' . PHP_EOL;
Expand Down

0 comments on commit b22ddba

Please sign in to comment.