Commit ec9dc2c3 authored by alexpott's avatar alexpott

Issue #1935908 by ParisLiakos, msonnabaum: Convert Graph tests to phpunit.

parent bdf7b002
...@@ -2,20 +2,22 @@ ...@@ -2,20 +2,22 @@
/** /**
* @file * @file
* Definition of Drupal\system\Tests\Graph\GraphUnitTest. * Contains \Drupal\Tests\Component\Graph\GraphTest.
*/ */
namespace Drupal\system\Tests\Graph; namespace Drupal\Tests\Component\Graph;
use Drupal\Component\Graph\Graph; use Drupal\Component\Graph\Graph;
use Drupal\simpletest\UnitTestBase; use Drupal\Tests\UnitTestCase;
/** /**
* Unit tests for the graph handling features. * Unit tests for the graph handling features.
* *
* @see Drupal\Component\Graph\Graph * @see Drupal\Component\Graph\Graph
* @group Graph
*/ */
class GraphUnitTest extends UnitTestBase { class GraphTest extends UnitTestCase {
public static function getInfo() { public static function getInfo() {
return array( return array(
'name' => 'Directed acyclic graph manipulation', 'name' => 'Directed acyclic graph manipulation',
...@@ -27,7 +29,7 @@ public static function getInfo() { ...@@ -27,7 +29,7 @@ public static function getInfo() {
/** /**
* Test depth-first-search features. * Test depth-first-search features.
*/ */
function testDepthFirstSearch() { public function testDepthFirstSearch() {
// The sample graph used is: // The sample graph used is:
// 1 --> 2 --> 3 5 ---> 6 // 1 --> 2 --> 3 5 ---> 6
// | ^ ^ // | ^ ^
...@@ -72,7 +74,7 @@ function testDepthFirstSearch() { ...@@ -72,7 +74,7 @@ function testDepthFirstSearch() {
$this->assertReversePaths($graph, $expected_reverse_paths); $this->assertReversePaths($graph, $expected_reverse_paths);
// Assert that DFS didn't created "missing" vertexes automatically. // Assert that DFS didn't created "missing" vertexes automatically.
$this->assertFALSE(isset($graph[6]), 'Vertex 6 has not been created'); $this->assertFalse(isset($graph[6]), 'Vertex 6 has not been created');
$expected_components = array( $expected_components = array(
array(1, 2, 3, 4, 5, 7), array(1, 2, 3, 4, 5, 7),
...@@ -91,9 +93,15 @@ function testDepthFirstSearch() { ...@@ -91,9 +93,15 @@ function testDepthFirstSearch() {
} }
/** /**
* Return a normalized version of a graph. * Normalizes a graph.
*
* @param $graph
* A graph array processed by \Drupal\Component\Graph\Graph::searchAndSort()
*
* @return array
* The normalized version of a graph.
*/ */
function normalizeGraph($graph) { protected function normalizeGraph($graph) {
$normalized_graph = array(); $normalized_graph = array();
foreach ($graph as $vertex => $edges) { foreach ($graph as $vertex => $edges) {
// Create vertex even if it hasn't any edges. // Create vertex even if it hasn't any edges.
...@@ -109,17 +117,16 @@ function normalizeGraph($graph) { ...@@ -109,17 +117,16 @@ function normalizeGraph($graph) {
* Verify expected paths in a graph. * Verify expected paths in a graph.
* *
* @param $graph * @param $graph
* A graph array processed by * A graph array processed by \Drupal\Component\Graph\Graph::searchAndSort()
* Drupal\Component\Graph\Graph::searchAndSort().
* @param $expected_paths * @param $expected_paths
* An associative array containing vertices with their expected paths. * An associative array containing vertices with their expected paths.
*/ */
function assertPaths($graph, $expected_paths) { protected function assertPaths($graph, $expected_paths) {
foreach ($expected_paths as $vertex => $paths) { foreach ($expected_paths as $vertex => $paths) {
// Build an array with keys = $paths and values = TRUE. // Build an array with keys = $paths and values = TRUE.
$expected = array_fill_keys($paths, TRUE); $expected = array_fill_keys($paths, TRUE);
$result = isset($graph[$vertex]['paths']) ? $graph[$vertex]['paths'] : array(); $result = isset($graph[$vertex]['paths']) ? $graph[$vertex]['paths'] : array();
$this->assertEqual($expected, $result, format_string('Expected paths for vertex @vertex: @expected-paths, got @paths', array('@vertex' => $vertex, '@expected-paths' => $this->displayArray($expected, TRUE), '@paths' => $this->displayArray($result, TRUE)))); $this->assertEquals($expected, $result, sprintf('Expected paths for vertex %s: %s, got %s', $vertex, $this->displayArray($expected, TRUE), $this->displayArray($result, TRUE)));
} }
} }
...@@ -127,17 +134,17 @@ function assertPaths($graph, $expected_paths) { ...@@ -127,17 +134,17 @@ function assertPaths($graph, $expected_paths) {
* Verify expected reverse paths in a graph. * Verify expected reverse paths in a graph.
* *
* @param $graph * @param $graph
* A graph array processed by Drupal\Component\Graph\Graph::searchAndSort(). * A graph array processed by \Drupal\Component\Graph\Graph::searchAndSort()
* @param $expected_reverse_paths * @param $expected_reverse_paths
* An associative array containing vertices with their expected reverse * An associative array containing vertices with their expected reverse
* paths. * paths.
*/ */
function assertReversePaths($graph, $expected_reverse_paths) { protected function assertReversePaths($graph, $expected_reverse_paths) {
foreach ($expected_reverse_paths as $vertex => $paths) { foreach ($expected_reverse_paths as $vertex => $paths) {
// Build an array with keys = $paths and values = TRUE. // Build an array with keys = $paths and values = TRUE.
$expected = array_fill_keys($paths, TRUE); $expected = array_fill_keys($paths, TRUE);
$result = isset($graph[$vertex]['reverse_paths']) ? $graph[$vertex]['reverse_paths'] : array(); $result = isset($graph[$vertex]['reverse_paths']) ? $graph[$vertex]['reverse_paths'] : array();
$this->assertEqual($expected, $result, format_string('Expected reverse paths for vertex @vertex: @expected-paths, got @paths', array('@vertex' => $vertex, '@expected-paths' => $this->displayArray($expected, TRUE), '@paths' => $this->displayArray($result, TRUE)))); $this->assertEquals($expected, $result, sprintf('Expected reverse paths for vertex %s: %s, got %s', $vertex, $this->displayArray($expected, TRUE), $this->displayArray($result, TRUE)));
} }
} }
...@@ -145,11 +152,11 @@ function assertReversePaths($graph, $expected_reverse_paths) { ...@@ -145,11 +152,11 @@ function assertReversePaths($graph, $expected_reverse_paths) {
* Verify expected components in a graph. * Verify expected components in a graph.
* *
* @param $graph * @param $graph
* A graph array processed by Drupal\Component\Graph\Graph::searchAndSort(). * A graph array processed by \Drupal\Component\Graph\Graph::searchAndSort().
* @param $expected_components * @param $expected_components
* An array containing of components defined as a list of their vertices. * An array containing of components defined as a list of their vertices.
*/ */
function assertComponents($graph, $expected_components) { protected function assertComponents($graph, $expected_components) {
$unassigned_vertices = array_fill_keys(array_keys($graph), TRUE); $unassigned_vertices = array_fill_keys(array_keys($graph), TRUE);
foreach ($expected_components as $component) { foreach ($expected_components as $component) {
$result_components = array(); $result_components = array();
...@@ -157,24 +164,24 @@ function assertComponents($graph, $expected_components) { ...@@ -157,24 +164,24 @@ function assertComponents($graph, $expected_components) {
$result_components[] = $graph[$vertex]['component']; $result_components[] = $graph[$vertex]['component'];
unset($unassigned_vertices[$vertex]); unset($unassigned_vertices[$vertex]);
} }
$this->assertEqual(1, count(array_unique($result_components)), format_string('Expected one unique component for vertices @vertices, got @components', array('@vertices' => $this->displayArray($component), '@components' => $this->displayArray($result_components)))); $this->assertEquals(1, count(array_unique($result_components)), sprintf('Expected one unique component for vertices %s, got %s', $this->displayArray($component), $this->displayArray($result_components)));
} }
$this->assertEqual(array(), $unassigned_vertices, format_string('Vertices not assigned to a component: @vertices', array('@vertices' => $this->displayArray($unassigned_vertices, TRUE)))); $this->assertEquals(array(), $unassigned_vertices, sprintf('Vertices not assigned to a component: %s', $this->displayArray($unassigned_vertices, TRUE)));
} }
/** /**
* Verify expected order in a graph. * Verify expected order in a graph.
* *
* @param $graph * @param $graph
* A graph array processed by Drupal\Component\Graph\Graph::searchAndSort(). * A graph array processed by \Drupal\Component\Graph\Graph::searchAndSort()
* @param $expected_orders * @param $expected_orders
* An array containing lists of vertices in their expected order. * An array containing lists of vertices in their expected order.
*/ */
function assertWeights($graph, $expected_orders) { protected function assertWeights($graph, $expected_orders) {
foreach ($expected_orders as $order) { foreach ($expected_orders as $order) {
$previous_vertex = array_shift($order); $previous_vertex = array_shift($order);
foreach ($order as $vertex) { foreach ($order as $vertex) {
$this->assertTrue($graph[$previous_vertex]['weight'] < $graph[$vertex]['weight'], format_string('Weights of @previous-vertex and @vertex are correct relative to each other', array('@previous-vertex' => $previous_vertex, '@vertex' => $vertex))); $this->assertTrue($graph[$previous_vertex]['weight'] < $graph[$vertex]['weight'], sprintf('Weights of %s and %s are correct relative to each other', $previous_vertex, $vertex));
} }
} }
} }
...@@ -187,7 +194,7 @@ function assertWeights($graph, $expected_orders) { ...@@ -187,7 +194,7 @@ function assertWeights($graph, $expected_orders) {
* @param $keys * @param $keys
* (optional) Whether to output the keys of $paths instead of the values. * (optional) Whether to output the keys of $paths instead of the values.
*/ */
function displayArray($paths, $keys = FALSE) { protected function displayArray($paths, $keys = FALSE) {
if (!empty($paths)) { if (!empty($paths)) {
return implode(', ', $keys ? array_keys($paths) : $paths); return implode(', ', $keys ? array_keys($paths) : $paths);
} }
...@@ -195,4 +202,5 @@ function displayArray($paths, $keys = FALSE) { ...@@ -195,4 +202,5 @@ function displayArray($paths, $keys = FALSE) {
return '(empty)'; return '(empty)';
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment