Working on unit tests.

This commit is contained in:
Josh Sherman 2014-09-16 22:28:53 -04:00
parent fcc46d6bfd
commit 926a19c5b3
2 changed files with 65 additions and 49 deletions

View file

@ -4,8 +4,8 @@ namespace joshtronic;
class GooglePlaces class GooglePlaces
{ {
public $client = '';
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;
@ -104,10 +104,7 @@ class GooglePlaces
if (!in_array($value, array('json', 'xml'))) if (!in_array($value, array('json', 'xml')))
{ {
throw new \Exception(' throw new \Exception('Invalid output, please specify either "json" or "xml".');
Invalid output, please specify either
"json" or "xml".
');
} }
break; break;
@ -117,10 +114,7 @@ class GooglePlaces
if (!in_array($value, array('prominence', 'distance'))) if (!in_array($value, array('prominence', 'distance')))
{ {
throw new \Exception(' throw new \Exception('Invalid rank by value, please specify either "prominence" or "distance".');
Invalid rank by value, please specify
either "prominence" or "distance".
');
} }
break; break;
@ -153,10 +147,7 @@ class GooglePlaces
case 'nearbysearch': case 'nearbysearch':
if (!isset($parameters['location'])) if (!isset($parameters['location']))
{ {
throw new \Exception(' throw new \Exception('You must specify a location before calling nearbysearch().');
You must specify a location before calling
nearbysearch().
');
} }
elseif (isset($parameters['rankby'])) elseif (isset($parameters['rankby']))
{ {
@ -167,10 +158,7 @@ class GooglePlaces
&& !isset($parameters['name']) && !isset($parameters['name'])
&& !isset($parameters['types'])) && !isset($parameters['types']))
{ {
throw new \Exception(' throw new \Exception('You much specify at least one of the following: keyword, name, types.');
You much specify at least one of the
following: keyword, name, types.
');
} }
if (isset($parameters['radius'])) if (isset($parameters['radius']))
@ -182,9 +170,7 @@ class GooglePlaces
case 'prominence': case 'prominence':
if (!isset($parameters['radius'])) if (!isset($parameters['radius']))
{ {
throw new \Exception(' throw new \Exception('You must specify a radius.');
You must specify a radius.
');
} }
break; break;
} }
@ -195,10 +181,7 @@ class GooglePlaces
case 'radarsearch': case 'radarsearch':
if (!isset($parameters['location'])) if (!isset($parameters['location']))
{ {
throw new \Exception(' throw new \Exception('You must specify a location before calling radarsearch().');
You must specify a location before calling
nearbysearch().
');
} }
elseif (!isset($parameters['radius'])) elseif (!isset($parameters['radius']))
{ {
@ -208,10 +191,7 @@ class GooglePlaces
&& empty($parameters['name']) && empty($parameters['name'])
&& empty($parameters['types'])) && empty($parameters['types']))
{ {
throw new \Exception(' throw new \Exception('A Radar Search request must include at least one of keyword, name, or types.');
A Radar Search request must include at least one
of keyword, name, or types.
');
} }
if (isset($parameters['rankby'])) if (isset($parameters['rankby']))
@ -225,10 +205,7 @@ class GooglePlaces
if (!(isset($parameters['reference']) if (!(isset($parameters['reference'])
^ isset($parameters['placeid']))) ^ isset($parameters['placeid'])))
{ {
throw new \Exception(' throw new \Exception('You must specify either a placeid or a reference (but not both) before calling details().');
You must specify either a placeid or a reference
(but not both) before calling details().
');
} }
if (isset($parameters['rankby'])) if (isset($parameters['rankby']))
@ -268,8 +245,6 @@ class GooglePlaces
$querystring .= $variable . '=' . $value; $querystring .= $variable . '=' . $value;
} }
var_dump($this->client);
$response = $this->client->get($url . '?' . $querystring); $response = $this->client->get($url . '?' . $querystring);
if ($this->output == 'json') if ($this->output == 'json')
@ -316,11 +291,7 @@ class GooglePlaces
|| $this->subradius < 200 || $this->subradius < 200
|| ($this->radius / $this->subradius) % 2) || ($this->radius / $this->subradius) % 2)
{ {
throw new \Exception(' 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)');
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);

View file

@ -1,18 +1,22 @@
<?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/GooglePlacesInterface.php';
require_once '../src/GooglePlacesClient.php'; require_once '../src/GooglePlacesClient.php';
class GooglePlacesTest extends PHPUnit_Framework_TestCase class GooglePlacesTest extends PHPUnit_Framework_TestCase
{ {
private $places;
public function setUp()
{
$this->places = new joshtronic\GooglePlaces('');
}
public function testSetVariable() public function testSetVariable()
{ {
$places = new joshtronic\GooglePlaces(''); $this->places->foo = 'bar';
$places->foo = 'bar'; $this->assertEquals('bar', $this->places->foo);
$this->assertEquals('bar', $places->foo);
} }
public function testNearbySearchProximity() public function testNearbySearchProximity()
@ -21,12 +25,53 @@ class GooglePlacesTest extends PHPUnit_Framework_TestCase
$client->expects($this->exactly(1)) $client->expects($this->exactly(1))
->method('get') ->method('get')
->will($this->returnValue('some return i expect')); ->will($this->returnValue('
{
"html_attributions" : [],
"next_page_token" : "...",
"results" : [
{ },
{ },
{ },
{ },
{ }
],
"status" : "OK"
}
'));
$places = new joshtronic\GooglePlaces('', $client); $this->places->client = $client;
$places->location = array(-33.86820, 151.1945860); $this->places->location = array(-33.86820, 151.1945860);
$places->radius = 800; $this->places->radius = 800;
$results = $places->nearbySearch(); $results = $this->places->nearbySearch();
$this->assertTrue(is_array($results['results']));
$this->assertEquals("OK", $results['status']);
}
/**
* @expectedException Exception
* @expectedExceptionMessage You must specify a location before calling nearbysearch().
*/
public function testNearbySearchWithoutLocation()
{
$results = $this->places->nearbySearch();
}
public function testMeters2Lng()
{
$this->assertEquals(
0.0010818843545785356,
$this->places->meters2lng(100, -33.86820)
);
}
public function testMeters2Lat()
{
$this->assertEquals(
0.0008983120716174308,
$this->places->meters2lat(100, 151.1945860)
);
} }
public function testNearbySearchDistance() public function testNearbySearchDistance()