Skip to content
Snippets Groups Projects
Commit 7e343fc3 authored by Olivier Deboyser's avatar Olivier Deboyser Committed by Pierre Dureau
Browse files

Issue #3439407 by oldeb, pdureau: Add unit tests for SchemaManager classes

parent 10752024
Branches
Tags 2.0.0-alpha2
No related merge requests found
<?php
declare(strict_types=1);
namespace Drupal\Tests\ui_patterns\Kernel;
use Drupal\Component\Serialization\Yaml;
use Drupal\KernelTests\KernelTestBase;
use Drupal\ui_patterns\SchemaManager\ReferencesResolver;
/**
* Test SchemaManager parts.
*
* @group ui_patterns
*/
final class SchemaManagerTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['ui_patterns'];
/**
* Test the StreamWrapper service.
*/
public function testStreamWrapper(): void {
$correctUri = 'ui-patterns://number';
$wrongUri = 'ui-patterns://wrongProptype';
$correctPropType = file_get_contents($correctUri);
$wrongPropType = file_get_contents($wrongUri);
self::assertEquals('{"type":["number","integer"]}', $correctPropType);
self::assertEquals('[]', $wrongPropType);
}
/**
* Test the ReferencesResolver service.
*
* @dataProvider provideResolveData
*/
public function testResolve(array $schema, array $expected): void {
$resolver = new ReferencesResolver();
$result = $resolver->resolve($schema);
// Skipping everything under patternProperties keys
// to avoid having to deal with std classes in the yaml file.
$this->cleanupSchema($result);
self::assertEqualsCanonicalizing($result, $expected);
}
/**
* Provide data for testResolve.
*/
public function provideResolveData(): \Generator {
$sources = Yaml::decode(file_get_contents(__DIR__ . "/../fixtures/ReferencesResolverData.yml"));
foreach ($sources as $source) {
yield [$source['schema'], $source['expected']];
};
}
/**
* Remove all "patternProperties" keys from the given schema.
*/
private function cleanupSchema(array &$schema) {
foreach ($schema as $key => &$value) {
if ($key == 'patternProperties') {
unset($schema[$key]);
}
elseif (is_array($value)) {
$this->cleanupSchema($value);
}
}
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\ui_patterns\Unit;
use Drupal\Component\Serialization\Yaml;
use Drupal\Tests\UnitTestCase;
use Drupal\ui_patterns\SchemaManager\Canonicalizer;
/**
* @coversDefaultClass \Drupal\ui_patterns\SchemaManager\Canonicalizer
*
* @group ui_patterns
*/
final class CanonicalizerTest extends UnitTestCase {
/**
* @covers ::canonicalize
*
* @dataProvider provideCanonicalizerData
*/
public function testCanonicalize(array $schema, array $expected): void {
$canonilizer = new Canonicalizer();
$canonilized = $canonilizer->canonicalize($schema);
self::assertEqualsCanonicalizing($canonilized, $expected);
}
/**
* Provide data for testCanonicalize.
*/
public function provideCanonicalizerData(): \Generator {
$sources = Yaml::decode(file_get_contents(__DIR__ . "/../../fixtures/CanonicalizerData.yml"));
foreach ($sources as $source) {
foreach ($source['tests'] as $test) {
$label = $source['label'] . ':' . $test['label'];
yield $label => [
$test['schema'],
$test['expected'],
];
}
};
}
}
......@@ -10,38 +10,36 @@ use Drupal\ui_patterns\SchemaManager\Canonicalizer;
use Drupal\ui_patterns\SchemaManager\CompatibilityChecker;
/**
* Test description.
* @coversDefaultClass \Drupal\ui_patterns\SchemaManager\CompatibilityChecker
*
* @group ui_patterns
*/
final class SchemaCompatibilityCheckerTest extends UnitTestCase {
final class CompatibilityCheckerTest extends UnitTestCase {
/**
* Tests something.
* @covers ::isCompatible
*
* @dataProvider provideIsCompatibleData
* @dataProvider provideCompatibilityCheckerData
*/
public function testIsCompatible(array $checked_schema, array $reference_schema, bool $expected_result): void {
public function testIsCompatible(array $referenceSchema, array $testData): void {
$validator = new CompatibilityChecker(new Canonicalizer());
self::assertEquals($expected_result, $validator->isCompatible($checked_schema, $reference_schema));
foreach ($testData as $test) {
$result = $validator->isCompatible($test['schema'], $referenceSchema);
self::assertEquals((bool) $test['result'], $result);
}
}
/**
* Provide data for SchemaCompatibilityChecker::isComaptible() method.
* Provide data for testIsCompatible.
*/
public function provideIsCompatibleData() {
$data = [];
$sources = Yaml::decode(file_get_contents(__DIR__ . "/schema_compatibility_checker_data.yml"));
public function provideCompatibilityCheckerData(): \Generator {
$sources = Yaml::decode(file_get_contents(__DIR__ . "/../../fixtures/CompatibilityCheckerData.yml"));
foreach ($sources as $source) {
foreach ($source["tests"] as $test) {
$data[] = [
$test["schema"],
$source["schema"],
(bool) $test["result"],
];
}
yield $source['label'] => [
$source['schema'],
$source['tests'],
];
};
return $data;
}
}
- label: "resolveQuirks"
tests:
- label: "propertiesToItems"
schema:
type: array
properties:
type: object
properties:
title: { type: string }
expected:
type: array
items:
type: object
properties:
title: { type: string }
- label: "checkRecursivity"
schema:
type: object
properties:
foo:
type: array
properties: { type: string }
expected:
type: object
properties:
foo:
type: array
items: { type: string }
- label: "keepOnlyUsefulProperties"
tests:
- label: "boolean"
schema:
type: boolean
enum: []
pattern: "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"
maximum: 5
expected:
type: boolean
enum: []
- label: "string"
schema:
type: string
enum: []
pattern: "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"
maximum: 5
expected:
type: string
enum: []
pattern: "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"
- label: "number"
schema:
type: number
enum: []
pattern: "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"
maximum: 5
expected:
type: number
enum: []
maximum: 5
- schema:
type: array
items:
type: object
properties:
links: { $ref: "ui-patterns://links" }
expected:
type: array
items:
type: object
properties:
links:
type: array
id: "ui-patterns://links"
items:
type: object
required:
- title
properties:
title: { type: string }
url:
type: string
format: "iri-reference"
id: "ui-patterns://url"
attributes:
type: object
id: "ui-patterns://attributes"
# Skip everything under patternProperties for simplicity.
link_attributes:
type: object
id: "ui-patterns://attributes"
# Skip everything under patternProperties for simplicity.
below:
type: array
items: { type: object }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment