3 class MockDatabaseSqlite
extends DatabaseSqliteStandalone
{
6 function __construct( ) {
7 parent
::__construct( ':memory:' );
10 function query( $sql, $fname = '', $tempIgnore = false ) {
11 $this->lastQuery
= $sql;
15 function replaceVars( $s ) {
16 return parent
::replaceVars( $s );
23 class DatabaseSqliteTest
extends PHPUnit_Framework_TestCase
{
26 public function setUp() {
27 if ( !Sqlite
::isPresent() ) {
28 $this->markTestSkipped( 'No SQLite support detected' );
30 $this->db
= new MockDatabaseSqlite();
33 private function replaceVars( $sql ) {
34 // normalize spacing to hide implementation details
35 return preg_replace( '/\s+/', ' ', $this->db
->replaceVars( $sql ) );
38 public function testReplaceVars() {
39 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
41 $this->assertEquals( "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
42 . "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
43 $this->replaceVars( "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
44 foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;" )
47 $this->assertEquals( "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
48 $this->replaceVars( "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );" )
51 $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
52 $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
55 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
56 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
60 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
61 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
64 $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
65 $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
69 public function testTableName() {
71 $db = new DatabaseSqliteStandalone( ':memory:' );
72 $this->assertEquals( 'foo', $db->tableName( 'foo' ) );
73 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
74 $db->tablePrefix( 'foo' );
75 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
76 $this->assertEquals( 'foobar', $db->tableName( 'bar' ) );
79 function testEntireSchema() {
82 $result = Sqlite
::checkSqlSyntax( "$IP/maintenance/tables.sql" );
83 if ( $result !== true ) {
84 $this->fail( $result );