HEX
Server: Apache/2.2.15 (CentOS)
System: Linux ip-10-0-2-146.eu-west-1.compute.internal 2.6.32-754.35.1.el6.centos.plus.x86_64 #1 SMP Sat Nov 7 11:33:42 UTC 2020 x86_64
User: root (0)
PHP: 5.6.40
Disabled: NONE
Upload Files
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));
	}
}