function RecipeValidationTest::testRecipeValidation

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Recipe/RecipeValidationTest.php \Drupal\KernelTests\Core\Recipe\RecipeValidationTest::testRecipeValidation()

Tests the validation of recipe.yml file.

@dataProvider providerRecipeValidation

Parameters

string $recipe: The contents of the `recipe.yml` file.

string[][]|null $expected_violations: (Optional) The expected validation violations, keyed by property path. Each value should be an array of error messages expected for that property.

string|null $recipe_name: (optional) The name of the directory containing `recipe.yml`, or NULL to randomly generate one.

File

core/tests/Drupal/KernelTests/Core/Recipe/RecipeValidationTest.php, line 314

Class

RecipeValidationTest
@group Recipe

Namespace

Drupal\KernelTests\Core\Recipe

Code

public function testRecipeValidation(string $recipe, ?array $expected_violations, ?string $recipe_name = NULL) : void {
  $dir = 'public://' . ($recipe_name ?? uniqid());
  mkdir($dir);
  file_put_contents($dir . '/recipe.yml', $recipe);
  try {
    Recipe::createFromDirectory($dir);
    // If there was no error, we'd better not have been expecting any.
    $this->assertNull($expected_violations, 'Validation errors were expected, but there were none.');
  } catch (RecipeFileException $e) {
    $this->assertIsArray($expected_violations, 'There were validation errors, but none were expected.');
    $this->assertIsObject($e->violations);
    $actual_violations = [];
    /** @var \Symfony\Component\Validator\ConstraintViolationInterface $violation */
    foreach ($e->violations as $violation) {
      $property_path = $violation->getPropertyPath();
      $actual_violations[$property_path][] = (string) $violation->getMessage();
    }
    ksort($actual_violations);
    ksort($expected_violations);
    $this->assertSame($expected_violations, $actual_violations);
  }
}

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