Skip to content

Tests modified for language option for SQL Azure #963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions test/functional/pdo_sqlsrv/MsCommon_mid-refactor.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 54 additions & 0 deletions test/functional/pdo_sqlsrv/pdo_929_language_option.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
--TEST--
GitHub issue 929 - able to change the language when connecting
--DESCRIPTION--
--ENV--
PHPT_EXEC=true
--SKIPIF--
<?php require('skipif_mid-refactor.inc'); ?>
--FILE--
<?php
function verifyErrorContents($e)
{
require_once('MsCommon_mid-refactor.inc');

$code = $e->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
20 changes: 20 additions & 0 deletions test/functional/sqlsrv/MsCommon.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 32 additions & 20 deletions test/functional/sqlsrv/test_error_encoding.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,35 @@ Encoding of sqlsrv errors
<?php require('skipif.inc'); ?>
--FILE--
<?php
header('content-type: text/plain;encoding=ISO-8859-1');
function verifyErrorContents()
{
require_once('MsCommon.inc');
$error = sqlsrv_errors()[0];

if ($error['SQLSTATE'] !== '42S22') {
echo "Expected SQLSTATE 42S22\n";
var_dump($error);
}

// 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[\"']\.";
}

require_once("MsCommon.inc");
if (!fnmatch($expected, $error['message'])) {
echo "Expected to find $expected in the error message\n";
var_dump($error);
}

}

require_once('MsSetup.inc');

$conn = connect(array( 'CharacterSet'=>'UTF-8' ));
$connectionOptions = array('UID' => $userName, 'PWD' => $userPassword, 'CharacterSet' => 'UTF-8');
$conn = sqlsrv_connect($server, $connectionOptions);
if (!$conn) {
die(print_r(sqlsrv_errors(), true));
}
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -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--
<?php require('skipif.inc'); ?>
--FILE--
<?php
header('content-type: text/plain;encoding=ISO-8859-1');
function verifyErrorContents()
{
require_once('MsCommon.inc');
$error = sqlsrv_errors()[0];

if ($error['SQLSTATE'] !== '42S22') {
echo "Expected SQLSTATE 42S22\n";
var_dump($error);
}

// 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[\"']\.";
}

require_once("MsCommon.inc");
if (!fnmatch($expected, $error['message'])) {
echo "Expected to find $expected in the error message\n";
var_dump($error);
}

}

require_once("MsSetup.inc");

$conn = connect(array( 'CharacterSet'=>'UTF-8','Language'=>'German' ));
$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