con)) $this->con->close(); } public function testConnect() { $this->assertNotNull($this->con, "No database connection"); } /** * @depends testConnect */ public function testBasics() { $this->assertNotEmpty($this->con->getDatabase(), "No database info"); $this->assertNotEmpty($this->con->getDialect(), "No database dialect"); $this->assertNotEmpty($this->con->getDialect()->getDdl(), "DDL is empty"); } /** * @depends testConnect */ public function testCreateDropTable() { $info = new TableInfo('testtable'); $info->addColumn(new ColumnInfo('c1', ColumnInfo::TYPE_INT, -1, array(ColumnInfo::P_AUTONUM))); $info->addColumn(new ColumnInfo('c2', ColumnInfo::TYPE_VCHAR, 32)); $info->addColumn(new ColumnInfo('c3', ColumnInfo::TYPE_DATETIME, -1, array(ColumnInfo::P_ALLOW_NULL))); $info->addColumn(new ColumnInfo('c4', ColumnInfo::TYPE_BOOLEAN), -1, array(ColumnInfo::P_ALLOW_NULL)); $info->addColumn(new ColumnInfo('c5', ColumnInfo::TYPE_VCHAR, 64, array(), $charset = NULL, $collate = NULL, 'DefText')); $info->setPrimary(array('c1')); if($this->con->tableExists('testtable')) $this->con->dropTable('testtable'); $this->con->createTable($info); $this->assertTrue($this->con->tableExists('testtable'), "Table not created"); $this->con->dropTable('testtable'); $this->assertFalse($this->con->tableExists('testtable'), "Table should not exist after drop"); // recreate table for other tests $this->con->createTable($info); $this->assertTrue(in_array('testtable', $this->con->tableList()), "Table not in table list"); } /** * @depends testCreateDropTable */ public function testGetTableInfo() { $info = $this->con->table('testtable')->getInfo(); $this->assertNotNull($info, "Info is null"); $this->assertEquals(5, $info->getColumnCount(), "Invalid column count info"); $cinfo = $info->getColumnInfo('c2'); $this->assertNotNull($cinfo, "ColumnInfo is null"); $this->assertEquals(ColumnInfo::TYPE_VCHAR, $cinfo->getType(), "Invalid column type info"); $this->assertFalse($cinfo->isNullAllowed(), "Invalid column null info"); $cinfo = $info->getColumnInfo('c3'); $this->assertNotNull($cinfo, "ColumnInfo is null"); $this->assertEquals(ColumnInfo::TYPE_DATETIME, $cinfo->getType(), "Invalid column type info"); $this->assertTrue($cinfo->isNullAllowed(), "Invalid column null info"); } /** * @depends testCreateDropTable */ public function testRenameTable() { $this->con->renameTable('testtable', 'testtable2'); $this->assertTrue($this->con->tableExists('testtable2'), "Table not renamed"); $this->assertFalse($this->con->tableExists('testtable'), "Table not renamed"); $this->con->renameTable('testtable2', 'testtable'); $this->assertTrue($this->con->tableExists('testtable'), "Table not renamed"); $this->assertFalse($this->con->tableExists('testtable2'), "Table not renamed"); } /** * @depends testCreateDropTable */ public function testAlterTable() { $t = $this->con->table('testtable'); $t->addColumn(new ColumnInfo('ctemp', ColumnInfo::TYPE_VCHAR, 8, array(ColumnInfo::P_ALLOW_NULL))); $info = $t->getInfo(); $this->assertNotNull($info, "Info is null"); $this->assertEquals(6, $info->getColumnCount(), "Invalid column count"); $t->removeColumn('ctemp'); $info = $t->getInfo(); $this->assertNotNull($info, "Info is null"); $this->assertEquals(5, $info->getColumnCount(), "Invalid column count"); } /** * @depends testCreateDropTable */ public function testInsertAndCountData() { $this->con->setAutocommit(true); $data = array('c2' => 'Testdata1','c3' => new DateTime('2012-10-24')); $this->con->table('testtable')->insert($data); $data = array('c2' => 'Testdata2','c3' => new DateTime('2012-10-25')); $this->con->table('testtable')->insert($data); $this->assertEquals(2, $this->con->table('testtable')->size(), "Invalid row count"); $data = new stdClass(); $data->c2 = 'Testdata3'; $data->c3 = new DateTime('2012-10-26'); $this->con->table('testtable')->insert($data); $this->assertEquals(3, $this->con->table('testtable')->size(), "Invalid row count"); if($this->con->isTransactionSupported()) { $this->con->setAutocommit(false); $this->con->startTransaction(); $data = new stdClass(); $data->c2 = 'Testdata4'; $data->c3 = new DateTime('2012-10-27'); $this->con->table('testtable')->insert($data); $this->con->rollback(); $this->assertEquals(3, $this->con->table('testtable')->size(), "Invalid row count"); $this->con->setAutocommit(true); } } /** * @depends testInsertAndCountData */ public function testSelectData() { $res = $this->con->query("SELECT * FROM testtable ORDER BY c2"); $this->assertNotNull($res, "No result"); $this->assertEquals(3, $res->getRowCount(), "Invalid result size"); $row = $res->fetchObject(); $this->assertTrue(is_object($row), "Invalid result type"); $this->assertEquals('Testdata1', $row->c2, "Invalid result data"); $this->assertEquals('DefText', $row->c5, "Invalid result data (not default value)"); $res->close(); $res = $this->con->table('testtable')->select(array('c2' => 'Testdata2')); $this->assertNotNull($res, "No result"); $this->assertEquals(1, $res->getRowCount(), "Invalid result size"); $row = $res->fetchArray(); $this->assertEquals('Testdata2', $row['c2'], "Invalid result data"); $this->assertTrue($row['c3'] instanceof DateTime, "Invalid result data type"); $res->close(); } /** * @depends testInsertAndCountData */ public function testSelectDataWithQueryBuilder() { $query = $this->con->select()->from('testtable')->where(array('c2' => 'Testdata2')); $res = $this->con->query($query); $this->assertNotNull($res, "No result"); $this->assertEquals(1, $res->getRowCount(), "Invalid result size"); $row = $res->fetchArray(); $this->assertEquals('Testdata2', $row['c2'], "Invalid result data"); $this->assertTrue($row['c3'] instanceof DateTime, "Invalid result data type"); $res->close(); } /** * @depends testInsertAndCountData */ public function testPreparedStatement() { $stmt = $this->con->prepareStatement("SELECT * FROM testtable WHERE c2=?"); $stmt->execute(array('Testdata3')); $this->assertEquals(1, $stmt->getRowCount(), "Invalid result size"); $row = $stmt->fetchObject(); $this->assertTrue(is_object($row), "Invalid result type"); $this->assertEquals('Testdata3', $row->c2, "Invalid result data"); $this->assertEquals('DefText', $row->c5, "Invalid result data (not default value)"); $this->assertTrue($row->c3 instanceof DateTime, "Invalid result data type"); $stmt->close(); } /** * @depends testInsertAndCountData */ public function testPreparedStatementWithQueryBuilder() { $query = $this->con->select()->from('testtable')->where(array('c2' => new Parameter())); $stmt = $this->con->prepareStatement($query); $stmt->execute(array('Testdata3')); $this->assertEquals(1, $stmt->getRowCount(), "Invalid result size"); $row = $stmt->fetchObject(); $this->assertTrue(is_object($row), "Invalid result type"); $this->assertEquals('Testdata3', $row->c2, "Invalid result data"); $this->assertEquals('DefText', $row->c5, "Invalid result data (not default value)"); $this->assertTrue($row->c3 instanceof DateTime, "Invalid result data type"); $stmt->close(); } /** * @depends testSelectData */ public function testUpdateData() { $res = $this->con->table('testtable')->update(array('c2' => 'Testdata21'), array('c2' => 'Testdata2')); $this->assertEquals(1, $res, "Invalid result size"); $res = $this->con->table('testtable')->select(array('c3' => new DateTime('2012-10-25'))); $this->assertNotNull($res, "No result"); $this->assertEquals(1, $res->getRowCount(), "Invalid result size"); $row = $res->fetchArray(); $this->assertEquals('Testdata21', $row['c2'], "Invalid result data"); $this->assertTrue($row['c3'] instanceof DateTime, "Invalid result data type"); $res->close(); } /** * @depends testUpdateData */ public function testDeleteData() { $res = $this->con->table('testtable')->delete(array('c2' => 'Testdata2')); $this->assertEquals(0, $res, "Invalid result size"); if($this->con->isTransactionSupported()) { $this->con->setAutocommit(false); $this->con->startTransaction(); $res = $this->con->table('testtable')->delete(array('c2' => 'Testdata21')); $this->con->rollback(); $this->assertEquals(3, $this->con->table('testtable')->size(), "Invalid row count"); $this->con->setAutocommit(true); } $res = $this->con->table('testtable')->delete(array('c2' => 'Testdata21')); $this->assertEquals(2, $this->con->table('testtable')->size(), "Invalid row count"); } /** * @depends testUpdateData */ public function testDeleteDataWithQueryBuilder() { $query = $this->con->delete()->from('testtable')->where(array('c2' => 'Testdata1')); $this->con->query($query); $this->assertEquals(1, $this->con->table('testtable')->size(), "Invalid row count"); } }