From a99e7c30bae87a83cf6057280ca62002959b85fa Mon Sep 17 00:00:00 2001 From: Jenny Tam Date: Fri, 29 Mar 2019 08:28:46 -0700 Subject: [PATCH] Tests modified for language option for SQL Azure (#963) --- .../pdo_sqlsrv/MsCommon_mid-refactor.inc | 23 ++++++++ .../pdo_sqlsrv/pdo_929_language_option.phpt | 54 ++++++++++++++++++ test/functional/sqlsrv/MsCommon.inc | 20 +++++++ .../sqlsrv/test_error_encoding.phpt | 52 ++++++++++------- ...t_error_encoding_with_language_option.phpt | 56 ++++++++++++------- 5 files changed, 164 insertions(+), 41 deletions(-) create mode 100644 test/functional/pdo_sqlsrv/pdo_929_language_option.phpt diff --git a/test/functional/pdo_sqlsrv/MsCommon_mid-refactor.inc b/test/functional/pdo_sqlsrv/MsCommon_mid-refactor.inc index d327d859..969d1446 100644 --- a/test/functional/pdo_sqlsrv/MsCommon_mid-refactor.inc +++ b/test/functional/pdo_sqlsrv/MsCommon_mid-refactor.inc @@ -624,6 +624,29 @@ function IsDaasMode() return ($daasMode ? true : false); } +function isSQLAzure() +{ + // 'SQL Azure' indicates SQL Database or SQL Data Warehouse + // For details, https://docs.microsoft.com/sql/t-sql/functions/serverproperty-transact-sql + try { + $conn = connect(); + $tsql = "SELECT SERVERPROPERTY ('edition')"; + $stmt = $conn->query($tsql); + + $result = $stmt->fetch(PDO::FETCH_NUM); + $edition = $result[0]; + + if ($edition === "SQL Azure") { + return true; + } else { + return false; + } + } catch (Exception $e) { + echo $e->getMessage(); + die("Could not fetch server property."); + } +} + function isAzureDW() { // Check if running Azure Data Warehouse diff --git a/test/functional/pdo_sqlsrv/pdo_929_language_option.phpt b/test/functional/pdo_sqlsrv/pdo_929_language_option.phpt new file mode 100644 index 00000000..870a8457 --- /dev/null +++ b/test/functional/pdo_sqlsrv/pdo_929_language_option.phpt @@ -0,0 +1,54 @@ +--TEST-- +GitHub issue 929 - able to change the language when connecting +--DESCRIPTION-- +--ENV-- +PHPT_EXEC=true +--SKIPIF-- + +--FILE-- +getCode(); + if ($code !== '42S22') { + echo "Expected SQLSTATE 42S22\n"; + var_dump($code); + } + + // The error message is different when testing against Azure DB / Data Warehouse + // Use wildcard patterns for matching + if (isSQLAzure()) { + $expected = "*Invalid column name [\"']BadColumn[\"']\."; + } else { + $expected = "*Ungültiger Spaltenname [\"']BadColumn[\"']\."; + } + + $message = $e->getMessage(); + if (!fnmatch($expected, $message)) { + echo "Expected to find $expected in the error message\n"; + var_dump($message); + } + +} + +require_once("MsSetup.inc"); + +try { + $conn = new PDO("sqlsrv:server=$server;Language = German", $uid, $pwd); + $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + $tsql = "SELECT *, BadColumn FROM sys.syslanguages"; + $conn->query($tsql); + echo 'This should have failed!\n'; +} catch (PDOException $e) { + verifyErrorContents($e); +} + +unset($conn); + +echo "Done\n"; +?> +--EXPECT-- +Done \ No newline at end of file diff --git a/test/functional/sqlsrv/MsCommon.inc b/test/functional/sqlsrv/MsCommon.inc index eb4933b7..dec27c55 100644 --- a/test/functional/sqlsrv/MsCommon.inc +++ b/test/functional/sqlsrv/MsCommon.inc @@ -84,6 +84,26 @@ function isDaasMode() return ($daasMode ? true : false); } +function isSQLAzure() +{ + // 'SQL Azure' indicates SQL Database or SQL Data Warehouse + // For details, https://docs.microsoft.com/sql/t-sql/functions/serverproperty-transact-sql + $conn = connect(); + $tsql = "SELECT SERVERPROPERTY ('edition')"; + $stmt = sqlsrv_query($conn, $tsql); + + if (sqlsrv_fetch($stmt)) { + $edition = sqlsrv_get_field($stmt, 0); + if ($edition === "SQL Azure") { + return true; + } else { + return false; + } + } else { + die("Could not fetch server property."); + } +} + function isAzureDW() { // Check if running Azure Data Warehouse diff --git a/test/functional/sqlsrv/test_error_encoding.phpt b/test/functional/sqlsrv/test_error_encoding.phpt index 76302e20..c14b41ce 100644 --- a/test/functional/sqlsrv/test_error_encoding.phpt +++ b/test/functional/sqlsrv/test_error_encoding.phpt @@ -4,11 +4,35 @@ Encoding of sqlsrv errors --FILE-- 'UTF-8' )); +require_once('MsSetup.inc'); + +$connectionOptions = array('UID' => $userName, 'PWD' => $userPassword, 'CharacterSet' => 'UTF-8'); +$conn = sqlsrv_connect($server, $connectionOptions); if (!$conn) { die(print_r(sqlsrv_errors(), true)); } @@ -22,27 +46,15 @@ sqlsrv_free_stmt($stmt); $stmt = sqlsrv_query($conn, "select *, BadColumn from sys.syslanguages"); if ($stmt) { - echo 'OK!'; + echo 'This should have failed!\n'; sqlsrv_free_stmt($stmt); } else { - $errs = sqlsrv_errors(); - print_r($errs); + verifyErrorContents(); } sqlsrv_close($conn); +echo "Done\n"; ?> ---EXPECTF-- -Array -( - [0] => Array - ( - [0] => 42S22 - [SQLSTATE] => 42S22 - [1] => 207 - [code] => 207 - [2] => %SUngültiger Spaltenname %cBadColumn%c. - [message] => %SUngültiger Spaltenname %cBadColumn%c. - ) - -) +--EXPECT-- +Done \ No newline at end of file diff --git a/test/functional/sqlsrv/test_error_encoding_with_language_option.phpt b/test/functional/sqlsrv/test_error_encoding_with_language_option.phpt index 0d5d4891..6309af7e 100644 --- a/test/functional/sqlsrv/test_error_encoding_with_language_option.phpt +++ b/test/functional/sqlsrv/test_error_encoding_with_language_option.phpt @@ -1,41 +1,55 @@ --TEST-- -Encoding of sqlsrv errors +GitHub issue 929 - able to change the language when connecting +--DESCRIPTION-- +A test similar to test_error_encoding, created for GitHub issue 929 --SKIPIF-- --FILE-- 'UTF-8','Language'=>'German' )); +require_once("MsSetup.inc"); + +$connectionOptions = array('UID' => $userName, 'PWD' => $userPassword, 'CharacterSet' => 'UTF-8', 'Language' => 'German'); +$conn = sqlsrv_connect($server, $connectionOptions); if (!$conn) { die(print_r(sqlsrv_errors(), true)); } $stmt = sqlsrv_query($conn, "select *, BadColumn from sys.syslanguages"); if ($stmt) { - echo 'OK!'; + echo 'This should have failed!\n'; sqlsrv_free_stmt($stmt); } else { - $errs = sqlsrv_errors(); - print_r($errs); + verifyErrorContents(); } sqlsrv_close($conn); +echo "Done\n"; ?> ---EXPECTF-- -Array -( - [0] => Array - ( - [0] => 42S22 - [SQLSTATE] => 42S22 - [1] => 207 - [code] => 207 - [2] => %SUngültiger Spaltenname %cBadColumn%c. - [message] => %SUngültiger Spaltenname %cBadColumn%c. - ) - -) +--EXPECT-- +Done \ No newline at end of file