MDL-32960 refactor phpunit integration to one class per file structure
[moodle.git] / lib / phpunit / classes / basic_testcase.php
CommitLineData
7e7cfe7a
PS
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 */
37abstract class basic_testcase extends PHPUnit_Framework_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() {
61 global $DB;
62
63 try {
64 parent::runBare();
65 } catch (Exception $e) {
66 // cleanup after failed expectation
67 phpunit_util::reset_all_data();
68 throw $e;
69 }
70
71 if ($DB->is_transaction_started()) {
72 phpunit_util::reset_all_data();
73 throw new coding_exception('basic_testcase '.$this->getName().' is not supposed to use database transactions!');
74 }
75
76 phpunit_util::reset_all_data(true);
77 }
78}