Made the database service class methods static for convenience \

This commit is contained in:
Christopher Allford 2020-01-08 21:59:04 -08:00
parent 1eb49ef8b8
commit 95e59fc777
2 changed files with 9 additions and 18 deletions

View File

@ -30,7 +30,7 @@ class WC_MaxMind_Geolocation_Database {
*
* @return string The local database path.
*/
public function get_database_path() {
public static function get_database_path() {
$database_path = WP_CONTENT_DIR . '/uploads/' . self::DATABASE . self::DATABASE_EXTENSION;
/**
@ -62,7 +62,7 @@ class WC_MaxMind_Geolocation_Database {
* @param string $license_key The license key to be used when downloading the database.
* @return string|WP_Error The path to the database file or an error if invalid.
*/
public function download_database( $license_key ) {
public static function download_database( $license_key ) {
$download_uri = 'https://download.maxmind.com/app/geoip_download?';
$download_uri .= http_build_query(
array(

View File

@ -10,13 +10,6 @@
*/
class WC_Tests_MaxMind_Database extends WC_Unit_Test_Case {
/**
* The database used by the tests.
*
* @var WC_MaxMind_Geolocation_Database
*/
private $database_service;
/**
* Run setup code for unit tests.
*/
@ -25,8 +18,6 @@ class WC_Tests_MaxMind_Database extends WC_Unit_Test_Case {
// Callback used by WP_HTTP_TestCase to decide whether to perform HTTP requests or to provide a mocked response.
$this->http_responder = array( $this, 'mock_http_responses' );
$this->database_service = new WC_MaxMind_Geolocation_Database();
}
/**
@ -35,17 +26,17 @@ class WC_Tests_MaxMind_Database extends WC_Unit_Test_Case {
* @expectedDeprecated woocommerce_geolocation_local_database_path
*/
public function test_database_path_filters() {
$path = $this->database_service->get_database_path();
$path = WC_MaxMind_Geolocation_Database::get_database_path();
$this->assertEquals( WP_CONTENT_DIR . '/uploads/' . WC_MaxMind_Geolocation_Database::DATABASE . WC_MaxMind_Geolocation_Database::DATABASE_EXTENSION, $path );
add_filter( 'woocommerce_geolocation_local_database_path', array( $this, 'filter_database_path_deprecated' ), 1, 2 );
$path = $this->database_service->get_database_path();
$path = WC_MaxMind_Geolocation_Database::get_database_path();
remove_filter( 'woocommerce_geolocation_local_database_path', array( $this, 'filter_database_path' ) );
$this->assertEquals( '/deprecated_filter', $path );
add_filter( 'woocommerce_geolocation_local_database_path', array( $this, 'filter_database_path' ) );
$path = $this->database_service->get_database_path();
$path = WC_MaxMind_Geolocation_Database::get_database_path();
remove_filter( 'woocommerce_geolocation_local_database_path', array( $this, 'filter_database_path' ) );
$this->assertEquals( '/filter', $path );
@ -55,7 +46,7 @@ class WC_Tests_MaxMind_Database extends WC_Unit_Test_Case {
* Tests that the database download works as expected.
*/
public function test_download_database_works() {
$result = $this->database_service->download_database( 'testing_license' );
$result = WC_MaxMind_Geolocation_Database::download_database( 'testing_license' );
$this->assertEquals( '/tmp/GeoLite2-Country_20200107/GeoLite2-Country.mmdb', $result );
}
@ -64,17 +55,17 @@ class WC_Tests_MaxMind_Database extends WC_Unit_Test_Case {
* Tests the that database download wraps the download and extraction errors.
*/
public function test_download_database_wraps_errors() {
$result = $this->database_service->download_database( 'invalid_license' );
$result = WC_MaxMind_Geolocation_Database::download_database( 'invalid_license' );
$this->assertWPError( $result );
$this->assertEquals( 'woocommerce_maxmind_geolocation_database_license_key', $result->get_error_code() );
$result = $this->database_service->download_database( 'generic_error' );
$result = WC_MaxMind_Geolocation_Database::download_database( 'generic_error' );
$this->assertWPError( $result );
$this->assertEquals( 'woocommerce_maxmind_geolocation_database_download', $result->get_error_code() );
$result = $this->database_service->download_database( 'archive_error' );
$result = WC_MaxMind_Geolocation_Database::download_database( 'archive_error' );
$this->assertWPError( $result );
$this->assertEquals( 'woocommerce_maxmind_geolocation_database_archive', $result->get_error_code() );