Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
1441 ariadna 2
use PHPUnit\Framework\Attributes\After;
3
use PHPUnit\Framework\Attributes\Before;
1 efrain 4
// This file is part of Moodle - http://moodle.org/
5
//
6
// Moodle is free software: you can redistribute it and/or modify
7
// it under the terms of the GNU General Public License as published by
8
// the Free Software Foundation, either version 3 of the License, or
9
// (at your option) any later version.
10
//
11
// Moodle is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
// GNU General Public License for more details.
15
//
16
// You should have received a copy of the GNU General Public License
17
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18
 
19
/**
20
 * Database driver test case.
21
 *
22
 * @package    core
23
 * @category   phpunit
24
 * @copyright  2012 Petr Skoda {@link http://skodak.org}
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
 
28
 
29
/**
30
 * Special test case for testing of DML drivers and DDL layer.
31
 *
32
 * Note: Use only 'test_table*' names when creating new tables.
33
 *
34
 * For DML/DDL developers: you can add following settings to config.php if you want to test different driver than the main one,
35
 *                         the reason is to allow testing of incomplete drivers that do not allow full PHPUnit environment
36
 *                         initialisation (the database can be empty).
37
 * $CFG->phpunit_extra_drivers = array(
38
 *      1=>array('dbtype'=>'mysqli', 'dbhost'=>'localhost', 'dbname'=>'moodle', 'dbuser'=>'root', 'dbpass'=>'', 'prefix'=>'phpu2_'),
39
 *      2=>array('dbtype'=>'pgsql', 'dbhost'=>'localhost', 'dbname'=>'moodle', 'dbuser'=>'postgres', 'dbpass'=>'', 'prefix'=>'phpu2_'),
40
 *      3=>array('dbtype'=>'sqlsrv', 'dbhost'=>'127.0.0.1', 'dbname'=>'moodle', 'dbuser'=>'sa', 'dbpass'=>'', 'prefix'=>'phpu2_'),
41
 * );
42
 * define('PHPUNIT_TEST_DRIVER')=1; //number is index in the previous array
43
 *
44
 * @package    core
45
 * @category   phpunit
46
 * @copyright  2012 Petr Skoda {@link http://skodak.org}
47
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
48
 */
49
abstract class database_driver_testcase extends base_testcase {
50
    /** @var moodle_database connection to extra database */
51
    private static $extradb = null;
52
 
53
    /** @var moodle_database used in these tests*/
54
    protected $tdb;
55
 
56
    /**
57
     * Constructs a test case with the given name.
58
     *
59
     * @param string $name
60
     */
1441 ariadna 61
    final public function __construct($name = null) {
62
        parent::__construct($name);
1 efrain 63
 
64
        $this->setBackupGlobals(false);
65
        $this->setRunTestInSeparateProcess(false);
66
    }
67
 
68
    public static function setUpBeforeClass(): void {
69
        global $CFG;
70
        parent::setUpBeforeClass();
71
 
72
        if (!defined('PHPUNIT_TEST_DRIVER')) {
73
            // use normal $DB
74
            return;
75
        }
76
 
77
        if (!isset($CFG->phpunit_extra_drivers[PHPUNIT_TEST_DRIVER])) {
78
            throw new exception('Can not find driver configuration options with index: '.PHPUNIT_TEST_DRIVER);
79
        }
80
 
81
        $dblibrary = empty($CFG->phpunit_extra_drivers[PHPUNIT_TEST_DRIVER]['dblibrary']) ? 'native' : $CFG->phpunit_extra_drivers[PHPUNIT_TEST_DRIVER]['dblibrary'];
82
        $dbtype = $CFG->phpunit_extra_drivers[PHPUNIT_TEST_DRIVER]['dbtype'];
83
        $dbhost = $CFG->phpunit_extra_drivers[PHPUNIT_TEST_DRIVER]['dbhost'];
84
        $dbname = $CFG->phpunit_extra_drivers[PHPUNIT_TEST_DRIVER]['dbname'];
85
        $dbuser = $CFG->phpunit_extra_drivers[PHPUNIT_TEST_DRIVER]['dbuser'];
86
        $dbpass = $CFG->phpunit_extra_drivers[PHPUNIT_TEST_DRIVER]['dbpass'];
87
        $prefix = $CFG->phpunit_extra_drivers[PHPUNIT_TEST_DRIVER]['prefix'];
88
        $dboptions = empty($CFG->phpunit_extra_drivers[PHPUNIT_TEST_DRIVER]['dboptions']) ? array() : $CFG->phpunit_extra_drivers[PHPUNIT_TEST_DRIVER]['dboptions'];
89
 
90
        $classname = "{$dbtype}_{$dblibrary}_moodle_database";
91
        require_once("$CFG->libdir/dml/$classname.php");
92
        $d = new $classname();
93
        if (!$d->driver_installed()) {
94
            throw new exception('Database driver for '.$classname.' is not installed');
95
        }
96
 
97
        $d->connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions);
98
 
99
        self::$extradb = $d;
100
    }
