Skip to content

Commit

Permalink
add a simple test case with an input file
Browse files Browse the repository at this point in the history
  • Loading branch information
yitam committed Feb 10, 2017
1 parent 471da9b commit 7ca8091
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
Binary file added test/pdo_sqlsrv/bike.jpg
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 test/pdo_sqlsrv/pdo_035_binary_encoding_error_bound_by_name.phpt
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

0 comments on commit 7ca8091

Please sign in to comment.