2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Database driver test case.
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 * Special test case for testing of DML drivers and DDL layer.
30 * Note: Use only 'test_table*' names when creating new tables.
32 * For DML/DDL developers: you can add following settings to config.php if you want to test different driver than the main one,
33 * the reason is to allow testing of incomplete drivers that do not allow full PHPUnit environment
34 * initialisation (the database can be empty).
35 * $CFG->phpunit_extra_drivers = array(
36 * 1=>array('dbtype'=>'mysqli', 'dbhost'=>'localhost', 'dbname'=>'moodle', 'dbuser'=>'root', 'dbpass'=>'', 'prefix'=>'phpu2_'),
37 * 2=>array('dbtype'=>'pgsql', 'dbhost'=>'localhost', 'dbname'=>'moodle', 'dbuser'=>'postgres', 'dbpass'=>'', 'prefix'=>'phpu2_'),
38 * 3=>array('dbtype'=>'sqlsrv', 'dbhost'=>'127.0.0.1', 'dbname'=>'moodle', 'dbuser'=>'sa', 'dbpass'=>'', 'prefix'=>'phpu2_'),
39 * 4=>array('dbtype'=>'oci', 'dbhost'=>'127.0.0.1', 'dbname'=>'XE', 'dbuser'=>'sa', 'dbpass'=>'', 'prefix'=>'t_'),
41 * define('PHPUNIT_TEST_DRIVER')=1; //number is index in the previous array
45 * @copyright 2012 Petr Skoda {@link http://skodak.org}
46 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
48 abstract class database_driver_testcase extends PHPUnit_Framework_TestCase {
49 /** @var moodle_database connection to extra database */
50 private static $extradb = null;
52 /** @var moodle_database used in these tests*/
56 * Constructs a test case with the given name.
60 * @param string $dataName
62 final public function __construct($name = null, array $data = array(), $dataName = '') {
63 parent::__construct($name, $data, $dataName);
65 $this->setBackupGlobals(false);
66 $this->setBackupStaticAttributes(false);
67 $this->setRunTestInSeparateProcess(false);
70 public static function setUpBeforeClass() {
72 parent::setUpBeforeClass();
74 if (!defined('PHPUNIT_TEST_DRIVER')) {
79 if (!isset($CFG->phpunit_extra_drivers[PHPUNIT_TEST_DRIVER])) {
80 throw new exception('Can not find driver configuration options with index: '.PHPUNIT_TEST_DRIVER);
83 $dblibrary = empty($CFG->phpunit_extra_drivers[PHPUNIT_TEST_DRIVER]['dblibrary']) ? 'native' : $CFG->phpunit_extra_drivers[PHPUNIT_TEST_DRIVER]['dblibrary'];
84 $dbtype = $CFG->phpunit_extra_drivers[PHPUNIT_TEST_DRIVER]['dbtype'];
85 $dbhost = $CFG->phpunit_extra_drivers[PHPUNIT_TEST_DRIVER]['dbhost'];
86 $dbname = $CFG->phpunit_extra_drivers[PHPUNIT_TEST_DRIVER]['dbname'];
87 $dbuser = $CFG->phpunit_extra_drivers[PHPUNIT_TEST_DRIVER]['dbuser'];
88 $dbpass = $CFG->phpunit_extra_drivers[PHPUNIT_TEST_DRIVER]['dbpass'];
89 $prefix = $CFG->phpunit_extra_drivers[PHPUNIT_TEST_DRIVER]['prefix'];
90 $dboptions = empty($CFG->phpunit_extra_drivers[PHPUNIT_TEST_DRIVER]['dboptions']) ? array() : $CFG->phpunit_extra_drivers[PHPUNIT_TEST_DRIVER]['dboptions'];
92 $classname = "{$dbtype}_{$dblibrary}_moodle_database";
93 require_once("$CFG->libdir/dml/$classname.php");
94 $d = new $classname();
95 if (!$d->driver_installed()) {
96 throw new exception('Database driver for '.$classname.' is not installed');
99 $d->connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions);
104 protected function setUp() {
108 if (self::$extradb) {
109 $this->tdb = self::$extradb;
115 protected function tearDown() {
116 // delete all test tables
117 $dbman = $this->tdb->get_manager();
118 $tables = $this->tdb->get_tables(false);
119 foreach($tables as $tablename) {
120 if (strpos($tablename, 'test_table') === 0) {
121 $table = new xmldb_table($tablename);
122 $dbman->drop_table($table);
128 public static function tearDownAfterClass() {
129 if (self::$extradb) {
130 self::$extradb->dispose();
131 self::$extradb = null;
133 phpunit_util::reset_all_data(null);
134 parent::tearDownAfterClass();
138 * Runs the bare test sequence.
141 public function runBare() {
145 } catch (Exception $e) {
146 if ($this->tdb->is_transaction_started()) {
147 $this->tdb->force_transaction_rollback();
155 * Return debugging messages from the current test.
156 * @return array with instances having 'message', 'level' and 'stacktrace' property.
158 public function getDebuggingMessages() {
159 return phpunit_util::get_debugging_messages();
163 * Clear all previous debugging messages in current test.
165 public function resetDebugging() {
166 phpunit_util::reset_debugging();
170 * Assert that exactly debugging was just called once.
172 * Discards the debugging message if successful.
174 * @param null|string $debugmessage null means any
175 * @param null|string $debuglevel null means any
176 * @param string $message
178 public function assertDebuggingCalled($debugmessage = null, $debuglevel = null, $message = '') {
179 $debugging = phpunit_util::get_debugging_messages();
180 $count = count($debugging);
183 if ($message === '') {
184 $message = 'Expectation failed, debugging() not triggered.';
186 $this->fail($message);
189 if ($message === '') {
190 $message = 'Expectation failed, debugging() triggered '.$count.' times.';
192 $this->fail($message);
194 $this->assertEquals(1, $count);
196 $debug = reset($debugging);
197 if ($debugmessage !== null) {
198 $this->assertSame($debugmessage, $debug->message, $message);
200 if ($debuglevel !== null) {
201 $this->assertSame($debuglevel, $debug->level, $message);
204 phpunit_util::reset_debugging();
208 * Call when no debugging() messages expected.
209 * @param string $message
211 public function assertDebuggingNotCalled($message = '') {
212 $debugging = phpunit_util::get_debugging_messages();
213 $count = count($debugging);
215 if ($message === '') {
216 $message = 'Expectation failed, debugging() was triggered.';
218 $this->assertEquals(0, $count, $message);