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.
Calling $this->assertEqual($array1, $array2) without a third argument triggers an error. Though unusual, array comparison using ==
and ===
is allowed in PHP.
The function looks like this:
protected function assertEqual($first, $second, $message = '', $group = 'Other') {
return $this->_assert($first == $second, $message ? $message : t('%first is equal to %second', array('%first' => $first, '%second' => $second)), $group);
}
In order to be used in the default message, the arrays should be converted to a string, e.g. using print_r() or similar.
Comment | File | Size | Author |
---|---|---|---|
#6 | 299461-assert-messages.patch | 5.25 KB | Damien Tournoud |
#1 | simpletest_assertEqual_array.patch | 1.45 KB | boombatower |
Comments
Comment #1
boombatower CreditAttribution: boombatower commentedMessed around with print_r and as expected it works fine with none array values such as strings or numbers.
This patch adds print_r($variable, TRUE).
Comment #2
Damien Tournoud CreditAttribution: Damien Tournoud commentedI'm not sure it makes sense to default to a message like "1 is equal to 1".
It makes much more sense to describe the variables that were asserted rather that the values. Something like:
As seen somewhere around the big world of glib.
Comment #3
c960657 CreditAttribution: c960657 commentedI like the idea, but I'm afraid that would be pretty hard to implement. AFAICT the assert function has no way of knowing the variable names, at least not without parsing the source code or using similar hacks. One alternative would be to make another function, e.g.
function assertEqualVariable($varname1, $varname2)
, that take variable names rather than values as input. But I guess this is outside the scope of this issue.Comment #4
boombatower CreditAttribution: boombatower commentedThis is a bug, that would be a feature request, and from experience creating a PHP debug script it is very hard to display the variable names. XDebug and the like might be able to, but once again separate issue.
Comment #5
Rok Žlender CreditAttribution: Rok Žlender commentedThis is not only problematic for arrays but also objects. example
will produce an error. Print_r will work but will produce huge output if we are looking at node. So maybe just output 'Object is null' or something similar.
Comment #6
Damien Tournoud CreditAttribution: Damien Tournoud commentedThis bit us too many times already (including today, during the presentation). I suggest changing all default assert messages to static texts. Patch attached.
Comment #7
boombatower CreditAttribution: boombatower commentedApplies, looks good. I agree.
Haven't run all tests or anything like that, just ran the block test and worked fine..
Comment #8
Dries CreditAttribution: Dries commentedCommitted to CVS HEAD. Thanks! :)
Comment #9
Anonymous (not verified) CreditAttribution: Anonymous commentedAutomatically closed -- issue fixed for two weeks with no activity.