protected $oldTablePrefix;
protected $useTemporaryTables = true;
protected $reuseDB = false;
+ protected $tablesUsed = array(); // tables with data
+
private static $dbSetup = false;
+ /**
+ * Holds the paths of temporary files/directories created through getNewTempFile,
+ * and getNewTempDirectory
+ *
+ * @var array
+ */
+ private $tmpfiles = array();
+
+
/**
* Table name prefixes. Oracle likes it shorter.
*/
}
}
+ /**
+ * obtains a new temporary file name
+ *
+ * The obtained filename is enlisted to be removed upon tearDown
+ *
+ * @returns string: absolute name of the temporary file
+ */
+ protected function getNewTempFile() {
+ $fname = tempnam( wfTempDir(), 'MW_PHPUnit_' . get_class( $this ) . '_' );
+ $this->tmpfiles[] = $fname;
+ return $fname;
+ }
+
+ /**
+ * obtains a new temporary directory
+ *
+ * The obtained directory is enlisted to be removed (recursively with all its contained
+ * files) upon tearDown.
+ *
+ * @returns string: absolute name of the temporary directory
+ */
+ protected function getNewTempDirectory() {
+ // Starting of with a temporary /file/.
+ $fname = $this->getNewTempFile();
+
+ // Converting the temporary /file/ to a /directory/
+ //
+ // The following is not atomic, but at least we now have a single place,
+ // where temporary directory creation is bundled and can be improved
+ unlink( $fname );
+ $this->assertTrue( wfMkdirParents( $fname ) );
+ return $fname;
+ }
+
+ protected function tearDown() {
+ // Cleaning up temoporary files
+ foreach ( $this->tmpfiles as $fname ) {
+ if ( is_file( $fname ) || ( is_link( $fname ) ) ) {
+ unlink( $fname );
+ } elseif ( is_dir( $fname ) ) {
+ wfRecursiveRemoveDir( $fname );
+ }
+ }
+
+ parent::tearDown();
+ }
+
function dbPrefix() {
return $this->db->getType() == 'oracle' ? self::ORA_DB_PREFIX : self::DB_PREFIX;
}
function needsDB() {
+ # if the test says it uses database tables, it needs the database
+ if ( $this->tablesUsed ) {
+ return true;
+ }
+
+ # if the test says it belongs to the Database group, it needs the database
$rc = new ReflectionClass( $this );
- return strpos( $rc->getDocComment(), '@group Database' ) !== false;
+ if ( preg_match( '/@group +Database/im', $rc->getDocComment() ) ) {
+ return true;
+ }
+
+ return false;
}
/**
function addDBData() {}
private function addCoreDBData() {
+ # disabled for performance
+ #$this->tablesUsed[] = 'page';
+ #$this->tablesUsed[] = 'revision';
+
if ( $this->db->getType() == 'oracle' ) {
# Insert 0 user to prevent FK violations
//Make 1 page with 1 revision
- $article = new Article( Title::newFromText( 'UTPage' ) );
- $article->doEdit( 'UTContent',
+ $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
+ if ( !$page->getId() == 0 ) {
+ $page->doEdit( 'UTContent',
'UTPageSummary',
EDIT_NEW,
false,
User::newFromName( 'UTSysop' ) );
+ }
}
private function initDB() {
throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
}
- $dbClone = new CloneDatabase( $this->db, $this->listTables(), $this->dbPrefix() );
+ $tablesCloned = $this->listTables();
+ $dbClone = new CloneDatabase( $this->db, $tablesCloned, $this->dbPrefix() );
$dbClone->useTemporaryTables( $this->useTemporaryTables );
if ( ( $this->db->getType() == 'oracle' || !$this->useTemporaryTables ) && $this->reuseDB ) {
wfGetLB()->closeAll();
$this->db = wfGetDB( DB_MASTER );
} else {
- foreach( $this->listTables() as $tbl ) {
+ foreach( $this->tablesUsed as $tbl ) {
if( $tbl == 'interwiki') continue;
$this->db->query( 'TRUNCATE TABLE '.$this->db->tableName($tbl), __METHOD__ );
}
}
} else {
- foreach( $this->listTables() as $tbl ) {
+ foreach( $this->tablesUsed as $tbl ) {
if( $tbl == 'interwiki' || $tbl == 'user' ) continue;
- $this->db->delete( $tbl, '*', __METHOD__ );
+ $this->db->delete( $tbl, '*', __METHOD__ );
}
}
}
}
- protected function destroyDB() {
- if ( is_null( $this->db ) ||
- ( $this->useTemporaryTables && $this->db->getType() != 'oracle' ) ||
- ( $this->reuseDB ) ) {
- # Don't need to do anything
- return;
- }
-
- $tables = $this->db->listTables( $this->dbPrefix(), __METHOD__ );
-
- foreach ( $tables as $table ) {
- try {
- $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table CASCADE CONSTRAINTS PURGE" : "DROP TABLE `$table`";
- $this->db->query( $sql, __METHOD__ );
- } catch( MWException $mwe ) {}
- }
-
- if ( $this->db->getType() == 'oracle' )
- $this->db->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__ );
-
- CloneDatabase::changePrefix( $this->oldTablePrefix );
- }
-
-
function __call( $func, $args ) {
static $compatibility = array(
'assertInternalType' => 'assertType',
wfDeprecated( $function );
wfRestoreWarnings();
}
+
+ /**
+ * Asserts that the given database query yields the rows given by $expectedRows.
+ * The expected rows should be given as indexed (not associative) arrays, with
+ * the values given in the order of the columns in the $fields parameter.
+ * Note that the rows are sorted by the columns given in $fields.
+ *
+ * @param $table String|Array the table(s) to query
+ * @param $fields String|Array the columns to include in the result (and to sort by)
+ * @param $condition String|Array "where" condition(s)
+ * @param $expectedRows Array - an array of arrays giving the expected rows.
+ *
+ * @throws MWException if this test cases's needsDB() method doesn't return true.
+ * Test cases can use "@group Database" to enable database test support,
+ * or list the tables under testing in $this->tablesUsed, or override the
+ * needsDB() method.
+ */
+ protected function assertSelect( $table, $fields, $condition, Array $expectedRows ) {
+ if ( !$this->needsDB() ) {
+ throw new MWException( 'When testing database state, the test cases\'s needDB()' .
+ ' method should return true. Use @group Database or $this->tablesUsed.');
+ }
+
+ $db = wfGetDB( DB_SLAVE );
+
+ $res = $db->select( $table, $fields, $condition, wfGetCaller(), array( 'ORDER BY' => $fields ) );
+ $this->assertNotEmpty( $res, "query failed: " . $db->lastError() );
+
+ $i = 0;
+
+ foreach ( $expectedRows as $expected ) {
+ $r = $res->fetchRow();
+ self::stripStringKeys( $r );
+
+ $i += 1;
+ $this->assertNotEmpty( $r, "row #$i missing" );
+
+ $this->assertEquals( $expected, $r, "row #$i mismatches" );
+ }
+
+ $r = $res->fetchRow();
+ self::stripStringKeys( $r );
+
+ $this->assertFalse( $r, "found extra row (after #$i)" );
+ }
+
+ /**
+ * Utility function for eliminating all string keys from an array.
+ * Useful to turn a database result row as returned by fetchRow() into
+ * a pure indexed array.
+ *
+ * @static
+ * @param $r mixed the array to remove string keys from.
+ */
+ protected static function stripStringKeys( &$r ) {
+ if ( !is_array( $r ) ) return;
+
+ foreach ( $r as $k => $v ) {
+ if ( is_string( $k ) ) unset( $r[$k] );
+ }
+ }
}