diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..719e245 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,17 @@ +# For more information about the properties used in +# this file, please see the EditorConfig documentation: +# http://editorconfig.org/ + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false + +[*.yml] +indent_size = 2 diff --git a/code/CloudFlare.php b/code/CloudFlare.php index f5543d0..10b2e06 100644 --- a/code/CloudFlare.php +++ b/code/CloudFlare.php @@ -1,6 +1,6 @@ '', + 'http://' => '', + 'https://' => '' + ); + $serverName = str_replace(array_keys($replaceWith), array_values($replaceWith), $serverName); + + // Allow extensions to modify or replace the server name if required + $this->extend('updateCloudFlareServerName', $serverName); + + return $serverName; + } + /** * Gets the CF Zone ID for the current domain. * @@ -316,19 +343,7 @@ public function fetchZoneID() return $cache; } - $replaceWith = array( - "www." => "", - "http://" => "", - "https://" => "" - ); - - $server = Convert::raw2xml($_SERVER); // "Fixes" #1 - - $serverName = str_replace(array_keys($replaceWith), array_values($replaceWith), $server[ 'SERVER_NAME' ]); - - if (getenv('TRAVIS')) { - $serverName = getenv('CLOUDFLARE_DUMMY_SITE'); - } + $serverName = $this->getServerName(); if ($serverName == 'localhost') { $this->setAlert( diff --git a/tests/CloudFlareTest.php b/tests/CloudFlareTest.php index 0ca2a3b..45121dc 100644 --- a/tests/CloudFlareTest.php +++ b/tests/CloudFlareTest.php @@ -1,29 +1,62 @@ getUrlVariants() + * @covers ::getUrlVariants */ - public function testGetUrlVariants() { + public function testGetUrlVariants() + { $urls = array( - "http://www.example.com", + 'http://www.example.com', ); - + $this->assertEquals( CloudFlare::inst()->getUrlVariants($urls), array( - "http://www.example.com", - "https://www.example.com", - "http://www.example.com?stage=Stage", - "https://www.example.com?stage=Stage" + 'http://www.example.com', + 'https://www.example.com', + 'http://www.example.com?stage=Stage', + 'https://www.example.com?stage=Stage' ) ); } - -} \ No newline at end of file + /** + * Ensures that the server name can be retrieved as expected from environment variables or an extension attached + * @covers ::getServerName + */ + public function testGetServerName() + { + // Ensures the CI environment can be factored in + putenv('TRAVIS=1'); + putenv('CLOUDFLARE_DUMMY_SITE=https://www.sometest.dev'); + $this->assertSame('sometest.dev', CloudFlare::inst()->getServerName()); + + // Apply a test extension, get a new instance of the CF class and test again to ensure the hook works + CloudFlare::add_extension('CloudFlareTest_Extension'); + $this->assertSame('extended.dev', CloudFlare::create()->getServerName()); + } +} + +/** + * A stub extension applied to CloudFlare as needed to test extension hooks + */ +class CloudFlareTest_Extension extends Extension implements TestOnly +{ + /** + * Set a dummy server name + * + * @see CloudFlare::getServerName + * @var string $serverName + */ + public function updateCloudFlareServerName(&$serverName) + { + $serverName = 'extended.dev'; + } +}