You are here

protected function GraphTest::assertComponents in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Component/Graph/GraphTest.php \Drupal\Tests\Component\Graph\GraphTest::assertComponents()
  2. 10 core/tests/Drupal/Tests/Component/Graph/GraphTest.php \Drupal\Tests\Component\Graph\GraphTest::assertComponents()

Verify expected components in a graph.

Parameters

$graph: A graph array processed by \Drupal\Component\Graph\Graph::searchAndSort().

$expected_components: An array containing of components defined as a list of their vertices.

1 call to GraphTest::assertComponents()
GraphTest::testDepthFirstSearch in core/tests/Drupal/Tests/Component/Graph/GraphTest.php
Tests depth-first-search features.

File

core/tests/Drupal/Tests/Component/Graph/GraphTest.php, line 146

Class

GraphTest
@coversDefaultClass \Drupal\Component\Graph\Graph @group Graph

Namespace

Drupal\Tests\Component\Graph

Code

protected function assertComponents($graph, $expected_components) {
  $unassigned_vertices = array_fill_keys(array_keys($graph), TRUE);
  foreach ($expected_components as $component) {
    $result_components = [];
    foreach ($component as $vertex) {
      $result_components[] = $graph[$vertex]['component'];
      unset($unassigned_vertices[$vertex]);
    }
    $this
      ->assertCount(1, array_unique($result_components), sprintf('Expected one unique component for vertices %s, got %s', $this
      ->displayArray($component), $this
      ->displayArray($result_components)));
  }
  $this
    ->assertEquals([], $unassigned_vertices, sprintf('Vertices not assigned to a component: %s', $this
    ->displayArray($unassigned_vertices, TRUE)));
}