Created
June 14, 2011 16:23
-
-
Save underwhelmed/1025257 to your computer and use it in GitHub Desktop.
Example of SimpleTest unit test with mock class
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
<?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; | |
} | |
} | |
?> |
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
<?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