अपलोड का परीक्षण करने के लिए मैं बाइनरी डेटा के साथ php://input
कैसे पॉप्युलेट कर सकता हूं? (या अन्यथा खंडित अपलोड का परीक्षण करें)। मैं अपने फ्रंटएंड के रूप में plupload का उपयोग कर रहा हूं, और मैं अपने बैकएंड को एकजुट करना चाहता हूं।PHPUnit परीक्षण बाइनरी डेटा अपलोड
public function recieve($file = 'file')
{
// Get parameters
$chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
$chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;
$fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : '';
$targetDir = $this->_uploadDir;
// Clean the fileName for security reasons
$fileName = preg_replace('/[^\w\._]+/', '_', $fileName);
// Make sure the fileName is unique but only if chunking is disabled
if ($chunks < 2 && file_exists($targetDir . DIRECTORY_SEPARATOR . $fileName)) {
$ext = strrpos($fileName, '.');
$fileName_a = substr($fileName, 0, $ext);
$fileName_b = substr($fileName, $ext);
$count = 1;
while (file_exists(
$targetDir . DIRECTORY_SEPARATOR . $fileName_a . '_' . $count . $fileName_b)) {
$count++;
}
$fileName = $fileName_a . '_' . $count . $fileName_b;
}
$filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;
// Create target dir
if (!file_exists($targetDir)) {
if (!is_writable(dirname($targetDir))) {
$this->_messages[] = 'Cannot write to ' . dirname($targetDir) . ' for mkdir';
return false;
}
mkdir($targetDir, 0777, true);
}
// Check permissions
if (!is_writable($targetDir)) {
$this->_messages[] = 'Unable to write to temp directory.';
return false;
}
// Look for the content type header
$contentType = null;
if (isset($_SERVER["HTTP_CONTENT_TYPE"]))
$contentType = $_SERVER["HTTP_CONTENT_TYPE"];
if (isset($_SERVER["CONTENT_TYPE"]))
$contentType = $_SERVER["CONTENT_TYPE"];
// Handle non multipart uploads older WebKit versions didn't support multipart in HTML5
if (strpos($contentType, "multipart") !== false) {
if (isset($_FILES[$file]['tmp_name']) && is_uploaded_file($_FILES[$file]['tmp_name'])) {
// Open temp file
$out = fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab");
if ($out) {
// Read binary input stream and append it to temp file
$in = fopen($_FILES[$file]['tmp_name'], "rb");
if ($in) {
while ($buff = fread($in, 4096)) {
fwrite($out, $buff);
}
} else {
$this->_messages[] = 'Failed to open input stream.';
return false;
}
fclose($in);
fclose($out);
unlink($_FILES[$file]['tmp_name']);
} else {
$this->_messages[] = 'Failed to open output stream.';
return false;
}
} else {
$this->_messages[] = 'Failed to move uploaded file.';
return false;
}
} else {
// Open temp file
$out = fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab");
if ($out) {
// Read binary input stream and append it to temp file
$in = fopen("php://input", "rb");
if ($in) {
while ($buff = fread($in, 4096)) {
fwrite($out, $buff);
}
} else {
$this->_messages[] = 'Failed to open input stream.';
return false;
}
fclose($in);
fclose($out);
} else {
$this->_messages[] = 'Failed to open output stream.';
return false;
}
}
// Check if file upload is complete
if (!$chunks || $chunk == $chunks - 1) {
// Strip the temp .part suffix off
rename("{$filePath}.part", $filePath);
return $filePath;
}
}
* संपादित:
जोड़ा अधिक कोड, दिखाने के क्या मैं इकाई परीक्षण करना चाहते हैं
क्या आप ** इकाई ** या एकीकरण परीक्षण करना चाहते हैं? यदि आप एकीकरण करना चाहते हैं तो इसका परीक्षण करें: क्या विस्तारित है? वेबसर्वर के माध्यम से जा रहा है या नहीं? $ _REQUEST एक एकीकरण परीक्षण का सुझाव देता है लेकिन मैं जवाब देने से पहले पूछना चाहता था। – edorian
मैं सर्वर कोड को प्राप्त करने के लिए सर्वर कोड का परीक्षण करना चाहता हूं, यह सत्यापित करने के लिए कि किसी भी अपलोड की गई फ़ाइल को अपेक्षित माना जाएगा –
फिर से पूछने के लिए खेद है लेकिन मैं इसका पालन नहीं कर रहा हूं। आप कोड के इस टुकड़े को यूनिट परीक्षण कर सकते हैं या वेब सर्वर के माध्यम से फ़ाइल अपलोड करने का परीक्षण कर सकते हैं, दोनों चीजें थोड़ी-थोड़ी समझ में नहीं आती हैं। यदि आप एक "नकली" फ़ाइल अपलोड करना चाहते हैं जो एकीकरण परीक्षण है। हो सकता है कि यहां शब्द ही मुद्दे हैं? http: // stackoverflow।कॉम/प्रश्न/516572/एम-आई-यूनिट-परीक्षण-या-एकीकरण-परीक्षण – edorian