Skip to content

Commit

Permalink
Merge pull request #557 from v-kaywon/refactorPDOTests
Browse files Browse the repository at this point in the history
Refactor pdo tests
  • Loading branch information
yukiwongky authored Oct 11, 2017
2 parents 2320782 + 1d31afe commit edac5b2
Show file tree
Hide file tree
Showing 78 changed files with 5,401 additions and 3,553 deletions.
1,803 changes: 1,803 additions & 0 deletions test/functional/pdo_sqlsrv/MsCommon_mid-refactor.inc

Large diffs are not rendered by default.

41 changes: 23 additions & 18 deletions test/functional/pdo_sqlsrv/pdo_002_connect_app.phpt
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
--TEST--
Connection option APP name unicode
--SKIPIF--
<?php require('skipif.inc'); ?>
<?php require('skipif_mid-refactor.inc'); ?>
--FILE--
<?php
require_once("MsSetup.inc");
require_once("MsCommon_mid-refactor.inc");

$appName = "APP_PoP_银河";
try {
$appName = "APP_PoP_银河";

// Connect
$conn = new PDO("sqlsrv:server=$server;APP=$appName","$uid","$pwd");
// Connect
$conn = connect("APP=$appName");

// Query and Fetch
$query = "SELECT APP_NAME()";
// Query and Fetch
$query = "SELECT APP_NAME()";

$stmt = $conn->query($query);
while ( $row = $stmt->fetch(PDO::FETCH_NUM) ){
echo $row[0]."\n";
}
$stmt = $conn->query($query);
while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
echo $row[0]."\n";
}

$stmt = $conn->query($query);
while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ){
echo $row['']."\n";
}
$stmt = $conn->query($query);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $row['']."\n";
}

// Free the connection
$conn=null;
echo "Done"
// Free the connection and statement
unset($stmt);
unset($conn);
echo "Done";
} catch (PDOException $e) {
var_dump($e->errorInfo);
}
?>

--EXPECTREGEX--
Expand Down
86 changes: 45 additions & 41 deletions test/functional/pdo_sqlsrv/pdo_011_quote.phpt
Original file line number Diff line number Diff line change
@@ -1,50 +1,54 @@
--TEST--
Insert with quoted parameters
--SKIPIF--
<?php require('skipif.inc'); ?>
<?php require('skipif_mid-refactor.inc'); ?>
--FILE--
<?php
require_once("MsSetup.inc");

// Connect
$conn = new PDO( "sqlsrv:server=$server; database=$databaseName", "$uid", "$pwd" );

$param = 'a \' g';
$param2 = $conn->quote( $param );

// Create a temporary table
$tableName = '#tmpTable';
$query = "CREATE TABLE $tableName (col1 VARCHAR(10), col2 VARCHAR(20))";
$stmt = $conn->exec($query);
if( $stmt === false ) { die(); }

// Inserd data
$query = "INSERT INTO $tableName VALUES( ?, '1' )";
$stmt = $conn->prepare( $query );
$stmt->execute(array($param));

// Inserd data
$query = "INSERT INTO $tableName VALUES( ?, ? )";
$stmt = $conn->prepare( $query );
$stmt->execute(array($param, $param2));

// Query
$query = "SELECT * FROM $tableName";
$stmt = $conn->query($query);
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
print_r( $row['col1'] ." was inserted\n" );
require_once("MsCommon_mid-refactor.inc");

try {
// Connect
$conn = connect();

$param = 'a \' g';
$param2 = $conn->quote($param);

// Create a temporary table
$tableName = getTableName();
$stmt = createTable($conn, $tableName, array("col1" => "varchar(10)", "col2" => "varchar(20)"));

// Insert data
if (!isColEncrypted()) {
$query = "INSERT INTO $tableName VALUES(?, '1')";
$stmt = $conn->prepare($query);
$stmt->execute(array($param));
} else {
insertRow($conn, $tableName, array("col1" => $param, "col2" => "1"), "prepareExecuteBind");
}

// Insert data
insertRow($conn, $tableName, array("col1" => $param, "col2" => $param2), "prepareExecuteBind");

// Query
$query = "SELECT * FROM $tableName";
$stmt = $conn->query($query);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
print_r($row['col1'] ." was inserted\n");
}

// Revert the inserts
$query = "delete from $tableName where col1 = ?";
$stmt = $conn->prepare($query);
$stmt->execute(array($param));

//free the statement and connection
dropTable($conn, $tableName);
unset($stmt);
unset($conn);
} catch (PDOException $e) {
var_dump($e->errorInfo);
}

// Revert the inserts
$query = "delete from $tableName where col1 = ?";
$stmt = $conn->prepare( $query );
$stmt->execute(array($param));

//free the statement and connection
$stmt=null;
$conn=null;
?>
--EXPECT--
a ' g was inserted
a ' g was inserted

a ' g was inserted
30 changes: 17 additions & 13 deletions test/functional/pdo_sqlsrv/pdo_012_bind_param.phpt
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
--TEST--
uses an input/output parameter
--SKIPIF--
<?php require('skipif.inc'); ?>
<?php require('skipif_mid-refactor.inc'); ?>
--FILE--
<?php
require_once("MsSetup.inc");
require_once("MsCommon_mid-refactor.inc");

