Skip to content

Commit

Permalink
fix test namespaces, add mtrace output module, always use mtrace outp…
Browse files Browse the repository at this point in the history
…ut module
  • Loading branch information
Glutamat42 committed Dec 16, 2024
1 parent 49f65cd commit b7662b6
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 7 deletions.
14 changes: 9 additions & 5 deletions classes/hook_listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@

namespace local_logging;

use core\di;
use core\hook\di_configuration;
use local_logging\local\output\log_output;
use local_logging\local\output\log_output_db;
use local_logging\local\output\log_output_mtrace;
use local_logging\local\output\log_output_stdout;

class hook_listener {
public static function inject_dependencies(di_configuration $hook): void {
$hook->add_definition(
id: log_output::class,
definition: function (): log_output {
if (defined('CLI_SCRIPT') || defined('PHPUNIT_TEST')) {
return new log_output_stdout();
} else {
return new log_output_db();
}
return di::get(log_output_mtrace::class);
// if (defined('CLI_SCRIPT') || defined('PHPUNIT_TEST')) {
// return new log_output_stdout();
// return di::get(log_output_mtrace::class);
// } else {
// return di::get(log_output_db::class);
// }
}
);
}
Expand Down
13 changes: 13 additions & 0 deletions classes/local/output/log_output_mtrace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace local_logging\local\output;

use local_logging\local\util;

class log_output_mtrace implements log_output {
public function output(string $message, int $level, string $component, string $title, int $time, ?int $user_id): void {
mtrace(
'[' . date('Y-m-d H:i:s', $time) . '] ' . util::convertLogLevelToString($level) . ' ' . $component . ' - ' . $title . ': ' . $message . PHP_EOL,
);
}
}
3 changes: 2 additions & 1 deletion tests/local/output/log_output_db_test.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

namespace local_logging\local\output;

use core\di;
use local_logging\lib\adler_testcase;
use local_logging\local\output\log_output_db;

defined('MOODLE_INTERNAL') || die();

Expand Down
42 changes: 42 additions & 0 deletions tests/local/output/log_output_mtrace_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace local_logging\local\output;

use local_logging\lib\adler_testcase;
use local_logging\local\util;

defined('MOODLE_INTERNAL') || die();

global $CFG;
require_once($CFG->dirroot . '/local/logging/tests/lib/adler_testcase.php');

class log_output_mtrace_test extends adler_testcase {
public function test_output() {
// Create an instance of log_output_stdout.
$logOutput = new log_output_mtrace();

// Define test data.
$message = 'Test message';
$level = util::LEVEL_TRACE; // Assuming 100 corresponds to TRACE level
$component = 'testcomponent';
$title = 'testtitle';
$time = time();
$user_id = null;

// Start output buffering.
ob_start();

// Call the output method.
$logOutput->output($message, $level, $component, $title, $time, $user_id);

// Get the output and clean the buffer.
$output = ob_get_clean();

// Assert that the output contains the expected components.
$this->assertStringContainsString('[' . date('Y-m-d H:i:s', $time) . ']', $output);
$this->assertStringContainsString('TRACE', $output);
$this->assertStringContainsString($component, $output);
$this->assertStringContainsString($title . ':', $output);
$this->assertStringContainsString($message, $output);
}
}
3 changes: 2 additions & 1 deletion tests/local/output/log_output_stdout_test.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

namespace local_logging\local\output;

use local_logging\lib\adler_testcase;
use local_logging\local\output\log_output_stdout;
use local_logging\local\util;

defined('MOODLE_INTERNAL') || die();
Expand Down

0 comments on commit b7662b6

Please sign in to comment.