-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a simple test case with an input file
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
89 changes: 89 additions & 0 deletions
89
test/pdo_sqlsrv/pdo_035_binary_encoding_error_bound_by_name.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
--TEST-- | ||
GitHub Issue #35 binary encoding error when binding by name | ||
--SKIPIF-- | ||
--FILE-- | ||
<?php | ||
function CompareBinaryData($inputFile, $data) | ||
{ | ||
// open input file first | ||
$stream = fopen($inputFile, "rb"); | ||
|
||
$len = strlen($data); | ||
$matched = true; | ||
$numbytes = 8192; | ||
|
||
$pos = 0; | ||
while (! feof($stream) && $pos < $len) | ||
{ | ||
$contents = fread($stream, $numbytes); | ||
|
||
// if $data is empty, check if $contents is also empty | ||
$contents_len = strlen($contents); | ||
if ($len == 0) | ||
{ | ||
$matched = ($contents_len == 0); | ||
break; | ||
} | ||
|
||
// Compare contents (case-sensitive) | ||
$count = ($contents_len < $numbytes) ? $contents_len : $numbytes; | ||
$result = substr_compare($data, $contents, $pos, $count); | ||
|
||
if ($result != 0) | ||
{ | ||
$matched = false; | ||
echo "Data corruption!!\nExpected: $contents\nActual:" . substr($data, $pos, $count) . "\n"; | ||
break; | ||
} | ||
|
||
$pos += $count; | ||
} | ||
|
||
// close the data stream | ||
fclose($stream); | ||
|
||
return $matched; | ||
} | ||
|
||
function test() | ||
{ | ||
require_once("autonomous_setup.php"); | ||
|
||
// Connect | ||
$conn = new PDO("sqlsrv:server=$serverName", $username, $password); | ||
|
||
// Create a temp table | ||
$tableName = "#testTableIssue35"; | ||
$sql = "CREATE TABLE $tableName (Picture varbinary(max))"; | ||
$stmt = $conn->query($sql); | ||
|
||
// Insert data using bind parameters | ||
$sql = "INSERT INTO $tableName VALUES (?)"; | ||
$stmt = $conn->prepare($sql); | ||
$file = dirname(__FILE__)."/bike.jpg"; | ||
$stream = fopen($file, "rb"); | ||
$stmt->setAttribute(constant('PDO::SQLSRV_ATTR_ENCODING'), PDO::SQLSRV_ENCODING_BINARY); | ||
$stmt->bindParam(1, $stream, PDO::PARAM_LOB); | ||
$result = $stmt->execute(); | ||
fclose($stream); | ||
|
||
// fetch it back | ||
$stmt = $conn->prepare("SELECT Picture FROM $tableName"); | ||
$stmt->execute(); | ||
$stmt->bindColumn('Picture', $image, PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY); | ||
$stmt->fetch(PDO::FETCH_BOUND); | ||
|
||
var_dump(CompareBinaryData($file, $image)); | ||
|
||
// Close connection | ||
$stmt = null; | ||
$conn = null; | ||
} | ||
|
||
test(); | ||
|
||
print "Done"; | ||
?> | ||
--EXPECT-- | ||
bool(true) | ||
Done |