$dbh = new PDO( "sqlsrv:server=$server; database=$databaseName", "$uid", "$pwd" );
try {
$dbh = connect();

$dbh->query("IF OBJECT_ID('dbo.sp_ReverseString', 'P') IS NOT NULL DROP PROCEDURE dbo.sp_ReverseString");
$dbh->query("CREATE PROCEDURE dbo.sp_ReverseString @String as VARCHAR(2048) OUTPUT as SELECT @String = REVERSE(@String)");
$stmt = $dbh->prepare("EXEC dbo.sp_ReverseString ?");
$string = "123456789";
$stmt->bindParam(1, $string, PDO::PARAM_STR | PDO::PARAM_INPUT_OUTPUT, 2048);
$stmt->execute();
print "Result: ".$string."\n"; // Expect 987654321
dropProc($dbh, "sp_ReverseString");
$dbh->query("CREATE PROCEDURE dbo.sp_ReverseString @String as VARCHAR(2048) OUTPUT as SELECT @String = REVERSE(@String)");
$stmt = $dbh->prepare("EXEC dbo.sp_ReverseString ?");
$string = "123456789";
$stmt->bindParam(1, $string, PDO::PARAM_STR | PDO::PARAM_INPUT_OUTPUT, 2048);
$stmt->execute();
print "Result: ".$string."\n"; // Expect 987654321

//free the statement and connection
$stmt = null;
$dbh = null;
//free the statement and connection
unset($stmt);
unset($conn);
} catch (PDOException $e) {
var_dump($e->errorInfo);
}
?>
--EXPECT--
Result: 987654321
104 changes: 71 additions & 33 deletions test/functional/pdo_sqlsrv/pdo_013_row_count.phpt
Original file line number Diff line number Diff line change
@@ -1,48 +1,86 @@
--TEST--
Number of rows in a result set
--SKIPIF--
<?php require('skipif.inc'); ?>
<?php require('skipif_mid-refactor.inc'); ?>
--FILE--
<?php
require_once("MsSetup.inc");
require_once("MsCommon_mid-refactor.inc");

$conn = new PDO( "sqlsrv:server=$server; database=$databaseName", "$uid", "$pwd" );
try {
$conn = connect();

// Create table
$tableName = '#tmpTable1';
$stmt = $conn->exec("CREATE TABLE $tableName (c1 VARCHAR(32))");
$stmt=null;
// Create table
$tableName = getTableName();
createTable($conn, $tableName, array("c1" => "varchar(32)"));

// Insert data
$query = "INSERT INTO $tableName VALUES ('Salmon'),('Butterfish'),('Cod'),('NULL'),('Crab')";
$stmt = $conn->query($query);
$res[] = $stmt->rowCount();
if (!isColEncrypted()) {
// Insert data
$query = "INSERT INTO $tableName VALUES ('Salmon'),('Butterfish'),('Cod'),('NULL'),('Crab')";
$stmt = $conn->query($query);
$res[] = $stmt->rowCount();

// Update data
$query = "UPDATE $tableName SET c1='Salmon' WHERE c1='Cod'";
$stmt = $conn->query($query);
$res[] = $stmt->rowCount();

// Update data
$query = "UPDATE $tableName SET c1='Salmon' WHERE c1='NULL'";
$stmt = $conn->query($query);
$res[] = $stmt->rowCount();

// Update data
$query = "UPDATE $tableName SET c1='Salmon' WHERE c1='Cod'";
$stmt = $conn->query($query);
$res[] = $stmt->rowCount();
// Update data
$query = "UPDATE $tableName SET c1='Salmon' WHERE c1='NO_NAME'";
$stmt = $conn->query($query);
$res[] = $stmt->rowCount();

// Update data
$query = "UPDATE $tableName SET c1='Salmon' WHERE c1='NULL'";
$stmt = $conn->query($query);
$res[] = $stmt->rowCount();
// Update data
$query = "UPDATE $tableName SET c1='N/A'";
$stmt = $conn->query($query);
$res[] = $stmt->rowCount();

unset($stmt);
} else {
// Insert data
// bind parameter does not work with inserting multiple rows in one SQL command, thus need to insert each row separately
$query = "INSERT INTO $tableName VALUES (?)";
$stmt = $conn->prepare($query);
$params = array("Salmon", "Butterfish", "Cod", "NULL", "Crab");
foreach ($params as $param) {
$stmt->execute(array($param));
}
$res[] = count($params);

// Update data
$query = "UPDATE $tableName SET c1=? WHERE c1=?";
$stmt = $conn->prepare($query);
$stmt->execute(array("Salmon", "Cod"));
$res[] = $stmt->rowCount();

// Update data
$stmt->execute(array("Salmon", "NULL"));
$res[] = $stmt->rowCount();

// Update data
$stmt->execute(array("Salmon", "NO_NAME"));
$res[] = $stmt->rowCount();

$query = "UPDATE $tableName SET c1=?";
$stmt = $conn->prepare($query);
$stmt->execute(array("N/A"));
$res[] = $stmt->rowCount();

unset($stmt);
}

// Update data
$query = "UPDATE $tableName SET c1='Salmon' WHERE c1='NO_NAME'";
$stmt = $conn->query($query);
$res[] = $stmt->rowCount();
print_r($res);

// Update data
$query = "UPDATE $tableName SET c1='N/A'";
$stmt = $conn->query($query);
$res[] = $stmt->rowCount();

print_r($res);

$stmt=null;
$conn=null;
print "Done"
dropTable($conn, $tableName);
unset($conn);
print "Done";
} catch (PDOException $e) {
var_dump($e->errorInfo);
}
?>
--EXPECT--
Array
Expand Down
Loading

0 comments on commit edac5b2

Please sign in to comment.