Skip to content

Commit

Permalink
Adding unit tests for caching
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Mar 1, 2014
1 parent 5caa082 commit d6d7221
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
15 changes: 7 additions & 8 deletions lib/Tmdb/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class Client
* @param ApiToken $token
* @param boolean $secure
*/
public function __construct(Token $token, ClientInterface $httpClient = null, $secure = false)
public function __construct(ApiToken $token, ClientInterface $httpClient = null, $secure = false)
{
$this->setToken($token);
$this->setSecure($secure);
Expand All @@ -118,16 +118,18 @@ private function constructHttpClient(ClientInterface $httpClient = null)
$acceptJsonHeaderPlugin = new AcceptJsonHeaderPlugin();
$httpClient->addSubscriber($acceptJsonHeaderPlugin);

if ($this->getToken() instanceof Token) {
if ($this->getToken() instanceof ApiToken) {
$apiTokenPlugin = new ApiTokenPlugin($this->getToken());
$httpClient->addSubscriber($apiTokenPlugin);
}

if ($this->cacheEnabled && !empty($this->cachePath)) {
if (!class_exists('Doctrine\Common\Cache\FilesystemCache')) {
/** @codeCoverageIgnoreStart */
throw new RuntimeException(
'Could not find the doctrine cache library, have you added doctrone-cache to your composer.json?'
);
/** @codeCoverageIgnoreEnd */
}

$cachePlugin = new CachePlugin(array(
Expand All @@ -144,7 +146,7 @@ private function constructHttpClient(ClientInterface $httpClient = null)

if ($this->getSessionToken() instanceof SessionToken) {
$sessionTokenPlugin = new SessionTokenPlugin($this->getSessionToken());
$httpClient->getClient()->addSubscriber($sessionTokenPlugin);
$httpClient->addSubscriber($sessionTokenPlugin);
}
}

Expand Down Expand Up @@ -405,13 +407,10 @@ public function getSecure()
*/
public function setSessionToken($sessionToken)
{
if ($this->httpClient->getClient() instanceof \Guzzle\Common\HasDispatcherInterface) {
$sessionTokenPlugin = new SessionTokenPlugin($sessionToken);
$this->httpClient->getClient()->addSubscriber($sessionTokenPlugin);
}

$this->sessionToken = $sessionToken;

$this->constructHttpClient();

return $this;
}

Expand Down
48 changes: 48 additions & 0 deletions test/Tmdb/Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,52 @@ public function assertInstances()
)
);
}

/**
* @test
*/
public function shouldAddCachePluginWhenEnabled()
{
$token = new \Tmdb\ApiToken(self::API_TOKEN);
$client = new \Tmdb\Client($token);
$client->setCaching(true, '/tmp/php-tmdb-api');

$listeners = $client->getHttpClient()
->getClient()
->getEventDispatcher()
->getListeners();

$this->assertEquals(true, $this->isListenerRegistered(
$listeners,
'Guzzle\Plugin\Cache\CachePlugin'
));
}

/**
* Find an plugin in an listeners array
*
* @param $listeners
* @param $class
* @return bool
*/
private function isListenerRegistered($listeners, $class)
{
if (is_object($class)) {
$class = get_class($class);
}

if (is_array($listeners)) {
foreach ($listeners as $subject) {
if (is_object($subject) && get_class($subject) === $class) {
return true;
}

if (is_array($subject)) {
return $this->isListenerRegistered($subject, $class);
}
}
}

return false;
}
}

0 comments on commit d6d7221

Please sign in to comment.