Skip to content

Commit

Permalink
MDL-67673 phpunit: Remove expectedException annotations
Browse files Browse the repository at this point in the history
While this is not strictly required, because removal will
happen in PHPUnit 9.0, we are already getting rid of all
uses in core.

From release notes:https://phpunit.de/announcements/phpunit-8.html

The annotations `@expectedException`, `@expectedExceptionCode`,
`@expectedExceptionMessage`, and `@expectedExceptionMessageRegExp`
are now deprecated.
Using these annotations will trigger a deprecation warning
in PHPUnit 8 and in PHPUnit 9 these annotations will be removed.

Also, all uses of expectExceptionMessageRegExp() has been moved
to expectExceptionMessageMatches(). See sebastianbergmann/phpunit#3957

TODO: Various weirdness found while doing the changes with these tests:
- vendor/bin/phpunit lib/tests/exporter_test.php (created MDL-69700)
- vendor/bin/phpunit competency/tests/external_test.php (same issue than prev one)
- vendor/bin/phpunit question/engine/tests/questionengine_test.php (created MDL-69624)
- vendor/bin/phpunit lib/tests/event_test.php (created MDL-69688)
  • Loading branch information
stronk7 committed Sep 20, 2020
1 parent 202b74d commit 63aa1c7
Show file tree
Hide file tree
Showing 59 changed files with 345 additions and 633 deletions.
3 changes: 1 addition & 2 deletions admin/tool/analytics/tests/external_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,14 @@ public function test_potential_contexts() {

/**
* test_potential_contexts description
*
* @expectedException required_capability_exception
*/
public function test_potential_contexts_no_manager() {
$this->resetAfterTest();

$user = $this->getDataGenerator()->create_user();
$this->setUser($user);

$this->expectException(required_capability_exception::class);
$this->assertCount(2, \tool_analytics\external::potential_contexts());
}
}
16 changes: 4 additions & 12 deletions admin/tool/cohortroles/tests/api_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,25 @@ protected function setUp(): void {
cohort_add_member($this->cohort->id, $this->userassignover->id);
}

/**
* @expectedException required_capability_exception
*/
public function test_create_cohort_role_assignment_without_permission() {
$this->setUser($this->userassignto);
$params = (object) array(
'userid' => $this->userassignto->id,
'roleid' => $this->roleid,
'cohortid' => $this->cohort->id
);
$this->expectException(required_capability_exception::class);
api::create_cohort_role_assignment($params);
}

/**
* @expectedException core_competency\invalid_persistent_exception
*/
public function test_create_cohort_role_assignment_with_invalid_data() {
$this->setAdminUser();
$params = (object) array(
'userid' => $this->userassignto->id,
'roleid' => -8,
'cohortid' => $this->cohort->id
);
$this->expectException(\core_competency\invalid_persistent_exception::class);
api::create_cohort_role_assignment($params);
}

Expand All @@ -100,9 +96,6 @@ public function test_create_cohort_role_assignment() {
$this->assertEquals($result->get('cohortid'), $this->cohort->id);
}

