Created
September 24, 2013 19:22
-
-
Save phcostabh/6689915 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| namespace Functional; | |
| use PHPUnit_Framework_TestCase; | |
| /** | |
| * Helper to testing RESTful Requests with PHPUnit. | |
| * | |
| * @uses PHPUnit_Framework_TestCase | |
| * @abstract | |
| */ | |
| abstract class FunctionalTestCase extends PHPUnit_Framework_TestCase | |
| { | |
| protected $curlResource; | |
| /** | |
| * Sets the tests up by cheking for the cURL extension. | |
| * | |
| */ | |
| public function setUp() | |
| { | |
| if (!extension_loaded('curl')) { | |
| $this->markTestSkipped('Warning: curl extension is not loaded.'); | |
| } | |
| } | |
| /** | |
| * Sets up the curl request. | |
| * | |
| * @param mixed $url | |
| * @return void | |
| */ | |
| protected function setUpRequest($url) | |
| { | |
| $this->curlResource = curl_init(); | |
| curl_setopt($this->curlResource, CURLOPT_URL, $url); | |
| curl_setopt($this->curlResource, CURLOPT_HEADER, true); | |
| curl_setopt($this->curlResource, CURLOPT_RETURNTRANSFER, true); | |
| } | |
| /** | |
| * makeGetRequest | |
| * | |
| * @param string $url | |
| * @return array | |
| */ | |
| public function makeGetRequest($url) | |
| { | |
| $this->setUpRequest($url); | |
| return $this->parseResponse(); | |
| } | |
| /** | |
| * makePostRequest | |
| * | |
| * @param string $url | |
| * @param array $data | |
| * @return array | |
| */ | |
| public function makePostRequest($url, $data) | |
| { | |
| $this->setUpRequest($url); | |
| curl_setopt($this->curlResource, CURLOPT_POST, count($data)); | |
| curl_setopt($this->curlResource, CURLOPT_POSTFIELDS, http_build_query($data)); | |
| return $this->parseResponse(); | |
| } | |
| /** | |
| * makePutRequest | |
| * | |
| * @param string $url | |
| * @param array $data | |
| * @return void | |
| */ | |
| public function makePutRequest($url, $data) | |
| { | |
| $this->setUpRequest($url); | |
| curl_setopt($this->curlResource, CURLOPT_CUSTOMREQUEST, 'PUT'); | |
| curl_setopt($this->curlResource, CURLOPT_POSTFIELDS, http_build_query($data)); | |
| return $this->parseResponse(); | |
| } | |
| /** | |
| * makeDeleteRequest | |
| * | |
| * @param string $url | |
| * @return void | |
| */ | |
| public function makeDeleteRequest($url) | |
| { | |
| $this->setUpRequest($url); | |
| curl_setopt($this->curlResource, CURLOPT_CUSTOMREQUEST, 'DELETE'); | |
| return $this->parseResponse(); | |
| } | |
| /** | |
| * Retorna um array com HTTP Status Code, Headers e Content | |
| * respectivamente. | |
| * | |
| * @param string $response | |
| * @return array | |
| */ | |
| protected function parseResponse() | |
| { | |
| $response = curl_exec($this->curlResource); | |
| $http_status_code = curl_getinfo($this->curlResource, CURLINFO_HTTP_CODE); | |
| curl_close($this->curlResource); | |
| $response = explode('\r\n\r\n', $response); | |
| $headers = $response[0]; | |
| $content = count($response) > 1 ? $response[1] : ''; | |
| return array( | |
| $http_status_code, | |
| $headers, | |
| $content | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment