JsonApiJsonSchemaTestTrait.php

Namespace

Drupal\Tests\jsonapi\Traits

File

core/modules/jsonapi/tests/src/Traits/JsonApiJsonSchemaTestTrait.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\jsonapi\Traits;

use Drupal\jsonapi\JsonApiSpec;
use Drupal\jsonapi\Normalizer\Value\CacheableNormalization;
use Drupal\Tests\serialization\Traits\JsonSchemaTestTrait;
use JsonSchema\Constraints\Factory;
use JsonSchema\Uri\UriRetriever;
use JsonSchema\Validator;
trait JsonApiJsonSchemaTestTrait {
    use JsonSchemaTestTrait {
        getNormalizationForValue as parentGetNormalizationForValue;
    }
    
    /**
     * {@inheritdoc}
     */
    protected function getJsonSchemaTestNormalizationFormat() : ?string {
        return 'api_json';
    }
    
    /**
     * {@inheritdoc}
     */
    protected function getValidator() : Validator {
        $uriRetriever = new UriRetriever();
        $uriRetriever->setTranslation('|^' . JsonApiSpec::SUPPORTED_SPECIFICATION_JSON_SCHEMA . '#?|', sprintf('file://%s/schema.json', realpath(__DIR__ . '/../../..')));
        return new Validator(new Factory(uriRetriever: $uriRetriever));
    }
    
    /**
     * {@inheritdoc}
     */
    protected function getNormalizationForValue(mixed $value) : mixed {
        $normalization = $this->parentGetNormalizationForValue($value);
        if ($normalization instanceof CacheableNormalization) {
            return $normalization->getNormalization();
        }
        return $normalization;
    }

}

Traits

Title Deprecated Summary
JsonApiJsonSchemaTestTrait

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