Skip to content

Instantly share code, notes, and snippets.

@till
Created Jun 15, 2011
Embed
What would you like to do?
<?php
class CouchDB
{
/**
* @param string $id
*
* @return stdClass
*/
public function getDocument($id)
{
$uri = 'http://server:5984/db/' . $id;
$response = $this->makeRequest($uri);
return $this->parseResponse($response);
}
/**
* @param string $uri
*/
protected function makeRequest($uri)
{
// use an http client to do a request
$response = my_super_duper_client($uri);
}
/**
* @param string $response Most likely JSON.
*
* @return stdClass
* @throws RuntimeException On 404.
*/
protected function parseResponse($response)
{
$obj = json_decode($response);
if (isset($obj->error)) {
throw new RuntimeException("Not found: {$obj->reason}", 404);
}
return $obj;
}
}
@s0enke
Copy link

s0enke commented Jun 15, 2011

btw this works because phpunit mock builder internally creates a new class that extends the original class and then overloads the stubbed methods. So it works for public/protected methods, but f. e. not for final methods.

@s0enke
Copy link

s0enke commented Jun 15, 2011

oops now it's gist-salad :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment