From 96539ea0f360bb7e7e2768f5d85e2b7ec61d95a1 Mon Sep 17 00:00:00 2001 From: davidepastore Date: Sat, 9 Jul 2016 11:56:56 +0200 Subject: [PATCH 1/2] Improve code coverage --- src/DavidePastore/Ipinfo/Host.php | 23 ++--- test/DavidePastore/Ipinfo/IpinfoTest.php | 115 +++++++++++++++++++++-- 2 files changed, 117 insertions(+), 21 deletions(-) diff --git a/src/DavidePastore/Ipinfo/Host.php b/src/DavidePastore/Ipinfo/Host.php index e742a68..9592b6c 100644 --- a/src/DavidePastore/Ipinfo/Host.php +++ b/src/DavidePastore/Ipinfo/Host.php @@ -9,22 +9,19 @@ */ class Host { - + /** * Contains all the properties of the host. * @var array */ protected $properties; - + /** * Create an Host object with all the properties. - * @param unknown $properties + * @param array $properties */ public function __construct($properties = array()) { - if (!$properties) { - $properties = array(); - } //Merge default values $this->properties = array_merge(array( Ipinfo::CITY => "", @@ -38,7 +35,7 @@ public function __construct($properties = array()) Ipinfo::REGION => "" ), $properties); } - + /** * Get the city value. */ @@ -46,7 +43,7 @@ public function getCity() { return $this->properties[Ipinfo::CITY]; } - + /** * Get the country value. */ @@ -54,7 +51,7 @@ public function getCountry() { return $this->properties[Ipinfo::COUNTRY]; } - + /** * Get the hostname value. */ @@ -70,7 +67,7 @@ public function getIp() { return $this->properties[Ipinfo::IP]; } - + /** * Get the loc value. */ @@ -86,7 +83,7 @@ public function getOrg() { return $this->properties[Ipinfo::ORG]; } - + /** * Get the phone value. */ @@ -94,7 +91,7 @@ public function getPhone() { return $this->properties[Ipinfo::PHONE]; } - + /** * Get the postal value. */ @@ -110,7 +107,7 @@ public function getRegion() { return $this->properties[Ipinfo::REGION]; } - + /** * Get all the properties. * @return array An associative array with all the properties. diff --git a/test/DavidePastore/Ipinfo/IpinfoTest.php b/test/DavidePastore/Ipinfo/IpinfoTest.php index 54a0a40..1a38eb2 100644 --- a/test/DavidePastore/Ipinfo/IpinfoTest.php +++ b/test/DavidePastore/Ipinfo/IpinfoTest.php @@ -9,7 +9,7 @@ */ class IpinfoTest extends \PHPUnit_Framework_TestCase { - + /** * Test full ip details. */ @@ -28,10 +28,50 @@ public function testGetFullIpDetails() "region" => "California" )); $actual = $ipinfo->getFullIpDetails("8.8.8.8"); - + $this->assertEquals($expected, $actual); } - + + /** + * Test all the get method of Host. + */ + public function testAllGet() + { + $ipinfo = new Ipinfo(); + $expectedCity = "Mountain View"; + $expectedCountry = "US"; + $expectedHostname = "google-public-dns-a.google.com"; + $expectedIp = "8.8.8.8"; + $expectedLoc = "37.3860,-122.0838"; + $expectedOrg = "AS15169 Google Inc."; + $expectedPhone = ""; + $expectedPostal = "94035"; + $expectedRegion = "California"; + $expected = array( + "city" => $expectedCity, + "country" => $expectedCountry, + "hostname" => $expectedHostname, + "ip" => $expectedIp, + "loc" => $expectedLoc, + "org" => $expectedOrg, + "phone" => $expectedPhone, + "postal" => $expectedPostal, + "region" => $expectedRegion + ); + $actual = $ipinfo->getFullIpDetails("8.8.8.8"); + + $this->assertEquals($expectedCity, $actual->getCity()); + $this->assertEquals($expectedCountry, $actual->getCountry()); + $this->assertEquals($expectedHostname, $actual->getHostname()); + $this->assertEquals($expectedIp, $actual->getIp()); + $this->assertEquals($expectedLoc, $actual->getLoc()); + $this->assertEquals($expectedOrg, $actual->getOrg()); + $this->assertEquals($expectedPhone, $actual->getPhone()); + $this->assertEquals($expectedPostal, $actual->getPostal()); + $this->assertEquals($expectedRegion, $actual->getRegion()); + $this->assertEquals($expected, $actual->getProperties()); + } + /** * Test city field value. */ @@ -40,12 +80,12 @@ public function testGetSpecificField() $ipinfo = new Ipinfo(); $expected = "Mountain View"; $actual = $ipinfo->getSpecificField("8.8.8.8", Ipinfo::CITY); - + $this->assertEquals($expected, $actual); } - + /** - * Test the faster geo call + * Test the faster geo call. */ public function testGeoDetails() { @@ -57,14 +97,73 @@ public function testGeoDetails() "loc" => "37.3860,-122.0838", "postal" => "94035", "region" => "California", - + // Other fields will be empty by default "hostname" => "", "org" => "", "phone" => "", )); $actual = $ipinfo->getIpGeoDetails("8.8.8.8"); - + + $this->assertEquals($expected, $actual); + } + + /** + * Test your own ip details. + */ + public function testYourOwnIpDetails() + { + $ipinfo = new Ipinfo(); + $host = $ipinfo->getYourOwnIpDetails(); + $actual = $host->getProperties(); + $this->assertArrayHasKey('city', $actual); + $this->assertArrayHasKey('country', $actual); + $this->assertArrayHasKey('hostname', $actual); + $this->assertArrayHasKey('ip', $actual); + $this->assertArrayHasKey('loc', $actual); + $this->assertArrayHasKey('org', $actual); + $this->assertArrayHasKey('phone', $actual); + $this->assertArrayHasKey('postal', $actual); + $this->assertArrayHasKey('region', $actual); + } + + /** + * Test your own specific field value. + */ + public function testGetYourOwnIpSpecificField() + { + $ipinfo = new Ipinfo(); + $expected = "Mountain View"; + $actual = $ipinfo->getYourOwnIpSpecificField(Ipinfo::CITY); + + $this->assertTrue(is_string($actual)); + } + + /** + * Test using a token. + */ + public function testWithToken() + { + $ipinfo = new Ipinfo(array( + "token" => 'justatest' + )); + $expected = "Mountain View"; + $actual = $ipinfo->getSpecificField("8.8.8.8", Ipinfo::CITY); + + $this->assertEquals($expected, $actual); + } + + /** + * Test in debug mode. + */ + public function testDebugMode() + { + $ipinfo = new Ipinfo(array( + "debug" => true + )); + $expected = "Mountain View"; + $actual = $ipinfo->getSpecificField("8.8.8.8", Ipinfo::CITY); + $this->assertEquals($expected, $actual); } } From 99dfdb1dbdb1776d5ded56fd85eac2c34e9bbe18 Mon Sep 17 00:00:00 2001 From: davidepastore Date: Sat, 9 Jul 2016 12:26:12 +0200 Subject: [PATCH 2/2] Fix code style (PSR-2) --- src/DavidePastore/Ipinfo/Host.php | 24 ++-- src/DavidePastore/Ipinfo/Ipinfo.php | 147 ++++++++++++++--------- test/DavidePastore/Ipinfo/IpinfoTest.php | 128 ++++++++++---------- 3 files changed, 164 insertions(+), 135 deletions(-) diff --git a/src/DavidePastore/Ipinfo/Host.php b/src/DavidePastore/Ipinfo/Host.php index 9592b6c..4362479 100644 --- a/src/DavidePastore/Ipinfo/Host.php +++ b/src/DavidePastore/Ipinfo/Host.php @@ -4,35 +4,36 @@ /** * Represent an host with all the details. - * @author davidepastore * + * @author davidepastore */ class Host { - /** * Contains all the properties of the host. + * * @var array */ protected $properties; /** * Create an Host object with all the properties. + * * @param array $properties */ public function __construct($properties = array()) { //Merge default values $this->properties = array_merge(array( - Ipinfo::CITY => "", - Ipinfo::COUNTRY => "", - Ipinfo::HOSTNAME => "", - Ipinfo::IP => "", - Ipinfo::LOC => "", - Ipinfo::ORG => "", - Ipinfo::PHONE => "", - Ipinfo::POSTAL => "", - Ipinfo::REGION => "" + Ipinfo::CITY => '', + Ipinfo::COUNTRY => '', + Ipinfo::HOSTNAME => '', + Ipinfo::IP => '', + Ipinfo::LOC => '', + Ipinfo::ORG => '', + Ipinfo::PHONE => '', + Ipinfo::POSTAL => '', + Ipinfo::REGION => '', ), $properties); } @@ -110,6 +111,7 @@ public function getRegion() /** * Get all the properties. + * * @return array An associative array with all the properties. */ public function getProperties() diff --git a/src/DavidePastore/Ipinfo/Ipinfo.php b/src/DavidePastore/Ipinfo/Ipinfo.php index d0bddc6..ca67d06 100644 --- a/src/DavidePastore/Ipinfo/Ipinfo.php +++ b/src/DavidePastore/Ipinfo/Ipinfo.php @@ -4,172 +4,197 @@ /** * ipinfo.io service wrapper. - * @author davidepastore * + * @author davidepastore */ class Ipinfo { - /** * The base url of the ipinfo service. + * * @var string */ - const BASE_URL = "http://ipinfo.io/"; - + const BASE_URL = 'http://ipinfo.io/'; + /** * The ip string. + * * @var string */ - const IP = "ip"; - + const IP = 'ip'; + /** * The hostname string. + * * @var string */ - const HOSTNAME = "hostname"; - + const HOSTNAME = 'hostname'; + /** * The loc string. + * * @var string */ - const LOC = "loc"; - + const LOC = 'loc'; + /** * The org string. + * * @var string */ - const ORG = "org"; - + const ORG = 'org'; + /** * The city string. + * * @var string */ - const CITY = "city"; - + const CITY = 'city'; + /** * The region string. + * * @var string */ - const REGION = "region"; - + const REGION = 'region'; + /** * The country string. + * * @var string */ - const COUNTRY = "country"; - + const COUNTRY = 'country'; + /** * The phone string. + * * @var string */ - const PHONE = "phone"; - + const PHONE = 'phone'; + /** * The geo string. + * * @var string */ - const GEO = "geo"; - + const GEO = 'geo'; + /** * The postal string. + * * @var string */ - const POSTAL = "postal"; - + const POSTAL = 'postal'; + /** - * All the settings + * All the settings. + * * @var array */ protected $settings; - + /** * Create an Ipinfo instance. + * * @param array $settings An array with all the settings. - * Supported keys are: - * - token: string the developer token; - * - debug: boolean active or not the debug. + * Supported keys are: + * - token: string the developer token; + * - debug: boolean active or not the debug. */ public function __construct($settings = array()) { //Merge user settings $this->settings = array_merge(array( 'token' => '', - 'debug' => false + 'debug' => false, ), $settings); } - - + /** * Get all the info about your own ip address. + * * @return \DavidePastore\Ipinfo\Host The Host object with all the info. */ public function getYourOwnIpDetails() { - $response = $this->makeCurlRequest($this::BASE_URL . "json"); + $response = $this->makeCurlRequest($this::BASE_URL.'json'); $response = json_decode($response, true); + return new Host($response); } - + /** * Get all the info about an ip address. + * * @param string $ipAddress The ip address. + * * @return \DavidePastore\Ipinfo\Host The Host object with all the info. */ public function getFullIpDetails($ipAddress) { - $response = $this->makeCurlRequest($this::BASE_URL . $ipAddress); + $response = $this->makeCurlRequest($this::BASE_URL.$ipAddress); $response = json_decode($response, true); + return new Host($response); } - + /** * Get a specific field value. + * * @param string $ipAddress The ip address. - * @param string $field The field. + * @param string $field The field. + * * @return string|\DavidePastore\Ipinfo\Host The value of the given field for the given ip. - * This could returns an Host object if you call it with for the field - * \DavidePastore\Ipinfo\Ipinfo::GEO. + * This could returns an Host object if you call it with for the field + * \DavidePastore\Ipinfo\Ipinfo::GEO. */ public function getSpecificField($ipAddress, $field) { - $response = $this->makeCurlRequest($this::BASE_URL . $ipAddress . "/" . $field); + $response = $this->makeCurlRequest($this::BASE_URL.$ipAddress.'/'.$field); $response = $this->checkGeo($field, $response); + return $response; } - + /** * Get a specific field value of your own ip address. + * * @param string $field The field. + * * @return string|\DavidePastore\Ipinfo\Host The value of the given field for your own ip. - * This could returns an Host object if you call it with for the field - * \DavidePastore\Ipinfo\Ipinfo::GEO. + * This could returns an Host object if you call it with for the field + * \DavidePastore\Ipinfo\Ipinfo::GEO. */ public function getYourOwnIpSpecificField($field) { - $response = $this->makeCurlRequest($this::BASE_URL . $field); + $response = $this->makeCurlRequest($this::BASE_URL.$field); $response = $this->checkGeo($field, $response); + return $response; } - + /** * Use the /geo call to get just the geolocation information, which will often be * faster than getting the full response. * * @param string $ipAddress The ip address. + * * @return \DavidePastore\Ipinfo\Host */ public function getIpGeoDetails($ipAddress) { return $this->getSpecificField($ipAddress, $this::GEO); } - + /** * Check if the response is GEO and set the parameters accordingly. - * @param string $field The field value. + * + * @param string $field The field value. * @param string $response The response from the server. + * * @return Ambigous <\DavidePastore\Ipinfo\Host, string> Returns an Host object if the request is - * of the GEO type, a string otherwise. If the field value is different from the GEO type, it will - * delete the last character ('\n'). + * of the GEO type, a string otherwise. If the field value is different from the GEO type, it will + * delete the last character ('\n'). */ private function checkGeo($field, $response) { @@ -179,36 +204,38 @@ private function checkGeo($field, $response) } else { $response = substr($response, 0, -1); } - + return $response; } - + /** * Make a curl request. + * * @param string $address The address of the request. + * * @return string Returns the response from the request. */ private function makeCurlRequest($address) { $curl = curl_init(); - + if (!empty($this->settings['token'])) { - $address .= "?token=" . $this->settings['token']; + $address .= '?token='.$this->settings['token']; } - + if ($this->settings['debug']) { - echo "Request address: " . $address . "\n"; + echo 'Request address: '.$address."\n"; } - + curl_setopt_array($curl, array( CURLOPT_RETURNTRANSFER => 1, - CURLOPT_URL => $address + CURLOPT_URL => $address, )); - + $response = curl_exec($curl); - + curl_close($curl); - + return $response; } } diff --git a/test/DavidePastore/Ipinfo/IpinfoTest.php b/test/DavidePastore/Ipinfo/IpinfoTest.php index 1a38eb2..c282837 100644 --- a/test/DavidePastore/Ipinfo/IpinfoTest.php +++ b/test/DavidePastore/Ipinfo/IpinfoTest.php @@ -1,15 +1,15 @@ "Mountain View", - "country" => "US", - "hostname" => "google-public-dns-a.google.com", - "ip" => "8.8.8.8", - "loc" => "37.3860,-122.0838", - "org" => "AS15169 Google Inc.", - "phone" => "", - "postal" => "94035", - "region" => "California" + 'city' => 'Mountain View', + 'country' => 'US', + 'hostname' => 'google-public-dns-a.google.com', + 'ip' => '8.8.8.8', + 'loc' => '37.3860,-122.0838', + 'org' => 'AS15169 Google Inc.', + 'phone' => '', + 'postal' => '94035', + 'region' => 'California', )); - $actual = $ipinfo->getFullIpDetails("8.8.8.8"); + $actual = $ipinfo->getFullIpDetails('8.8.8.8'); $this->assertEquals($expected, $actual); } @@ -37,39 +37,39 @@ public function testGetFullIpDetails() */ public function testAllGet() { - $ipinfo = new Ipinfo(); - $expectedCity = "Mountain View"; - $expectedCountry = "US"; - $expectedHostname = "google-public-dns-a.google.com"; - $expectedIp = "8.8.8.8"; - $expectedLoc = "37.3860,-122.0838"; - $expectedOrg = "AS15169 Google Inc."; - $expectedPhone = ""; - $expectedPostal = "94035"; - $expectedRegion = "California"; - $expected = array( - "city" => $expectedCity, - "country" => $expectedCountry, - "hostname" => $expectedHostname, - "ip" => $expectedIp, - "loc" => $expectedLoc, - "org" => $expectedOrg, - "phone" => $expectedPhone, - "postal" => $expectedPostal, - "region" => $expectedRegion - ); - $actual = $ipinfo->getFullIpDetails("8.8.8.8"); - - $this->assertEquals($expectedCity, $actual->getCity()); - $this->assertEquals($expectedCountry, $actual->getCountry()); - $this->assertEquals($expectedHostname, $actual->getHostname()); - $this->assertEquals($expectedIp, $actual->getIp()); - $this->assertEquals($expectedLoc, $actual->getLoc()); - $this->assertEquals($expectedOrg, $actual->getOrg()); - $this->assertEquals($expectedPhone, $actual->getPhone()); - $this->assertEquals($expectedPostal, $actual->getPostal()); - $this->assertEquals($expectedRegion, $actual->getRegion()); - $this->assertEquals($expected, $actual->getProperties()); + $ipinfo = new Ipinfo(); + $expectedCity = 'Mountain View'; + $expectedCountry = 'US'; + $expectedHostname = 'google-public-dns-a.google.com'; + $expectedIp = '8.8.8.8'; + $expectedLoc = '37.3860,-122.0838'; + $expectedOrg = 'AS15169 Google Inc.'; + $expectedPhone = ''; + $expectedPostal = '94035'; + $expectedRegion = 'California'; + $expected = array( + 'city' => $expectedCity, + 'country' => $expectedCountry, + 'hostname' => $expectedHostname, + 'ip' => $expectedIp, + 'loc' => $expectedLoc, + 'org' => $expectedOrg, + 'phone' => $expectedPhone, + 'postal' => $expectedPostal, + 'region' => $expectedRegion, + ); + $actual = $ipinfo->getFullIpDetails('8.8.8.8'); + + $this->assertEquals($expectedCity, $actual->getCity()); + $this->assertEquals($expectedCountry, $actual->getCountry()); + $this->assertEquals($expectedHostname, $actual->getHostname()); + $this->assertEquals($expectedIp, $actual->getIp()); + $this->assertEquals($expectedLoc, $actual->getLoc()); + $this->assertEquals($expectedOrg, $actual->getOrg()); + $this->assertEquals($expectedPhone, $actual->getPhone()); + $this->assertEquals($expectedPostal, $actual->getPostal()); + $this->assertEquals($expectedRegion, $actual->getRegion()); + $this->assertEquals($expected, $actual->getProperties()); } /** @@ -78,8 +78,8 @@ public function testAllGet() public function testGetSpecificField() { $ipinfo = new Ipinfo(); - $expected = "Mountain View"; - $actual = $ipinfo->getSpecificField("8.8.8.8", Ipinfo::CITY); + $expected = 'Mountain View'; + $actual = $ipinfo->getSpecificField('8.8.8.8', Ipinfo::CITY); $this->assertEquals($expected, $actual); } @@ -91,19 +91,19 @@ public function testGeoDetails() { $ipinfo = new Ipinfo(); $expected = new Host(array( - "city" => "Mountain View", - "country" => "US", - "ip" => "8.8.8.8", - "loc" => "37.3860,-122.0838", - "postal" => "94035", - "region" => "California", + 'city' => 'Mountain View', + 'country' => 'US', + 'ip' => '8.8.8.8', + 'loc' => '37.3860,-122.0838', + 'postal' => '94035', + 'region' => 'California', // Other fields will be empty by default - "hostname" => "", - "org" => "", - "phone" => "", + 'hostname' => '', + 'org' => '', + 'phone' => '', )); - $actual = $ipinfo->getIpGeoDetails("8.8.8.8"); + $actual = $ipinfo->getIpGeoDetails('8.8.8.8'); $this->assertEquals($expected, $actual); } @@ -133,7 +133,7 @@ public function testYourOwnIpDetails() public function testGetYourOwnIpSpecificField() { $ipinfo = new Ipinfo(); - $expected = "Mountain View"; + $expected = 'Mountain View'; $actual = $ipinfo->getYourOwnIpSpecificField(Ipinfo::CITY); $this->assertTrue(is_string($actual)); @@ -145,10 +145,10 @@ public function testGetYourOwnIpSpecificField() public function testWithToken() { $ipinfo = new Ipinfo(array( - "token" => 'justatest' + 'token' => 'justatest', )); - $expected = "Mountain View"; - $actual = $ipinfo->getSpecificField("8.8.8.8", Ipinfo::CITY); + $expected = 'Mountain View'; + $actual = $ipinfo->getSpecificField('8.8.8.8', Ipinfo::CITY); $this->assertEquals($expected, $actual); } @@ -159,10 +159,10 @@ public function testWithToken() public function testDebugMode() { $ipinfo = new Ipinfo(array( - "debug" => true + 'debug' => true, )); - $expected = "Mountain View"; - $actual = $ipinfo->getSpecificField("8.8.8.8", Ipinfo::CITY); + $expected = 'Mountain View'; + $actual = $ipinfo->getSpecificField('8.8.8.8', Ipinfo::CITY); $this->assertEquals($expected, $actual); }