Most of the assert methods in VoteTest are used wrongly, and some are used in an extremely confusing/messed-up way.
Take for example:
assertFalse(empty($results['vote']), 'Results for test vote type are available.')
Because empty()
returns a boolean, this is checking that the array element is not empty. So we should be using assertNotEmpty()
instead. like this:
assertNotEmpty($results['vote'], 'Results for test vote type are available.');
I think you will agree that this is easier to understand and clearer about what is being tested.
and that's not even a confusing one. Try to understand this:
assertNotEmpty(empty($results['test']['vote_count']), 'Result removed via alter hook was not calculated.')
;
Again, empty()
returns a boolean, so by using assertNotEmpty()
like this we're checking to see if empty()
is TRUE. In other words, this should really be written as:
assertEmpty($results['test']['vote_count'], 'Result removed via alter hook was not calculated.');
The current usage is the code equivalent of a double negative.
There are a bunch of these.
This patch also fixes the misuse of assertEquals()
. The first two arguments to this function should be 1) the expected value, followed by 2) the actual value. ALL uses of assertEquals()
in this test have these arguments reversed, probably because this code was ported from using the Simpletest assertEqual()
(without the trailing 's'!) which uses the same arguments in reverse.
Fixing this is important for readability because all PHPUnit asserts have the expected value as the first argument.
This patch also changes a few assertEquals()
to assertCount()
. For example:
assertEquals(count($votes), 0, 'When an entity is deleted, the votes are also deleted.');
Becomes:
assertCount(0, $votes, 'When an entity is deleted, the votes are also deleted.');
Comment | File | Size | Author |
---|---|---|---|
#7 | interdiff-3-7.txt | 1.42 KB | TR |
#7 | 3142786-7-asserts.patch | 7.37 KB | TR |
|
Comments
Comment #2
TR CreditAttribution: TR commentedComment #3
TR CreditAttribution: TR commentedWhoops, I added one coding standards problem. Here's a new patch with that fixed.
Comment #4
TR CreditAttribution: TR commentedThis patch will have to be re-rolled after #2592875: Module override of result set isn't working and #3061823: Implement hook_votingapi_results_alter are committed. Both of those should be committed FIRST since they are fixing real problems. This current issue is more of a clean-up task to make things easier to understand and maintain.
Comment #5
pifagorComment #6
TR CreditAttribution: TR commentedComment #7
TR CreditAttribution: TR commentedI cancelled the test in #6 because the patch was wrong. Here's the correct patch and interdiff.
Comment #8
Krzysztof DomańskiNice improvement!
Comment #10
pifagor