/**
* @expectedException required_capability_exception
*/
public function test_delete_cohort_role_assignment_without_permission() {
$this->setAdminUser();
$params = (object) array(
Expand All @@ -112,12 +105,10 @@ public function test_delete_cohort_role_assignment_without_permission() {
);
$result = api::create_cohort_role_assignment($params);
$this->setUser($this->userassignto);
$this->expectException(required_capability_exception::class);
api::delete_cohort_role_assignment($result->get('id'));
}

/**
* @expectedException dml_missing_record_exception
*/
public function test_delete_cohort_role_assignment_with_invalid_data() {
$this->setAdminUser();
$params = (object) array(
Expand All @@ -126,6 +117,7 @@ public function test_delete_cohort_role_assignment_with_invalid_data() {
'cohortid' => $this->cohort->id
);
$result = api::create_cohort_role_assignment($params);
$this->expectException(dml_missing_record_exception::class);
api::delete_cohort_role_assignment($result->get('id') + 1);
}

Expand Down
18 changes: 6 additions & 12 deletions admin/tool/langimport/tests/events_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ public function test_langpack_updated() {
$this->assertEquals(context_system::instance(), $event->get_context());
}

/**
* @expectedException coding_exception
* @expectedExceptionMessage The 'langcode' value must be set to a valid language code
*/
public function test_langpack_updated_validation() {

$this->expectException('coding_exception');
$this->expectExceptionMessage("The 'langcode' value must be set to a valid language code");
\tool_langimport\event\langpack_updated::event_with_langcode('broken langcode');
}

Expand All @@ -78,12 +76,10 @@ public function test_langpack_installed() {
$this->assertEquals(context_system::instance(), $event->get_context());
}

/**
* @expectedException coding_exception
* @expectedExceptionMessage The 'langcode' value must be set to a valid language code
*/
public function test_langpack_installed_validation() {

$this->expectException('coding_exception');
$this->expectExceptionMessage("The 'langcode' value must be set to a valid language code");
\tool_langimport\event\langpack_imported::event_with_langcode('broken langcode');
}

Expand All @@ -100,12 +96,10 @@ public function test_langpack_removed() {
$this->assertEquals(context_system::instance(), $event->get_context());
}

/**
* @expectedException coding_exception
* @expectedExceptionMessage The 'langcode' value must be set to a valid language code
*/
public function test_langpack_removed_validation() {

$this->expectException('coding_exception');
$this->expectExceptionMessage("The 'langcode' value must be set to a valid language code");
\tool_langimport\event\langpack_removed::event_with_langcode('broken langcode');
}
}
3 changes: 1 addition & 2 deletions admin/tool/monitor/tests/subscription_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,10 @@ public function test_magic_isset() {

/**
* Test for the magic __get method.
*
* @expectedException coding_exception
*/
public function test_magic_get() {
$this->assertEquals(20, $this->subscription->courseid);
$this->expectException(coding_exception::class);
$this->subscription->ruleid;
}
}
10 changes: 3 additions & 7 deletions admin/tool/uploadcourse/tests/course_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,24 @@
*/
class tool_uploadcourse_course_testcase extends advanced_testcase {

/**
* @expectedException coding_exception
*/
public function test_proceed_without_prepare() {
$this->resetAfterTest(true);
$mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
$updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
$data = array();
$co = new tool_uploadcourse_course($mode, $updatemode, $data);
$this->expectException(coding_exception::class);
$co->proceed();
}

/**
* @expectedException moodle_exception
*/
public function test_proceed_when_prepare_failed() {
$this->resetAfterTest(true);
$mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
$updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
$data = array();
$co = new tool_uploadcourse_course($mode, $updatemode, $data);
$this->assertFalse($co->prepare());
$this->expectException(moodle_exception::class);
$co->proceed();
}

Expand Down Expand Up @@ -1504,4 +1500,4 @@ protected function create_custom_field(\core_customfield\category_controller $ca
'configdata' => $configdata,
]);
}
}
}
8 changes: 2 additions & 6 deletions admin/tool/uploadcourse/tests/processor_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,6 @@ public function test_shortname_template() {
$this->assertEquals('ID123: Course 1', $c->shortname);
}

/**
* @expectedException moodle_exception
*/
public function test_empty_csv() {
$this->resetAfterTest(true);

Expand All @@ -174,12 +171,10 @@ public function test_empty_csv() {
$cir->init();

$options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW);
$this->expectException(moodle_exception::class);
$p = new tool_uploadcourse_processor($cir, $options, array());
}

/**
* @expectedException moodle_exception
*/
public function test_not_enough_columns() {
$this->resetAfterTest(true);

Expand All @@ -194,6 +189,7 @@ public function test_not_enough_columns() {
$cir->init();

$options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW);
$this->expectException(moodle_exception::class);
$p = new tool_uploadcourse_processor($cir, $options, array());
}

