Finished up unit tests

Got pretty intense trying to test the subradius logic. Refactored a bit along
the way.
This commit is contained in:
Josh Sherman 2014-09-20 01:46:33 -04:00
parent 926a19c5b3
commit dbd661091b
4 changed files with 338 additions and 81 deletions

View file

@ -1,7 +1,6 @@
<?php
require_once '../src/GooglePlaces.php';
require_once '../src/GooglePlacesInterface.php';
require_once '../src/GooglePlacesClient.php';
class GooglePlacesTest extends PHPUnit_Framework_TestCase
@ -10,7 +9,27 @@ class GooglePlacesTest extends PHPUnit_Framework_TestCase
public function setUp()
{
$this->places = new joshtronic\GooglePlaces('');
$this->places = new joshtronic\GooglePlaces('');
$this->places->sleep = 0;
}
private function clientSetUp($next = false)
{
$client = $this->getMock('GooglePlacesClient', array('get'));
$client
->expects($this->once())
->method('get')
->will($this->returnValue('
{
"html_attributions" : [],
' . ($next ? '"next_page_token" : "...",' : '') . '
"results" : [],
"status" : "OK"
}
'));
$this->places->client = $client;
}
public function testSetVariable()
@ -21,32 +40,13 @@ class GooglePlacesTest extends PHPUnit_Framework_TestCase
public function testNearbySearchProximity()
{
$client = $this->getMock('GooglePlacesInterface', array('get'));
$client->expects($this->exactly(1))
->method('get')
->will($this->returnValue('
{
"html_attributions" : [],
"next_page_token" : "...",
"results" : [
{ },
{ },
{ },
{ },
{ }
],
"status" : "OK"
}
'));
$this->places->client = $client;
$this->clientSetUp(true);
$this->places->location = array(-33.86820, 151.1945860);
$this->places->radius = 800;
$results = $this->places->nearbySearch();
$this->assertTrue(is_array($results['results']));
$this->assertEquals("OK", $results['status']);
$this->assertEquals('OK', $results['status']);
}
/**
@ -55,7 +55,230 @@ class GooglePlacesTest extends PHPUnit_Framework_TestCase
*/
public function testNearbySearchWithoutLocation()
{
$results = $this->places->nearbySearch();
$this->places->nearbySearch();
}
/**
* @expectedException Exception
* @expectedExceptionMessage You must specify a radius.
*/
public function testNearbySearchWithoutRadius()
{
$this->places->location = array(-33.86820, 151.1945860);
$this->places->nearbySearch();
}
/**
* @expectedException Exception
* @expectedExceptionMessage Invalid output, please specify either "json" or "xml".
*/
public function testNearbySearchInvalidOutput()
{
$this->places->location = array(-33.86820, 151.1945860);
$this->places->radius = 800;
$this->places->output = 'foo';
$this->places->nearbySearch();
}
/**
* @expectedException Exception
* @expectedExceptionMessage Invalid rank by value, please specify either "prominence" or "distance".
*/
public function testNearbySearchInvalidRankBy()
{
$this->places->location = array(-33.86820, 151.1945860);
$this->places->radius = 800;
$this->places->rankby = 'foo';
$this->places->nearbySearch();
}
/**
* @expectedException Exception
* @expectedExceptionMessage You much specify at least one of the following: "keyword", "name", "types".
*/
public function testNearbySearchMissingParameters()
{
$this->places->location = array(-33.86820, 151.1945860);
$this->places->rankby = 'distance';
$this->places->nearbySearch();
}
public function testNearbySearchUnsetRadius()
{
$this->places->location = array(-33.86820, 151.1945860);
$this->places->rankby = 'distance';
$this->places->keyword = 'cafe';
$this->places->radius = 800;
$results = $this->places->nearbySearch();
$this->assertFalse(isset($this->places->radius));
}
public function testNearbySearchDistance()
{
$client = $this->getMock('GooglePlacesClient', array('get'));
$client
->expects($this->once())
->method('get')
->will($this->returnValue('
{
"html_attributions" : [],
"next_page_token" : "...",
"results" : [],
"status" : "OK"
}
'));
$client
->expects($this->once())
->method('get')
->will($this->returnValue('
{
"html_attributions" : [],
"results" : [],
"status" : "OK"
}
'));
$this->places->client = $client;
$this->places->location = array(-33.86820, 151.1945860);
$this->places->rankby = 'distance';
$this->places->types = array('restaurant', 'business');
$results = $this->places->nearbySearch();
$this->assertTrue(is_array($results['results']));
$this->assertEquals('OK', $results['status']);
}
public function testSetPageToken()
{
$client = $this->getMock('GooglePlacesClient', array('get'));
$client
->expects($this->once())
->method('get')
->will($this->returnValue('
{
"html_attributions" : [],
"next_page_token" : "...",
"results" : [],
"status" : "OK"
}
'));
$client
->expects($this->once())
->method('get')
->will($this->returnValue('
{
"html_attributions" : [],
"results" : [],
"status" : "OK"
}
'));
$this->places->client = $client;
$this->places->location = array(-33.86820, 151.1945860);
$this->places->radius = 100;
$this->places->pagetoken = '...';
$results = $this->places->nearbySearch();
$this->assertTrue(is_array($results['results']));
$this->assertEquals('OK', $results['status']);
}
public function testRadarSearch()
{
$this->clientSetUp();
$this->places->location = array(-33.86820, 151.1945860);
$this->places->radius = 100;
$this->places->keyword = 'restaurant';
$results = $this->places->radarSearch();
$this->assertTrue(is_array($results['results']));
$this->assertEquals('OK', $results['status']);
}
/**
* @expectedException Exception
* @expectedExceptionMessage You must specify a location before calling radarsearch().
*/
public function testRadarSearchWithoutLocation()
{
$this->places->radarSearch();
}
/**
* @expectedException Exception
* @expectedExceptionMessage You must specify a radius.
*/
public function testRadarSearchWithoutRadius()
{
$this->places->location = array(-33.86820, 151.1945860);
$this->places->radarSearch();
}
/**
* @expectedException Exception
* @expectedExceptionMessage You much specify at least one of the following: "keyword", "name", "types".
*/
public function testRadarSearchMissingParameters()
{
$this->places->location = array(-33.86820, 151.1945860);
$this->places->radius = 100;
$this->places->radarSearch();
}
public function testDetails()
{
$this->clientSetUp();
$this->places->placeid = '123';
$this->places->rankby = 'distance';
$results = $this->places->details();
$this->assertTrue(is_array($results['results']));
$this->assertEquals('OK', $results['status']);
}
/**
* @expectedException Exception
* @expectedExceptionMessage You must specify either a "placeid" or a "reference" (but not both) before calling details().
*/
public function testDetailsMissingParameters()
{
$this->places->details();
}
/**
* @expectedException Exception
* @expectedExceptionMessage The returned JSON was malformed or nonexistent.
*/
public function testInvalidJSON()
{
$client = $this->getMock('GooglePlacesClient', array('get'));
$client
->expects($this->once())
->method('get')
->will($this->returnValue('[{ foo: "ba"r,, }];'));
$this->places->client = $client;
$this->places->location = array(-33.86820, 151.1945860);
$this->places->radius = 100;
$this->places->nearbysearch();
}
/**
* @expectedException Exception
* @expectedExceptionMessage XML is terrible, don't use it, ever.
*/
public function testOutputXML()
{
$this->places->location = array(-33.86820, 151.1945860);
$this->places->radius = 800;
$this->places->output = 'xml';
$this->places->nearbySearch();
}
public function testMeters2Lng()
@ -74,24 +297,67 @@ class GooglePlacesTest extends PHPUnit_Framework_TestCase
);
}
public function testNearbySearchDistance()
public function testNearbySearchProximitySubradius()
{
$client = $this->getMock('GooglePlacesClient', array('get'));
for ($i = 0; $i < 18; $i++)
{
$client
->expects($this->at($i))
->method('get')
->will($this->returnValue('
{
"html_attributions" : [],
' . ($i % 2 ? '' : '"next_page_token" : "...",') . '
"results" : [{}, {}],
"status" : "OK"
}
'));
}
$this->places->client = $client;
$this->places->location = array(-33.86820, 151.1945860);
$this->places->radius = 400;
$this->places->subradius = 200;
$results = $this->places->nearbySearch();
$this->assertTrue(is_array($results['results']));
$this->assertEquals(36, count($results['results']));
}
public function testNearbySearchPagination()
/**
* @expectedException Exception
* @expectedExceptionMessage Subradius should be at least 200 meters
*/
public function testNearbySearchProximitySubradiusBelow200()
{
$this->places->location = array(-33.86820, 151.1945860);
$this->places->radius = 2000;
$this->places->subradius = 100;
$results = $this->places->nearbySearch();
}
public function testRadarSearch()
/**
* @expectedException Exception
* @expectedExceptionMessage Subradius should divide evenly into radius.
*/
public function testNearbySearchProximitySubradiusDivisionError()
{
$this->places->location = array(-33.86820, 151.1945860);
$this->places->radius = 2000;
$this->places->subradius = 233;
$results = $this->places->nearbySearch();
}
public function testDetails()
/**
* @expectedException Exception
* @expectedExceptionMessage CURL Error: Could not resolve host: bar
*/
public function testClientError()
{
$client = new joshtronic\GooglePlacesClient();
$client->get('http://foo@bar:google.com');
}
}