| 1 | <?php |
| 2 | |
| 3 | require_once 'PHPUnit.php'; |
| 4 | |
| 5 | require_once 'DB.php'; |
| 6 | require_once 'diogenes/diogenes.database.inc.php'; |
| 7 | |
| 8 | class DBInitialTest extends PHPUnit_TestCase |
| 9 | { |
| 10 | var $db = NULL; |
| 11 | |
| 12 | function DBInitialTest($name) |
| 13 | { |
| 14 | parent::PHPUnit_TestCase($name); |
| 15 | |
| 16 | if (!extension_loaded('mysql') && !dl('mysql.so')) |
| 17 | { |
| 18 | die("The MySQL module is not your PHP configuration and could not be loaded, fix your PHP setup!"); |
| 19 | } |
| 20 | |
| 21 | $conn = mysql_connect('localhost', 'test'); |
| 22 | |
| 23 | if (!$conn) |
| 24 | { |
| 25 | die("You need to have a MySQL database running on the local host, with a user\ncalled 'test' with no password."); |
| 26 | } |
| 27 | |
| 28 | $rv = mysql_query("DROP DATABASE test"); |
| 29 | if (!$rv) |
| 30 | { |
| 31 | die("Database drop failed. The user 'test' must have full privileges to the\n'test' database."); |
| 32 | } |
| 33 | |
| 34 | mysql_query("CREATE DATABASE test"); |
| 35 | |
| 36 | system("mysql -u test test < config/db/diogenes.sql"); |
| 37 | } |
| 38 | |
| 39 | function testTables() |
| 40 | { |
| 41 | $tables = array('diogenes_auth', 'diogenes_logactions', |
| 42 | 'diogenes_logevents', |
| 43 | 'diogenes_logsessions', 'diogenes_option', |
| 44 | 'diogenes_perm', 'diogenes_site'); |
| 45 | |
| 46 | $DB = DB::Connect('mysql://test@localhost/test'); |
| 47 | if (DB::isError($DB)) |
| 48 | { |
| 49 | die($DB->getMessage()."\n".$DB->getUserInfo()."\n"); |
| 50 | } |
| 51 | |
| 52 | $this->assertEquals($tables, $DB->getCol('show tables')); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | class DiogenesDatabaseTest extends PHPUnit_TestCase |
| 57 | { |
| 58 | function testConstructor() |
| 59 | { |
| 60 | $DB = new DiogenesDatabase('localhost', 'test', 'test', ''); |
| 61 | |
| 62 | $this->assertTrue(is_object($DB)); |
| 63 | } |
| 64 | |
| 65 | function testErrors() |
| 66 | { |
| 67 | $db = new DiogenesDatabase('tst', 'localhost', 'test', ''); |
| 68 | |
| 69 | $this->assertTrue($db->err()); |
| 70 | $this->assertEquals(1049, $db->errno()); |
| 71 | $this->assertEquals("Unknown database 'tst'", $db->error()); |
| 72 | |
| 73 | $db->ResetError(); |
| 74 | $this->assertFalse($db->err()); |
| 75 | $this->assertEquals(0, $db->errno()); |
| 76 | $this->assertEquals('', $db->error()); |
| 77 | |
| 78 | } |
| 79 | |
| 80 | function testMoreErrors() |
| 81 | { |
| 82 | $db = new DiogenesDatabase('test', 'localhost', 'test', ''); |
| 83 | |
| 84 | $this->assertFalse($db->err(), "Should be no error"); |
| 85 | |
| 86 | $db->query("SLECT"); |
| 87 | |
| 88 | $this->assertTrue($db->err(), "Should be error"); |
| 89 | $this->assertEquals(1064, $db->errno()); |
| 90 | $this->assertEquals("You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'SLECT' at line 1", $db->error()); |
| 91 | $this->assertEquals("SLECT", $db->errinfo()); |
| 92 | |
| 93 | $db->ResetError(); |
| 94 | $this->assertEquals('', $db->errinfo()); |
| 95 | } |
| 96 | } |