Expand Down
2 changes: 1 addition & 1 deletion analytics/tests/indicator_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public function validate_calculated_value() {
* @param string $indicatorclass
* @param string $willreturn
* @dataProvider validate_calculated_value_exceptions
* @expectedException \coding_exception
* @return null
*/
public function test_validate_calculated_value_exceptions($indicatorclass, $willreturn) {
Expand All @@ -80,6 +79,7 @@ public function test_validate_calculated_value_exceptions($indicatorclass, $will
->setMethods(['calculate_sample'])
->getMock();
$indicatormock->method('calculate_sample')->willReturn($willreturn);
$this->expectException(coding_exception::class);
list($values, $unused) = $indicatormock->calculate([1], 'notrelevanthere');

}
Expand Down
16 changes: 4 additions & 12 deletions backup/converter/moodle1/tests/moodle1_converter_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,12 @@ public function test_convert_factory() {
$this->assertInstanceOf('moodle1_converter', $converter);
}

/**
* @expectedException moodle1_convert_storage_exception
*/
public function test_stash_storage_not_created() {
$converter = convert_factory::get_converter('moodle1', $this->tempdir);
$this->expectException(moodle1_convert_storage_exception::class);
$converter->set_stash('tempinfo', 12);
}

/**
* @expectedException moodle1_convert_empty_storage_exception
*/
public function test_stash_requiring_empty_stash() {
$this->resetAfterTest(true);
$converter = convert_factory::get_converter('moodle1', $this->tempdir);
Expand All @@ -113,6 +108,7 @@ public function test_stash_requiring_empty_stash() {

} catch (moodle1_convert_empty_storage_exception $e) {
// we must drop the storage here so we are able to re-create it in the next test
$this->expectException(moodle1_convert_empty_storage_exception::class);
$converter->drop_stash_storage();
throw new moodle1_convert_empty_storage_exception('rethrowing');
}
Expand Down Expand Up @@ -440,9 +436,6 @@ public function test_convert_path_explicit_recipes() {
$this->assertSame(null, $data['nothing']);
}

/**
* @expectedException convert_path_exception
*/
public function test_grouped_data_on_nongrouped_convert_path() {
// prepare some grouped data
$data = array(
Expand All @@ -468,12 +461,10 @@ public function test_grouped_data_on_nongrouped_convert_path() {
$path = new convert_path('beer_style', '/ROOT/BEER_STYLES/BEER_STYLE');

// an attempt to apply recipes throws exception because we do not expect grouped data
$this->expectException(convert_path_exception::class);
$data = $path->apply_recipes($data);
}

/**
* @expectedException convert_path_exception
*/
public function test_grouped_convert_path_with_recipes() {
// prepare some grouped data
$data = array(
Expand Down Expand Up @@ -501,6 +492,7 @@ public function test_grouped_convert_path_with_recipes() {
$this->assertEquals('Heineken', $data['beers'][1]['beer']['name']);

// an attempt to provide explicit recipes on grouped elements throws exception
$this->expectException(convert_path_exception::class);
$path = new convert_path(
'beer_style', '/ROOT/BEER_STYLES/BEER_STYLE',
array(
Expand Down
3 changes: 1 addition & 2 deletions calendar/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,6 @@ public function test_create_calendar_events () {

/**
* Test delete_calendar_events
*
* @expectedException moodle_exception
*/
public function test_delete_calendar_events() {
global $DB, $USER;
Expand Down Expand Up @@ -282,6 +280,7 @@ public function test_delete_calendar_events() {
array('eventid' => $userevent->id, 'repeat' => 0),
array('eventid' => $groupevent->id, 'repeat' => 0)
);
$this->expectException(moodle_exception::class);
core_calendar_external::delete_calendar_events($events);
}

Expand Down
6 changes: 2 additions & 4 deletions cohort/tests/cohortlib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ public function test_cohort_add_cohort() {
$this->assertSame($newcohort->timecreated, $newcohort->timemodified);
}

/**
* @expectedException coding_exception
* @expectedExceptionMessage Missing cohort name in cohort_add_cohort().
*/
public function test_cohort_add_cohort_missing_name() {
$cohort = new stdClass();
$cohort->contextid = context_system::instance()->id;
Expand All @@ -77,6 +73,8 @@ public function test_cohort_add_cohort_missing_name() {
$cohort->description = 'test cohort desc';
$cohort->descriptionformat = FORMAT_HTML;

$this->expectException(coding_exception::class);
$this->expectExceptionMessage('Missing cohort name in cohort_add_cohort()');
cohort_add_cohort($cohort);
}

Expand Down
Loading

0 comments on commit 63aa1c7

Please sign in to comment.