protected function ViewUnitTestBase::assertIdenticalResultsetHelper

Performs View result assertions.

This is a helper method for ViewTestBase::assertIdenticalResultset() and ViewTestBase::assertNotIdenticalResultset().

Parameters

\Drupal\views\ViewExecutable $view: An executed View.

array $expected_result: An expected result set.

array $column_map: An associative array mapping the columns of the result set from the view (as keys) and the expected result set (as values).

string $assert_method: The TestBase assertion method to use (either 'assertIdentical' or 'assertNotIdentical').

string $message: (optional) The message to display with the assertion.

Return value

bool TRUE if the assertion succeeded, or FALSE otherwise.

See also

\Drupal\views\Tests\ViewTestBase::assertIdenticalResultset()

\Drupal\views\Tests\ViewTestBase::assertNotIdenticalResultset()

2 calls to ViewUnitTestBase::assertIdenticalResultsetHelper()
ViewUnitTestBase::assertIdenticalResultset in drupal/core/modules/views/lib/Drupal/views/Tests/ViewUnitTestBase.php
Verifies that a result set returned by a View matches expected values.
ViewUnitTestBase::assertNotIdenticalResultset in drupal/core/modules/views/lib/Drupal/views/Tests/ViewUnitTestBase.php
Verifies that a result set returned by a View differs from certain values.

File

drupal/core/modules/views/lib/Drupal/views/Tests/ViewUnitTestBase.php, line 144
Contains \Drupal\views\Tests\ViewUnitTestBase.

Class

ViewUnitTestBase
Defines a base class for Views unit testing.

Namespace

Drupal\views\Tests

Code

protected function assertIdenticalResultsetHelper($view, $expected_result, $column_map, $assert_method, $message = NULL) {

  // Convert $view->result to an array of arrays.
  $result = array();
  foreach ($view->result as $key => $value) {
    $row = array();
    foreach ($column_map as $view_column => $expected_column) {

      // The comparison will be done on the string representation of the value.
      $row[$expected_column] = (string) $value->{$view_column};
    }
    $result[$key] = $row;
  }

  // Remove the columns we don't need from the expected result.
  foreach ($expected_result as $key => $value) {
    $row = array();
    foreach ($column_map as $expected_column) {

      // The comparison will be done on the string representation of the value.
      $row[$expected_column] = (string) (is_object($value) ? $value->{$expected_column} : $value[$expected_column]);
    }
    $expected_result[$key] = $row;
  }
  $this
    ->verbose('<pre style="white-space: pre-wrap;">' . "\n\nQuery:\n" . $view->build_info['query'] . "\n\nQuery arguments:\n" . var_export($view->build_info['query_args'], TRUE) . "\n\nActual result:\n" . var_export($result, TRUE) . "\n\nExpected result:\n" . var_export($expected_result, TRUE));

  // Reset the numbering of the arrays.
  $result = array_values($result);
  $expected_result = array_values($expected_result);

  // Do the actual comparison.
  if (!isset($message)) {
    $not = strpos($assert_method, 'Not') ? 'not' : '';
    $message = format_string("Actual result <pre>\n@actual\n</pre> is {$not} identical to expected <pre>\n@expected\n</pre>", array(
      '@actual' => var_export($result, TRUE),
      '@expected' => var_export($expected_result, TRUE),
    ));
  }
  return $this
    ->{$assert_method}($result, $expected_result, $message);
}