Skip to content

Instantly share code, notes, and snippets.

@andrewnicols
Last active July 1, 2020 03:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewnicols/861c19789d941d8edcd7087af1e4efbe to your computer and use it in GitHub Desktop.
Save andrewnicols/861c19789d941d8edcd7087af1e4efbe to your computer and use it in GitHub Desktop.
<?php
class core_dml_testcase extends database_driver_testcase {
/**
* Test the database instrumentation as SQL comment.
*/
public function test_instrument_sql() {
global $CFG;
require_once($CFG->dirroot . '/lib/tests/fixtures/test_dml_instrumentation_fixture.php');
$fixture = new \core\test_dml_instrumentation_fixture($this);
$sql = "SELECT * FROM {users}";
$CFG->debugsqltrace = 0;
$out = $fixture->four($sql);
$this->assertEquals("SELECT * FROM {users}", $out);
$CFG->debugsqltrace = 1;
$out = $fixture->four($sql);
$expected = <<<EOD
SELECT * FROM {users}
-- line 22 of /lib/tests/fixtures/test_dml_instrumentation_fixture.php: call to core\\test_dml_instrumentation_fixture->one()
EOD;
$this->assertEquals($expected, $out);
$CFG->debugsqltrace = 2;
$out = $fixture->four($sql);
$expected = <<<EOD
SELECT * FROM {users}
-- line 22 of /lib/tests/fixtures/test_dml_instrumentation_fixture.php: call to core\\test_dml_instrumentation_fixture->one()
-- line 26 of /lib/tests/fixtures/test_dml_instrumentation_fixture.php: call to core\\test_dml_instrumentation_fixture->two()
EOD;
$this->assertEquals($expected, $out);
$CFG->debugsqltrace = 10;
$out = $fixture->recurse($sql, 9);
$expected = <<<EOD
SELECT * FROM {users}
-- line 35 of /lib/tests/fixtures/test_dml_instrumentation_fixture.php: call to core\\test_dml_instrumentation_fixture->one()
-- line 37 of /lib/tests/fixtures/test_dml_instrumentation_fixture.php: call to core\\test_dml_instrumentation_fixture->recurse()
-- line 37 of /lib/tests/fixtures/test_dml_instrumentation_fixture.php: call to core\\test_dml_instrumentation_fixture->recurse()
-- line 37 of /lib/tests/fixtures/test_dml_instrumentation_fixture.php: call to core\\test_dml_instrumentation_fixture->recurse()
-- line 37 of /lib/tests/fixtures/test_dml_instrumentation_fixture.php: call to core\\test_dml_instrumentation_fixture->recurse()
-- line 37 of /lib/tests/fixtures/test_dml_instrumentation_fixture.php: call to core\\test_dml_instrumentation_fixture->recurse()
-- line 37 of /lib/tests/fixtures/test_dml_instrumentation_fixture.php: call to core\\test_dml_instrumentation_fixture->recurse()
-- line 37 of /lib/tests/fixtures/test_dml_instrumentation_fixture.php: call to core\\test_dml_instrumentation_fixture->recurse()
-- line 37 of /lib/tests/fixtures/test_dml_instrumentation_fixture.php: call to core\\test_dml_instrumentation_fixture->recurse()
-- line 37 of /lib/tests/fixtures/test_dml_instrumentation_fixture.php: call to core\\test_dml_instrumentation_fixture->recurse()
EOD;
$this->assertEquals($expected, $out);
}
<?php
namespace core;
class test_dml_instrumentation_fixture {
private $db;
public function __construct($testcase) {
$this->db = $testcase->getMockBuilder(\moodle_database::class)
->getMockForAbstractClass();
}
public function get_mock() {
return $this->db;
}
public function one(string $sql) {
return $this->db->instrument_sql($sql, debug_backtrace(2));
}
public function two(string $sql) {
return $this->one($sql);
}
public function three(string $sql) {
return $this->two($sql);
}
public function four(string $sql) {
return $this->three($sql);
}
public function recurse(string $sql, int $count = 5) {
if ($count === 0) {
return $this->one($sql);
} else {
return $this->recurse($sql, $count - 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment