<?php
require_once 'simpletest/unit_tester.php';
require_once 'simpletest/reporter.php';
class PDOPureTestCase extends UnitTestCase
{
private $_oDb;
private $_oInsertStmt;
private $_oSelectStmt;
public function __construct()
{
parent::__construct( 'Test rozszerzenia PDO' );
// Połączenie z bazą
$aParams = array( PDO
::ATTR_ERRMODE => PDO
::ERRMODE_EXCEPTION, PDO::ATTR_CASE => PDO::CASE_LOWER,
PDO::ATTR_PERSISTENT => 1,
PDO::ATTR_TIMEOUT => 5 );
$this->_oDb = new PDO( 'mysql:host=localhost;dbname=test', 'root', 'blazej', $aParams );
$this->_oDb->setAttribute( PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, 1 );
$this->_oInsertStmt = $this->_oDb->prepare( 'INSERT INTO test ( id,
name )
VALUES ( ?, ? )' );
$this->_oSelectStmt = $this->_oDb->prepare( 'SELECT id,
name
FROM test
WHERE name = ?' );
$this->_oDeleteStmt = $this->_oDb->prepare( 'DELETE FROM test
WHERE id = ?' );
}
function setUp() {
$this->_oDb->exec( "INSERT INTO test ( id,
name )
VALUES ( 1,
'Krzysztof' ) " );
}
function tearDown() {
$this->_oDb->exec( 'DELETE FROM test' );
}
public function testTransactionCommit()
{
try
{
$bCaughtException = false;
$this->_oDb->beginTransaction();
$this->_oDeleteStmt->bindValue( 1, 1, PDO::PARAM_INT );
$iDeletedRows = $this->_oDeleteStmt->execute();
$this->_oDb->commit();
$this->assertEqual( 1 , $iDeletedRows );
$this->assertEqual( 0 , $this->_oDb->query( 'SELECT COUNT(*) FROM test' )->fetchColumn() );
}
catch ( Exception $oE )
{
$bCaughtException = true;
}
$this->assertFalse( $bCaughtException );
}
public function testTransactionRollBack()
{
try
{
$bCaughtException = false;
$this->_oDb->beginTransaction();
$this->_oDeleteStmt->bindValue( 1, 1, PDO::PARAM_INT );
$iDeletedRows = $this->_oDeleteStmt->execute();
$this->_oDb->rollBack();
$this->assertEqual( 1 , $iDeletedRows );
$this->_oSelectStmt->bindValue( 1, 'Krzysztof', PDO::PARAM_STR );
$this->_oSelectStmt->execute();
$this->assertEqual( 'Krzysztof' , $this->_oSelectStmt->fetchColumn( 1 ) );
}
catch ( Exception $oE )
{
$bCaughtException = true;
}
$this->assertFalse( $bCaughtException );
}
public function testOnceQuery()
{
try
{
$bCaughtException = false;
$aResult = array( 0 => 1, 1 => 'Krzysztof' );
$oStmt = $this->_oDb->query( 'SELECT * FROM test' );
$this->assertIsA( $oStmt, 'PDOStatement' );
$this->assertEqual( $oStmt->fetch( PDO::FETCH_NUM ), $aResult );
}
catch ( Exception $oE )
{
$bCaughtException = true;
}
$this->assertFalse( $bCaughtException );
}
public function testStoredProcedure()
{
try
{
$bCaughtException = false;
$this->_oDb->exec( 'CALL select_name( @param )' );
$oStmt = $this->_oDb->query( 'SELECT @param' );
$this->assertEqual( 'Krzysztof', $oStmt->fetchColumn() );
}
catch ( Exception $oE )
{
$bCaughtException = true;
}
$this->assertFalse( $bCaughtException );
}
public function testStoredFunction()
{
try
{
$oStmt = $this->_oDb->query( "SELECT hello('Krzysztof')" );
$this->assertEqual( 'Hello, Krzysztof!', $oStmt->fetchColumn() );
}
catch ( Exception $oE )
{
$bCaughtException = true;
}
$this->assertFalse( $bCaughtException );
}
}
?>