Solved problem with SQLite: Globals were getting stored, and when a PDO reference...
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiTestCase.php
1 <?php
2
3 abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
4 public $suite;
5 public $regex = '';
6 public $runDisabled = false;
7
8 protected $db;
9 protected $dbClone;
10 protected $oldTablePrefix;
11 protected $useTemporaryTables = true;
12
13 function __construct( $name = null, array $data = array(), $dataName = '' ) {
14 if ($name !== null) {
15 $this->setName($name);
16 }
17
18 $this->data = $data;
19 $this->dataName = $dataName;
20
21 $this->backupGlobals = false;
22 $this->backupStaticAttributes = false;
23 }
24
25 function run( PHPUnit_Framework_TestResult $result = NULL ) {
26
27 if( $this->needsDB() ) {
28
29 $this->db = wfGetDB( DB_MASTER );
30
31 $this->destroyDB();
32
33 $this->initDB();
34 $this->addCoreDBData();
35 $this->addDBData();
36 }
37
38 parent::run( $result );
39 }
40
41 function __destruct() {
42 $this->destroyDB();
43 }
44
45 function needsDB() {
46 $rc = new ReflectionClass( $this );
47 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
48 }
49
50 function addDBData() {}
51
52 private function addCoreDBData() {
53
54 //Make sysop user
55 $user = User::newFromName( 'UTSysop' );
56
57 if ( $user->idForName() == 0 ) {
58 $user->addToDatabase();
59 $user->setPassword( 'UTSysopPassword' );
60
61 $user->addGroup( 'sysop' );
62 $user->addGroup( 'bureaucrat' );
63 $user->saveSettings();
64 }
65
66
67 //Make 1 page with 1 revision
68 $article = new Article( Title::newFromText( 'UTPage' ) );
69 $article->doEdit( 'UTContent',
70 'UTPageSummary',
71 EDIT_NEW,
72 false,
73 User::newFromName( 'UTSysop' ) );
74 }
75
76 private function initDB() {
77 global $wgDBprefix;
78
79 $dbType = $this->db->getType();
80
81 if ( $wgDBprefix === 'unittest_' || ( $dbType == 'oracle' && $wgDBprefix === 'ut_' ) ) {
82 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
83 }
84
85 $this->oldTablePrefix = $wgDBprefix;
86
87 $tables = $this->listTables();
88
89 $prefix = $dbType != 'oracle' ? 'unittest_' : 'ut_';
90
91 $this->dbClone = new CloneDatabase( $this->db, $tables, $prefix );
92 $this->dbClone->cloneTableStructure();
93
94 if ( $dbType == 'oracle' )
95 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
96
97 if ( $dbType == 'oracle' ) {
98 # Insert 0 user to prevent FK violations
99
100 # Anonymous user
101 $this->db->insert( 'user', array(
102 'user_id' => 0,
103 'user_name' => 'Anonymous' ) );
104 }
105
106 }
107
108 protected function destroyDB() {
109 global $wgDBprefix;
110
111 if ( $this->useTemporaryTables ) {
112 # Don't need to do anything
113 //return;
114 //Temporary tables seem to be broken ATM, delete anyway
115 }
116
117 if( $this->db->getType() == 'oracle' ) {
118 $tables = $this->db->listTables( 'ut_', __METHOD__ );
119 }
120 else {
121 $tables = $this->db->listTables( 'unittest_', __METHOD__ );
122 }
123
124 foreach ( $tables as $table ) {
125 if( $this->db->tableExists( "`$table`" ) ) {
126 $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table DROP CONSTRAINTS" : "DROP TABLE `$table`";
127 $this->db->query( $sql, __METHOD__ );
128 }
129 }
130
131 if ( $this->db->getType() == 'oracle' )
132 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__ );
133
134 CloneDatabase::changePrefix( $this->oldTablePrefix );
135 }
136
137 function __call( $func, $args ) {
138 static $compatibility = array(
139 'assertInternalType' => 'assertType',
140 'assertNotInternalType' => 'assertNotType',
141 'assertInstanceOf' => 'assertType',
142 );
143
144 if ( method_exists( $this->suite, $func ) ) {
145 return call_user_func_array( array( $this->suite, $func ), $args);
146 } elseif ( isset( $compatibility[$func] ) ) {
147 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
148 } else {
149 throw new MWException( "Called non-existant $func method on "
150 . get_class( $this ) );
151 }
152 }
153
154 static private function unprefixTable( $tableName ) {
155 global $wgDBprefix;
156 return substr( $tableName, strlen( $wgDBprefix ) );
157 }
158
159 protected function listTables() {
160 global $wgDBprefix;
161
162 $tables = $this->db->listTables( $wgDBprefix, __METHOD__ );
163 $tables = array_map( array( __CLASS__, 'unprefixTable' ), $tables );
164 return $tables;
165
166 }
167 }
168