File: /www/exchange0old/tests/Bin/DbTest.php
<?php
/**
* Created by PhpStorm.
* User: MisterX
* Date: 25.04.2016
* Time: 14:42
*/
class Bin_DbTest extends PHPUnit_Framework_TestCase
{
/**
* @var Bin_Db
*/
protected $connection;
protected $testTable = TEST_TABLE_NAME;
public function setUp(){
$this->connection = Bin_Db::connect();
$this->testTable = $this->connection->prefixTable(TEST_TABLE_NAME);
$this->connection->setSaveQueries(true);
$sql = <<<SQL
CREATE TABLE :table (
id int(11) NOT NULL AUTO_INCREMENT,
vl varchar(255) DEFAULT NULL,
PRIMARY KEY (id)
)
ENGINE = INNODB
SQL;
$this->assertNotFalse($this->connection->query($sql,array(':table'=>$this->testTable)),$this->connection->getLastError());
}
public function tearDown()
{
parent::tearDown(); // TODO: Change the autogenerated stub
$this->connection->query('DROP TABLE :table',array(':table'=>$this->testTable));
}
public function testConnect(){
$this->assertInstanceOf('mysqli',$this->connection->getConnection());
$this->assertTrue($this->connection->getConnection()->ping());
}
public function testDatabase()
{
$requiredDB = $this->connection->getConfig('database');
$result = $this->connection->query('SELECT DATABASE() as db');
//Test array
$row = $result->getFirstRow();
$this->assertInternalType('array',$row);
$this->assertArrayHasKey('db',$row);
$this->assertSame($row['db'],$requiredDB);
}
protected function getCountRecordsInTestTable(){
return (int)$this->connection->query('select count(*) as c from :table',array(':table'=>$this->testTable))->getFirstRow('object')->c;
}
public function testTransactionRollback(){
$connection = $this->connection;
$countRecords = $this->getCountRecordsInTestTable();
$this->assertTrue($connection->transactionStart());
$result = $connection->query("insert into :table (vl) VALUES ('transaction value')",array(':table'=>$this->testTable));
$this->assertNotFalse($result);
$this->assertSame(1,$connection->affectedRows());
$this->assertTrue($connection->transactionRollback());
$newCountRecords = $this->getCountRecordsInTestTable();
$this->assertSame($countRecords,$newCountRecords);
}
public function testTransactionCommit(){
$connection = $this->connection;
$countRecords = $this->getCountRecordsInTestTable();
$this->assertTrue($connection->transactionStart());
$result = $connection->query("insert into :table (vl) VALUES ('transaction value')",array(':table'=>$this->testTable));
$this->assertNotFalse($result);
$this->assertSame(1,$connection->affectedRows());
$this->assertTrue($connection->transactionCommit());
$newCountRecords = $this->getCountRecordsInTestTable();
$this->assertSame(($countRecords+1),$newCountRecords);
}
public function testTransactionMultilevel(){
$connection = $this->connection;
$countRecords = $this->getCountRecordsInTestTable();
$this->assertTrue($connection->transactionStart());
$result = $connection->query("insert into :table (vl) VALUES ('transaction value')",array(':table'=>$this->testTable));
$this->assertNotFalse($result);
$this->assertSame(1,$connection->affectedRows());
$this->assertTrue($connection->transactionStart());
$result = $connection->query("insert into :table (vl) VALUES ('transaction value')",array(':table'=>$this->testTable));
$this->assertNotFalse($result);
$this->assertSame(1,$connection->affectedRows());
$this->assertTrue($connection->transactionRollback());
$this->assertTrue($connection->transactionStart());
$result = $connection->query("insert into :table (vl) VALUES ('transaction value')",array(':table'=>$this->testTable));
$this->assertNotFalse($result);
$this->assertSame(1,$connection->affectedRows());
$this->assertTrue($connection->transactionCommit());
$connection->transactionCommit();
$newCountRecords = $this->getCountRecordsInTestTable();
$this->assertSame(($countRecords+2),$newCountRecords);
}
public function tesBuilderInsert(){
$builder = $this->connection->builder();
echo $builder->insert('test')->setValue('test',false)->getSQL();
}
public function testCount(){
$result = $this->connection->query('SELECT 1;');
$this->assertSame(1,$result->getNumRows());
}
public function escapeValueProvider(){
return array(
array('Test',"'Test'"),
array(true,"'1'"),
array(false,"'0'"),
array(array(1,2),array("'1'","'2'")),
);
}
/**
* @dataProvider escapeValueProvider
* @param $raw
* @param $expected
*/
public function testEscapeValue($raw,$expected){
$this->assertSame($expected, $this->connection->escapeValue($raw));
}
}