New test for SQLite that checks tables.sql conversion as a whole
[lhc/web/wiklou.git] / maintenance / tests / DatabaseSqliteTest.php
1 <?php
2
3 class MockDatabaseSqlite extends DatabaseSqliteStandalone {
4 var $lastQuery;
5
6 function __construct( ) {
7 parent::__construct( '' );
8 }
9
10 function query( $sql, $fname = '', $tempIgnore = false ) {
11 $this->lastQuery = $sql;
12 return true;
13 }
14
15 function replaceVars( $s ) {
16 return parent::replaceVars( $s );
17 }
18 }
19
20 class DatabaseSqliteTest extends PHPUnit_Framework_TestCase {
21 var $db;
22
23 function setup() {
24 if ( !extension_loaded( 'pdo_sqlite' ) ) {
25 $this->markTestIncomplete( 'No SQLite support detected' );
26 }
27 $this->db = new MockDatabaseSqlite();
28 }
29
30 function replaceVars( $sql ) {
31 // normalize spacing to hide implementation details
32 return preg_replace( '/\s+/', ' ', $this->db->replaceVars( $sql ) );
33 }
34
35 function testReplaceVars() {
36 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
37
38 $this->assertEquals( "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
39 . "foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
40 $this->replaceVars( "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
41 foo_name varchar(255) binary NOT NULL DEFAULT '', foo_int tinyint( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;" )
42 );
43
44 $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
45 $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
46 );
47
48 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
49 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
50 'Table name changed'
51 );
52
53 $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
54 $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
55 );
56 }
57
58 function testEntireSchema() {
59 global $IP;
60
61 $allowedTypes = array_flip( array(
62 'integer',
63 'real',
64 'text',
65 'blob', // NULL type is omitted intentionally
66 ) );
67
68 $db = new DatabaseSqliteStandalone( ':memory:' );
69 $db->sourceFile( "$IP/maintenance/tables.sql" );
70
71 $tables = $db->query( "SELECT name FROM sqlite_master WHERE type='table'", __METHOD__ );
72 foreach ( $tables as $table ) {
73 if ( strpos( $table->name, 'sqlite_' ) === 0 ) continue;
74
75 $columns = $db->query( "PRAGMA table_info({$table->name})", __METHOD__ );
76 foreach ( $columns as $col ) {
77 if ( !isset( $allowedTypes[strtolower( $col->type )] ) ) {
78 $this->fail( "Table {$table->name} has column {$col->name} with non-native type '{$col->type}'" );
79 }
80 }
81 }
82 $db->close();
83 }
84 }