function JsonSchemaTestTrait::testNormalizedValuesAgainstJsonSchema

Test normalized values against the JSON schema.

@dataProvider jsonSchemaDataProvider

File

core/modules/serialization/tests/src/Traits/JsonSchemaTestTrait.php, line 117

Class

JsonSchemaTestTrait
Trait for testing JSON Schema validity and fit to sample data.

Namespace

Drupal\Tests\serialization\Traits

Code

public function testNormalizedValuesAgainstJsonSchema(mixed $value) : void {
  // Explicitly test the JSON Schema's validity here, because it will depend
  // on the type of the data being normalized, e.g. a class implementing the
  // interface defined in ::getSupportedTypes().
  if ($value instanceof \Closure) {
    $value = $value($this);
  }
  $schema = $this->doTestJsonSchemaIsValid($value);
  $validator = $this->getValidator();
  // Test the value validates to the schema.
  // All associative arrays must be encoded as objects.
  $normalized = json_decode(json_encode($this->getNormalizationForValue($value)));
  $validator->validate($normalized, $schema);
  $this->assertSame([], $validator->getErrors(), 'Validation errors on object ' . print_r($normalized, TRUE) . ' with schema ' . print_r($schema, TRUE));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.