2011-11-07 19:55:49 +04:00
< ? php
/*
* @ copyright NetMonsters < team @ netmonsters . ru >
* @ link http :// netmonsters . ru
* @ package Majestic
* @ subpackage UnitTests
* @ since 2011 - 11 - 7
*
* Unit tests for MySQLiDriver class
*/
require_once dirname ( __FILE__ ) . '/../../model/Db.php' ;
require_once dirname ( __FILE__ ) . '/../../model/DbStatement.php' ;
require_once dirname ( __FILE__ ) . '/../../model/MySQLiStatement.php' ;
require_once dirname ( __FILE__ ) . '/../../model/DbDriver.php' ;
require_once dirname ( __FILE__ ) . '/../../model/MySQLiDriver.php' ;
2011-11-25 19:50:41 +04:00
require_once dirname ( __FILE__ ) . '/../../exception/GeneralException.php' ;
2011-11-07 19:55:49 +04:00
class MySQLiDriverTest extends PHPUnit_Extensions_Database_TestCase
{
static private $pdo = null ;
private $conn = null ;
2011-11-08 11:58:38 +04:00
private $conf = array ();
2011-11-07 19:55:49 +04:00
protected function getConnection ()
{
if ( $this -> conn === null ) {
if ( self :: $pdo == null ) {
self :: $pdo = new PDO ( $GLOBALS [ 'DB_DSN' ], $GLOBALS [ 'DB_USER' ], $GLOBALS [ 'DB_PASSWD' ]);
}
$this -> conn = $this -> createDefaultDBConnection ( self :: $pdo , $GLOBALS [ 'DB_DBNAME' ]);
}
2011-11-08 11:58:38 +04:00
$this -> conf = array ( 'hostname' => 'localhost' , 'database' => $GLOBALS [ 'DB_DBNAME' ], 'username' => $GLOBALS [ 'DB_USER' ], 'password' => $GLOBALS [ 'DB_PASSWD' ]);
2011-11-07 19:55:49 +04:00
return $this -> conn ;
}
protected function getDataSet ()
{
return $this -> createFlatXMLDataSet ( dirname ( __FILE__ ) . '/testData.xml' );
}
public function run ( PHPUnit_Framework_TestResult $result = NULL )
{
$this -> setPreserveGlobalState ( false );
return parent :: run ( $result );
}
public function testDriver ()
{
if ( ! defined ( 'DEBUG' )) {
define ( 'DEBUG' , false );
}
2011-11-08 11:58:38 +04:00
$driver = new MySQLiDriver ( $this -> conf );
2011-11-07 19:55:49 +04:00
$queryTable = $this -> getConnection () -> createQueryTable (
'table' , 'SELECT * FROM `table`'
);
$expectedTable = $this -> createFlatXmlDataSet ( dirname ( __FILE__ ) . '/testData.xml' )
-> getTable ( " table " );
$this -> assertTablesEqual ( $expectedTable , $queryTable );
$this -> assertEquals ( 1 , $driver -> insert ( 'table' , array ( 'id' => 3 , 'user' => 'tony' , 'content' => 'some test content' , 'created' => '2011-11-07 11:35:20' )));
$this -> assertEquals ( 3 , $this -> getConnection () -> getRowCount ( 'table' ));
}
public function testGetConnection ()
{
if ( ! defined ( 'DEBUG' )) {
define ( 'DEBUG' , false );
}
2011-11-08 11:58:38 +04:00
$driver = new MySQLiDriver ( $this -> conf );
2011-11-07 19:55:49 +04:00
$this -> assertInstanceOf ( 'mysqli' , $driver -> getConnection ());
$this -> assertTrue ( $driver -> isConnected ());
}
/**
2011-11-25 19:50:41 +04:00
* @ expectedException GeneralException
2011-11-07 19:55:49 +04:00
* @ expectedExceptionMessage Unknown database
*/
public function testGetConnectionWrongConfig ()
{
if ( ! defined ( 'DEBUG' )) {
define ( 'DEBUG' , false );
}
2011-11-08 12:01:07 +04:00
$this -> conf [ 'database' ] = 'nodb' ;
$driver = new MySQLiDriver ( $this -> conf );
2011-11-07 19:55:49 +04:00
$this -> assertNull ( 'mysqli' , $driver -> getConnection ());
}
public function testDisconnect ()
{
if ( ! defined ( 'DEBUG' )) {
define ( 'DEBUG' , false );
}
2011-11-08 11:58:38 +04:00
$driver = new MySQLiDriver ( $this -> conf );
2011-11-07 19:55:49 +04:00
$driver -> disconnect ();
$this -> assertAttributeEquals ( null , 'connection' , $driver );
$this -> assertInstanceOf ( 'mysqli' , $driver -> getConnection ());
$this -> assertAttributeInstanceOf ( 'mysqli' , 'connection' , $driver );
$driver -> disconnect ();
$this -> assertAttributeEquals ( null , 'connection' , $driver );
}
public function testInsert ()
{
if ( ! defined ( 'DEBUG' )) {
define ( 'DEBUG' , false );
}
2011-11-08 11:58:38 +04:00
$driver = new MySQLiDriver ( $this -> conf );
2011-11-07 19:55:49 +04:00
$this -> assertEquals ( 1 , $driver -> insert ( 'table' , array ( 'id' => 3 , 'user' => 'tony' , 'content' => 'some test content' , 'created' => '2011-11-07 11:35:20' )));
$this -> assertEquals ( 3 , $driver -> getInsertId ());
$this -> assertEquals ( 1 , $driver -> insert ( 'table' , array ( 'id' => null , 'user' => 'tony' , 'content' => 'some test content' , 'created' => '2011-11-07 11:35:20' )));
$this -> assertEquals ( 4 , $driver -> getInsertId ());
$this -> assertEquals ( 1 , $driver -> insert ( 'table' , array ( 'id' => null , 'user' => true , 'content' => 'some test content' , 'created' => '2011-11-07 11:35:20' )));
$this -> assertEquals ( 5 , $driver -> getInsertId ());
$this -> assertEquals ( 2 , $driver -> insert ( 'table' , array ( 'id' => '5' , 'user' => true , 'content' => 'some test content' , 'created' => '2011-11-07 11:35:20' ), array ( 'id' => 8 )));
$this -> assertEquals ( 8 , $driver -> getInsertId ());
}
/**
* @ TODO : DbDriver :: getInsertId ( $table = null , $key = null ) - params not used
*/
public function testGetInsertId ()
{
if ( ! defined ( 'DEBUG' )) {
define ( 'DEBUG' , false );
}
2011-11-08 11:58:38 +04:00
$driver = new MySQLiDriver ( $this -> conf );
2011-11-07 19:55:49 +04:00
$this -> assertEquals ( 1 , $driver -> insert ( 'table' , array ( 'id' => 3 , 'user' => 'tony' , 'content' => 'some test content' , 'created' => '2011-11-07 11:35:20' )));
$this -> assertEquals ( 3 , $driver -> getInsertId ());
}
public function testTransaction ()
{
if ( ! defined ( 'DEBUG' )) {
define ( 'DEBUG' , false );
}
2011-11-08 11:58:38 +04:00
$driver = new MySQLiDriver ( $this -> conf );
2011-11-07 19:55:49 +04:00
$queryTable = $this -> getConnection () -> createQueryTable (
'table' , 'SELECT * FROM `table`'
);
$expectedTable = $this -> createFlatXmlDataSet ( dirname ( __FILE__ ) . '/testData.xml' )
-> getTable ( " table " );
$this -> assertTablesEqual ( $expectedTable , $queryTable );
$driver -> getConnection ();
$driver -> beginTransaction ();
$driver -> insert ( 'table' , array ( 'id' => null , 'user' => 'tony' , 'content' => 'some test content' , 'created' => '2011-11-07 11:35:20' ));
$driver -> insert ( 'table' , array ( 'id' => null , 'user' => 'tony' , 'content' => 'some test content' , 'created' => '2011-11-07 11:35:20' ));
$driver -> insert ( 'table' , array ( 'id' => null , 'user' => 'tony' , 'content' => 'some test content' , 'created' => '2011-11-07 11:35:20' ));
$driver -> insert ( 'table' , array ( 'id' => null , 'user' => 'tony' , 'content' => 'some test content' , 'created' => '2011-11-07 11:35:20' ));
$queryTable = $this -> getConnection () -> createQueryTable (
'table' , 'SELECT * FROM `table`'
);
$driver -> commit ();
$expectedTable = $this -> createFlatXmlDataSet ( dirname ( __FILE__ ) . '/testDataAfterCommit.xml' )
-> getTable ( " table " );
$this -> assertTablesEqual ( $expectedTable , $queryTable );
}
public function testRollback ()
{
if ( ! defined ( 'DEBUG' )) {
define ( 'DEBUG' , false );
}
2011-11-08 11:58:38 +04:00
$driver = new MySQLiDriver ( $this -> conf );
2011-11-07 19:55:49 +04:00
$queryTable = $this -> getConnection () -> createQueryTable (
'table' , 'SELECT * FROM `table`'
);
$expectedTable = $this -> createFlatXmlDataSet ( dirname ( __FILE__ ) . '/testData.xml' )
-> getTable ( " table " );
$this -> assertTablesEqual ( $expectedTable , $queryTable );
$driver -> getConnection ();
$driver -> beginTransaction ();
$driver -> insert ( 'table' , array ( 'id' => null , 'user' => 'tony' , 'content' => 'some test content' , 'created' => '2011-11-07 11:35:20' ));
$driver -> insert ( 'table' , array ( 'id' => null , 'user' => 'tony' , 'content' => 'some test content' , 'created' => '2011-11-07 11:35:20' ));
$driver -> insert ( 'table' , array ( 'id' => null , 'user' => 'tony' , 'content' => 'some test content' , 'created' => '2011-11-07 11:35:20' ));
$driver -> insert ( 'table' , array ( 'id' => null , 'user' => 'tony' , 'content' => 'some test content' , 'created' => '2011-11-07 11:35:20' ));
$driver -> rollback ();
$queryTable = $this -> getConnection () -> createQueryTable (
'table' , 'SELECT * FROM `table`'
);
$this -> assertTablesEqual ( $expectedTable , $queryTable );
}
}