Skip to content

Commit

Permalink
MDL-67673 phpunit: Remove deprecated assertEquals() params
Browse files Browse the repository at this point in the history
The optional params of optional parameters of assertEquals() and assertNotEquals()
are deprecated in PHPUnit 8 (to be removed in PHPUnit 9):

- delta => use assertEqualsWithDelta()
- canonicalize => use assertEqualsCanonicalizing()
- ignoreCase => use assertEqualsIgnoringCase
- maxDepth => removed without replacement.

More info @ sebastianbergmann/phpunit#3341
  • Loading branch information
stronk7 committed Sep 1, 2020
1 parent 0f4e001 commit f46e445
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 56 deletions.
2 changes: 1 addition & 1 deletion favourites/tests/privacy_test.php
Expand Up @@ -164,7 +164,7 @@ public function test_add_userids_for_context() {
$user1->id,
$user2->id
];
$this->assertEquals($expected, $userlist->get_userids(), '', 0.0, 10, true);
$this->assertEqualsCanonicalizing($expected, $userlist->get_userids());

// Ask the favourites privacy api to export userids for favourites of the type we just created, in the user1 context.
$userlist = new \core_privacy\local\request\userlist($user1context, 'core_course');
Expand Down
6 changes: 3 additions & 3 deletions lib/ddl/tests/ddl_test.php
Expand Up @@ -574,7 +574,7 @@ public function test_row_size_limits() {
$id = $DB->insert_record('test_innodb', $data);
$expected = (array)$data;
$expected['id'] = (string)$id;
$this->assertEquals($expected, (array)$DB->get_record('test_innodb', array('id' => $id)), '', 0, 10, true);
$this->assertEqualsCanonicalizing($expected, (array)$DB->get_record('test_innodb', array('id' => $id)));
} catch (dml_exception $e) {
// Give some nice error message when known problematic MySQL with InnoDB detected.
if ($DB->get_dbfamily() === 'mysql') {
Expand Down Expand Up @@ -607,7 +607,7 @@ public function test_row_size_limits() {
$id = $DB->insert_record('test_innodb', $data);
$expected = (array)$data;
$expected['id'] = (string)$id;
$this->assertEquals($expected, (array)$DB->get_record('test_innodb', array('id' => $id)), '', 0, 10, true);
$this->assertEqualsCanonicalizing($expected, (array)$DB->get_record('test_innodb', array('id' => $id)));
}

$dbman->drop_table($table);
Expand All @@ -628,7 +628,7 @@ public function test_row_size_limits() {
$id = $DB->insert_record('test_innodb', $data);
$expected = (array)$data;
$expected['id'] = (string)$id;
$this->assertEquals($expected, (array)$DB->get_record('test_innodb', array('id' => $id)), '', 0, 10, true);
$this->assertEqualsCanonicalizing($expected, (array)$DB->get_record('test_innodb', array('id' => $id)));

$dbman->drop_table($table);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/dml/tests/dml_test.php
Expand Up @@ -1410,7 +1410,7 @@ public function test_export_table_recordset() {
$rids[] = $record->id;
}
$rs->close();
$this->assertEquals($ids, $rids, '', 0, 0, true);
$this->assertEqualsCanonicalizing($ids, $rids);
}

public function test_get_records() {
Expand Down Expand Up @@ -3824,7 +3824,7 @@ public function test_cast_char2real() {
$this->assertSame('91.10', next($records)->name);
// And verify we can operate with them without too much problem with at least 6 decimals scale accuracy.
$sql = "SELECT AVG(" . $DB->sql_cast_char2real('name') . ") FROM {{$tablename}}";
$this->assertEquals(37.44444443333333, (float)$DB->get_field_sql($sql), '', 1.0E-6);
$this->assertEqualsWithDelta(37.44444443333333, (float)$DB->get_field_sql($sql), 1.0E-6);

// Casting text field.
$sql = "SELECT * FROM {{$tablename}} WHERE ".$DB->sql_cast_char2real('nametext', true)." > res";
Expand All @@ -3839,7 +3839,7 @@ public function test_cast_char2real() {
$this->assertSame('91.10', next($records)->nametext);
// And verify we can operate with them without too much problem with at least 6 decimals scale accuracy.
$sql = "SELECT AVG(" . $DB->sql_cast_char2real('nametext', true) . ") FROM {{$tablename}}";
$this->assertEquals(37.44444443333333, (float)$DB->get_field_sql($sql), '', 1.0E-6);
$this->assertEqualsWithDelta(37.44444443333333, (float)$DB->get_field_sql($sql), 1.0E-6);

// Check it works with values passed as param.
$sql = "SELECT name FROM {{$tablename}} WHERE FLOOR(res - " . $DB->sql_cast_char2real(':param') . ") = 0";
Expand Down
2 changes: 1 addition & 1 deletion lib/phpunit/tests/basic_test.php
Expand Up @@ -72,7 +72,7 @@ public function test_assert_behaviour() {
$this->assertNotEquals($a, $b);
$this->assertNotEquals($a, $d);
$this->assertEquals($a, $c);
$this->assertEquals($a, $b, '', 0, 10, true);
$this->assertEqualsCanonicalizing($a, $b);

// Objects.
$a = new stdClass();
Expand Down
17 changes: 9 additions & 8 deletions lib/tests/accesslib_test.php
Expand Up @@ -676,13 +676,13 @@ public function test_get_roles_with_capability() {
assign_capability('moodle/backup:backupcourse', CAP_PREVENT, $teacher->id, $frontcontext->id);

$roles = get_roles_with_capability('moodle/backup:backupcourse');
$this->assertEquals(array($teacher->id, $manager->id), array_keys($roles), '', 0, 10, true);
$this->assertEqualsCanonicalizing(array($teacher->id, $manager->id), array_keys($roles), true);

$roles = get_roles_with_capability('moodle/backup:backupcourse', CAP_ALLOW);
$this->assertEquals(array($manager->id), array_keys($roles), '', 0, 10, true);
$this->assertEqualsCanonicalizing(array($manager->id), array_keys($roles), true);

$roles = get_roles_with_capability('moodle/backup:backupcourse', null, $syscontext);
$this->assertEquals(array($manager->id), array_keys($roles), '', 0, 10, true);
$this->assertEqualsCanonicalizing(array($manager->id), array_keys($roles), true);
}

/**
Expand Down Expand Up @@ -762,7 +762,8 @@ public function test_get_all_roles() {
$role = reset($allroles);
$role = (array)$role;

$this->assertEquals(array('id', 'name', 'shortname', 'description', 'sortorder', 'archetype'), array_keys($role), '', 0, 10, true);
$this->assertEqualsCanonicalizing(array('id', 'name', 'shortname', 'description', 'sortorder', 'archetype'),
array_keys($role));

foreach ($allroles as $roleid => $role) {
$this->assertEquals($role->id, $roleid);
Expand All @@ -784,7 +785,7 @@ public function test_get_all_roles() {
$role = reset($allroles);
$role = (array)$role;

$this->assertEquals(array('id', 'name', 'shortname', 'description', 'sortorder', 'archetype', 'coursealias'), array_keys($role), '', 0, 10, true);
$this->assertEqualsCanonicalizing(array('id', 'name', 'shortname', 'description', 'sortorder', 'archetype', 'coursealias'), array_keys($role));

foreach ($allroles as $roleid => $role) {
$this->assertEquals($role->id, $roleid);
Expand Down Expand Up @@ -3789,15 +3790,15 @@ public function test_count_role_users() {
$generator->role_assign($roleid2, $user5->id, context_coursecat::instance($category->id));
// Check that the correct users are found on the course.
$this->assertEquals(2, count_role_users($roleid1, context_course::instance($course->id), false));
$this->assertEquals(3, count_role_users($roleid1, context_course::instance($course->id), true));
$this->assertEquals(3, count_role_users($roleid1, context_course::instance($course->id)));
// Check for the category.
$this->assertEquals(1, count_role_users($roleid1, context_coursecat::instance($category->id), false));
$this->assertEquals(1, count_role_users($roleid1, context_coursecat::instance($category->id), true));
$this->assertEquals(1, count_role_users($roleid1, context_coursecat::instance($category->id)));
// Have a user with the same role at both the category and course level.
$generator->role_assign($roleid1, $user1->id, context_coursecat::instance($category->id));
// The course level checks should remain the same.
$this->assertEquals(2, count_role_users($roleid1, context_course::instance($course->id), false));
$this->assertEquals(3, count_role_users($roleid1, context_course::instance($course->id), true));
$this->assertEquals(3, count_role_users($roleid1, context_course::instance($course->id)));
}

/**
Expand Down
28 changes: 13 additions & 15 deletions lib/tests/blocklib_test.php
Expand Up @@ -98,15 +98,15 @@ public function test_add_regions() {
// Exercise SUT.
$this->blockmanager->add_regions($regions, false);
// Validate.
$this->assertEquals($regions, $this->blockmanager->get_regions(), '', 0, 10, true);
$this->assertEqualsCanonicalizing($regions, $this->blockmanager->get_regions());
}

public function test_add_region_twice() {
// Exercise SUT.
$this->blockmanager->add_region('a-region-name', false);
$this->blockmanager->add_region('another-region', false);
// Validate.
$this->assertEquals(array('a-region-name', 'another-region'), $this->blockmanager->get_regions(), '', 0, 10, true);
$this->assertEqualsCanonicalizing(array('a-region-name', 'another-region'), $this->blockmanager->get_regions());
}

public function test_cannot_add_region_after_loaded() {
Expand Down Expand Up @@ -142,7 +142,7 @@ public function test_add_custom_regions() {
// Exercise SUT.
$this->blockmanager->add_regions($regions);
// Validate.
$this->assertEquals($regions, $this->blockmanager->get_regions(), '', 0, 10, true);
$this->assertEqualsCanonicalizing($regions, $this->blockmanager->get_regions());
$this->assertTrue(isset($SESSION->custom_block_regions));
$this->assertArrayHasKey('phpunit-block-test', $SESSION->custom_block_regions);
$this->assertTrue(in_array('another-custom-region', $SESSION->custom_block_regions['phpunit-block-test']));
Expand All @@ -156,11 +156,9 @@ public function test_add_custom_region_twice() {
$this->blockmanager->add_region('a-custom-region-name');
$this->blockmanager->add_region('another-custom-region');
// Validate.
$this->assertEquals(
$this->assertEqualsCanonicalizing(
array('a-custom-region-name', 'another-custom-region'),
$this->blockmanager->get_regions(),
'', 0, 10, true
);
$this->blockmanager->get_regions());
}

/**
Expand Down Expand Up @@ -198,17 +196,17 @@ public function test_cannot_change_default_region_after_loaded() {
}

public function test_matching_page_type_patterns() {
$this->assertEquals(array('site-index', 'site-index-*', 'site-*', '*'),
matching_page_type_patterns('site-index'), '', 0, 10, true);
$this->assertEqualsCanonicalizing(array('site-index', 'site-index-*', 'site-*', '*'),
matching_page_type_patterns('site-index'));

$this->assertEquals(array('mod-quiz-report-overview', 'mod-quiz-report-overview-*', 'mod-quiz-report-*', 'mod-quiz-*', 'mod-*', '*'),
matching_page_type_patterns('mod-quiz-report-overview'), '', 0, 10, true);
$this->assertEqualsCanonicalizing(array('mod-quiz-report-overview', 'mod-quiz-report-overview-*', 'mod-quiz-report-*', 'mod-quiz-*', 'mod-*', '*'),
matching_page_type_patterns('mod-quiz-report-overview'));

$this->assertEquals(array('mod-forum-view', 'mod-*-view', 'mod-forum-view-*', 'mod-forum-*', 'mod-*', '*'),
matching_page_type_patterns('mod-forum-view'), '', 0, 10, true);
$this->assertEqualsCanonicalizing(array('mod-forum-view', 'mod-*-view', 'mod-forum-view-*', 'mod-forum-*', 'mod-*', '*'),
matching_page_type_patterns('mod-forum-view'));

$this->assertEquals(array('mod-forum-index', 'mod-*-index', 'mod-forum-index-*', 'mod-forum-*', 'mod-*', '*'),
matching_page_type_patterns('mod-forum-index'), '', 0, 10, true);
$this->assertEqualsCanonicalizing(array('mod-forum-index', 'mod-*-index', 'mod-forum-index-*', 'mod-forum-*', 'mod-*', '*'),
matching_page_type_patterns('mod-forum-index'));
}

protected function get_a_page_and_block_manager($regions, $context, $pagetype, $subpage = '') {
Expand Down
2 changes: 1 addition & 1 deletion lib/tests/filterlib_test.php
Expand Up @@ -339,7 +339,7 @@ protected function remove_all_filters_from_config() {
}

private function assert_filter_list($expectedfilters, $filters) {
$this->assertEquals($expectedfilters, array_keys($filters), '', 0, 10, true);
$this->assertEqualsCanonicalizing($expectedfilters, array_keys($filters));
}

public function test_globally_on_is_returned() {
Expand Down
16 changes: 7 additions & 9 deletions lib/tests/grouplib_test.php
Expand Up @@ -1109,10 +1109,9 @@ public function test_groups_get_all_groups_in_grouping_with_members() {
// Test without userid.
$groups = groups_get_all_groups($course1->id, null, $c1grouping1->id, 'g.*', true);

$this->assertEquals(
$this->assertEqualsCanonicalizing(
[$c1group1->id, $c1group2->id],
array_keys($groups),
'', 0.0, 10, true
array_keys($groups)
);
$this->assertEquals(
[$c1user1->id => $c1user1->id, $c12user1->id => $c12user1->id],
Expand All @@ -1127,10 +1126,9 @@ public function test_groups_get_all_groups_in_grouping_with_members() {
$groups = groups_get_all_groups($course1->id, $c1user1->id, $c1grouping1->id, 'g.*', true);

$this->assertEquals([$c1group1->id], array_keys($groups));
$this->assertEquals(
$this->assertEqualsCanonicalizing(
[$c1user1->id, $c12user1->id],
$groups[$c1group1->id]->members,
'', 0.0, 10, true
$groups[$c1group1->id]->members
);
}

Expand Down Expand Up @@ -1847,12 +1845,12 @@ public function test_groups_get_activity_shared_group_members() {
// Retrieve users sharing groups with user1.
$members = groups_get_activity_shared_group_members($cm, $user1->id);
$this->assertCount(2, $members);
$this->assertEquals([$user1->id, $user2->id], array_keys($members), '', 0.0, 10, true);
$this->assertEqualsCanonicalizing([$user1->id, $user2->id], array_keys($members));

// Retrieve users sharing groups with user2.
$members = groups_get_activity_shared_group_members($cm, $user2->id);
$this->assertCount(2, $members);
$this->assertEquals([$user1->id, $user2->id], array_keys($members), '', 0.0, 10, true);
$this->assertEqualsCanonicalizing([$user1->id, $user2->id], array_keys($members));

// Retrieve users sharing groups with user3.
$members = groups_get_activity_shared_group_members($cm, $user3->id);
Expand Down Expand Up @@ -1881,6 +1879,6 @@ public function test_groups_get_activity_shared_group_members() {
$generator->create_group_member(array('groupid' => $group3->id, 'userid' => $user1->id));
$members = groups_get_activity_shared_group_members($cm, $user1->id);
$this->assertCount(2, $members); // Now I see members of group 3.
$this->assertEquals([$user1->id, $user3->id], array_keys($members), '', 0.0, 10, true);
$this->assertEqualsCanonicalizing([$user1->id, $user3->id], array_keys($members));
}
}
10 changes: 5 additions & 5 deletions lib/tests/mathslib_test.php
Expand Up @@ -295,19 +295,19 @@ public function test_rounding_function() {

public function test_scientific_notation() {
$formula = new calc_formula('=10e10');
$this->assertEquals(1e11, $formula->evaluate(), '', 1e11*1e-15);
$this->assertEqualsWithDelta(1e11, $formula->evaluate(), 1e11*1e-15);

$formula = new calc_formula('=10e-10');
$this->assertEquals(1e-9, $formula->evaluate(), '', 1e11*1e-15);
$this->assertEqualsWithDelta(1e-9, $formula->evaluate(), 1e11*1e-15);

$formula = new calc_formula('=10e+10');
$this->assertEquals(1e11, $formula->evaluate(), '', 1e11*1e-15);
$this->assertEqualsWithDelta(1e11, $formula->evaluate(), 1e11*1e-15);

$formula = new calc_formula('=10e10*5');
$this->assertEquals(5e11, $formula->evaluate(), '', 1e11*1e-15);
$this->assertEqualsWithDelta(5e11, $formula->evaluate(), 1e11*1e-15);

$formula = new calc_formula('=10e10^2');
$this->assertEquals(1e22, $formula->evaluate(), '', 1e22*1e-15);
$this->assertEqualsWithDelta(1e22, $formula->evaluate(), 1e22*1e-15);
}

public function test_rand_float() {
Expand Down
2 changes: 1 addition & 1 deletion lib/tests/moodlelib_test.php
Expand Up @@ -3937,7 +3937,7 @@ public function test_complex_random_string() {
$this->assertRegExp('/^[' . $pool . ']+$/', $result);

$result = complex_random_string();
$this->assertEquals(28, strlen($result), '', 4); // Expected length is 24 - 32.
$this->assertEqualsWithDelta(28, strlen($result), 4); // Expected length is 24 - 32.
$this->assertRegExp('/^[' . $pool . ']+$/', $result);

$this->assertDebuggingNotCalled();
Expand Down
4 changes: 2 additions & 2 deletions lib/tests/session_manager_test.php
Expand Up @@ -64,7 +64,7 @@ public function test_init_empty_session() {
$this->assertSame($GLOBALS['SESSION'], $SESSION);

$this->assertInstanceOf('stdClass', $USER);
$this->assertEquals(array('id' => 0, 'mnethostid' => 1), (array)$USER, '', 0, 10, true);
$this->assertEqualsCanonicalizing(array('id' => 0, 'mnethostid' => 1), (array)$USER);
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
$this->assertSame($GLOBALS['USER'], $USER);

Expand Down Expand Up @@ -154,7 +154,7 @@ public function test_terminate_current() {
$this->assertSame($GLOBALS['SESSION'], $SESSION);

$this->assertInstanceOf('stdClass', $USER);
$this->assertEquals(array('id' => 0, 'mnethostid' => 1), (array)$USER, '', 0, 10, true);
$this->assertEqualsCanonicalizing(array('id' => 0, 'mnethostid' => 1), (array)$USER);
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
$this->assertSame($GLOBALS['USER'], $USER);
}
Expand Down
14 changes: 7 additions & 7 deletions lib/tests/time_splittings_test.php
Expand Up @@ -247,13 +247,13 @@ public function test_periodic() {
$ranges = $pastweek->get_most_recent_prediction_range();
$range = reset($ranges);
$this->assertEquals(3, key($ranges));
$this->assertEquals(time(), $range['time'], '', 1);
$this->assertEqualsWithDelta(time(), $range['time'], 1);
// 1 second delta for the start just in case a second passes between the set_analysable call
// and this checking below.
$time = new \DateTime();
$time->sub($pastweek->periodicity());
$this->assertEquals($time->getTimestamp(), $range['start'], '', 1.0);
$this->assertEquals(time(), $range['end'], '', 1);
$this->assertEqualsWithDelta($time->getTimestamp(), $range['start'], 1.0);
$this->assertEqualsWithDelta(time(), $range['end'], 1);

$starttime = time();

Expand All @@ -264,17 +264,17 @@ public function test_periodic() {
$ranges = $upcomingweek->get_all_ranges();
$this->assertEquals(1, count($ranges));
$range = reset($ranges);
$this->assertEquals(time(), $range['time'], '', 1);
$this->assertEquals(time(), $range['start'], '', 1);
$this->assertEqualsWithDelta(time(), $range['time'], 1);
$this->assertEqualsWithDelta(time(), $range['start'], 1);
$this->assertGreaterThan(time(), $range['end']);

$this->assertCount(0, $upcomingweek->get_training_ranges());

$ranges = $upcomingweek->get_most_recent_prediction_range();
$range = reset($ranges);
$this->assertEquals(0, key($ranges));
$this->assertEquals(time(), $range['time'], '', 1);
$this->assertEquals(time(), $range['start'], '', 1);
$this->assertEqualsWithDelta(time(), $range['time'], 1);
$this->assertEqualsWithDelta(time(), $range['start'], 1);
$this->assertGreaterThanOrEqual($starttime, $range['time']);
$this->assertGreaterThanOrEqual($starttime, $range['start']);
$this->assertGreaterThan(time(), $range['end']);
Expand Down

0 comments on commit f46e445

Please sign in to comment.