From 96f534c59c6816d062f298d1ebdbae470613ce41 Mon Sep 17 00:00:00 2001 From: Jake B Date: Thu, 26 Apr 2018 09:11:33 +1000 Subject: [PATCH] Add file collection test, Prepare for SS4 upgrade --- .gitattributes | 1 + .travis.yml | 4 +- README.md | 1 + composer.json | 1 + phpcs.xml.dist | 24 ++++ phpstan.neon | 16 ++- src/Cloudflare.php | 191 ++++++++++++++++----------- src/Filesystem.php | 76 ++++++++--- src/PurgeTask.php | 19 ++- src/SiteTreeExtension.php | 9 +- tests/.htaccess | 1 + tests/CloudflarePurgeFileTest.php | 139 +++++++++++++++++++ tests/CloudflareTest.php | 20 --- tests/assets/test_combined_css_a.css | 3 + tests/assets/test_combined_css_b.css | 3 + tests/bootstrap-phpstan.php | 12 ++ 16 files changed, 389 insertions(+), 131 deletions(-) create mode 100644 phpcs.xml.dist create mode 100644 tests/.htaccess create mode 100644 tests/CloudflarePurgeFileTest.php delete mode 100644 tests/CloudflareTest.php create mode 100644 tests/assets/test_combined_css_a.css create mode 100644 tests/assets/test_combined_css_b.css create mode 100644 tests/bootstrap-phpstan.php diff --git a/.gitattributes b/.gitattributes index be6d1eb..ccf7d4d 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,6 +2,7 @@ /docs export-ignore /.travis.yml export-ignore /.scrutinizer.yml export-ignore +/phpcs.xml.dist export-ignore /phpstan.neon export-ignore README.md export-ignore LICENSE.md export-ignore diff --git a/.travis.yml b/.travis.yml index d9cdad6..ba41886 100644 --- a/.travis.yml +++ b/.travis.yml @@ -37,10 +37,10 @@ before_script: - phpenv rehash - git clone git://github.com/silverstripe-labs/silverstripe-travis-support.git ~/travis-support - if [[ -z $PHPSTAN_TEST ]]; then php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss; fi - - if [[ $PHPSTAN_TEST ]]; then php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss --require phpstan/phpstan-shim:0.8.4,silbinarywolf/silverstripe-phpstan:dev-master; fi + - if [[ $PHPSTAN_TEST ]]; then php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss --require phpstan/phpstan-shim:0.9.2; fi - cd ~/builds/ss script: - if [[ $PHPCS_TEST ]]; then vendor/bin/phpcs --standard=PSR2 oldman/src/ oldman/tests/ -n; fi - - if [[ $PHPSTAN_TEST ]]; then vendor/bin/phpstan.phar analyse oldman/src -c "oldman/phpstan.neon" -a "vendor/silbinarywolf/silverstripe-phpstan/bootstrap.php" --level 3; fi + - if [[ $PHPSTAN_TEST ]]; then vendor/bin/phpstan.phar analyse oldman/src -c "oldman/phpstan.neon" -a "oldman/tests/bootstrap-phpstan.php" --level 3; fi - vendor/bin/phpunit oldman/tests/ diff --git a/README.md b/README.md index 37c7cca..b65c922 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ composer require silbinarywolf/silverstripe-oldman:~1.0 ## Requirements +* PHP 5.4+ * SilverStripe 3.2+ ## Documentation diff --git a/composer.json b/composer.json index cd8a750..6a13975 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,7 @@ } ], "require": { + "php": ">=5.4", "silverstripe/framework": "~3.1", "silverstripe/cms": "~3.1", "jamesryanbell/cloudflare": "~1.11" diff --git a/phpcs.xml.dist b/phpcs.xml.dist new file mode 100644 index 0000000..9924cfd --- /dev/null +++ b/phpcs.xml.dist @@ -0,0 +1,24 @@ + + + Coding standard for SilverStripe 4.x + + + */vendor/* + */thirdparty/* + + + + + + + + + + + + + + + + + diff --git a/phpstan.neon b/phpstan.neon index cccbefb..58ac275 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,7 +1,19 @@ parameters: + earlyTerminatingMethodCalls: + Controller: + - redirect + universalObjectCratesClasses: + - ArrayData + - Config_ForClass + - DataObject + - AbstractQueuedJob # symbiote/silverstripe-queuedjobs module support + excludes_analyse: + - silverstripe-cache ignoreErrors: # Temporary until "class_exists" feature is added # See here: https://github.com/phpstan/phpstan/issues/323 - '%Class Site not found%' -includes: - - ../vendor/silbinarywolf/silverstripe-phpstan/extension.neon + # No SilverStripe support yet + - '%Call to an undefined method SiteTree::Parent()%' +#includes: +# - ../vendor/silbinarywolf/silverstripe-phpstan/extension.neon diff --git a/src/Cloudflare.php b/src/Cloudflare.php index 9ed7ef3..8f1cf40 100644 --- a/src/Cloudflare.php +++ b/src/Cloudflare.php @@ -2,23 +2,57 @@ namespace Symbiote\Cloudflare; -class Cloudflare extends \Object +use Object; +use Controller; +use Director; +use File; +use Injector; +use SiteTree; +use Site; +use Requirements; +use Cloudflare\Api; +use Cloudflare\Zone\Cache; + +class Cloudflare extends Object { /** * Cloudflare can only purge 500 files per request. */ const MAX_PURGE_FILES_PER_REQUEST = 500; + /** + * String representation of this class. + * NOTE: Using this as PHP 5.4 does not support `Cloudflare::class` + * + * @var string + */ + const CLOUDFLARE_CLASS = 'Symbiote\Cloudflare\Cloudflare'; + + /** + * String representation of the "Filesystem" class. + * NOTE: Using this as PHP 5.4 does not support `Filesystem::class` + * + * @var string + */ + const FILESYSTEM_CLASS = 'Symbiote\Cloudflare\Filesystem'; + + /** + * String representation of a Multisite "Site" DataObject class. + * NOTE: Using this as PHP 5.4 does not support `Site::class` + * + * @var string + */ + const SITE_CLASS = 'Site'; + /** * @var boolean + * @config */ private static $enabled = false; /** - * Email - * eg. silverstripe@gmail.com - * * @var string + * @config */ private static $email = ''; @@ -27,7 +61,8 @@ class Cloudflare extends \Object * * eg. 24ca61e15fb2aa62a31212a90f2674f3451f8 * - * @var string + * @var string + * @config */ private static $auth_key = ''; @@ -36,7 +71,8 @@ class Cloudflare extends \Object * * eg. 73a40b2c0c10f468cb658f67b9d46fff * - * @var string + * @var string + * @config */ private static $zone_id = ''; @@ -48,31 +84,15 @@ class Cloudflare extends \Object * * TODO(Jake): Perhaps change this to a array/list for Multisite support? * + * @var string * @config - * @var string */ private static $base_url = ''; - /** - * TODO(Jake): Not implemented yet as not necessary. - * - * The directories to scan recursively for CSS/JS. If empty array, defaults to: - * - array('%BASE_FOLDER%') - * - * It's recommended that if you only use CSS/JS/Images from your theme folder that you - * configure this in YML to: - * - '%BASE_FOLDER%/themes' - * - * @config - * @var string - */ - //private static $scan_directories = array(); - /** * Files with these extensions to purge when clearing images. * The other file extensions are read from File::app_categories['image']. * - * @config * @var array */ private static $image_file_extensions = array( @@ -93,38 +113,22 @@ class Cloudflare extends \Object public function __construct() { parent::__construct(); + $this->filesystem = Injector::inst()->get(self::FILESYSTEM_CLASS); if ($this->config()->enabled) { - $this->client = new \Cloudflare\Api($this->config()->email, $this->config()->auth_key); - $this->filesystem = \Injector::inst()->get('Symbiote\Cloudflare\Filesystem'); + $this->client = new Api($this->config()->email, $this->config()->auth_key); } } /** * @return CloudflareResult|null */ - public function purgePage(\SiteTree $page) + public function purgePage(SiteTree $page) { if (!$this->client) { return null; } - $files = array(); - - // Use alternate base url if defined for cache clearing - $baseURL = $this->config()->base_url; - $pageLink = ''; - if ($baseURL) { - $pageLink = \Controller::join_links($baseURL, $page->Link()); - } else { - $pageLink = $page->AbsoluteLink(); - } - $files[] = $pageLink; - - // If /home/ for HomePage, also add "/" to be cleared. - if ($this->isHomePage($page)) { - $files[] = substr($pageLink, 0, (strrpos($pageLink, '/home'))); - } - - $cache = new \Cloudflare\Zone\Cache($this->client); + $files = $this->getLinksToPurgeByPage($page); + $cache = new Cache($this->client); $response = $cache->purge_files($this->getZoneIdentifier(), $files); $result = new CloudflareResult($files, $response->errors); return $result; @@ -138,7 +142,7 @@ public function purgeAll() if (!$this->client) { return null; } - $cache = new \Cloudflare\Zone\Cache($this->client); + $cache = new Cache($this->client); $response = $cache->purge($this->getZoneIdentifier(), true); $result = new CloudflareResult(array(), $response->errors); @@ -150,7 +154,7 @@ public function purgeAll() */ public function purgeImages() { - $appCategories = \File::config()->app_categories; + $appCategories = File::config()->app_categories; if (!isset($appCategories['image'])) { user_error('Missing "image" category on File::app_categories.', E_USER_WARNING); return null; @@ -188,7 +192,7 @@ public function purgeURLs(array $absoluteOrRelativeURLList) // Get base URL (for conversion of relative URL to absolute URL) $baseURL = $this->config()->base_url; if (!$baseURL) { - $baseURL = \Director::absoluteBaseURL(); + $baseURL = Director::absoluteBaseURL(); } // Process list of relative/absolute URLs @@ -199,13 +203,13 @@ public function purgeURLs(array $absoluteOrRelativeURLList) // Convert to absolute URL if (!$isAbsoluteURL) { - $urlsToPurge[] = \Controller::join_links($baseURL, $absoluteOrRelativeURL); + $urlsToPurge[] = Controller::join_links($baseURL, $absoluteOrRelativeURL); continue; } $urlsToPurge[] = $absoluteOrRelativeURL; } - $cache = new \Cloudflare\Zone\Cache($this->client); + $cache = new Cache($this->client); $response = $cache->purge_files($this->getZoneIdentifier(), $urlsToPurge); $result = new CloudflareResult($urlsToPurge, $response->errors); @@ -228,32 +232,11 @@ protected function purgeFilesByExtensions(array $fileExtensions) if (!$this->client) { return null; } - $cache = new \Cloudflare\Zone\Cache($this->client); - $zoneIdentifier = $this->getZoneIdentifier(); - - // Scan files in the project directory to purge - $folderList = array( - // Get all files built by `Requirements` system (*.css, *.js) - \Director::baseFolder().'/'.\Requirements::backend()->getCombinedFilesFolder(), - // Get all module / theme files - \Director::baseFolder() - ); - $files = array(); - foreach ($folderList as $folder) { - $files = array_merge($files, $this->filesystem->getFilesWithExtensionsRecursively($folder, $fileExtensions)); - } - - // Get all files in database and purge (not using local scan for /assets/ so we can support remotely hosted files in S3/etc) - $fileExtensionsPrefixedWithDot = array(); - foreach ($fileExtensions as $fileExtension) { - $fileExtensionsPrefixedWithDot[] = '.'.$fileExtension; - } - $fileRecordList = \File::get()->filter(array( - 'Filename:EndsWith' => $fileExtensionsPrefixedWithDot - )); - $files = array_merge($files, $fileRecordList->map('ID', 'Link')->toArray()); + $files = $this->getFilesToPurgeByExtensions($fileExtensions, false); // Purge files + $cache = new Cache($this->client); + $zoneIdentifier = $this->getZoneIdentifier(); $errors = array(); foreach (array_chunk($files, self::MAX_PURGE_FILES_PER_REQUEST) as $filesChunk) { $response = $cache->purge_files($zoneIdentifier, $filesChunk); @@ -269,13 +252,69 @@ protected function purgeFilesByExtensions(array $fileExtensions) /** * Check if page is the home page. - * Supports Multisites. (ie. \Site record exists at top of tree) + * Supports Multisites. (ie. "Site" record exists at top of tree) * * @return boolean */ - protected function isHomePage(\SiteTree $page) + protected function isHomePage(SiteTree $page) { $parent = $page->Parent(); - return $page->URLSegment === 'home' && ((class_exists('Site') && $parent instanceof \Site) || !$parent->exists()); + return $page->URLSegment === 'home' && ((class_exists(self::SITE_CLASS) && $parent instanceof Site) || !$parent->exists()); + } + + /** + * @return array + */ + private function getLinksToPurgeByPage(SiteTree $page) + { + $files = array(); + // Use alternate base url if defined for cache clearing + $baseURL = $this->config()->base_url; + $pageLink = ''; + if ($baseURL) { + $pageLink = Controller::join_links($baseURL, $page->Link()); + } else { + $pageLink = $page->AbsoluteLink(); + } + $files[] = $pageLink; + + // If /home/ for HomePage, also add "/" to be cleared. + if ($this->isHomePage($page)) { + $files[] = substr($pageLink, 0, (strrpos($pageLink, '/home'))); + } + return $files; + } + + /** + * @return array + */ + private function getFilesToPurgeByExtensions(array $fileExtensions, $ignoreDatabaseRecords) + { + // Scan files in the project directory to purge + $folderList = array( + // Get all files built by `Requirements` system (*.css, *.js) + Director::baseFolder().'/'.Requirements::backend()->getCombinedFilesFolder(), + // Get all module / theme files + Director::baseFolder() + ); + $files = array(); + foreach ($folderList as $folder) { + $files = array_merge($files, $this->filesystem->getFilesWithExtensionsRecursively($folder, $fileExtensions)); + } + + // Get all files in database and purge (not using local scan for /assets/ so we can support remotely hosted files in S3/etc) + if (!$ignoreDatabaseRecords) { + $fileExtensionsPrefixedWithDot = array(); + foreach ($fileExtensions as $fileExtension) { + $fileExtensionsPrefixedWithDot[] = '.'.$fileExtension; + } + $fileRecordList = File::get()->filter( + array( + 'Filename:EndsWith' => $fileExtensionsPrefixedWithDot + ) + ); + $files = array_merge($files, $fileRecordList->map('ID', 'Link')->toArray()); + } + return $files; } } diff --git a/src/Filesystem.php b/src/Filesystem.php index 0569de3..7ed3ad4 100644 --- a/src/Filesystem.php +++ b/src/Filesystem.php @@ -2,6 +2,9 @@ namespace Symbiote\Cloudflare; +use Director; +use Config; + class Filesystem { /** @@ -23,13 +26,27 @@ class Filesystem /** * Ban certain directories from being traversed to increase purge response time. - * ie. - Without blacklisting framework/cms, purging CSS/JS takes 45 seconds. - * - Blacklisting framework/cms, purging CSS/JS takes 6 seconds. * * @config * @var array */ private static $blacklist_absolute_pathnames = array( + ); + + /** + * @config + * @var boolean + */ + private static $disable_default_blacklist_absolute_pathnames = false; + + /** + * The default directories that are ignored from being traversed to increase purge response time. + * ie. - Without blacklisting framework/cms, purging CSS/JS takes ~45 seconds. + * - Blacklisting framework/cms, purging CSS/JS takes ~6 seconds. + * + * @var array + */ + protected $defaultBlacklistAbsolutePathnames = array( '%BASE_FOLDER%/framework', '%BASE_FOLDER%/cms', '%BASE_FOLDER%/assets', @@ -46,61 +63,78 @@ class Filesystem public function getFilesWithExtensionsRecursively($directory, array $extensionsToMatch) { $directory_stack = array($directory); - $ignored_filename_list = \Config::inst()->get(__CLASS__, 'blacklist_filenames'); - $ignored_pathname_list = \Config::inst()->get(__CLASS__, 'blacklist_absolute_pathnames'); - $base_folder = \Director::baseFolder(); - $base_url = \Config::inst()->get('Symbiote\Cloudflare\Cloudflare', 'base_url'); + $ignored_filename_list = Config::inst()->get(__CLASS__, 'blacklist_filenames'); + $ignored_pathname_list = array(); + if (!Config::inst()->get(__CLASS__, 'disable_default_blacklist_absolute_pathnames')) { + $ignored_pathname_list = $this->defaultBlacklistAbsolutePathnames; + } + $custom_ignored_pathname_list = Config::inst()->get(__CLASS__, 'blacklist_absolute_pathnames'); + if ($custom_ignored_pathname_list) { + $ignored_pathname_list = array_merge($ignored_pathname_list, $custom_ignored_pathname_list); + } + + $base_folder = Director::baseFolder(); + $base_folder = str_replace('\\', '/', $base_folder); + $base_url = Config::inst()->get(Cloudflare::CLOUDFLARE_CLASS, 'base_url'); if (!$base_url) { - $base_url = \Director::absoluteURL('/'); + $base_url = Director::absoluteURL('/'); } $base_url = rtrim($base_url, '/'); // Convert from flat arrays to lookup for speed $ignored_filename_lookup = array(); - foreach ($ignored_filename_list as $ignored_filename) { - $ignored_filename_lookup[$ignored_filename] = true; + if ($ignored_filename_list) { + foreach ($ignored_filename_list as $ignored_filename) { + $ignored_filename_lookup[$ignored_filename] = true; + } } $ignored_pathname_lookup = array(); - foreach ($ignored_pathname_list as $ignored_pathname) { - $ignored_pathname = str_replace('%BASE_FOLDER%', $base_folder, $ignored_pathname); - $ignored_pathname_lookup[$ignored_pathname] = true; + if ($ignored_pathname_list) { + foreach ($ignored_pathname_list as $ignored_pathname) { + $ignored_pathname = str_replace(array('%BASE_FOLDER%', '\\'), array($base_folder, '/'), $ignored_pathname); + $ignored_pathname_lookup[$ignored_pathname] = true; + } } // Get all files $result_file_list = array(); while ($directory_stack) { $current_directory = array_shift($directory_stack); + $current_directory = str_replace('\\', '/', $current_directory); $files = scandir($current_directory); foreach ($files as $filename) { + $filename = str_replace('\\', '/', $filename); // Skip all files/directories with: // - (Disabled) A starting '.' // - (Disabled) A starting '_' - if (isset($filename[0]) && (isset($ignored_filename_lookup[$filename]) ) + if (isset($filename[0]) + && isset($ignored_filename_lookup[$filename]) ) { continue; } - // Skip full pathnames - $pathname = $current_directory . DIRECTORY_SEPARATOR . $filename; - // Ignore folder paths - // - {PROJECT_DIR}/vendor - // - {PROJECT_DIR}/assets - if (isset($ignored_pathname_lookup[$pathname])) { + // - %BASE_FOLDER%/vendor + // - %BASE_FOLDER%/assets + if (isset($ignored_pathname_lookup[$current_directory])) { continue; } + $pathname = $current_directory.'/'.$filename; + if (is_dir($pathname) === true) { $directory_stack[] = $pathname; continue; } $file_extension = pathinfo($pathname, PATHINFO_EXTENSION); if (in_array($file_extension, $extensionsToMatch)) { - // Convert path to URL + // Two things: + // - Convert path to URL // ie. "/shared/httpd/{project-folder}/htdocs/betterbuttons/css/betterbuttons_nested_form.css" // to: "http://{project-folder}.symlocal/betterbuttons/css/betterbuttons_nested_form.css" - $pathname = str_replace($base_folder, $base_url, $pathname); + // + $pathname = str_replace(array($base_folder), array($base_url), $pathname); $result_file_list[] = $pathname; } diff --git a/src/PurgeTask.php b/src/PurgeTask.php index 0bbb9ba..ce78172 100644 --- a/src/PurgeTask.php +++ b/src/PurgeTask.php @@ -2,20 +2,25 @@ namespace Symbiote\Cloudflare; -abstract class PurgeTask extends \BuildTask +use BuildTask; +use Injector; +use Director; +use SS_Log; + +abstract class PurgeTask extends BuildTask { abstract protected function callPurgeFunction(Cloudflare $client); public function run($request = null) { - $client = \Injector::inst()->get('Symbiote\Cloudflare\Cloudflare'); + $client = Injector::inst()->get(Cloudflare::CLOUDFLARE_CLASS); if (!$client->config()->enabled) { $this->log('Cloudflare is not currently enabled in YML.'); return; } // If accessing via web-interface, add an "are you sure" message. - if (!\Director::is_cli()) { + if (!Director::is_cli()) { if ($request->getVar('purge') != true) { $this->log('Append "?purge=true" to the URL to confirm execution.'); return; @@ -46,7 +51,7 @@ public function run($request = null) if ($errors) { $status = 'PURGE ERRORS'; if ($errors) { - echo \Director::is_cli() ? "\n" : '
'; + echo Director::is_cli() ? "\n" : '
'; $this->log('Error(s):', E_USER_WARNING); foreach ($errors as $error) { $this->log($error, E_USER_WARNING); @@ -56,7 +61,7 @@ public function run($request = null) // If no successes or errors, assume success. // ie. this is for purge everything. - echo \Director::is_cli() ? "\n" : '
'; + echo Director::is_cli() ? "\n" : '
'; if (!$successes && !$errors) { $this->log($status.'.', E_USER_WARNING); } else { @@ -67,10 +72,10 @@ public function run($request = null) protected function log($message, $errorType = null) { - $newline = \Director::is_cli() ? "\n" : "
"; + $newline = Director::is_cli() ? "\n" : "
"; echo $message.$newline; if ($errorType) { - \SS_Log::log($message, $errorType); + SS_Log::log($message, $errorType); } } } diff --git a/src/SiteTreeExtension.php b/src/SiteTreeExtension.php index 19ab84e..314962e 100644 --- a/src/SiteTreeExtension.php +++ b/src/SiteTreeExtension.php @@ -2,15 +2,18 @@ namespace Symbiote\Cloudflare; -class SiteTreeExtension extends \DataExtension +use Injector; +use DataExtension; + +class SiteTreeExtension extends DataExtension { public function onAfterPublish() { - \Injector::inst()->get('Symbiote\Cloudflare\Cloudflare')->purgePage($this->owner); + Injector::inst()->get(Cloudflare::CLOUDFLARE_CLASS)->purgePage($this->owner); } public function onAfterUnpublish() { - \Injector::inst()->get('Symbiote\Cloudflare\Cloudflare')->purgePage($this->owner); + Injector::inst()->get(Cloudflare::CLOUDFLARE_CLASS)->purgePage($this->owner); } } diff --git a/tests/.htaccess b/tests/.htaccess new file mode 100644 index 0000000..3a42882 --- /dev/null +++ b/tests/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/tests/CloudflarePurgeFileTest.php b/tests/CloudflarePurgeFileTest.php new file mode 100644 index 0000000..3aecaed --- /dev/null +++ b/tests/CloudflarePurgeFileTest.php @@ -0,0 +1,139 @@ +getFilesToPurgeByExtensions( + array( + 'css', + 'js', + 'json', + ) + ); + $expectedFiles = array( + // Make sure we purge the _combinedfile + 'assets/_combinedfiles/combined.min.css', + 'oldman/tests/assets/test_combined_css_a.css', + 'oldman/tests/assets/test_combined_css_b.css', + ); + // Search for matches + $matchCount = 0; + foreach ($files as $file) { + foreach ($expectedFiles as $expectedFile) { + if (strpos($file, $expectedFile) !== false) { + $matchCount++; + break; + } + } + } + $this->assertEquals( + count($expectedFiles), + $matchCount, + "Expected file list:\n".print_r($expectedFiles, true)."Instead got:\n".print_r($files, true) + ); + + // If it has a file from the 'framework' module, fail this test as it should be ignored. + $hasFramework = false; + foreach ($files as $file) { + $hasFramework = $hasFramework || (strpos($file, self::FRAMEWORK_CSS_FILE) !== false); + } + $this->assertFalse($hasFramework, 'Expected to specifically not get the "framework" file: '.self::FRAMEWORK_CSS_FILE); + } + + /** + * Test if this can detect the CSS file in framework when the default blacklist is disabled. + */ + public function testAllowBlacklistedDirectories() + { + Config::inst()->update(Cloudflare::FILESYSTEM_CLASS, 'disable_default_blacklist_absolute_pathnames', true); + $files = $this->getFilesToPurgeByExtensions( + array( + 'css', + 'js', + 'json', + ) + ); + Config::inst()->update(Cloudflare::FILESYSTEM_CLASS, 'disable_default_blacklist_absolute_pathnames', false); + + // If it has a file from the 'framework' module, fail this test as it should be ignored. + $hasFramework = false; + foreach ($files as $file) { + $hasFramework = $hasFramework || (strpos($file, self::FRAMEWORK_CSS_FILE) !== false); + } + $this->assertTrue( + $hasFramework, + 'Expected to get "framework" file: '.self::FRAMEWORK_CSS_FILE."\nInstead got:".print_r($files, true) + ); + } + + /** + * Wrapper to expose private method 'getFilesToPurgeByExtensions' + * + * @return array + */ + private function getFilesToPurgeByExtensions(array $fileExtensions) + { + $service = Injector::inst()->get(Cloudflare::CLOUDFLARE_CLASS); + $reflector = new ReflectionObject($service); + $method = $reflector->getMethod('getFilesToPurgeByExtensions'); + $method->setAccessible(true); + // NOTE(Jake): 2018-04-18 + // + // We skip "File::get()" calls with the $skipDatabaseRecords parameter. + // This is to make executing tests faster. + // + $skipDatabaseRecords = true; + $results = $method->invoke($service, $fileExtensions, $skipDatabaseRecords); + // NOTE(Jake): 2018-04-18 + // + // Searching through a directory recursively will have files unordered. + // We sort in tests so that datasets are more predictable. + // + sort($results); + return $results; + } +} diff --git a/tests/CloudflareTest.php b/tests/CloudflareTest.php deleted file mode 100644 index c0288b8..0000000 --- a/tests/CloudflareTest.php +++ /dev/null @@ -1,20 +0,0 @@ -get('Symbiote\Cloudflare\Cloudflare')->purgePage($page); - // Expects `null` when not configured. - $this->assertNull($result); - } -} diff --git a/tests/assets/test_combined_css_a.css b/tests/assets/test_combined_css_a.css new file mode 100644 index 0000000..8a75e19 --- /dev/null +++ b/tests/assets/test_combined_css_a.css @@ -0,0 +1,3 @@ +.selector_a { + width: 100%; +} diff --git a/tests/assets/test_combined_css_b.css b/tests/assets/test_combined_css_b.css new file mode 100644 index 0000000..061fce6 --- /dev/null +++ b/tests/assets/test_combined_css_b.css @@ -0,0 +1,3 @@ +.selector_b { + width: 100%; +} diff --git a/tests/bootstrap-phpstan.php b/tests/bootstrap-phpstan.php new file mode 100644 index 0000000..6354cbd --- /dev/null +++ b/tests/bootstrap-phpstan.php @@ -0,0 +1,12 @@ +