Skip to content

Commit

Permalink
Merge pull request #171 from josepico01/Issue#169
Browse files Browse the repository at this point in the history
Issue #169: Update unit test  to disable custom metrics.
  • Loading branch information
cameron1729 committed Apr 23, 2024
2 parents 7420003 + 5102732 commit 887aa60
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions tests/tool_cloudmetrics_collect_metrics_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

namespace tool_cloudmetrics;

use DateTime;
use Exception;
use tool_cloudmetrics\metric\manager;
use tool_cloudmetrics\task\collect_metrics_task;

Expand Down Expand Up @@ -104,42 +106,58 @@ protected function setUp(): void {
* @param string $timestr The 'current' time to be used.
* @param array $meta List of [<frequency>, <last_generate_time>].
* @param array $expected The metrics that are expected to be in the result set.
* @throws \Exception
* @covers \tool_cloudmetrics\task\collect_metrics_task
* @throws Exception
*/
public function test_execute(string $timestr, array $meta, array $expected) {
$tz = \core_date::get_server_timezone_object();

$this->disable_metrics($expected);
foreach ($meta as $metric => $data) {
$classname = '\tool_cloudmetrics\metric\\' . $metric;
$metric = new $classname();
$metric->set_frequency($data[0]);
if (!empty($data[1])) {
$metric->set_last_generate_time((new \DateTime($data[1], $tz))->getTimestamp());
$metric->set_last_generate_time((new DateTime($data[1], $tz))->getTimestamp());
} else {
$metric->set_last_generate_time(0);
}
$metric->set_enabled(true);
}
$time = (new \DateTime($timestr, $tz))->getTimestamp();
$time = (new DateTime($timestr, $tz))->getTimestamp();

$mock = $this->createMock(mock_receiver::class);
$mock->expects($this->once())
->method('receive')
->with($expected);

$task = new helper_collect_metrics_task($mock);
$task->set_time($time);
ob_start();
$task->execute();
ob_end_clean();
}

/**
* This method gets all metrics (except those in sent in the data provider) and sets them into a disable state.
* @param array $expected The expected metrics.
*
*/
private function disable_metrics(array $expected) {
$metrics = metric\manager::get_metrics(true);

foreach ($metrics as $metric) {
if (!in_array($metric->get_name(), $expected)) {
$metric->set_enabled(false);
}
}
}

/**
* Provider function for test_execute.
*
* @return array[] Each element contains a time string, a list of frequency settings,
* and a list of metrics that should be measured.
*/
public function execute_provider(): array {
public static function execute_provider(): array {
return [
[
'midnight +75 minutes',
Expand Down

0 comments on commit 887aa60

Please sign in to comment.