Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
adds minimal test coverage for core API classes - PHPUnit
  • Loading branch information
bshaffer committed Mar 9, 2011
1 parent 511044c commit a4fbb46
Show file tree
Hide file tree
Showing 13 changed files with 473 additions and 106 deletions.
2 changes: 1 addition & 1 deletion lib/EchoNest/Api.php
Expand Up @@ -6,7 +6,7 @@
* @author Brent Shaffer <bshafs at gmail dot com>
* @license MIT License
*/
abstract class EchoNest_Api
abstract class EchoNest_Api implements EchoNest_ApiInterface
{
/**
* The core EchoNest Client
Expand Down
5 changes: 5 additions & 0 deletions lib/EchoNest/ApiInterface.php
@@ -0,0 +1,5 @@
<?php

interface EchoNest_ApiInterface
{
}
2 changes: 1 addition & 1 deletion lib/EchoNest/Autoloader.php
Expand Up @@ -26,7 +26,7 @@ static public function autoload($class)
if (0 !== strpos($class, 'EchoNest')) {
return;
}

if (file_exists($file = dirname(__FILE__).'/../'.str_replace('_', '/', $class).'.php')) {
require $file;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/EchoNest/Client.php
Expand Up @@ -202,7 +202,7 @@ public function getCatalogApi()
* @param EchoNestApiAbstract $api the API instance
* @return EchoNest_Client fluent interface
*/
public function setApi($name, EchoNestApiAbstract $instance)
public function setApi($name, EchoNest_ApiInterface $instance)
{
$this->apis[$name] = $instance;

Expand Down
31 changes: 22 additions & 9 deletions lib/EchoNest/HttpClient.php
Expand Up @@ -25,15 +25,28 @@ abstract class EchoNest_HttpClient implements EchoNest_HttpClientInterface
'debug' => false
);

/**
* Instanciate a new request
*
* @param array $options Request options
*/
public function __construct(array $options = array())
{
$this->configure($options);
}
/**
* Instanciate a new request
*
* @param array $options Request options
*/
public function __construct(array $options = array())
{
$this->configure($options);
}

/**
* Configure the request
*
* @param array $options Request options
* @return EchoNestApiRequest $this Fluent interface
*/
public function configure(array $options)
{
$this->options = array_merge($this->options, $options);

return $this;
}

/**
* Send a request to the server, receive a response
Expand Down
180 changes: 86 additions & 94 deletions lib/EchoNest/HttpClient/Curl.php
Expand Up @@ -8,19 +8,6 @@
*/
class EchoNest_HttpClient_Curl extends EchoNest_HttpClient
{
/**
* Configure the request
*
* @param array $options Request options
* @return EchoNestApiRequest $this Fluent interface
*/
public function configure(array $options)
{
$this->options = array_merge($this->options, $options);

return $this;
}

/**
* Send a request to the server, receive a response,
* decode the response and returns an associative array
Expand Down Expand Up @@ -50,106 +37,111 @@ public function send($apiPath, array $parameters = array(), $httpMethod = 'GET',
return $response['response'];
}

/**
* Send a request to the server, receive a response
*
* @param string $apiPath Request API path
* @param array $parameters Parameters
* @param string $httpMethod HTTP method to use
*
* @return string HTTP response
*/
public function doRequest($url, array $parameters = array(), $httpMethod = 'GET', array $options = array())
{
if($this->options['api_key'])
/**
* Send a request to the server, receive a response
*
* @param string $apiPath Request API path
* @param array $parameters Parameters
* @param string $httpMethod HTTP method to use
*
* @return string HTTP response
*/
public function doRequest($url, array $parameters = array(), $httpMethod = 'GET', array $options = array())
{
$parameters = array_merge(array(
'format' => $this->options['format'],
'api_key' => $this->options['api_key']
), $parameters);
}
if($this->options['api_key'])
{
$parameters = array_merge(array(
'format' => $this->options['format'],
'api_key' => $this->options['api_key']
), $parameters);
}

$curlOptions = array();
$curlOptions = array();

if (!empty($parameters))
{
$queryString = utf8_encode($this->buildQuery($parameters));

if('GET' === $httpMethod)
{
$url .= '?' . $queryString;
}
else
{
$curlOptions += array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $queryString
);
}
}

$this->debug('send '.$httpMethod.' request: '.$url);

if (!empty($parameters))
{
$queryString = utf8_encode($this->buildQuery($parameters));

if('GET' === $httpMethod)
{
$url .= '?' . $queryString;
}
else
{
$curlOptions += array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $queryString
CURLOPT_URL => $url,
CURLOPT_PORT => $this->options['http_port'],
CURLOPT_USERAGENT => $this->options['user_agent'],
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => $this->options['timeout']
);
}
}

$this->debug('send '.$httpMethod.' request: '.$url);
$response = $this->doCurlCall($curlOptions);

$curlOptions += array(
CURLOPT_URL => $url,
CURLOPT_PORT => $this->options['http_port'],
CURLOPT_USERAGENT => $this->options['user_agent'],
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => $this->options['timeout']
);

$curl = curl_init();

curl_setopt_array($curl, $curlOptions);
if (!in_array($response['headers']['http_code'], array(0, 200, 201))) {
throw new EchoNest_HttpClient_Exception(null, (int) $response['headers']['http_code']);
}

$response = curl_exec($curl);
$headers = curl_getinfo($curl);
$errorNumber = curl_errno($curl);
$errorMessage = curl_error($curl);
if ($response['errorNumber'] != '') {
throw new EchoNest_HttpClient_Exception('error '.$response['errorNumber']);
}

curl_close($curl);

if (!in_array($headers['http_code'], array(0, 200, 201)))
{
throw new EchoNest_HttpClient_Exception(null, (int) $headers['http_code']);
return $response['response'];
}


if ($errorNumber != '')
protected function doCurlCall(array $curlOptions)
{
throw new EchoNest_HttpClient_Exception($errorMessage, $errorNumber);
}
$curl = curl_init();

return $response;
}
curl_setopt_array($curl, $curlOptions);

