Working on those unit tests.

This commit is contained in:
Josh Sherman 2014-09-15 23:18:05 -04:00
parent 5198091371
commit fcc46d6bfd
4 changed files with 182 additions and 78 deletions

View file

@ -5,6 +5,7 @@ namespace joshtronic;
class GooglePlaces class GooglePlaces
{ {
private $key = ''; private $key = '';
private $client = '';
private $base_url = 'https://maps.googleapis.com/maps/api/place'; private $base_url = 'https://maps.googleapis.com/maps/api/place';
private $method = null; private $method = null;
private $response = null; private $response = null;
@ -27,9 +28,15 @@ class GooglePlaces
public $getmax = true; public $getmax = true;
private $grid = null; private $grid = null;
public function __construct($key) private $exceptions = array(
'base_url', 'client', 'exceptions', 'getmax', 'grid', 'method',
'output', 'pagetoken', 'response', 'subradius',
);
public function __construct($key, $client = false)
{ {
$this->key = $key; $this->key = $key;
$this->client = $client ? $client : new GooglePlacesClient();
} }
function __set($variable, $value) function __set($variable, $value)
@ -44,13 +51,19 @@ class GooglePlaces
$method = $this->method = strtolower($method); $method = $this->method = strtolower($method);
$url = implode('/', array($this->base_url, $method, $this->output)); $url = implode('/', array($this->base_url, $method, $this->output));
$parameters = array(); $parameters = array();
$parameters = $this->parameterBuilder($parameters); $parameters = $this->parameterBuilder($parameters);
$parameters = $this->methodChecker($parameters, $method); $parameters = $this->methodChecker($parameters, $method);
if (!empty($this->subradius)) { if (!empty($this->subradius))
{
return $this->subdivide($url, $parameters); return $this->subdivide($url, $parameters);
} }
if ($this->pagetoken !== null)
{
$parameters['pagetoken'] = $this->pagetoken;
}
return $this->queryGoogle($url, $parameters); return $this->queryGoogle($url, $parameters);
} }
@ -62,7 +75,7 @@ class GooglePlaces
foreach (get_object_vars($this) as $variable => $value) foreach (get_object_vars($this) as $variable => $value)
{ {
// Except these variables // Except these variables
if (!in_array($variable, array('base_url', 'method', 'output', 'pagetoken', 'response', 'subradius', 'getmax','grid'))) if (!in_array($variable, $this->exceptions))
{ {
// Assuming it's not null // Assuming it's not null
if ($value !== null) if ($value !== null)
@ -91,7 +104,10 @@ class GooglePlaces
if (!in_array($value, array('json', 'xml'))) if (!in_array($value, array('json', 'xml')))
{ {
throw new \Exception('Invalid output, please specify either "json" or "xml".'); throw new \Exception('
Invalid output, please specify either
"json" or "xml".
');
} }
break; break;
@ -101,7 +117,10 @@ class GooglePlaces
if (!in_array($value, array('prominence', 'distance'))) if (!in_array($value, array('prominence', 'distance')))
{ {
throw new \Exception('Invalid rank by value, please specify either "prominence" or "distance".'); throw new \Exception('
Invalid rank by value, please specify
either "prominence" or "distance".
');
} }
break; break;
@ -134,16 +153,24 @@ class GooglePlaces
case 'nearbysearch': case 'nearbysearch':
if (!isset($parameters['location'])) if (!isset($parameters['location']))
{ {
throw new \Exception('You must specify a location before calling nearbysearch().'); throw new \Exception('
You must specify a location before calling
nearbysearch().
');
} }
elseif (isset($parameters['rankby'])) elseif (isset($parameters['rankby']))
{ {
switch ($parameters['rankby']) switch ($parameters['rankby'])
{ {
case 'distance': case 'distance':
if (!isset($parameters['keyword']) && !isset($parameters['name']) && !isset($parameters['types'])) if (!isset($parameters['keyword'])
&& !isset($parameters['name'])
&& !isset($parameters['types']))
{ {
throw new \Exception('You much specify at least one of the following: keyword, name, types.'); throw new \Exception('
You much specify at least one of the
following: keyword, name, types.
');
} }
if (isset($parameters['radius'])) if (isset($parameters['radius']))
@ -155,7 +182,9 @@ class GooglePlaces
case 'prominence': case 'prominence':
if (!isset($parameters['radius'])) if (!isset($parameters['radius']))
{ {
throw new \Exception('You must specify a radius.'); throw new \Exception('
You must specify a radius.
');
} }
break; break;
} }
@ -166,15 +195,23 @@ class GooglePlaces
case 'radarsearch': case 'radarsearch':
if (!isset($parameters['location'])) if (!isset($parameters['location']))
{ {
throw new \Exception('You must specify a location before calling nearbysearch().'); throw new \Exception('
You must specify a location before calling
nearbysearch().
');
} }
elseif (!isset($parameters['radius'])) elseif (!isset($parameters['radius']))
{ {
throw new \Exception('You must specify a radius.'); throw new \Exception('You must specify a radius.');
} }
elseif (empty($parameters['keyword']) && empty($parameters['name']) && empty($parameters['types'])) elseif (empty($parameters['keyword'])
&& empty($parameters['name'])
&& empty($parameters['types']))
{ {
throw new \Exception('A Radar Search request must include at least one of keyword, name, or types.'); throw new \Exception('
A Radar Search request must include at least one
of keyword, name, or types.
');
} }
if (isset($parameters['rankby'])) if (isset($parameters['rankby']))
@ -185,9 +222,13 @@ class GooglePlaces
break; break;
case 'details': case 'details':
if (!(isset($parameters['reference']) ^ isset($parameters['placeid']))) if (!(isset($parameters['reference'])
^ isset($parameters['placeid'])))
{ {
throw new \Exception('You must specify either a placeid or a reference (but not both) before calling details().'); throw new \Exception('
You must specify either a placeid or a reference
(but not both) before calling details().
');
} }
if (isset($parameters['rankby'])) if (isset($parameters['rankby']))
@ -198,9 +239,11 @@ class GooglePlaces
break; break;
} }
} }
return $parameters; return $parameters;
} }
/** /**
* Submits request via curl, sets the response, then returns the response * Submits request via curl, sets the response, then returns the response
*/ */
@ -225,23 +268,9 @@ class GooglePlaces
$querystring .= $variable . '=' . $value; $querystring .= $variable . '=' . $value;
} }
$curl = curl_init(); var_dump($this->client);
$options = array( $response = $this->client->get($url . '?' . $querystring);
CURLOPT_URL => $url . '?' . $querystring,
CURLOPT_HEADER => false,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_RETURNTRANSFER => true,
);
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
if ($error = curl_error($curl))
{
throw new \Exception('CURL Error: ' . $error);
}
if ($this->output == 'json') if ($this->output == 'json')
{ {
@ -257,8 +286,6 @@ class GooglePlaces
throw new \Exception('XML is terrible, don\'t use it, ever.'); throw new \Exception('XML is terrible, don\'t use it, ever.');
} }
curl_close($curl);
$this->response = $response; $this->response = $response;
return $this->response; return $this->response;
@ -285,9 +312,15 @@ class GooglePlaces
*/ */
private function subdivide($url, $parameters) private function subdivide($url, $parameters)
{ {
if (($this->radius % $this->subradius) || ($this->subradius < 200) || (($this->radius / $this->subradius) % 2)) if ($this->radius % $this->subradius
|| $this->subradius < 200
|| ($this->radius / $this->subradius) % 2)
{ {
throw new \Exception('Subradius should divide evenly into radius. Also, subradius should be 200 meters or so. (ex: 2000/200 = 10x10 grid. NOT 2000/33 = 60.6x60.6 grid. NOT 2000/16 = 125x125 grid)'); throw new \Exception('
Subradius should divide evenly into radius. Also, subradius
should be 200 meters or so. (ex: 2000/200 = 10x10 grid. NOT
2000/33 = 60.6x60.6 grid. NOT 2000/16 = 125x125 grid)
');
} }
$center = explode(',', $this->location); $center = explode(',', $this->location);
@ -314,16 +347,27 @@ class GooglePlaces
$this->queryGoogle($url, $parameters); $this->queryGoogle($url, $parameters);
$this->grid[$i][$j] = $this->response; $this->grid[$i][$j] = $this->response;
$this->grid['results'] = array_merge($this->grid['results'], $this->response['results']);
$this->grid['results'] = array_merge(
$this->grid['results'],
$this->response['results']
);
while ($this->response['next_page_token']) while ($this->response['next_page_token'])
{ {
$this->pagetoken = $this->response['next_page_token']; $this->pagetoken = $this->response['next_page_token'];
$this->response = $this->client->get($url, $parameters);
$this->queryGoogle($url, $parameters); $this->grid[$i][$j] = array_merge(
$this->grid[$i][$j],
$this->response
);
$this->grid['results'] = array_merge(
$this->grid['results'],
$this->response['results']
);
$this->grid[$i][$j] = array_merge($this->grid[$i][$j], $this->response);
$this->grid['results'] = array_merge($this->grid['results'], $this->response['results']);
$this->pagetoken = null; $this->pagetoken = null;
} }
} }

View file

@ -0,0 +1,32 @@
<?php
namespace joshtronic;
class GooglePlacesClient implements GooglePlacesInterface
{
public function get($url)
{
$curl = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => false,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_RETURNTRANSFER => true,
);
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
if ($error = curl_error($curl))
{
throw new \Exception('CURL Error: ' . $error);
}
curl_close($curl);
return $response;
}
}

View file

@ -0,0 +1,9 @@
<?php
namespace joshtronic;
interface GooglePlacesInterface
{
public function get($url);
}

View file

@ -1,12 +1,32 @@
<?php <?php
//$places = new joshtronic\GooglePlaces('AIzaSyCT6dVNQaPTRsXwDqb1CjoUJncyzGqKDPY');
require_once '../src/GooglePlaces.php'; require_once '../src/GooglePlaces.php';
require_once '../src/GooglePlacesInterface.php';
require_once '../src/GooglePlacesClient.php';
class GooglePlacesTest extends PHPUnit_Framework_TestCase class GooglePlacesTest extends PHPUnit_Framework_TestCase
{ {
public function testSetVariable()
{
$places = new joshtronic\GooglePlaces('');
$places->foo = 'bar';
$this->assertEquals('bar', $places->foo);
}
public function testNearbySearchProximity() public function testNearbySearchProximity()
{ {
$client = $this->getMock('GooglePlacesInterface', array('get'));
$client->expects($this->exactly(1))
->method('get')
->will($this->returnValue('some return i expect'));
$places = new joshtronic\GooglePlaces('', $client);
$places->location = array(-33.86820, 151.1945860);
$places->radius = 800;
$results = $places->nearbySearch();
} }
public function testNearbySearchDistance() public function testNearbySearchDistance()
@ -30,4 +50,3 @@ class GooglePlacesTest extends PHPUnit_Framework_TestCase
} }
} }
?>