GeoIP2-php/tests/GeoIp2/Test/Model/OmniTest.php

249 lines
6.9 KiB
PHP
Raw Normal View History

2013-05-09 14:28:01 +00:00
<?php
namespace GeoIp2\Test\Model;
2013-05-09 14:28:01 +00:00
use GeoIp2\Model\Omni;
2013-05-09 14:28:01 +00:00
class OmniTest extends \PHPUnit_Framework_TestCase
{
public function testFull()
{
$raw = array(
'city' => array(
'confidence' => 76,
'geoname_id' => 9876,
'names' => array( 'en' => 'Minneapolis' ),
),
'continent' => array(
2013-05-21 20:28:37 +00:00
'code' => 'NA',
'geoname_id' => 42,
'names' => array( 'en' => 'North America' ),
2013-05-09 14:28:01 +00:00
),
'country' => array(
'confidence' => 99,
'geoname_id' => 1,
'iso_code' => 'US',
'names' => array( 'en' => 'United States of America' ),
),
'location' => array(
'accuracy_radius' => 1500,
'latitude' => 44.98,
'longitude' => 93.2636,
'metro_code' => 765,
'postal_code' => '55401',
'postal_confidence' => 33,
'time_zone' => 'America/Chicago',
),
2013-05-21 20:46:11 +00:00
'maxmind' => array(
'queries_remaining' => 22,
),
2013-05-09 14:28:01 +00:00
'registered_country' => array(
'geoname_id' => 2,
'iso_code' => 'CA',
'names' => array( 'en' => 'Canada' ),
),
'represented_country' => array(
'geoname_id' => 3,
'iso_code' => 'GB',
'names' => array( 'en' => 'United Kingdom' ),
),
'subdivisions' => array(
array(
'confidence' => 88,
'geoname_id' => 574635,
'iso_code' => 'MN',
'names' => array( 'en' => 'Minnesota' ),
)
),
'traits' => array(
'autonomous_system_number' => 1234,
'autonomous_system_organization' => 'AS Organization',
'domain' => 'example.com',
'ip_address' => '1.2.3.4',
'is_satellite_provider' => 1,
'isp' => 'Comcast',
'organization' => 'Blorg',
'user_type' => 'college',
),
);
$model = new Omni($raw, array('en'));
$this->assertInstanceOf(
'GeoIp2\Model\Omni',
2013-05-09 14:28:01 +00:00
$model,
'GeoIp2\Model\Omni object'
2013-05-09 14:28:01 +00:00
);
$this->assertInstanceOf(
'GeoIp2\Record\City',
2013-05-09 14:28:01 +00:00
$model->city,
'$model->city'
);
$this->assertInstanceOf(
'GeoIp2\Record\Continent',
2013-05-09 14:28:01 +00:00
$model->continent,
'$model->continent'
);
$this->assertInstanceOf(
'GeoIp2\Record\Country',
2013-05-09 14:28:01 +00:00
$model->country,
'$model->country'
);
$this->assertInstanceOf(
'GeoIp2\Record\Location',
2013-05-09 14:28:01 +00:00
$model->location,
'$model->location'
);
$this->assertInstanceOf(
'GeoIp2\Record\Country',
2013-05-09 14:28:01 +00:00
$model->registeredCountry,
'$model->registeredCountry'
);
$this->assertInstanceOf(
'GeoIp2\Record\RepresentedCountry',
2013-05-09 14:28:01 +00:00
$model->representedCountry,
'$model->representedCountry'
);
$subdivisions = $model->subdivisions;
foreach ($subdivisions as $subdiv) {
$this->assertInstanceOf('GeoIp2\Record\Subdivision', $subdiv);
2013-05-09 14:28:01 +00:00
}
$this->assertInstanceOf(
'GeoIp2\Record\Subdivision',
2013-05-09 14:28:01 +00:00
$model->mostSpecificSubdivision,
'$model->mostSpecificSubdivision'
);
$this->assertInstanceOf(
'GeoIp2\Record\Traits',
2013-05-09 14:28:01 +00:00
$model->traits,
'$model->traits'
);
2013-05-21 20:46:11 +00:00
$this->assertEquals(
22,
$model->maxmind->queriesRemaining,
'queriesRemaining is correct'
);
2013-05-09 14:28:01 +00:00
$this->assertEquals(
$raw,
$model->raw,
'raw method returns raw input'
);
}
public function testEmptyObjects()
{
$raw = array( 'traits' => array( 'ip_address' => '5.6.7.8' ) );
$model = new Omni($raw, array('en'));
$this->assertInstanceOf(
'GeoIp2\Model\Omni',
2013-05-09 14:28:01 +00:00
$model,
'GeoIp2\Model\Omni object with no data except traits.ipAddress'
2013-05-09 14:28:01 +00:00
);
$this->assertInstanceOf(
'GeoIp2\Record\City',
2013-05-09 14:28:01 +00:00
$model->city,
'$model->city'
);
$this->assertInstanceOf(
'GeoIp2\Record\Continent',
2013-05-09 14:28:01 +00:00
$model->continent,
'$model->continent'
);
$this->assertInstanceOf(
'GeoIp2\Record\Country',
2013-05-09 14:28:01 +00:00
$model->country,
'$model->country'
);
$this->assertInstanceOf(
'GeoIp2\Record\Location',
2013-05-09 14:28:01 +00:00
$model->location,
'$model->location'
);
$this->assertInstanceOf(
'GeoIp2\Record\Country',
2013-05-09 14:28:01 +00:00
$model->registeredCountry,
'$model->registeredCountry'
);
$this->assertInstanceOf(
'GeoIp2\Record\RepresentedCountry',
2013-05-09 14:28:01 +00:00
$model->representedCountry,
'$model->representedCountry'
);
2013-05-21 20:28:37 +00:00
2013-05-09 14:28:01 +00:00
$this->assertCount(
0,
$model->subdivisions,
'$model->subdivisions returns an empty list'
);
$this->assertInstanceOf(
'GeoIp2\Record\Subdivision',
2013-05-09 14:28:01 +00:00
$model->mostSpecificSubdivision,
'$model->mostSpecificSubdivision'
);
$this->assertInstanceOf(
'GeoIp2\Record\Traits',
2013-05-09 14:28:01 +00:00
$model->traits,
'$model->traits'
);
$this->assertEquals(
$raw,
$model->raw,
'raw method returns raw input with no added empty values'
);
}
public function testUnknown()
{
$raw = array(
'new_top_level' => array( 'foo' => 42 ),
'city' => array(
'confidence' => 76,
'geoname_id_id' => 9876,
'names' => array( 'en' => 'Minneapolis' ),
'population' => 50,
),
'traits' => array( 'ip_address' => '5.6.7.8' )
);
// checking whether there are exceptions with unknown keys
$model = new Omni($raw, array('en'));
$this->assertInstanceOf(
'GeoIp2\Model\Omni',
2013-05-09 14:28:01 +00:00
$model,
'no exception when Omni model gets raw data with unknown keys'
);
$this->assertEquals(
$raw,
$model->raw,
'raw method returns raw input'
);
}
}