From b2e962100d246316a2246deb344e187886755003 Mon Sep 17 00:00:00 2001 From: David Puglielli Date: Thu, 16 Nov 2017 16:43:45 -0800 Subject: [PATCH] Refactored tests --- .../pdo_sqlsrv/pdo_empty_result_error.phpt | 263 ++++-------- .../sqlsrv/sqlsrv_empty_result_error.phpt | 393 ++++-------------- 2 files changed, 168 insertions(+), 488 deletions(-) diff --git a/test/functional/pdo_sqlsrv/pdo_empty_result_error.phpt b/test/functional/pdo_sqlsrv/pdo_empty_result_error.phpt index 679ca0fa..0d2b730a 100644 --- a/test/functional/pdo_sqlsrv/pdo_empty_result_error.phpt +++ b/test/functional/pdo_sqlsrv/pdo_empty_result_error.phpt @@ -9,8 +9,38 @@ Test that calling nextRowset() and fetching on nonempty, empty, and null result require_once("MsSetup.inc"); require_once("MsCommon.inc"); +// These are the error messages we expect at various points below +$errorNoMoreResults = "There are no more results returned by the query."; +$errorNoFields = "The active result for the query contains no fields."; + +// This function compares the expected error message and the error returned by errorInfo(). +function CheckError($stmt, $expectedError=NULL) +{ + $actualError = $stmt->errorInfo(); + + if ($actualError[2] != $expectedError) { + echo "Wrong error message:\n"; + print_r($actualError); + } +} + +function Fetch($stmt, $error=NULL) +{ + echo "Fetch...\n"; + $result = $stmt->fetchObject(); + print_r($result); + CheckError($stmt, $error); +} + +function NextResult($stmt, $error=NULL) +{ + echo "Next result...\n"; + $stmt->nextRowset(); + CheckError($stmt, $error); +} + $conn = new PDO( "sqlsrv:Server = $server; Database = $databaseName; ", $uid, $pwd ); -$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); +$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT ); DropTable($conn, 'TestEmptySetTable'); $stmt = $conn->query("CREATE TABLE TestEmptySetTable ([c1] nvarchar(10),[c2] nvarchar(10))"); @@ -38,191 +68,71 @@ $stmt = $conn->query("CREATE PROCEDURE TestEmptySetProc @a nvarchar(10), @b nvar // Call fetch on a nonempty result set echo "Nonempty result set, call fetch first: ###############################\n"; -try -{ - $stmt = $conn->query("TestEmptySetProc @a='a', @b='b'"); +$stmt = $conn->query("TestEmptySetProc @a='a', @b='b'"); +Fetch($stmt); +NextResult($stmt); +Fetch($stmt); +NextResult($stmt, $errorNoMoreResults); - echo "First fetch...\n"; - $result = $stmt->fetchObject(); - print_r($result); +// Call nextRowset on a nonempty result set +echo "Nonempty result set, call nextRowset first: #########################\n"; - echo "Next result...\n"; - $stmt->nextRowset(); +$stmt = $conn->query("TestEmptySetProc @a='a', @b='b'"); +NextResult($stmt); +Fetch($stmt); +NextResult($stmt, $errorNoMoreResults); - echo "Fetch...\n"; - $result = $stmt->fetch(); - print_r($result); +// Call nextRowset twice in succession on a nonempty result set +echo "Nonempty result set, call nextRowset twice: #########################\n"; - echo "Next result...\n"; - $stmt->nextRowset(); -} -catch(Exception $e) -{ - echo $e->getMessage()."\n"; -} - -// Call next_result on a nonempty result set -echo "Nonempty result set, call next_result first: #########################\n"; - -try -{ - $stmt = $conn->query("TestEmptySetProc @a='a', @b='b'"); - - echo "Next result...\n"; - $stmt->nextRowset(); - - echo "Fetch...\n"; - $result = $stmt->fetchObject(); - print_r($result); - - echo "Next result...\n"; - $stmt->nextRowset(); -} -catch(Exception $e) -{ - echo $e->getMessage()."\n"; -} - -// Call next_result twice in succession on a nonempty result set -echo "Nonempty result set, call next_result twice: #########################\n"; -try -{ - $stmt = $conn->query("TestEmptySetProc @a='a', @b='b'"); - - echo "Next result...\n"; - $stmt->nextRowset(); - - echo "Next result...\n"; - $stmt->nextRowset(); -} -catch(Exception $e) -{ - echo $e->getMessage()."\n"; -} +$stmt = $conn->query("TestEmptySetProc @a='a', @b='b'"); +NextResult($stmt); +NextResult($stmt, $errorNoMoreResults); // Call fetch on an empty result set echo "Empty result set, call fetch first: ##################################\n"; -try -{ - $stmt = $conn->query("TestEmptySetProc @a='a', @b='w'"); +$stmt = $conn->query("TestEmptySetProc @a='a', @b='w'"); +Fetch($stmt); +NextResult($stmt); +Fetch($stmt); +NextResult($stmt, $errorNoMoreResults); - echo "First fetch...\n"; - $result = $stmt->fetchObject(); - print_r($result); +// Call nextRowset on an empty result set +echo "Empty result set, call nextRowset first: ############################\n"; - echo "Next result...\n"; - $stmt->nextRowset(); +$stmt = $conn->query("TestEmptySetProc @a='a', @b='w'"); +NextResult($stmt); +Fetch($stmt); +NextResult($stmt, $errorNoMoreResults); - echo "Fetch...\n"; - $result = $stmt->fetchObject(); - print_r($result); +// Call nextRowset twice in succession on an empty result set +echo "Empty result set, call nextRowset twice: ############################\n"; - echo "Next result...\n"; - $stmt->nextRowset(); -} -catch(Exception $e) -{ - echo $e->getMessage()."\n"; -} - -// Call next_result on an empty result set -echo "Empty result set, call next_result first: ############################\n"; - -try -{ - $stmt = $conn->query("TestEmptySetProc @a='a', @b='w'"); - - echo "First go to next result...\n"; - $stmt->nextRowset(); - - echo "Fetch...\n"; - $result = $stmt->fetchObject(); - print_r($result); - - echo "Next result...\n"; - $stmt->nextRowset(); -} -catch(Exception $e) -{ - echo $e->getMessage()."\n"; -} - -// Call next_result twice in succession on an empty result set -echo "Empty result set, call next_result twice: ############################\n"; - -try -{ - $stmt = $conn->query("TestEmptySetProc @a='a', @b='w'"); - - echo "Next result...\n"; - $stmt->nextRowset(); - - echo "Next result...\n"; - $stmt->nextRowset(); -} -catch(Exception $e) -{ - echo $e->getMessage()."\n"; -} +$stmt = $conn->query("TestEmptySetProc @a='a', @b='w'"); +NextResult($stmt); +NextResult($stmt, $errorNoMoreResults); // Call fetch on a null result set echo "Null result set, call fetch first: ###################################\n"; -try -{ - $stmt = $conn->query("TestEmptySetProc @a='a', @b='c'"); +$stmt = $conn->query("TestEmptySetProc @a='a', @b='c'"); +Fetch($stmt, $errorNoFields); +NextResult($stmt); - echo "Fetch...\n"; - $result = $stmt->fetchObject(); - print_r($result); - - echo "Next result...\n"; - $stmt->nextRowset(); -} -catch(Exception $e) -{ - echo $e->getMessage()."\n"; -} - -// Call next_result on a null result set +// Call nextRowset on a null result set echo "Null result set, call next result first: #############################\n"; -try -{ - $stmt = $conn->query("TestEmptySetProc @a='a', @b='c'"); +$stmt = $conn->query("TestEmptySetProc @a='a', @b='c'"); +NextResult($stmt); +Fetch($stmt); - echo "Next result...\n"; - $stmt->nextRowset(); - - echo "Fetch...\n"; - $result = $stmt->fetchObject(); - print_r($result); -} -catch(Exception $e) -{ - echo $e->getMessage()."\n"; -} - -// Call next_result twice in succession on a null result set +// Call nextRowset twice in succession on a null result set echo "Null result set, call next result twice: #############################\n"; -try -{ - $stmt = $conn->query("TestEmptySetProc @a='a', @b='c'"); - - echo "Next result...\n"; - $stmt->nextRowset(); - - - echo "Next result...\n"; - $stmt->nextRowset(); - -} -catch(Exception $e) -{ - echo $e->getMessage()."\n"; -} +$stmt = $conn->query("TestEmptySetProc @a='a', @b='c'"); +NextResult($stmt); +NextResult($stmt, $errorNoMoreResults); $stmt = $conn->query("DROP TABLE TestEmptySetTable"); $stmt = $conn->query("DROP PROCEDURE TestEmptySetProc"); @@ -231,7 +141,7 @@ $conn = null; ?> --EXPECT-- Nonempty result set, call fetch first: ############################### -First fetch... +Fetch... stdClass Object ( [testValue] => a @@ -239,38 +149,31 @@ stdClass Object Next result... Fetch... Next result... -SQLSTATE[IMSSP]: There are no more results returned by the query. -Nonempty result set, call next_result first: ######################### +Nonempty result set, call nextRowset first: ######################### Next result... Fetch... Next result... -SQLSTATE[IMSSP]: There are no more results returned by the query. -Nonempty result set, call next_result twice: ######################### +Nonempty result set, call nextRowset twice: ######################### Next result... Next result... -SQLSTATE[IMSSP]: There are no more results returned by the query. Empty result set, call fetch first: ################################## -First fetch... +Fetch... Next result... Fetch... Next result... -SQLSTATE[IMSSP]: There are no more results returned by the query. -Empty result set, call next_result first: ############################ -First go to next result... +Empty result set, call nextRowset first: ############################ +Next result... Fetch... Next result... -SQLSTATE[IMSSP]: There are no more results returned by the query. -Empty result set, call next_result twice: ############################ +Empty result set, call nextRowset twice: ############################ Next result... Next result... -SQLSTATE[IMSSP]: There are no more results returned by the query. Null result set, call fetch first: ################################### Fetch... -SQLSTATE[IMSSP]: The active result for the query contains no fields. +Next result... Null result set, call next result first: ############################# Next result... Fetch... Null result set, call next result twice: ############################# Next result... -Next result... -SQLSTATE[IMSSP]: There are no more results returned by the query. +Next result... \ No newline at end of file diff --git a/test/functional/sqlsrv/sqlsrv_empty_result_error.phpt b/test/functional/sqlsrv/sqlsrv_empty_result_error.phpt index df90cbe3..482bd466 100644 --- a/test/functional/sqlsrv/sqlsrv_empty_result_error.phpt +++ b/test/functional/sqlsrv/sqlsrv_empty_result_error.phpt @@ -9,6 +9,61 @@ Test that calling sqlsrv_next_result() and fetching on nonempty, empty, and null require_once("MsSetup.inc"); require_once("MsCommon.inc"); +// These are the error messages we expect at various points below +$errorNoMoreResults = "There are no more results returned by the query."; +$errorNoMoreRows = "There are no more rows in the active result set. Since this result set is not scrollable, no more data may be retrieved."; +$errorNoFields = "The active result for the query contains no fields."; + +// Variable function gets an error message that depends on the OS +function getFuncSeqError() +{ + if ( strtoupper( substr( php_uname( 's' ),0,3 ) ) === 'WIN' ) { + return "[Microsoft][ODBC Driver Manager] Function sequence error"; + } else { + return "[unixODBC][Driver Manager] Function sequence error"; + } +} + +$errorFuncSeq = 'getFuncSeqError'; + +// This function takes an array of expected error messages and compares the +// contents to the actual errors +function CheckError($expectedErrors) +{ + $actualErrors = sqlsrv_errors(); + + if (sizeof($actualErrors) != sizeof($expectedErrors)) { + echo "Wrong size for error array\n"; + print_r($actualErrors); + return; + } + + $i = 0; + + foreach ($expectedErrors as $e) { + if ($actualErrors[$i]['message'] != $e) { + echo "Wrong error message:\n"; + print_r($actualErrors[$i]); + } + $i++; + } +} + +function Fetch($stmt, $errors) +{ + echo "Fetch...\n"; + $result = sqlsrv_fetch_array($stmt); + print_r($result); + CheckError($errors); +} + +function NextResult($stmt, $errors) +{ + echo "Next result...\n"; + sqlsrv_next_result($stmt); + CheckError($errors); +} + $conn = sqlsrv_connect($server, array("Database"=>$databaseName, "uid"=>$uid, "pwd"=>$pwd)); DropTable($conn, 'TestEmptySetTable'); @@ -38,158 +93,79 @@ $stmt = sqlsrv_query($conn, "CREATE PROCEDURE TestEmptySetProc @a nvarchar(10), echo "Nonempty result set, call fetch first: ###############################\n"; $stmt = sqlsrv_query($conn,"TestEmptySetProc @a='a', @b='b'"); - -echo "First fetch...\n"; -$result = sqlsrv_fetch_array($stmt);//$result=sqlsrv_get_field($stmt,0); -print_r($result); -print_r(sqlsrv_errors()); - -echo "Next result...\n"; -sqlsrv_next_result($stmt); -print_r(sqlsrv_errors()); - -echo "Fetch...\n"; -$result = sqlsrv_fetch_array($stmt); -print_r($result); -print_r(sqlsrv_errors()); - -echo "Next result...\n"; -sqlsrv_next_result($stmt); -print_r(sqlsrv_errors()); +Fetch($stmt, []); +NextResult($stmt, []); +Fetch($stmt, [$errorFuncSeq()]); +NextResult($stmt, [$errorNoMoreResults, $errorFuncSeq()]); // Call next_result on a nonempty result set echo "Nonempty result set, call next_result first: #########################\n"; $stmt = sqlsrv_query($conn,"TestEmptySetProc @a='a', @b='b'"); - -echo "Next result...\n"; -sqlsrv_next_result($stmt); -print_r(sqlsrv_errors()); - -echo "Fetch...\n"; -$result = sqlsrv_fetch_array($stmt); -print_r($result); -print_r(sqlsrv_errors()); - -echo "Next result...\n"; -sqlsrv_next_result($stmt); -print_r(sqlsrv_errors()); +NextResult($stmt, []); +Fetch($stmt, [$errorFuncSeq()]); +NextResult($stmt, [$errorNoMoreResults, $errorFuncSeq()]); // Call next_result twice in succession on a nonempty result set echo "Nonempty result set, call next_result twice: #########################\n"; $stmt = sqlsrv_query($conn, "TestEmptySetProc @a='a', @b='b'"); - -echo "Next result...\n"; -sqlsrv_next_result($stmt); -print_r(sqlsrv_errors()); - -echo "Next result...\n"; -sqlsrv_next_result($stmt); -print_r(sqlsrv_errors()); +NextResult($stmt, []); +NextResult($stmt, [$errorNoMoreResults]); // Call fetch on an empty result set echo "Empty result set, call fetch first: ##################################\n"; $stmt = sqlsrv_query($conn,"TestEmptySetProc @a='a', @b='w'"); - -echo "First fetch...\n"; -$result = sqlsrv_fetch_array($stmt); -print_r($result); -print_r(sqlsrv_errors()); - -echo "Next result...\n"; -sqlsrv_next_result($stmt); -print_r(sqlsrv_errors()); - -echo "Fetch...\n"; -$result = sqlsrv_fetch_array($stmt); -print_r($result); -print_r(sqlsrv_errors()); - -echo "Next result...\n"; -sqlsrv_next_result($stmt); -print_r(sqlsrv_errors()); +Fetch($stmt, []); +NextResult($stmt, []); +Fetch($stmt, [$errorNoMoreRows]); +NextResult($stmt, [$errorNoMoreResults]); // Call next_result on an empty result set echo "Empty result set, call next_result first: ############################\n"; $stmt = sqlsrv_query($conn,"TestEmptySetProc @a='a', @b='w'"); - -echo "First go to next result...\n"; -sqlsrv_next_result($stmt); -print_r(sqlsrv_errors()); - -echo "Fetch...\n"; -$result = sqlsrv_fetch_array($stmt); -print_r($result); -print_r(sqlsrv_errors()); - -echo "Next result...\n"; -sqlsrv_next_result($stmt); -print_r(sqlsrv_errors()); +NextResult($stmt, []); +Fetch($stmt, [$errorFuncSeq()]); +NextResult($stmt, [$errorNoMoreResults, $errorFuncSeq()]); // Call next_result twice in succession on an empty result set echo "Empty result set, call next_result twice: ############################\n"; $stmt = sqlsrv_query($conn, "TestEmptySetProc @a='a', @b='w'"); - -echo "Next result...\n"; -sqlsrv_next_result($stmt); -print_r(sqlsrv_errors()); - -echo "Next result...\n"; -sqlsrv_next_result($stmt); -print_r(sqlsrv_errors()); +NextResult($stmt, []); +NextResult($stmt, [$errorNoMoreResults]); // Call fetch on a null result set echo "Null result set, call fetch first: ###################################\n"; $stmt = sqlsrv_query($conn, "TestEmptySetProc @a='a', @b='c'"); - -echo "Fetch...\n"; -$result = sqlsrv_fetch_array($stmt); -print_r($result); -print_r(sqlsrv_errors()); - -echo "Next result...\n"; -sqlsrv_next_result($stmt); -print_r(sqlsrv_errors()); +Fetch($stmt, [$errorNoFields]); +NextResult($stmt, []); // Call next_result on a null result set echo "Null result set, call next result first: #############################\n"; $stmt = sqlsrv_query($conn, "TestEmptySetProc @a='a', @b='c'"); - -echo "Next result...\n"; -sqlsrv_next_result($stmt); -print_r(sqlsrv_errors()); - -echo "Fetch...\n"; -$result = sqlsrv_fetch_array($stmt); -print_r(sqlsrv_errors()); +NextResult($stmt, []); +Fetch($stmt, [$errorFuncSeq()]); // Call next_result twice in succession on a null result set echo "Null result set, call next result twice: #############################\n"; $stmt = sqlsrv_query($conn, "TestEmptySetProc @a='a', @b='c'"); - -echo "Next result...\n"; -sqlsrv_next_result($stmt); -print_r(sqlsrv_errors()); - -echo "Next result...\n"; -sqlsrv_next_result($stmt); -print_r(sqlsrv_errors()); +NextResult($stmt, []); +NextResult($stmt, [$errorNoMoreResults]); $stmt = sqlsrv_query($conn, "DROP TABLE TestEmptySetTable"); $stmt = sqlsrv_query($conn, "DROP PROCEDURE TestEmptySetProc"); sqlsrv_free_stmt($stmt); sqlsrv_close($conn); ?> ---EXPECTF-- +--EXPECT-- Nonempty result set, call fetch first: ############################### -First fetch... +Fetch... Array ( [0] => a @@ -197,231 +173,32 @@ Array ) Next result... Fetch... -Array -( - [0] => Array - ( - [0] => HY010 - [SQLSTATE] => HY010 - [1] => 0 - [code] => 0 - [2] => [%rMicrosoft|unixODBC%r][%rODBC D|D%rriver Manager]Function sequence error - [message] => [%rMicrosoft|unixODBC%r][%rODBC D|D%rriver Manager]Function sequence error - ) - -) Next result... -Array -( - [0] => Array - ( - [0] => IMSSP - [SQLSTATE] => IMSSP - [1] => -26 - [code] => -26 - [2] => There are no more results returned by the query. - [message] => There are no more results returned by the query. - ) - - [1] => Array - ( - [0] => HY010 - [SQLSTATE] => HY010 - [1] => 0 - [code] => 0 - [2] => [%rMicrosoft|unixODBC%r][%rODBC D|D%rriver Manager]Function sequence error - [message] => [%rMicrosoft|unixODBC%r][%rODBC D|D%rriver Manager]Function sequence error - ) - -) Nonempty result set, call next_result first: ######################### Next result... Fetch... -Array -( - [0] => Array - ( - [0] => HY010 - [SQLSTATE] => HY010 - [1] => 0 - [code] => 0 - [2] => [%rMicrosoft|unixODBC%r][%rODBC D|D%rriver Manager]Function sequence error - [message] => [%rMicrosoft|unixODBC%r][%rODBC D|D%rriver Manager]Function sequence error - ) - -) Next result... -Array -( - [0] => Array - ( - [0] => IMSSP - [SQLSTATE] => IMSSP - [1] => -26 - [code] => -26 - [2] => There are no more results returned by the query. - [message] => There are no more results returned by the query. - ) - - [1] => Array - ( - [0] => HY010 - [SQLSTATE] => HY010 - [1] => 0 - [code] => 0 - [2] => [%rMicrosoft|unixODBC%r][%rODBC D|D%rriver Manager]Function sequence error - [message] => [%rMicrosoft|unixODBC%r][%rODBC D|D%rriver Manager]Function sequence error - ) - -) Nonempty result set, call next_result twice: ######################### Next result... Next result... -Array -( - [0] => Array - ( - [0] => IMSSP - [SQLSTATE] => IMSSP - [1] => -26 - [code] => -26 - [2] => There are no more results returned by the query. - [message] => There are no more results returned by the query. - ) - -) Empty result set, call fetch first: ################################## -First fetch... +Fetch... Next result... Fetch... -Array -( - [0] => Array - ( - [0] => IMSSP - [SQLSTATE] => IMSSP - [1] => -22 - [code] => -22 - [2] => There are no more rows in the active result set. Since this result set is not scrollable, no more data may be retrieved. - [message] => There are no more rows in the active result set. Since this result set is not scrollable, no more data may be retrieved. - ) - -) Next result... -Array -( - [0] => Array - ( - [0] => IMSSP - [SQLSTATE] => IMSSP - [1] => -26 - [code] => -26 - [2] => There are no more results returned by the query. - [message] => There are no more results returned by the query. - ) - -) Empty result set, call next_result first: ############################ -First go to next result... -Fetch... -Array -( - [0] => Array - ( - [0] => HY010 - [SQLSTATE] => HY010 - [1] => 0 - [code] => 0 - [2] => [%rMicrosoft|unixODBC%r][%rODBC D|D%rriver Manager]Function sequence error - [message] => [%rMicrosoft|unixODBC%r][%rODBC D|D%rriver Manager]Function sequence error - ) - -) Next result... -Array -( - [0] => Array - ( - [0] => IMSSP - [SQLSTATE] => IMSSP - [1] => -26 - [code] => -26 - [2] => There are no more results returned by the query. - [message] => There are no more results returned by the query. - ) - - [1] => Array - ( - [0] => HY010 - [SQLSTATE] => HY010 - [1] => 0 - [code] => 0 - [2] => [%rMicrosoft|unixODBC%r][%rODBC D|D%rriver Manager]Function sequence error - [message] => [%rMicrosoft|unixODBC%r][%rODBC D|D%rriver Manager]Function sequence error - ) - -) +Fetch... +Next result... Empty result set, call next_result twice: ############################ Next result... Next result... -Array -( - [0] => Array - ( - [0] => IMSSP - [SQLSTATE] => IMSSP - [1] => -26 - [code] => -26 - [2] => There are no more results returned by the query. - [message] => There are no more results returned by the query. - ) - -) Null result set, call fetch first: ################################### Fetch... -Array -( - [0] => Array - ( - [0] => IMSSP - [SQLSTATE] => IMSSP - [1] => -28 - [code] => -28 - [2] => The active result for the query contains no fields. - [message] => The active result for the query contains no fields. - ) - -) Next result... Null result set, call next result first: ############################# Next result... Fetch... -Array -( - [0] => Array - ( - [0] => HY010 - [SQLSTATE] => HY010 - [1] => 0 - [code] => 0 - [2] => [%rMicrosoft|unixODBC%r][%rODBC D|D%rriver Manager]Function sequence error - [message] => [%rMicrosoft|unixODBC%r][%rODBC D|D%rriver Manager]Function sequence error - ) - -) Null result set, call next result twice: ############################# Next result... -Next result... -Array -( - [0] => Array - ( - [0] => IMSSP - [SQLSTATE] => IMSSP - [1] => -26 - [code] => -26 - [2] => There are no more results returned by the query. - [message] => There are no more results returned by the query. - ) - -) +Next result... \ No newline at end of file