I'm trying to create a wrapper for an API class using TDD with phpspec.
I've written a Client
class which deals with requesting/retrieving data from a REST API which is then mapped to of one several Entity
classes, so it behaves like an ORM.
I'm a bit stuck now I've come to test and extend the application with TDD. As the Client
is a dependency of the entity classes (so that they can request their own child objects), I'm struggling with mocking this.
For example, here's what one of the entities, Comic.php
, might look like:
class Comic {
protected $client;
public $id;
public function __construct(Client $client)
{
$this->client = $client;
}
public function getCharacters()
{
// just an example, this would return an array of Character objects
return $this->client->request("comic/{$this->id}/characters");
}
}
And for the sake of brevity here's what a simplified down version of Client.php
looks like:
class Client {
public function __construct($publicKey, $privateKey)
{
// make token from $publicKey, $privateKey
}
public function request($endpoint)
{
// use token for cURL request to endpoint and return data
}
}
So how might a test on ComicSpec.php
for it_gets_all_characters()
look, as an example?
Hope this makes sense, can provide more info if needed.
Thanks for taking a look.