* Removed require/require_once from maintenance scripts where possible, replaced...
[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( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
54 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
55 );
56
57 $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
58 $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
59 );
60 }
61
62 function testEntireSchema() {
63 global $IP;
64
65 $allowedTypes = array_flip( array(
66 'integer',
67 'real',
68 'text',
69 'blob', // NULL type is omitted intentionally
70 ) );
71
72 $db = new DatabaseSqliteStandalone( ':memory:' );
73 $db->sourceFile( "$IP/maintenance/tables.sql" );
74
75 $tables = $db->query( "SELECT name FROM sqlite_master WHERE type='table'", __METHOD__ );
76 foreach ( $tables as $table ) {
77 if ( strpos( $table->name, 'sqlite_' ) === 0 ) continue;
78
79 $columns = $db->query( "PRAGMA table_info({$table->name})", __METHOD__ );
80 foreach ( $columns as $col ) {
81 if ( !isset( $allowedTypes[strtolower( $col->type )] ) ) {
82 $this->fail( "Table {$table->name} has column {$col->name} with non-native type '{$col->type}'" );
83 }
84 }
85 }
86 $db->close();
87 }
88 }