Source of file GeocodableApiValuesTest.php
Size: 5,252 Bytes - Last Modified: 2021-12-23T10:49:15+00:00
/var/www/docs.ssmods.com/process/src/tests/GeocodableApiValuesTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 | <?php namespace Symbiote\Addressable\Tests; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Environment; use SilverStripe\Dev\SapphireTest; use Symbiote\Addressable\GoogleGeocodeService; class GeocodableApiValuesTest extends SapphireTest { protected $envApiKey = 'env-key'; protected $googleGeocodeServiceApiKey = 'google-geocode-service-key'; protected $geocodeServiceApiKey = 'geocode-service-key'; protected $googleGeocodeServiceApiUrl = 'https://example.com/googleGeocodeServiceApiUrl'; protected $geocodeServiceApiUrl = 'https://example.com/geocodeServiceApiUrl'; /** * Set up config */ public function setUp() { parent::setUp(); Environment::setEnv('GOOGLE_API_KEY', $this->envApiKey); Config::inst()->update( GoogleGeocodeService::class, 'google_api_key', $this->googleGeocodeServiceApiKey ); Config::inst()->update( 'Symbiote\\Addressable\\GeocodeService', 'google_api_key', $this->geocodeServiceApiKey ); Config::inst()->update( GoogleGeocodeService::class, 'google_api_url', $this->googleGeocodeServiceApiUrl ); Config::inst()->update( 'Symbiote\\Addressable\\GeocodeService', 'google_api_url', $this->geocodeServiceApiUrl ); } /** * Reset config */ public function tearDown() { parent::tearDown(); Environment::setEnv('GOOGLE_API_KEY', false); Config::inst()->update( GoogleGeocodeService::class, 'google_api_key', null ); Config::inst()->update( 'Symbiote\\Addressable\\GeocodeService', 'google_api_key', null ); Config::inst()->update( GoogleGeocodeService::class, 'google_api_url', null ); Config::inst()->update( 'Symbiote\\Addressable\\GeocodeService', 'google_api_url', null ); } /** * Test setting and getting API key, including legacy config */ public function testApiKeySetGet() { $service = new GoogleGeocodeService(); $key = $service->getApiKey(); $this->assertEquals($this->envApiKey, $key, "The API key should be the key set from environment"); // false environment value Environment::setEnv('GOOGLE_API_KEY', false); $key = $service->getApiKey(); $this->assertEquals($this->googleGeocodeServiceApiKey, $key, "The API key should be the key set via config API on GoogleGeocodeService"); // null environment value Environment::setEnv('GOOGLE_API_KEY', null); $key = $service->getApiKey(); $this->assertEquals($this->googleGeocodeServiceApiKey, $key, "The API key should be the key set via config API on GoogleGeocodeService"); // empty string value Environment::setEnv('GOOGLE_API_KEY', false); Config::inst()->update( GoogleGeocodeService::class, 'google_api_key', '' ); $key = $service->getApiKey(); $this->assertEquals($this->geocodeServiceApiKey, $key, "The API key should be the key set via config API on legacy GeocodeService"); // null value Environment::setEnv('GOOGLE_API_KEY', false); Config::inst()->update( GoogleGeocodeService::class, 'google_api_key', null ); $key = $service->getApiKey(); $this->assertEquals($this->geocodeServiceApiKey, $key, "The API key should be the key set via config API on legacy GeocodeService"); // lack of config value should throw an \Exception Environment::setEnv('GOOGLE_API_KEY', false); Config::inst()->update( GoogleGeocodeService::class, 'google_api_key', null ); Config::inst()->update( 'Symbiote\\Addressable\\GeocodeService', 'google_api_key', null ); try { $key = $service->getApiKey(); $this->assertTrue(false, "Lack of an API key should trigger an \Exception"); } catch (\Exception $e) { // noop } } /** * Test setting and getting API URL, including legacy config */ public function testApiUrlSetGet() { $service = new GoogleGeocodeService(); $url = $service->getApiUrl(); $this->assertEquals($this->googleGeocodeServiceApiUrl, $url, "The API URL should be the URL set via config API on GoogleGeocodeService"); // empty string value Config::inst()->update( GoogleGeocodeService::class, 'google_api_url', '' ); $url = $service->getApiUrl(); $this->assertEquals($this->geocodeServiceApiUrl, $url, "The API url should be the url set via config API on legacy GeocodeService"); // null value Config::inst()->update( GoogleGeocodeService::class, 'google_api_url', null ); $url = $service->getApiUrl(); $this->assertEquals($this->geocodeServiceApiUrl, $url, "The API url should be the url set via config API on legacy GeocodeService"); // lack of config value should throw an \Exception Config::inst()->update( GoogleGeocodeService::class, 'google_api_url', null ); Config::inst()->update( 'Symbiote\\Addressable\\GeocodeService', 'google_api_url', null ); try { $url = $service->getApiUrl(); $this->assertTrue(false, "Lack of an API URL should trigger an \Exception"); } catch (\Exception $e) { // noop } } } |