Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
Comment | File | Size | Author |
---|---|---|---|
#1 | drupal-1870786-1.patch | 1.89 KB | dawehner |
Comments
Comment #1
dawehnerThere seems to be no specific tests for the assertions, or they are hard to find :)
Comment #2
dawehner#1: drupal-1870786-1.patch queued for re-testing.
Comment #3
Anonymous (not verified) CreditAttribution: Anonymous commentedI 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.
It's not feature complete (but works for me). It needs support for Objects and some other general features...
Comment #4
damiankloip CreditAttribution: damiankloip commentedThe 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?
Comment #5
dawehnerso what about assertArrayEqual and assertArrayKeyEqual?
Comment #6
alansaviolobo CreditAttribution: alansaviolobo commentedwouldnt the logic be similar to assertIdenticalObject() in the same file ?
Comment #7
jhedstromUsing
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 utilizesDiffArray::diffAssocRecursive
?Comment #8
mpp CreditAttribution: mpp as a volunteer and at AmeXio commentedHave 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()
Comment #12
Mile23Current 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