Problem/Motivation

You still deal with a lot of arrays in drupal, thought it's not totally trivial to compare two arrays (as the order of the keys depense on the insertion).

#1810480: Provide the plugin_id to support views metadata integration for example is one of these issues which adds such a helper method.

Proposed resolution

Add one method to the TestBase.

CommentFileSizeAuthor
#1 drupal-1870786-1.patch1.89 KBdawehner
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dawehner’s picture

Status: Active » Needs review
Issue tags: +Needs tests
FileSize
1.89 KB

There seems to be no specific tests for the assertions, or they are hard to find :)

dawehner’s picture

#1: drupal-1870786-1.patch queued for re-testing.

Anonymous’s picture

I created such a method for my test suite (in D7). It deepen compares the arrays and provides the trace to the differing elements in the error message:
I've seen this when using PHPunit in another project end wanted this fine granuar approach in Drupal and simpletest.

class Wake2eBaseUnitTestCase extends DrupalUnitTestCase {

  public function assertArrayEqual(array $first, array $second, $message = '', $group = 'Other') {
      $results = $this->_check_test_results($first, $second);
      if (count($results)) {
        $this->assert(false, implode("\n", $results));
      }
  }

  protected function _check_test_results($first = NULL, $second = NULL, $path = '') {
    $results = array();
    if (is_array($first)) {
      if (! is_array($second)) {
        $results[] = "ERROR: \$second is not an array - at $path";
        return $results;
      }

      // Don't run assertEqual() directly, because we need to aggregate 
      // teste results
      if (count($first) != count($second)) {
        $results[] = t('Length of array differ (@first != @second) at @path.',
          array('@first' => count($first), '@second' => count($second), '@path' => $path));
        return $results;
      }

      foreach(array_keys($first) as $key) {
        $results += $this->_check_test_results($first[$key], $second[$key], $path."[$key]");
      }
    }
    else {
      // Don't run assertEqual() directly, because we need to aggregate 
      // teste results
      if($first != $second) {
        $results[] = t('Value @first is equal to value @second at @path.',
          array('@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE), '@path' => $path));
      }
    }
    return $results;
  }
}

It's not feature complete (but works for me). It needs support for Objects and some other general features...

damiankloip’s picture

The patch looks good, and it still applies :) I am just thinking, this will only check that all the values are the same, is it work also doing a version with array_diff_key?

dawehner’s picture

so what about assertArrayEqual and assertArrayKeyEqual?

alansaviolobo’s picture

Issue summary: View changes

wouldnt the logic be similar to assertIdenticalObject() in the same file ?

jhedstrom’s picture

Using array_diff() will only work on the top-level, and won't work for multidimensional arrays. Not sure if that's relevant for this use case or not, but if it is, perhaps a second assertion that utilizes DiffArray::diffAssocRecursive?

mpp’s picture

Have a look at PHP Unit. I would have expected array assertions like assertContains, assertArrayHasKey etc:: https://phpunit.de/manual/current/en/appendixes.assertions.html

Note that PHP Unit has a mixed argument assertEquals()

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Mile23’s picture

Status: Needs review » Closed (outdated)

Current PHPUnit framework tests have assertEquals(), which can compare arrays. The solution to adding this assertion to TestBase-based tests is to convert to BrowserTestBase: https://www.drupal.org/node/2783189