SHARE
TWEET

Giorgio Sironi

a guest Sep 30th, 2009 313 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /**
  4.  * For brevity I use public fields instead of getters and setters.
  5.  */
  6. class User
  7. {
  8.     public $location;
  9.     public $latitude;
  10.     public $longitude;
  11. }
  12.  
  13. class GoogleMaps
  14. {
  15.     public function getLatitudeAndLongitude($location)
  16.     {
  17.         // some obscure network code to contact maps.google.com
  18.         // ...
  19.         return array('latitude' => $someValue, 'longitude' => $someOtherValue);
  20.     }
  21. }
  22.  
  23. class GeolocationService
  24. {
  25.     private $_maps;
  26.  
  27.     public function __construct(GoogleMaps $maps)
  28.     {
  29.         $this->_maps = $maps;
  30.     }
  31.  
  32.     public function locate(User $user)
  33.     {
  34.         $location = $user->location;
  35.         if ($location === null) {
  36.             $location = 'Milan';
  37.         }
  38.         $coordinates = $this->_maps->getLatitudeAndLongitude($location);
  39.         $user->latitude = $coordinates['latitude'];
  40.         $user->longitude = $coordinates['longitude'];
  41.     }
  42. }
  43.  
  44. class GeolocationServiceTest extends PHPUnit_Framework_TestCase
  45. {
  46.     public function testProvidesLatitudeAndLongitudeForAnUser()
  47.     {
  48.         $coordinates = array('latitude' => '42N', 'longitude' => '12E');
  49.         $googleMapsMock = $this->getMock('GoogleMaps', array('getLatitudeAndLongitude'));
  50.         $googleMapsMock->expects($this->any())
  51.                        ->method('getLatitudeAndLongitude')
  52.                        ->will($this->returnValue($coordinates));
  53.         $service = new GeolocationService($googleMapsMock);
  54.         $user = new User;
  55.         $user->location = 'Rome';
  56.         $service->locate($user);
  57.         $this->assertEquals('42N', $user->latitude);
  58.         $this->assertEquals('12E', $user->longitude);
  59.     }
  60. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top