Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 1... Línea 1...
1
<?php
1
<?php
-
 
2
use PHPUnit\Framework\Attributes\After;
2
// This file is part of Moodle - http://moodle.org/
3
// This file is part of Moodle - http://moodle.org/
3
//
4
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// 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
// 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
// the Free Software Foundation, either version 3 of the License, or
Línea 13... Línea 14...
13
//
14
//
14
// You should have received a copy of the GNU General Public License
15
// 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
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Línea 16... Línea 17...
16
 
17
 
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
/**
18
/**
28
 * The simplest PHPUnit test case customised for Moodle
19
 * The simplest PHPUnit test case customised for Moodle
29
 *
20
 *
30
 * It is intended for isolated tests that do not modify database or any globals.
21
 * It is intended for isolated tests that do not modify database or any globals.
31
 *
22
 *
32
 * @package    core
23
 * @package    core
33
 * @category   phpunit
24
 * @category   phpunit
34
 * @copyright  2012 Petr Skoda {@link http://skodak.org}
25
 * @copyright  2012 Petr Skoda {@link http://skodak.org}
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
27
 */
37
abstract class basic_testcase extends base_testcase {
-
 
38
 
28
abstract class basic_testcase extends base_testcase {
39
    /**
29
    /**
40
     * Constructs a test case with the given name.
30
     * Constructs a test case with the given name.
41
     *
-
 
42
     * Note: use setUp() or setUpBeforeClass() in your test cases.
31
     *
43
     *
32
     *
44
     * @param string $name
-
 
45
     * @param array  $data
-
 
46
     * @param string $dataName
33
     * @param string $name
47
     */
34
     */
48
    final public function __construct($name = null, array $data = array(), $dataName = '') {
35
    final public function __construct($name = null) {
Línea 49... Línea 36...
49
        parent::__construct($name, $data, $dataName);
36
        parent::__construct($name);
50
 
-
 
51
        $this->setBackupGlobals(false);
37
 
52
        $this->setBackupStaticAttributes(false);
38
        $this->setBackupGlobals(false);
Línea 53... Línea 39...
53
        $this->setRunTestInSeparateProcess(false);
39
        $this->setRunTestInSeparateProcess(false);
54
    }
40
    }
55
 
-
 
56
    /**
41
 
-
 
42
    /**
57
     * Runs the bare test sequence and log any changes in global state or database.
43
     * Teardown method to reset data after tests.
58
     * @return void
44
     */
Línea 59... Línea -...
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();
45
    #[After]
76
            throw $e;
46
    final public function teardown_after_test(): void {
77
        }
47
        global $DB;
78
 
48
 
Línea 79... Línea 49...
79
        if ($DB->is_transaction_started()) {
49
        if ($DB->is_transaction_started()) {
80
            phpunit_util::reset_all_data();
50
            phpunit_util::reset_all_data();
-
 
51
            throw new coding_exception('basic_testcase ' . $this->getName() . ' is not supposed to use database transactions!');
-
 
52
        }
-
 
53
 
-
 
54
        phpunit_util::reset_all_data(true);
-
 
55
    }
-
 
56
 
-
 
57
    /**
-
 
58
     * Get the name of the test.
-
 
59
     *
-
 
60
     * Replaces the original PHPUnit method.
81
            throw new coding_exception('basic_testcase '.$this->getName().' is not supposed to use database transactions!');
61
     * @return string