2017-05-03 23:44:43 +02:00
--TEST--
This test verifies that GitHub issue #378 is fixed.
--DESCRIPTION--
GitHub issue #378 - output parameters appends garbage info when variable is initialized with different data type
2019-04-01 22:20:05 +02:00
Steps to reproduce the issue:
2017-05-03 23:44:43 +02:00
1- create a store procedure with print and output parameter
2- initialize output parameters to a different data type other than the type declared in sp.
3- set the WarningsReturnAsErrors to true
2017-11-29 01:40:49 +01:00
4- call sp.
2019-04-01 22:20:05 +02:00
Also check error conditions by passing output parameters NOT by reference.
--ENV--
PHPT_EXEC=true
2017-11-29 01:40:49 +01:00
--SKIPIF--
<?php require('skipif_versions_old.inc'); ?>
2017-05-03 23:44:43 +02:00
--FILE--
<?php
2017-10-04 23:22:56 +02:00
require_once('MsCommon.inc');
2017-05-03 23:44:43 +02:00
2017-11-29 01:40:49 +01:00
$conn = AE\connect();
2017-05-03 23:44:43 +02:00
//----------------Main---------------------------
2017-11-29 01:40:49 +01:00
$procName = 'test_378';
2017-05-03 23:44:43 +02:00
createSP($conn, $procName);
2019-04-01 22:20:05 +02:00
runTests($conn, $procName, true);
runTests($conn, $procName, false);
2017-11-29 01:40:49 +01:00
dropProc($conn, $procName);
echo "Done\n";
2017-05-03 23:44:43 +02:00
//-------------------functions-------------------
2017-10-04 23:22:56 +02:00
function createSP($conn, $procName)
{
2017-11-29 01:40:49 +01:00
dropProc($conn, $procName);
2017-10-04 23:22:56 +02:00
$sp_sql="create proc $procName @p1 integer, @p2 integer, @p3 integer output
2017-05-03 23:44:43 +02:00
as
begin
select @p3 = @p1 + @p2
print @p3
end
";
2017-10-04 23:22:56 +02:00
$stmt = sqlsrv_query($conn, $sp_sql);
if ($stmt === false) {
fatalError("Failed to create stored procedure");
}
2017-05-03 23:44:43 +02:00
}
2019-04-01 22:20:05 +02:00
//-------------------functions-------------------
function runTests($conn, $procName, $warningAsErrors)
{
sqlsrv_configure('WarningsReturnAsErrors', $warningAsErrors);
trace("\nWarningsReturnAsErrors: $warningAsErrors\n");
executeSP($conn, $procName, true, false);
executeSP($conn, $procName, true, true);
executeSP($conn, $procName, false, false);
executeSP($conn, $procName, false, true);
}
function compareErrors()
{
$message = 'Variable parameter 3 not passed by reference (prefaced with an &). Output or bidirectional variable parameters (SQLSRV_PARAM_OUT and SQLSRV_PARAM_INOUT) passed to sqlsrv_prepare or sqlsrv_query should be passed by reference, not by value.';
$error = sqlsrv_errors()[0]['message'];
if ($error !== $message) {
print_r(sqlsrv_errors(), true);
return;
}
trace("Comparing errors: matched!\n");
}
function executeSP($conn, $procName, $noRef, $prepare)
2017-10-04 23:22:56 +02:00
{
$expected = 3;
$v1 = 1;
$v2 = 2;
2020-08-06 23:02:40 +02:00
$v3 = 0;
2017-11-29 01:40:49 +01:00
$res = true;
2019-04-01 22:20:05 +02:00
$tsql = "{call $procName( ?, ?, ?)}";
if ($noRef) {
$params = array($v1, $v2, array($v3, SQLSRV_PARAM_OUT));
} else {
$params = array($v1, $v2, array(&$v3, SQLSRV_PARAM_OUT));
}
trace("No reference: $noRef\n");
trace("Use prepared stmt: $prepare\n");
if (AE\isColEncrypted() || $prepare) {
$stmt = sqlsrv_prepare($conn, $tsql, $params);
2017-11-29 01:40:49 +01:00
if ($stmt) {
$res = sqlsrv_execute($stmt);
2019-04-01 22:20:05 +02:00
} else {
fatalError("executeSP: failed in preparing statement with reference($noRef)");
2017-11-29 01:40:49 +01:00
}
2019-04-01 22:20:05 +02:00
if ($noRef) {
if ($res !== false) {
echo "Expect this to fail!\n";
}
compareErrors();
return;
}
2017-11-29 01:40:49 +01:00
} else {
2019-04-01 22:20:05 +02:00
$stmt = sqlsrv_query($conn, $tsql, $params);
if ($noRef) {
if ($stmt !== false) {
echo "Expect this to fail!\n";
}
compareErrors();
return;
}
2017-11-29 01:40:49 +01:00
}
2019-04-01 22:20:05 +02:00
trace("No errors: $v3 and $expected\n");
// No errors expected
2017-11-29 01:40:49 +01:00
if ($stmt === false || !$res) {
2017-10-04 23:22:56 +02:00
print_r(sqlsrv_errors(), true);
}
if ($v3 != $expected) {
fatalError("The expected value is $expected, actual value is $v3\n");
}
2017-05-03 23:44:43 +02:00
}
?>
--EXPECT--
2017-10-04 23:22:56 +02:00
Done