Per wikitech-l discussion: Move tests from maintenance/tests/ to tests/. They're...
[lhc/web/wiklou.git] / tests / phpunit / includes / db / DatabaseSqliteTest.php
1 <?php
2
3 class MockDatabaseSqlite extends DatabaseSqliteStandalone {
4 var $lastQuery;
5
6 function __construct( ) {
7 parent::__construct( ':memory:' );
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 /**
21 * @group sqlite
22 */
23 class DatabaseSqliteTest extends PHPUnit_Framework_TestCase {
24 var $db;
25
26 public function setUp() {
27 if ( !Sqlite::isPresent() ) {
28 $this->markTestSkipped( 'No SQLite support detected' );
29 }
30 $this->db = new MockDatabaseSqlite();
31 }
32
33 private function replaceVars( $sql ) {
34 // normalize spacing to hide implementation details
35 return preg_replace( '/\s+/', ' ', $this->db->replaceVars( $sql ) );
36 }
37
38 public function testReplaceVars() {
39 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
40
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;" )
45 );
46
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 );" )
49 );
50
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) );" )
53 );
54
55 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
56 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
57 'Table name changed'
58 );
59
60 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
61 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
62 );
63
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" )
66 );
67 }
68
69 public function testTableName() {
70 // @todo Moar!
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' ) );
77 }
78
79 function testEntireSchema() {
80 global $IP;
81
82 $result = Sqlite::checkSqlSyntax( "$IP/maintenance/tables.sql" );
83 if ( $result !== true ) {
84 $this->fail( $result );
85 }
86 }
87 }