$response = curl_exec($curl);
$headers = curl_getinfo($curl);
$errorNumber = curl_errno($curl);
$errorMessage = curl_error($curl);

curl_close($curl);

return compact('response', 'headers', 'errorNumber', 'errorMessage');
}

protected function buildQuery($parameters)
{
$append = '';
foreach ($parameters as $key => $value)
protected function buildQuery($parameters)
{
// multiple parameter passed
if (is_array($value))
{
foreach ($value as $val) {
$append.=sprintf('&%s=%s', $key, $val);
$append = '';
foreach ($parameters as $key => $value)
{
// multiple parameter passed
if (is_array($value)) {
foreach ($value as $val) {
$append.=sprintf('&%s=%s', $key, $val);
}
unset($parameters[$key]);
}
elseif (is_bool($value)) {
$parameters[$key] = $value ? 'true' : 'false';
}
}
unset($parameters[$key]);
}
elseif (is_bool($value))
{
$parameters[$key] = $value ? 'true' : 'false';
}

return http_build_query($parameters, '', '&') . $append;
}

return http_build_query($parameters, '', '&') . $append;
}

protected function debug($message)
{
// if($this->options['debug'])
protected function debug($message)
{
print $message."\n";
if($this->options['debug'])
{
print $message."\n";
}
}
}
}
25 changes: 25 additions & 0 deletions phpunit.xml
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="test/bootstrap.php"
>
<testsuites>
<testsuite name="php-echonest-api Test Suite">
<directory>./test/EchoNest/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">./lib/EchoNest/</directory>
</whitelist>
</filter>
</phpunit>
14 changes: 14 additions & 0 deletions test/EchoNest/Tests/ApiTest.php
@@ -0,0 +1,14 @@
<?php

abstract class EchoNest_Tests_ApiTest extends PHPUnit_Framework_TestCase
{
abstract protected function getApiClass();

protected function getApiMock()
{
return $this->getMockBuilder($this->getApiClass())
->setMethods(array('get', 'post'))
->disableOriginalConstructor()
->getMock();
}
}
12 changes: 12 additions & 0 deletions test/EchoNest/Tests/AutoloaderTest.php
@@ -0,0 +1,12 @@
<?php

class EchoNest_Tests_AutoloaderTest extends PHPUnit_Framework_TestCase
{
public function testAutoload()
{
$this->assertFalse(class_exists('FooBarFoo'), '->autoload() does not try to load classes that does not begin with EchoNest');

$autoloader = new EchoNest_Autoloader();
$this->assertNull($autoloader->autoload('Foo'), '->autoload() returns false if it is not able to load a class');
}
}

0 comments on commit a4fbb46

Please sign in to comment.