Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
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.
8
//
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.
13
//
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/>.
16
 
17
/**
18
 * Basic test case.
19
 *
20
 * @package    core
21
 * @category   phpunit
22
 * @copyright  2012 Petr Skoda {@link http://skodak.org}
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
 
27
/**
28
 * The simplest PHPUnit test case customised for Moodle
29
 *
30
 * It is intended for isolated tests that do not modify database or any globals.
31
 *
32
 * @package    core
33
 * @category   phpunit
34
 * @copyright  2012 Petr Skoda {@link http://skodak.org}
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
abstract class basic_testcase extends base_testcase {
38
 
39
    /**
40
     * Constructs a test case with the given name.
41
     *
42
     * Note: use setUp() or setUpBeforeClass() in your test cases.
43
     *
44
     * @param string $name
45
     * @param array  $data
46
     * @param string $dataName
47
     */
48
    final public function __construct($name = null, array $data = array(), $dataName = '') {
49
        parent::__construct($name, $data, $dataName);
50
 
51
        $this->setBackupGlobals(false);
52
        $this->setBackupStaticAttributes(false);
53
        $this->setRunTestInSeparateProcess(false);
54
    }
55
 
56
    /**
57
     * Runs the bare test sequence and log any changes in global state or database.
58
     * @return void
59
     */
60
    final public function runBare(): void {
61
        global $DB;
62
 
63
        try {
64
            parent::runBare();
65
 
66
        } catch (Exception $ex) {
67
            $e = $ex;
68
        } catch (Throwable $ex) {
69
            // Engine errors in PHP7 throw exceptions of type Throwable (this "catch" will be ignored in PHP5).
70
            $e = $ex;
71
        }
72
 
73
        if (isset($e)) {
74
            // cleanup after failed expectation
75
            phpunit_util::reset_all_data();
76
            throw $e;
77
        }
78
 
79
        if ($DB->is_transaction_started()) {
80
            phpunit_util::reset_all_data();
81
            throw new coding_exception('basic_testcase '.$this->getName().' is not supposed to use database transactions!');
82
        }
83
 
84
        phpunit_util::reset_all_data(true);
85
    }
86
}