<?php
-class MockDatabaseSqlite extends DatabaseSqliteStandalone {
+class DatabaseSqliteMock extends DatabaseSqlite {
private $lastQuery;
- function __construct() {
- parent::__construct( ':memory:' );
+ public static function newInstance( array $p = array() ) {
+ $p['dbFilePath'] = ':memory:';
+ $p['schema'] = false;
+
+ return DatabaseBase::factory( 'SqliteMock', $p );
}
function query( $sql, $fname = '', $tempIgnore = false ) {
* @group medium
*/
class DatabaseSqliteTest extends MediaWikiTestCase {
- /** @var MockDatabaseSqlite */
+ /** @var DatabaseSqliteMock */
protected $db;
protected function setUp() {
if ( !Sqlite::isPresent() ) {
$this->markTestSkipped( 'No SQLite support detected' );
}
- $this->db = new MockDatabaseSqlite();
+ $this->db = DatabaseSqliteMock::newInstance();
if ( version_compare( $this->db->getServerVersion(), '3.6.0', '<' ) ) {
$this->markTestSkipped( "SQLite at least 3.6 required, {$this->db->getServerVersion()} found" );
}
*/
public function testAddQuotes( $value, $expected ) {
// check quoting
- $db = new DatabaseSqliteStandalone( ':memory:' );
+ $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
$this->assertEquals( $expected, $db->addQuotes( $value ), 'string not quoted as expected' );
// ok, quoting works as expected, now try a round trip.
*/
public function testTableName() {
// @todo Moar!
- $db = new DatabaseSqliteStandalone( ':memory:' );
+ $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
$this->assertEquals( 'foo', $db->tableName( 'foo' ) );
$this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
$db->tablePrefix( 'foo' );
* @covers DatabaseSqlite::duplicateTableStructure
*/
public function testDuplicateTableStructure() {
- $db = new DatabaseSqliteStandalone( ':memory:' );
+ $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
$db->query( 'CREATE TABLE foo(foo, barfoo)' );
+ $db->query( 'CREATE INDEX index1 ON foo(foo)' );
+ $db->query( 'CREATE UNIQUE INDEX index2 ON foo(barfoo)' );
$db->duplicateTableStructure( 'foo', 'bar' );
$this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)',
$db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
'Normal table duplication'
);
+ $indexList = $db->query( 'PRAGMA INDEX_LIST("bar")' );
+ $index = $indexList->next();
+ $this->assertEquals( 'bar_index1', $index->name );
+ $this->assertEquals( '0', $index->unique );
+ $index = $indexList->next();
+ $this->assertEquals( 'bar_index2', $index->name );
+ $this->assertEquals( '1', $index->unique );
$db->duplicateTableStructure( 'foo', 'baz', true );
$this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)',
$db->selectField( 'sqlite_temp_master', 'sql', array( 'name' => 'baz' ) ),
'Creation of temporary duplicate'
);
+ $indexList = $db->query( 'PRAGMA INDEX_LIST("baz")' );
+ $index = $indexList->next();
+ $this->assertEquals( 'baz_index1', $index->name );
+ $this->assertEquals( '0', $index->unique );
+ $index = $indexList->next();
+ $this->assertEquals( 'baz_index2', $index->name );
+ $this->assertEquals( '1', $index->unique );
$this->assertEquals( 0,
$db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
'Create a temporary duplicate only'
* @covers DatabaseSqlite::duplicateTableStructure
*/
public function testDuplicateTableStructureVirtual() {
- $db = new DatabaseSqliteStandalone( ':memory:' );
+ $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
if ( $db->getFulltextSearchModule() != 'FTS3' ) {
$this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
}
* @covers DatabaseSqlite::deleteJoin
*/
public function testDeleteJoin() {
- $db = new DatabaseSqliteStandalone( ':memory:' );
+ $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
$db->query( 'CREATE TABLE a (a_1)', __METHOD__ );
$db->query( 'CREATE TABLE b (b_1, b_2)', __METHOD__ );
$db->insert( 'a', array(
'user_newtalk.user_last_timestamp', // r84185
);
- $currentDB = new DatabaseSqliteStandalone( ':memory:' );
+ $currentDB = DatabaseSqlite::newStandaloneInstance( ':memory:' );
$currentDB->sourceFile( "$IP/maintenance/tables.sql" );
$profileToDb = false;
* @covers DatabaseSqlite::insertId
*/
public function testInsertIdType() {
- $db = new DatabaseSqliteStandalone( ':memory:' );
+ $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
$databaseCreation = $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__ );
$this->assertInstanceOf( 'ResultWrapper', $databaseCreation, "Database creation" );
}
global $IP;
- $db = new DatabaseSqliteStandalone( ':memory:' );
+ $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
$db->sourceFile( "$IP/tests/phpunit/data/db/sqlite/tables-$version.sql" );
$updater = DatabaseUpdater::newForDB( $db, false, $maint );
$updater->doUpdates( array( 'core' ) );
public function testCaseInsensitiveLike() {
// TODO: Test this for all databases
- $db = new DatabaseSqliteStandalone( ':memory:' );
+ $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
$res = $db->query( 'SELECT "a" LIKE "A" AS a' );
$row = $res->fetchRow();
$this->assertFalse( (bool)$row['a'] );
* @covers DatabaseSqlite::numFields
*/
public function testNumFields() {
- $db = new DatabaseSqliteStandalone( ':memory:' );
+ $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
$databaseCreation = $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__ );
$this->assertInstanceOf( 'ResultWrapper', $databaseCreation, "Failed to create table a" );