101
 
1441 ariadna 102
    #[Before]
103
    protected function setup_extradb(): void {
1 efrain 104
        global $DB;
105
 
106
        if (self::$extradb) {
107
            $this->tdb = self::$extradb;
108
        } else {
109
            $this->tdb = $DB;
110
        }
111
    }
112
 
1441 ariadna 113
    #[After]
114
    protected function teardown_extradb(): void {
1 efrain 115
        // delete all test tables
116
        $dbman = $this->tdb->get_manager();
117
        $tables = $this->tdb->get_tables(false);
118
        foreach($tables as $tablename) {
119
            if (strpos($tablename, 'test_table') === 0) {
120
                $table = new xmldb_table($tablename);
121
                $dbman->drop_table($table);
122
            }
123
        }
124
    }
125
 
126
    public static function tearDownAfterClass(): void {
127
        if (self::$extradb) {
128
            self::$extradb->dispose();
129
            self::$extradb = null;
130
        }
131
        phpunit_util::reset_all_data(null);
132
        parent::tearDownAfterClass();
133
    }
134
 
1441 ariadna 135
    #[After]
136
    public function check_debugging(): void {
137
        // Deal with any debugging messages.
138
        $debugerror = phpunit_util::display_debugging_messages(true);
139
        $this->resetDebugging();
140
        if (!empty($debugerror)) {
141
            trigger_error('Unexpected debugging() call detected.' . "\n" . $debugerror, E_USER_NOTICE);
1 efrain 142
        }
143
    }
144
 
145
    /**
146
     * Return debugging messages from the current test.
147
     * @return array with instances having 'message', 'level' and 'stacktrace' property.
148
     */
149
    public function getDebuggingMessages() {
150
        return phpunit_util::get_debugging_messages();
151
    }
152
 
153
    /**
154
     * Clear all previous debugging messages in current test.
155
     */
156
    public function resetDebugging() {
157
        phpunit_util::reset_debugging();
158
    }
159
 
160
    /**
161
     * Assert that exactly debugging was just called once.
162
     *
163
     * Discards the debugging message if successful.
164
     *
165
     * @param null|string $debugmessage null means any
166
     * @param null|string $debuglevel null means any
167
     * @param string $message
168
     */
169
    public function assertDebuggingCalled($debugmessage = null, $debuglevel = null, $message = '') {
170
        $debugging = $this->getDebuggingMessages();
171
        $count = count($debugging);
172
 
173
        if ($count == 0) {
174
            if ($message === '') {
175
                $message = 'Expectation failed, debugging() not triggered.';
176
            }
177
            $this->fail($message);
178
        }
179
        if ($count > 1) {
180
            if ($message === '') {
181
                $message = 'Expectation failed, debugging() triggered '.$count.' times.';
182
            }
183
            $this->fail($message);
184
        }
185
        $this->assertEquals(1, $count);
186
 
187
        $debug = reset($debugging);
188
        if ($debugmessage !== null) {
189
            $this->assertSame($debugmessage, $debug->message, $message);
190
        }
191
        if ($debuglevel !== null) {
192
            $this->assertSame($debuglevel, $debug->level, $message);
193
        }
194
 
195
        $this->resetDebugging();
196
    }
197
 
198
    /**
199
     * Call when no debugging() messages expected.
200
     * @param string $message
201
     */
202
    public function assertDebuggingNotCalled($message = '') {
203
        $debugging = $this->getDebuggingMessages();
204
        $count = count($debugging);
205
 
206
        if ($message === '') {
207
            $message = 'Expectation failed, debugging() was triggered.';
208
        }
209
        $this->assertEquals(0, $count, $message);
210
    }
211
}