database_service = $this->getMockBuilder( 'WC_Integration_maxMind_Database_Service' ) ->disableOriginalConstructor() ->getMock(); add_filter( 'woocommerce_maxmind_geolocation_database_service', array( $this, 'override_integration_service' ) ); } /** * Make sure that the database is not updated if no target database path is given. */ public function test_update_database_does_nothing_without_database_path() { $this->database_service->expects( $this->once() ) ->method( 'get_database_path' ) ->willReturn( '' ); ( new WC_Integration_MaxMind_Geolocation() )->update_database(); } /** * Makes sure that the database can be updated to a given database. */ public function test_update_database_to_parameter_file() { $this->database_service->expects( $this->once() ) ->method( 'get_database_path' ) ->willReturn( '/testing' ); ( new WC_Integration_MaxMind_Geolocation() )->update_database( '/tmp/noop.mmdb' ); } /** * Makes sure that the integration uses the license key correctly. */ public function test_update_database_uses_license_key() { $this->database_service->expects( $this->once() ) ->method( 'get_database_path' ) ->willReturn( '/testing' ); $this->database_service->expects( $this->once() ) ->method( 'download_database' ) ->with( 'test_license' ) ->willReturn( '/tmp/MaxMind-Geolocation-Database.' . WC_Integration_MaxMind_Database_Service::DATABASE_EXTENSION ); $integration = new WC_Integration_MaxMind_Geolocation(); $integration->update_option( 'license_key', 'test_license' ); $integration->update_database(); } /** * Make sure that the geolocate_ip method does not squash existing country codes. */ public function test_geolocate_ip_returns_existing_country_code() { $country_code = ( new WC_Integration_MaxMind_Geolocation() )->geolocate_ip( 'US', '192.168.1.1' ); $this->assertEquals( 'US', $country_code ); } /** * Make sure that the geolocate_ip method does nothing if IP is not set. */ public function test_geolocate_ip_returns_empty_without_ip_address() { $country_code = ( new WC_Integration_MaxMind_Geolocation() )->geolocate_ip( false, '' ); $this->assertFalse( $country_code ); } /** * Make sure that the geolocate_ip method uses the appropriate service methods.. */ public function test_geolocate_ip_uses_service() { $this->database_service->expects( $this->once() ) ->method( 'get_iso_country_code_for_ip' ) ->with( '192.168.1.1' ) ->willReturn( 'US' ); $country_code = ( new WC_Integration_MaxMind_Geolocation() )->geolocate_ip( false, '192.168.1.1' ); $this->assertEquals( 'US', $country_code ); } /** * Overrides the filesystem method. * * @return string */ public function override_filesystem_method() { return 'Base'; } /** * Overrides the database service used by the integration. * * @return mixed */ public function override_integration_service() { return $this->database_service; } }