|
|
@@ -2,6 +2,7 @@
|
|
|
App::uses('GeocoderBehavior', 'Tools.Model/Behavior');
|
|
|
App::uses('Set', 'Utility');
|
|
|
App::uses('AppModel', 'Model');
|
|
|
+App::uses('AppController', 'Controller');
|
|
|
|
|
|
class GeocoderBehaviorTest extends CakeTestCase {
|
|
|
|
|
|
@@ -9,14 +10,85 @@ class GeocoderBehaviorTest extends CakeTestCase {
|
|
|
'core.comment', 'plugin.tools.address'
|
|
|
);
|
|
|
|
|
|
-
|
|
|
public function startTest() {
|
|
|
$this->Comment = ClassRegistry::init('Comment');
|
|
|
|
|
|
-
|
|
|
$this->Comment->Behaviors->attach('Tools.Geocoder', array('real'=>false));
|
|
|
}
|
|
|
|
|
|
+ public function testDistance() {
|
|
|
+ $res = $this->Comment->distance(12, 14);
|
|
|
+ $expected = '6371.04 * ACOS( COS( PI()/2 - RADIANS(90 - Comment.lat)) * COS( PI()/2 - RADIANS(90 - 12)) * COS( RADIANS(Comment.lat) - RADIANS(14)) + SIN( PI()/2 - RADIANS(90 - Comment.lng)) * SIN( PI()/2 - RADIANS(90 - 12)))';
|
|
|
+ $this->assertEquals($expected, $res);
|
|
|
+
|
|
|
+ $this->Comment->Behaviors->detach('Geocoder');
|
|
|
+ $this->Comment->Behaviors->attach('Tools.Geocoder', array('lat'=>'x', 'lng'=>'y'));
|
|
|
+ $res = $this->Comment->distance(12, 14);
|
|
|
+ $expected = '6371.04 * ACOS( COS( PI()/2 - RADIANS(90 - Comment.x)) * COS( PI()/2 - RADIANS(90 - 12)) * COS( RADIANS(Comment.x) - RADIANS(14)) + SIN( PI()/2 - RADIANS(90 - Comment.y)) * SIN( PI()/2 - RADIANS(90 - 12)))';
|
|
|
+ $this->assertEquals($expected, $res);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testDistanceField() {
|
|
|
+ $res = $this->Comment->distanceField(12, 14);
|
|
|
+ $expected = '6371.04 * ACOS( COS( PI()/2 - RADIANS(90 - Comment.lat)) * COS( PI()/2 - RADIANS(90 - 12)) * COS( RADIANS(Comment.lat) - RADIANS(14)) + SIN( PI()/2 - RADIANS(90 - Comment.lng)) * SIN( PI()/2 - RADIANS(90 - 12))) AS Comment.distance';
|
|
|
+ $this->assertEquals($expected, $res);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testSetDistanceAsVirtualField() {
|
|
|
+ $this->Address = ClassRegistry::init('Address');
|
|
|
+ $this->Address->Behaviors->attach('Tools.Geocoder');
|
|
|
+ $this->Address->setDistanceAsVirtualField(13.3, 19.2);
|
|
|
+ $options = array('order' => array('Address.distance' => 'ASC'));
|
|
|
+ $res = $this->Address->find('all', $options);
|
|
|
+ $this->assertTrue($res[0]['Address']['distance'] < $res[1]['Address']['distance']);
|
|
|
+ $this->assertTrue($res[1]['Address']['distance'] < $res[2]['Address']['distance']);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testPagination() {
|
|
|
+ $this->Controller = new TestController(new CakeRequest(null, false), null);
|
|
|
+ $this->Controller->constructClasses();
|
|
|
+ $this->Controller->Address->Behaviors->attach('Tools.Geocoder');
|
|
|
+ $this->Controller->Address->setDistanceAsVirtualField(13.3, 19.2);
|
|
|
+ $this->Controller->paginate = array(
|
|
|
+ 'conditions'=>array('distance <' => 3000),
|
|
|
+ 'order' => array('distance' => 'ASC')
|
|
|
+ );
|
|
|
+ $res = $this->Controller->paginate();
|
|
|
+ $this->assertEquals(2, count($res));
|
|
|
+ $this->assertTrue($res[0]['Address']['distance'] < $res[1]['Address']['distance']);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testValidate() {
|
|
|
+ $is = $this->Comment->validateLatitude(44);
|
|
|
+ $this->assertTrue($is);
|
|
|
+
|
|
|
+ $is = $this->Comment->validateLatitude(110);
|
|
|
+ $this->assertFalse($is);
|
|
|
+
|
|
|
+ $is = $this->Comment->validateLongitude(150);
|
|
|
+ $this->assertTrue($is);
|
|
|
+
|
|
|
+ $is = $this->Comment->validateLongitude(-190);
|
|
|
+ $this->assertFalse($is);
|
|
|
+
|
|
|
+ $this->Comment->validator()->add('lat', 'validateLatitude', array('rule'=>'validateLatitude', 'message'=>'validateLatitudeError'));
|
|
|
+ $this->Comment->validator()->add('lng', 'validateLongitude', array('rule'=>'validateLongitude', 'message'=>'validateLongitudeError'));
|
|
|
+ $data = array(
|
|
|
+ 'lat' => 44,
|
|
|
+ 'lng' => 190,
|
|
|
+ );
|
|
|
+ $this->Comment->set($data);
|
|
|
+ $res = $this->Comment->validates();
|
|
|
+ $this->assertFalse($res);
|
|
|
+ $expectedErrors = array(
|
|
|
+ 'lng' => array(__('validateLongitudeError'))
|
|
|
+ );
|
|
|
+ $this->assertEquals($expectedErrors, $this->Comment->validationErrors);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * geocoding tests using the google webservice
|
|
|
+ */
|
|
|
public function testBasic() {
|
|
|
echo '<h3>'.__FUNCTION__.'</h3>';
|
|
|
|
|
|
@@ -171,66 +243,13 @@ class GeocoderBehaviorTest extends CakeTestCase {
|
|
|
$this->assertTrue(!empty($res['Comment']['lat']) && !empty($res['Comment']['lng']));
|
|
|
}
|
|
|
|
|
|
+}
|
|
|
|
|
|
- public function testDistance() {
|
|
|
- $res = $this->Comment->distance(12, 14);
|
|
|
- $expected = '6371.04 * ACOS( COS( PI()/2 - RADIANS(90 - Comment.lat)) * COS( PI()/2 - RADIANS(90 - 12)) * COS( RADIANS(Comment.lat) - RADIANS(14)) + SIN( PI()/2 - RADIANS(90 - Comment.lng)) * SIN( PI()/2 - RADIANS(90 - 12)))';
|
|
|
- $this->assertEquals($expected, $res);
|
|
|
-
|
|
|
- $this->Comment->Behaviors->detach('Geocoder');
|
|
|
- $this->Comment->Behaviors->attach('Tools.Geocoder', array('lat'=>'x', 'lng'=>'y'));
|
|
|
- $res = $this->Comment->distance(12, 14);
|
|
|
- $expected = '6371.04 * ACOS( COS( PI()/2 - RADIANS(90 - Comment.x)) * COS( PI()/2 - RADIANS(90 - 12)) * COS( RADIANS(Comment.x) - RADIANS(14)) + SIN( PI()/2 - RADIANS(90 - Comment.y)) * SIN( PI()/2 - RADIANS(90 - 12)))';
|
|
|
- $this->assertEquals($expected, $res);
|
|
|
- }
|
|
|
-
|
|
|
- public function testDistanceField() {
|
|
|
- $res = $this->Comment->distanceField(12, 14);
|
|
|
- $expected = '6371.04 * ACOS( COS( PI()/2 - RADIANS(90 - Comment.lat)) * COS( PI()/2 - RADIANS(90 - 12)) * COS( RADIANS(Comment.lat) - RADIANS(14)) + SIN( PI()/2 - RADIANS(90 - Comment.lng)) * SIN( PI()/2 - RADIANS(90 - 12))) AS Comment.distance';
|
|
|
- $this->assertEquals($expected, $res);
|
|
|
- }
|
|
|
-
|
|
|
- public function testSetDistanceAsVirtualField() {
|
|
|
- $this->Address = ClassRegistry::init('Address');
|
|
|
- $this->Address->Behaviors->attach('Tools.Geocoder');
|
|
|
- $this->Address->setDistanceAsVirtualField(13.3, 19.2);
|
|
|
- $options = array('order' => array('Address.distance' => 'ASC'));
|
|
|
- $res = $this->Address->find('all', $options);
|
|
|
- $this->assertTrue($res[0]['Address']['distance'] < $res[1]['Address']['distance']);
|
|
|
- $this->assertTrue($res[1]['Address']['distance'] < $res[2]['Address']['distance']);
|
|
|
- }
|
|
|
-
|
|
|
- public function testValidate() {
|
|
|
- $is = $this->Comment->validateLatitude(44);
|
|
|
- $this->assertTrue($is);
|
|
|
-
|
|
|
- $is = $this->Comment->validateLatitude(110);
|
|
|
- $this->assertFalse($is);
|
|
|
-
|
|
|
- $is = $this->Comment->validateLongitude(150);
|
|
|
- $this->assertTrue($is);
|
|
|
-
|
|
|
- $is = $this->Comment->validateLongitude(-190);
|
|
|
- $this->assertFalse($is);
|
|
|
-
|
|
|
- $this->Comment->validator()->add('lat', 'validateLatitude', array('rule'=>'validateLatitude', 'message'=>'validateLatitudeError'));
|
|
|
- $this->Comment->validator()->add('lng', 'validateLongitude', array('rule'=>'validateLongitude', 'message'=>'validateLongitudeError'));
|
|
|
- $data = array(
|
|
|
- 'lat' => 44,
|
|
|
- 'lng' => 190,
|
|
|
- );
|
|
|
- $this->Comment->set($data);
|
|
|
- $res = $this->Comment->validates();
|
|
|
- $this->assertFalse($res);
|
|
|
- $expectedErrors = array(
|
|
|
- 'lng' => array(__('validateLongitudeError'))
|
|
|
- );
|
|
|
- $this->assertEquals($expectedErrors, $this->Comment->validationErrors);
|
|
|
- }
|
|
|
|
|
|
+class TestController extends AppController {
|
|
|
|
|
|
+ public $uses = array('Address');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|