Testing CURL wrapper
Posted: Sat Oct 20, 2007 5:33 pm
I'm wondering how I would unit test this:
I suppose I could use partial mocks to handle the download() function, but it'd be laborious and wouldn't cover all of the classes' functionality. Another would be to pass CURL a URL using a stream wrapper, which would remove the overhead of an HTTP call. Any other ideas?
Code: Select all
<?php
/**
* Object-oriented wrapper for CURL, see http://php.net/manual/en/ref.curl.php
*/
class CURL
{
public $handle;
// basic constructs
public function __construct() {
$this->handle = curl_init();
}
public function __clone() {
$this->handle = curl_copy_handle($this->handle);
}
// baton methods
public function setopt($option, $value) {
return curl_setopt($this->handle, $option, $value);
}
public function setoptArray($options) {
return curl_setopt_array($this->handle, $options);
}
public function exec() {
return curl_exec($this->handle);
}
// convenience methods
/**
* Downloads a URL to a string, or a file if specified
* @param $url string URL to download
* @param $filename string Filename to download to, download is returned
* if not set.
* @return String download result, or handle to created file
*/
public function download($url, $filename = false) {
$this->setopt(CURLOPT_URL, $url);
$fh = false;
if ($filename) {
$fh = fopen($filename, 'w');
$this->setopt(CURLOPT_FILE, $fh);
} else {
$this->setopt(CURLOPT_RETURNTRANSFER, true);
}
$result = $this->exec();
if ($fh) return $fh;
else return $result;
}
}