MDL-69688 phpunit: Improve test_context_not_used

This adds a few changes to the old test_context_not_used test:

- Move it to become an advanced_test, because its mission
  is to verify that the assertEventContextNotUsed() assertion
  works as expected.
- For consistency, also move the fixtures to own phpunit fixtures.
- Add proper coverage tags, to verify that the assertion is being
  covered.
- Add a data provider to provide all the current cases and ease
  any future case that may be needed in the future. One by one
  because previously there was code never executed with the
  warning expectation causing the test to stop.
- Run them in isolation, while this is not strictly required, it's
  including external fixtures and, we'll need that isolation soon
  (for changes coming when moving the test to PHPUnit 9.6 in MDL-81266).
This commit is contained in:
Eloy Lafuente (stronk7)
2024-03-21 08:31:08 +01:00
parent 39b8e198ff
commit 0f3775088f
5 changed files with 131 additions and 42 deletions
+48
View File
@@ -339,6 +339,54 @@ class advanced_test extends \advanced_testcase {
}
}
/**
* Test the assertEventContextNotUsed() assertion.
*
* Verify that events using the event context in some of their
* methods are detected properly (will throw a warning if they are).
*
* To do so, we'll be using some fixture events (context_used_in_event_xxxx),
* that, on purpose, use the event context (incorrectly) in their methods.
*
* Note that because we are using imported fixture classes, and because we
* are testing for warnings, better we run the tests in a separate process.
*
* @param string $fixture The fixture class to use.
* @param bool $phpwarn Whether a PHP warning is expected.
*
* @runInSeparateProcess
* @dataProvider assert_event_context_not_used_provider
* @covers ::assertEventContextNotUsed
*/
public function test_assert_event_context_not_used($fixture, $phpwarn): void {
require(__DIR__ . '/fixtures/event_fixtures.php');
// Create an event that uses the event context in its get_url() and get_description() methods.
$event = $fixture::create([
'other' => [
'sample' => 1,
'xx' => 10,
],
]);
if ($phpwarn) {
$this->expectWarning();
}
$this->assertEventContextNotUsed($event);
}
/**
* Data provider for test_assert_event_context_not_used().
*
* @return array
*/
public static function assert_event_context_not_used_provider(): array {
return [
'correct' => ['\core\event\context_used_in_event_correct', false],
'wrong_get_url' => ['\core\event\context_used_in_event_get_url', true],
'wrong_get_description' => ['\core\event\context_used_in_event_get_description', true],
];
}
public function test_message_processors_reset() {
global $DB;