You are here

protected function ConfigDiffTest::assertYamlEdit in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Config/ConfigDiffTest.php \Drupal\KernelTests\Core\Config\ConfigDiffTest::assertYamlEdit()
  2. 9 core/tests/Drupal/KernelTests/Core/Config/ConfigDiffTest.php \Drupal\KernelTests\Core\Config\ConfigDiffTest::assertYamlEdit()

Helper method to test that an edit is found in the diff of two storages.

@internal

Parameters

array $edits: A list of edits.

string $field: The field key that is being asserted.

string $type: The type of edit that is being asserted.

mixed $orig: (optional) The original value of the edit. If not supplied, assertion is skipped.

mixed $closing: (optional) The closing value of the edit. If not supplied, assertion is skipped.

2 calls to ConfigDiffTest::assertYamlEdit()
ConfigDiffTest::testCollectionDiff in core/tests/Drupal/KernelTests/Core/Config/ConfigDiffTest.php
Tests calculating the difference between two sets of config collections.
ConfigDiffTest::testDiff in core/tests/Drupal/KernelTests/Core/Config/ConfigDiffTest.php
Tests calculating the difference between two sets of configuration.

File

core/tests/Drupal/KernelTests/Core/Config/ConfigDiffTest.php, line 157

Class

ConfigDiffTest
Calculating the difference between two sets of configuration.

Namespace

Drupal\KernelTests\Core\Config

Code

protected function assertYamlEdit(array $edits, string $field, string $type, $orig = NULL, $closing = NULL) : void {
  $match = FALSE;
  foreach ($edits as $edit) {

    // Choose which section to search for the field.
    $haystack = $type == 'add' ? $edit->closing : $edit->orig;

    // Look through each line and try and find the key.
    if (is_array($haystack)) {
      foreach ($haystack as $item) {
        if (strpos($item, $field . ':') === 0) {
          $match = TRUE;

          // Assert that the edit is of the type specified.
          $this
            ->assertEquals($type, $edit->type, "The {$field} item in the diff is a {$type}");

          // If an original value was given, assert that it matches.
          if (isset($orig)) {
            $this
              ->assertSame($orig, $edit->orig, "The original value for key '{$field}' is correct.");
          }

          // If a closing value was given, assert that it matches.
          if (isset($closing)) {
            $this
              ->assertSame($closing, $edit->closing, "The closing value for key '{$field}' is correct.");
          }

          // Break out of the search entirely.
          break 2;
        }
      }
    }
  }

  // If we didn't match anything, fail.
  if (!$match) {
    $this
      ->fail("{$field} edit was not matched");
  }
}