Skip to content

Instantly share code, notes, and snippets.

@underwhelmed
Created June 14, 2011 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save underwhelmed/1025257 to your computer and use it in GitHub Desktop.
Save underwhelmed/1025257 to your computer and use it in GitHub Desktop.
Example of SimpleTest unit test with mock class
<?php
class CurlService {
function __construct($url) {
$this->url = $url;
}
private $url;
function getResponse($post) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ats = curl_exec($ch);
curl_close($ch);
return $ats;
}
}
?>
<?php
require_once('simpletest/autorun.php');
require_once('simpletest/unit_tester.php');
require_once('simpletest/mock_objects.php');
require_once('../USPSRateService.php');
require_once('../CurlService.php');
Mock::generate('CurlService');
class RateTests extends UnitTestCase {
function testXMLParser() {
$curl = &new MockCurlService();
$curl->setReturnValue('getResponse', 96);
$svc = new USPSRateService($curl, "066PRADE4585");
$rate = $svc->getInternationalRate("Albania", 2, 0);
$this->assertEqual($rate, 96);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment