MDL-71036 phpunit: assertContains() now performs strict comparison

The methods assertContains() and assertNotContains() now perform
strict (type and value) comparison, pretty much like assertSame()
does.

A couple of new assertContainsEquals() and assertNotContainsEquals()
methods have been created to provide old (non-strict) behavior, pretty
much like assertEquals() do.

Apart from replacing the calls needing a relaxed comparison to those
new methods, there are also a couple of alternative, about how to
fix this, depending of every case:

- If the test is making any array_values() conversion, then it's better
  to remove that conversion and use assertArrayHasKey(), that is not
  strict.
- Sometimes if may be also possible to, simply, cast the expectation
  to the exact type coming in the array. I've not applied this technique
  to any of the cases in core.

Link: https://github.com/sebastianbergmann/phpunit/issues/3426
This commit is contained in:
Eloy Lafuente (stronk7) 2021-02-25 12:25:33 +01:00
parent 8940f67486
commit 3dd26fe334
27 changed files with 139 additions and 143 deletions

View file

@ -1545,7 +1545,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
$conversations = \core_message\api::get_conversations($user1->id);
// Consider first conversations is self-conversation.
$this->assertCount(7, $conversations);
$this->assertContains($gc2->id, array_column($conversations, 'id'));
$this->assertContainsEquals($gc2->id, array_column($conversations, 'id'));
// Delete all messages from an individual conversation the user is in - it should not be returned.
$this->assertTrue(\core_message\api::is_user_in_conversation($user1->id, $ic1->id));
@ -1557,7 +1557,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
$conversations = \core_message\api::get_conversations($user1->id);
// Consider first conversations is self-conversation.
$this->assertCount(6, $conversations);
$this->assertNotContains($ic1->id, array_column($conversations, 'id'));
$this->assertNotContainsEquals($ic1->id, array_column($conversations, 'id'));
}
/**
@ -5580,8 +5580,8 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
// however, we can still determine the number and ids of any recipients this way.
$this->assertCount(1, $events);
$userids = array_column($events, 'userid');
$this->assertNotContains($user1->id, $userids);
$this->assertContains($user2->id, $userids);
$this->assertNotContainsEquals($user1->id, $userids);
$this->assertContainsEquals($user2->id, $userids);
}
/**
@ -5629,9 +5629,9 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
// however, we can still determine the number and ids of any recipients this way.
$this->assertCount(2, $events);
$userids = array_column($events, 'userid');
$this->assertNotContains($user1->id, $userids);
$this->assertContains($user3->id, $userids);
$this->assertContains($user4->id, $userids);
$this->assertNotContainsEquals($user1->id, $userids);
$this->assertContainsEquals($user3->id, $userids);
$this->assertContainsEquals($user4->id, $userids);
}
/**

View file

@ -2828,14 +2828,14 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
$members = $result['members'];
$this->assertCount(3, $members);
$membersid = [$members[0]['id'], $members[1]['id'], $members[2]['id']];
$this->assertContains($user1->id, $membersid);
$this->assertContains($user2->id, $membersid);
$this->assertContains($user3->id, $membersid);
$this->assertContainsEquals($user1->id, $membersid);
$this->assertContainsEquals($user2->id, $membersid);
$this->assertContainsEquals($user3->id, $membersid);
$membersfullnames = [$members[0]['fullname'], $members[1]['fullname'], $members[2]['fullname']];
$this->assertContains(fullname($user1), $membersfullnames);
$this->assertContains(fullname($user2), $membersfullnames);
$this->assertContains(fullname($user3), $membersfullnames);
$this->assertContainsEquals(fullname($user1), $membersfullnames);
$this->assertContainsEquals(fullname($user2), $membersfullnames);
$this->assertContainsEquals(fullname($user3), $membersfullnames);
// Confirm the messages data is correct.
$messages = $result['messages'];
@ -2957,9 +2957,9 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
$members = $result['members'];
$this->assertCount(3, $members);
$membersid = [$members[0]['id'], $members[1]['id'], $members[2]['id']];
$this->assertContains($user1->id, $membersid);
$this->assertContains($user2->id, $membersid);
$this->assertContains($user3->id, $membersid);
$this->assertContainsEquals($user1->id, $membersid);
$this->assertContainsEquals($user2->id, $membersid);
$this->assertContainsEquals($user3->id, $membersid);
// Confirm the message data is correct.
$messages = $result['messages'];

View file

@ -1550,8 +1550,8 @@ class core_message_privacy_provider_testcase extends \core_privacy\tests\provide
$contextlist = new contextlist();
provider::add_contexts_for_conversations($contextlist, $user1->id, $component, $itemtype);
$this->assertCount(2, $contextlist);
$this->assertContains($coursecontext1->id, $contextlist->get_contextids());
$this->assertContains($coursecontext2->id, $contextlist->get_contextids());
$this->assertContainsEquals($coursecontext1->id, $contextlist->get_contextids());
$this->assertContainsEquals($coursecontext2->id, $contextlist->get_contextids());
// Test for user2 (is member of the conversation and has sent a message).
$contextlist = new contextlist();
@ -2012,12 +2012,12 @@ class core_message_privacy_provider_testcase extends \core_privacy\tests\provide
// There should be 5 messages - 3 individual - 2 group (course2).
$this->assertEquals(5, $DB->count_records('messages'));
$messages = array_keys($DB->get_records('messages'));
$this->assertContains($im1, $messages);
$this->assertContains($im2, $messages);
$this->assertContains($im3, $messages);
$this->assertContains($gm4, $messages);
$this->assertContains($gm5, $messages);
$messages = $DB->get_records('messages');
$this->assertArrayHasKey($im1, $messages);
$this->assertArrayHasKey($im2, $messages);
$this->assertArrayHasKey($im3, $messages);
$this->assertArrayHasKey($gm4, $messages);
$this->assertArrayHasKey($gm5, $messages);
// There should be 3 user actions - 2 for reading the message, 1 for deleting.
$this->assertEquals(3, $DB->count_records('message_user_actions'));
@ -2529,11 +2529,11 @@ class core_message_privacy_provider_testcase extends \core_privacy\tests\provide
// There should be 4 messages - 3 private + 1 group sent by user2.
$this->assertEquals(4, $DB->count_records('messages'));
$messages = array_keys($DB->get_records('messages'));
$this->assertContains($pm1, $messages);
$this->assertContains($pm2, $messages);
$this->assertContains($pm3, $messages);
$this->assertContains($gm3, $messages);
$messages = $DB->get_records('messages');
$this->assertArrayHasKey($pm1, $messages);
$this->assertArrayHasKey($pm2, $messages);
$this->assertArrayHasKey($pm3, $messages);
$this->assertArrayHasKey($gm3, $messages);
// There should be 3 user actions - 2 for reading the message, one for deleting.
$this->assertEquals(3, $DB->count_records('message_user_actions'));
@ -2763,11 +2763,11 @@ class core_message_privacy_provider_testcase extends \core_privacy\tests\provide
// There should be 4 messages - 3 private + 1 group sent by user3.
$this->assertEquals(4, $DB->count_records('messages'));
$messages = array_keys($DB->get_records('messages'));
$this->assertContains($pm1, $messages);
$this->assertContains($pm2, $messages);
$this->assertContains($pm3, $messages);
$this->assertContains($gm3, $messages);
$messages = $DB->get_records('messages');
$this->assertArrayHasKey($pm1, $messages);
$this->assertArrayHasKey($pm2, $messages);
$this->assertArrayHasKey($pm3, $messages);
$this->assertArrayHasKey($gm3, $messages);
// There should be 3 user actions - 2 for reading the message, one for deleting.
$this->assertEquals(3, $DB->count_records('message_user_actions'));