2010-12-29 15:52:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
|
|
|
|
|
public $suite;
|
|
|
|
|
public $regex = '';
|
|
|
|
|
public $runDisabled = false;
|
2011-05-24 21:22:36 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var DatabaseBase
|
|
|
|
|
*/
|
2010-12-29 15:52:07 +00:00
|
|
|
protected $db;
|
|
|
|
|
protected $oldTablePrefix;
|
|
|
|
|
protected $useTemporaryTables = true;
|
2011-11-10 13:29:32 +00:00
|
|
|
protected $reuseDB = false;
|
2011-12-10 05:47:46 +00:00
|
|
|
protected $tablesUsed = array(); // tables with data
|
|
|
|
|
|
2011-05-24 21:22:36 +00:00
|
|
|
private static $dbSetup = false;
|
2011-01-10 18:34:59 +00:00
|
|
|
|
2012-04-10 14:28:42 +00:00
|
|
|
/**
|
|
|
|
|
* Holds the paths of temporary files/directories created through getNewTempFile,
|
|
|
|
|
* and getNewTempDirectory
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
private $tmpfiles = array();
|
|
|
|
|
|
|
|
|
|
|
2011-01-10 18:34:59 +00:00
|
|
|
/**
|
|
|
|
|
* Table name prefixes. Oracle likes it shorter.
|
|
|
|
|
*/
|
|
|
|
|
const DB_PREFIX = 'unittest_';
|
|
|
|
|
const ORA_DB_PREFIX = 'ut_';
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2010-12-31 21:10:36 +00:00
|
|
|
protected $supportedDBs = array(
|
|
|
|
|
'mysql',
|
2011-01-10 19:22:27 +00:00
|
|
|
'sqlite',
|
2011-10-24 19:28:31 +00:00
|
|
|
'postgres',
|
2011-01-10 19:22:27 +00:00
|
|
|
'oracle'
|
2010-12-31 21:10:36 +00:00
|
|
|
);
|
2010-12-29 15:52:07 +00:00
|
|
|
|
|
|
|
|
function __construct( $name = null, array $data = array(), $dataName = '' ) {
|
2011-02-05 16:25:18 +00:00
|
|
|
parent::__construct( $name, $data, $dataName );
|
2010-12-29 15:52:07 +00:00
|
|
|
|
2010-12-31 20:47:48 +00:00
|
|
|
$this->backupGlobals = false;
|
2011-02-20 17:52:09 +00:00
|
|
|
$this->backupStaticAttributes = false;
|
2010-12-29 15:52:07 +00:00
|
|
|
}
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2010-12-29 15:52:07 +00:00
|
|
|
function run( PHPUnit_Framework_TestResult $result = NULL ) {
|
2011-02-26 21:49:42 +00:00
|
|
|
/* Some functions require some kind of caching, and will end up using the db,
|
|
|
|
|
* which we can't allow, as that would open a new connection for mysql.
|
|
|
|
|
* Replace with a HashBag. They would not be going to persist anyway.
|
|
|
|
|
*/
|
2011-03-06 23:15:10 +00:00
|
|
|
ObjectCache::$instances[CACHE_DB] = new HashBagOStuff;
|
2011-04-29 16:26:20 +00:00
|
|
|
|
2010-12-30 17:30:35 +00:00
|
|
|
if( $this->needsDB() ) {
|
2010-12-31 21:01:17 +00:00
|
|
|
global $wgDBprefix;
|
2011-11-10 13:29:32 +00:00
|
|
|
|
|
|
|
|
$this->useTemporaryTables = !$this->getCliArg( 'use-normal-tables' );
|
|
|
|
|
$this->reuseDB = $this->getCliArg('reuse-db');
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2010-12-31 20:42:39 +00:00
|
|
|
$this->db = wfGetDB( DB_MASTER );
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2010-12-31 21:10:36 +00:00
|
|
|
$this->checkDbIsSupported();
|
2011-06-01 22:43:19 +00:00
|
|
|
|
2010-12-31 21:01:17 +00:00
|
|
|
$this->oldTablePrefix = $wgDBprefix;
|
2011-05-24 21:22:36 +00:00
|
|
|
|
|
|
|
|
if( !self::$dbSetup ) {
|
2011-12-10 05:47:46 +00:00
|
|
|
$this->initDB();
|
2011-05-24 21:22:36 +00:00
|
|
|
self::$dbSetup = true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-29 15:52:07 +00:00
|
|
|
$this->addCoreDBData();
|
|
|
|
|
$this->addDBData();
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2010-12-31 21:01:17 +00:00
|
|
|
parent::run( $result );
|
2011-05-28 21:19:24 +00:00
|
|
|
|
2011-06-01 22:43:19 +00:00
|
|
|
$this->resetDB();
|
2011-05-01 19:31:18 +00:00
|
|
|
} else {
|
2010-12-31 21:01:17 +00:00
|
|
|
parent::run( $result );
|
2010-12-29 15:52:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
2011-03-05 15:44:28 +00:00
|
|
|
|
2012-04-10 14:28:42 +00:00
|
|
|
/**
|
|
|
|
|
* 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() {
|
2012-08-27 12:38:25 +00:00
|
|
|
// Cleaning up temporary files
|
2012-04-10 14:28:42 +00:00
|
|
|
foreach ( $this->tmpfiles as $fname ) {
|
|
|
|
|
if ( is_file( $fname ) || ( is_link( $fname ) ) ) {
|
|
|
|
|
unlink( $fname );
|
|
|
|
|
} elseif ( is_dir( $fname ) ) {
|
|
|
|
|
wfRecursiveRemoveDir( $fname );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-27 12:38:25 +00:00
|
|
|
// clean up open transactions
|
|
|
|
|
if( $this->needsDB() && $this->db ) {
|
|
|
|
|
while( $this->db->trxLevel() > 0 ) {
|
|
|
|
|
$this->db->rollback();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-10 14:28:42 +00:00
|
|
|
parent::tearDown();
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-05 15:44:28 +00:00
|
|
|
function dbPrefix() {
|
|
|
|
|
return $this->db->getType() == 'oracle' ? self::ORA_DB_PREFIX : self::DB_PREFIX;
|
|
|
|
|
}
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2010-12-29 15:52:07 +00:00
|
|
|
function needsDB() {
|
2012-05-04 14:02:09 +00:00
|
|
|
# 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
|
2010-12-29 15:52:07 +00:00
|
|
|
$rc = new ReflectionClass( $this );
|
2012-05-04 14:02:09 +00:00
|
|
|
if ( preg_match( '/@group +Database/im', $rc->getDocComment() ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2010-12-29 15:52:07 +00:00
|
|
|
}
|
2011-01-10 18:34:59 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stub. If a test needs to add additional data to the database, it should
|
|
|
|
|
* implement this method and do so
|
|
|
|
|
*/
|
2010-12-29 15:52:07 +00:00
|
|
|
function addDBData() {}
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2010-12-29 15:52:07 +00:00
|
|
|
private function addCoreDBData() {
|
2012-01-25 23:11:53 +00:00
|
|
|
# disabled for performance
|
|
|
|
|
#$this->tablesUsed[] = 'page';
|
|
|
|
|
#$this->tablesUsed[] = 'revision';
|
|
|
|
|
|
2011-11-10 13:29:32 +00:00
|
|
|
if ( $this->db->getType() == 'oracle' ) {
|
|
|
|
|
|
|
|
|
|
# Insert 0 user to prevent FK violations
|
|
|
|
|
# Anonymous user
|
|
|
|
|
$this->db->insert( 'user', array(
|
|
|
|
|
'user_id' => 0,
|
|
|
|
|
'user_name' => 'Anonymous' ), __METHOD__, array( 'IGNORE' ) );
|
|
|
|
|
|
|
|
|
|
# Insert 0 page to prevent FK violations
|
|
|
|
|
# Blank page
|
|
|
|
|
$this->db->insert( 'page', array(
|
|
|
|
|
'page_id' => 0,
|
|
|
|
|
'page_namespace' => 0,
|
|
|
|
|
'page_title' => ' ',
|
|
|
|
|
'page_restrictions' => NULL,
|
|
|
|
|
'page_counter' => 0,
|
|
|
|
|
'page_is_redirect' => 0,
|
|
|
|
|
'page_is_new' => 0,
|
|
|
|
|
'page_random' => 0,
|
|
|
|
|
'page_touched' => $this->db->timestamp(),
|
|
|
|
|
'page_latest' => 0,
|
|
|
|
|
'page_len' => 0 ), __METHOD__, array( 'IGNORE' ) );
|
|
|
|
|
|
|
|
|
|
}
|
2011-01-25 16:32:43 +00:00
|
|
|
|
|
|
|
|
User::resetIdByNameCache();
|
|
|
|
|
|
2010-12-29 15:52:07 +00:00
|
|
|
//Make sysop user
|
|
|
|
|
$user = User::newFromName( 'UTSysop' );
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2010-12-29 15:52:07 +00:00
|
|
|
if ( $user->idForName() == 0 ) {
|
|
|
|
|
$user->addToDatabase();
|
|
|
|
|
$user->setPassword( 'UTSysopPassword' );
|
|
|
|
|
|
|
|
|
|
$user->addGroup( 'sysop' );
|
|
|
|
|
$user->addGroup( 'bureaucrat' );
|
|
|
|
|
$user->saveSettings();
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2010-12-29 15:52:07 +00:00
|
|
|
//Make 1 page with 1 revision
|
2011-12-16 18:55:20 +00:00
|
|
|
$page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
|
2012-01-25 23:11:53 +00:00
|
|
|
if ( !$page->getId() == 0 ) {
|
|
|
|
|
$page->doEdit( 'UTContent',
|
|
|
|
|
'UTPageSummary',
|
|
|
|
|
EDIT_NEW,
|
|
|
|
|
false,
|
|
|
|
|
User::newFromName( 'UTSysop' ) );
|
|
|
|
|
}
|
2010-12-29 15:52:07 +00:00
|
|
|
}
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2010-12-29 15:52:07 +00:00
|
|
|
private function initDB() {
|
|
|
|
|
global $wgDBprefix;
|
2011-03-05 15:44:28 +00:00
|
|
|
if ( $wgDBprefix === $this->dbPrefix() ) {
|
2010-12-29 15:52:07 +00:00
|
|
|
throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-05 07:22:27 +00:00
|
|
|
$tablesCloned = $this->listTables();
|
|
|
|
|
$dbClone = new CloneDatabase( $this->db, $tablesCloned, $this->dbPrefix() );
|
2011-05-24 21:22:36 +00:00
|
|
|
$dbClone->useTemporaryTables( $this->useTemporaryTables );
|
2011-11-10 13:29:32 +00:00
|
|
|
|
|
|
|
|
if ( ( $this->db->getType() == 'oracle' || !$this->useTemporaryTables ) && $this->reuseDB ) {
|
|
|
|
|
CloneDatabase::changePrefix( $this->dbPrefix() );
|
|
|
|
|
$this->resetDB();
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
$dbClone->cloneTableStructure();
|
|
|
|
|
}
|
2011-06-01 22:43:19 +00:00
|
|
|
|
|
|
|
|
if ( $this->db->getType() == 'oracle' ) {
|
|
|
|
|
$this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
|
|
|
|
|
}
|
2011-05-24 21:22:36 +00:00
|
|
|
}
|
2010-12-29 15:52:07 +00:00
|
|
|
|
2011-05-24 21:22:36 +00:00
|
|
|
/**
|
|
|
|
|
* Empty all tables so they can be repopulated for tests
|
|
|
|
|
*/
|
|
|
|
|
private function resetDB() {
|
|
|
|
|
if( $this->db ) {
|
2011-11-10 13:29:32 +00:00
|
|
|
if ( $this->db->getType() == 'oracle' ) {
|
|
|
|
|
if ( $this->useTemporaryTables ) {
|
|
|
|
|
wfGetLB()->closeAll();
|
|
|
|
|
$this->db = wfGetDB( DB_MASTER );
|
|
|
|
|
} else {
|
2011-12-10 05:47:46 +00:00
|
|
|
foreach( $this->tablesUsed as $tbl ) {
|
2011-11-10 13:29:32 +00:00
|
|
|
if( $tbl == 'interwiki') continue;
|
|
|
|
|
$this->db->query( 'TRUNCATE TABLE '.$this->db->tableName($tbl), __METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2011-12-10 05:47:46 +00:00
|
|
|
foreach( $this->tablesUsed as $tbl ) {
|
2011-11-10 13:29:32 +00:00
|
|
|
if( $tbl == 'interwiki' || $tbl == 'user' ) continue;
|
2011-12-05 07:22:27 +00:00
|
|
|
$this->db->delete( $tbl, '*', __METHOD__ );
|
2011-11-10 13:29:32 +00:00
|
|
|
}
|
2011-05-24 21:22:36 +00:00
|
|
|
}
|
2010-12-29 15:52:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
2011-06-01 22:43:19 +00:00
|
|
|
|
2010-12-29 15:52:07 +00:00
|
|
|
function __call( $func, $args ) {
|
|
|
|
|
static $compatibility = array(
|
|
|
|
|
'assertInternalType' => 'assertType',
|
|
|
|
|
'assertNotInternalType' => 'assertNotType',
|
|
|
|
|
'assertInstanceOf' => 'assertType',
|
2011-02-10 09:51:31 +00:00
|
|
|
'assertEmpty' => 'assertEmpty2',
|
2010-12-29 15:52:07 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ( method_exists( $this->suite, $func ) ) {
|
|
|
|
|
return call_user_func_array( array( $this->suite, $func ), $args);
|
|
|
|
|
} elseif ( isset( $compatibility[$func] ) ) {
|
|
|
|
|
return call_user_func_array( array( $this, $compatibility[$func] ), $args);
|
|
|
|
|
} else {
|
|
|
|
|
throw new MWException( "Called non-existant $func method on "
|
|
|
|
|
. get_class( $this ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-10 09:51:31 +00:00
|
|
|
|
2011-02-10 10:03:38 +00:00
|
|
|
private function assertEmpty2( $value, $msg ) {
|
2011-02-10 09:51:31 +00:00
|
|
|
return $this->assertTrue( $value == '', $msg );
|
|
|
|
|
}
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2010-12-30 16:24:29 +00:00
|
|
|
static private function unprefixTable( $tableName ) {
|
|
|
|
|
global $wgDBprefix;
|
|
|
|
|
return substr( $tableName, strlen( $wgDBprefix ) );
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-02 17:58:34 +00:00
|
|
|
static private function isNotUnittest( $table ) {
|
|
|
|
|
return strpos( $table, 'unittest_' ) !== 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-29 15:52:07 +00:00
|
|
|
protected function listTables() {
|
|
|
|
|
global $wgDBprefix;
|
2011-06-01 22:43:19 +00:00
|
|
|
|
2011-06-01 23:17:29 +00:00
|
|
|
$tables = $this->db->listTables( $wgDBprefix, __METHOD__ );
|
|
|
|
|
$tables = array_map( array( __CLASS__, 'unprefixTable' ), $tables );
|
|
|
|
|
|
2011-06-02 17:58:34 +00:00
|
|
|
// Don't duplicate test tables from the previous fataled run
|
|
|
|
|
$tables = array_filter( $tables, array( __CLASS__, 'isNotUnittest' ) );
|
|
|
|
|
|
2011-06-01 23:17:29 +00:00
|
|
|
if ( $this->db->getType() == 'sqlite' ) {
|
|
|
|
|
$tables = array_flip( $tables );
|
|
|
|
|
// these are subtables of searchindex and don't need to be duped/dropped separately
|
|
|
|
|
unset( $tables['searchindex_content'] );
|
|
|
|
|
unset( $tables['searchindex_segdir'] );
|
|
|
|
|
unset( $tables['searchindex_segments'] );
|
|
|
|
|
$tables = array_flip( $tables );
|
2011-03-05 16:09:10 +00:00
|
|
|
}
|
2011-06-01 23:17:29 +00:00
|
|
|
return $tables;
|
2010-12-29 15:52:07 +00:00
|
|
|
}
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2010-12-31 21:10:36 +00:00
|
|
|
protected function checkDbIsSupported() {
|
|
|
|
|
if( !in_array( $this->db->getType(), $this->supportedDBs ) ) {
|
|
|
|
|
throw new MWException( $this->db->getType() . " is not currently supported for unit testing." );
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2011-01-01 05:53:04 +00:00
|
|
|
public function getCliArg( $offset ) {
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2011-01-01 05:53:04 +00:00
|
|
|
if( isset( MediaWikiPHPUnitCommand::$additionalOptions[$offset] ) ) {
|
|
|
|
|
return MediaWikiPHPUnitCommand::$additionalOptions[$offset];
|
|
|
|
|
}
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2011-01-01 05:53:04 +00:00
|
|
|
}
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2011-01-01 05:53:04 +00:00
|
|
|
public function setCliArg( $offset, $value ) {
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2011-01-01 05:53:04 +00:00
|
|
|
MediaWikiPHPUnitCommand::$additionalOptions[$offset] = $value;
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2011-01-01 05:53:04 +00:00
|
|
|
}
|
2011-06-22 21:02:07 +00:00
|
|
|
|
2011-07-25 16:27:33 +00:00
|
|
|
/**
|
|
|
|
|
* Don't throw a warning if $function is deprecated and called later
|
|
|
|
|
*
|
|
|
|
|
* @param $function String
|
|
|
|
|
* @return null
|
|
|
|
|
*/
|
|
|
|
|
function hideDeprecated( $function ) {
|
|
|
|
|
wfSuppressWarnings();
|
|
|
|
|
wfDeprecated( $function );
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
}
|
2012-05-10 20:56:34 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
2012-08-12 19:35:37 +00:00
|
|
|
* @since 1.20
|
|
|
|
|
*
|
2012-05-10 20:56:34 +00:00
|
|
|
* @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 );
|
|
|
|
|
|
2012-06-05 22:58:54 +00:00
|
|
|
$res = $db->select( $table, $fields, $condition, wfGetCaller(), array( 'ORDER BY' => $fields ) );
|
2012-05-10 20:56:34 +00:00
|
|
|
$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)" );
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-12 19:37:54 +00:00
|
|
|
/**
|
|
|
|
|
* Utility method taking an array of elements and wrapping
|
|
|
|
|
* each element in it's own array. Useful for data providers
|
|
|
|
|
* that only return a single argument.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.20
|
|
|
|
|
*
|
|
|
|
|
* @param array $elements
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function arrayWrap( array $elements ) {
|
|
|
|
|
return array_map(
|
|
|
|
|
function( $element ) {
|
|
|
|
|
return array( $element );
|
|
|
|
|
},
|
|
|
|
|
$elements
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-02 14:24:10 +00:00
|
|
|
/**
|
|
|
|
|
* Assert that two arrays are equal. By default this means that both arrays need to hold
|
|
|
|
|
* the same set of values. Using additional arguments, order and associated key can also
|
|
|
|
|
* be set as relevant.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.20
|
|
|
|
|
*
|
|
|
|
|
* @param array $expected
|
|
|
|
|
* @param array $actual
|
|
|
|
|
* @param boolean $ordered If the order of the values should match
|
|
|
|
|
* @param boolean $named If the keys should match
|
|
|
|
|
*/
|
|
|
|
|
protected function assertArrayEquals( array $expected, array $actual, $ordered = false, $named = false ) {
|
|
|
|
|
if ( !$ordered ) {
|
2012-08-12 20:55:55 +00:00
|
|
|
$this->objectAssociativeSort( $expected );
|
|
|
|
|
$this->objectAssociativeSort( $actual );
|
2012-07-02 14:24:10 +00:00
|
|
|
}
|
|
|
|
|
|
2012-07-26 18:08:54 +00:00
|
|
|
if ( !$named ) {
|
|
|
|
|
$expected = array_values( $expected );
|
|
|
|
|
$actual = array_values( $actual );
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-02 14:24:10 +00:00
|
|
|
call_user_func_array(
|
|
|
|
|
array( $this, 'assertEquals' ),
|
|
|
|
|
array_merge( array( $expected, $actual ), array_slice( func_get_args(), 4 ) )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-12 20:55:55 +00:00
|
|
|
/**
|
|
|
|
|
* Does an associative sort that works for objects.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.20
|
|
|
|
|
*
|
|
|
|
|
* @param array $array
|
|
|
|
|
*/
|
|
|
|
|
protected function objectAssociativeSort( array &$array ) {
|
|
|
|
|
uasort(
|
|
|
|
|
$array,
|
|
|
|
|
function( $a, $b ) {
|
|
|
|
|
return serialize( $a ) > serialize( $b ) ? 1 : -1;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-10 20:56:34 +00:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
2012-08-12 19:35:37 +00:00
|
|
|
* @since 1.20
|
|
|
|
|
*
|
2012-05-10 20:56:34 +00:00
|
|
|
* @param $r mixed the array to remove string keys from.
|
|
|
|
|
*/
|
|
|
|
|
protected static function stripStringKeys( &$r ) {
|
2012-08-12 19:35:37 +00:00
|
|
|
if ( !is_array( $r ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-05-10 20:56:34 +00:00
|
|
|
|
|
|
|
|
foreach ( $r as $k => $v ) {
|
2012-08-12 19:35:37 +00:00
|
|
|
if ( is_string( $k ) ) {
|
|
|
|
|
unset( $r[$k] );
|
|
|
|
|
}
|
2012-05-10 20:56:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
2012-08-12 19:35:37 +00:00
|
|
|
|
2011-07-25 16:27:33 +00:00
|
|
|
}
|