From a8da7ae5fc1d1f524c4a9b625dc18456af176d7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Fri, 21 Jun 2024 16:42:27 -0400 Subject: [PATCH] Add PHPStan on tests (#6733) --- composer.json | 4 +- inc/ThirdParty/Hostings/Godaddy.php | 1 - inc/ThirdParty/Hostings/Pressidium.php | 2 - inc/ThirdParty/Hostings/Savvii.php | 4 - inc/ThirdParty/Hostings/WPEngine.php | 2 - .../Ecommerce/WooCommerceSubscriber.php | 6 +- .../Plugins/I18n/TranslatePress.php | 9 +- inc/ThirdParty/Plugins/SEO/SEOPress.php | 1 - .../Plugins/SEO/TheSEOFramework.php | 4 - .../Security/WordFenceCompatibility.php | 7 -- phpstan.neon.dist | 81 +++++++++-------- .../Minify/CSS/AdminSubscriber/onUpdate.php | 6 +- .../Hostings/Godaddy/GodaddyTestCase.php | 2 +- .../Cloudflare/disableCloudflareOption.php | 36 ++++---- .../Themes/Divi/removeAssetsGenerated.php | 7 +- .../excludeJqueryDeferjsWithCartDrawer.php | 7 +- .../inc/admin/rocketAfterSaveOptions.php | 26 ------ .../inc/admin/rocketFirstInstall.php | 5 +- .../changeCacheRejectUriWithPermalink.php | 25 +++--- .../Engine/Cache/Purge/purgeDatesArchives.php | 19 ++-- .../Engine/Cache/Purge/purgePostTermsUrls.php | 23 ++--- .../Strategies/ResetRetryProcess/execute.php | 36 ++++---- .../enqueueAdminCpcssHeartbeatScript.php | 17 ++-- .../CriticalCSS/processHandler.php | 10 ++- .../DataManager/getCacheJobId.php | 16 +--- .../checkOnUpdateOptions.php | 2 +- .../maybeRecreateAsTables.php | 2 +- .../License/API/UserClient/getUserData.php | 4 +- .../GoogleFonts/Subscriber/preconnect.php | 26 +++--- .../Minify/CSS/AdminSubscriber/onUpdate.php | 49 +++++------ .../Admin/Database/dropResourcesTable.php | 2 +- .../RUCSS/Controller/UsedCSS/UsedCSSTrait.php | 88 ------------------- .../addPluginsToResult.php | 2 + .../Preload/Cron/Subscriber/addInterval.php | 47 +++++----- .../scheduleCleanNotCommonlyUsedRows.php | 26 +++--- .../Cron/Subscriber/schedulePendingJobs.php | 21 ++--- .../scheduleRevertOldFailedRows.php | 1 - .../HostSubscriberFactory/getSubscriber.php | 19 ++-- .../Themes/Divi/handleSaveTemplate.php | 2 +- 39 files changed, 249 insertions(+), 398 deletions(-) delete mode 100644 tests/Unit/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/UsedCSSTrait.php diff --git a/composer.json b/composer.json index a8df0ada84..ae50c7ae2b 100644 --- a/composer.json +++ b/composer.json @@ -54,9 +54,10 @@ "mikey179/vfsstream": "1.6.11", "mnsami/composer-custom-directory-installer": "^2.0", "mobiledetect/mobiledetectlib": "^2.8", + "php-stubs/wordpress-tests-stubs": "^6.5", "phpcompatibility/phpcompatibility-wp": "^2.0", "phpstan/extension-installer": "^1.3", - "phpstan/phpstan": "^1.8", + "phpstan/phpstan-phpunit": "^1.4", "phpunit/phpunit": "^7.5 || ^8 || ^9", "psr/container": "1.1.1", "roave/security-advisories": "dev-master", @@ -74,6 +75,7 @@ "wpackagist-plugin/pdf-embedder": "4.6.*", "wpackagist-plugin/simple-custom-css": "^4.0.3", "wpackagist-plugin/spinupwp": "^1.1", + "wpackagist-plugin/the-events-calendar": "6.5.0.1", "wpackagist-plugin/woocommerce": "^8", "wpackagist-plugin/wp-smushit": "^3" }, diff --git a/inc/ThirdParty/Hostings/Godaddy.php b/inc/ThirdParty/Hostings/Godaddy.php index 0876e85b09..e4601b57b3 100644 --- a/inc/ThirdParty/Hostings/Godaddy.php +++ b/inc/ThirdParty/Hostings/Godaddy.php @@ -22,7 +22,6 @@ class Godaddy implements Subscriber_Interface { * @param string $vip_url Godaddy vip url. */ public function __construct( $vip_url = '' ) { - // @phpstan-ignore-next-line $this->vip_url = method_exists( '\WPaas\Plugin', 'vip' ) ? \WPaas\Plugin::vip() : $vip_url; } diff --git a/inc/ThirdParty/Hostings/Pressidium.php b/inc/ThirdParty/Hostings/Pressidium.php index 5fd03ad8f2..a248b9e0ed 100644 --- a/inc/ThirdParty/Hostings/Pressidium.php +++ b/inc/ThirdParty/Hostings/Pressidium.php @@ -73,7 +73,6 @@ public function clear_cache_after_pressidium() { * @return void */ public function clean_pressidium() { - // @phpstan-ignore-next-line $plugin = NinukisCaching::get_instance(); $plugin->purgeAllCaches(); } @@ -107,7 +106,6 @@ private function get_paths( $urls ) { * @return void */ private function purge_cache( $paths ) { - // @phpstan-ignore-next-line NinukisCaching::get_instance()->purge_cache( $paths ); } diff --git a/inc/ThirdParty/Hostings/Savvii.php b/inc/ThirdParty/Hostings/Savvii.php index a6f7417546..ead1090b18 100644 --- a/inc/ThirdParty/Hostings/Savvii.php +++ b/inc/ThirdParty/Hostings/Savvii.php @@ -63,18 +63,14 @@ public function varnish_addon_title( $settings ) { public function clear_cache_after_savvii() { if ( ! ( - // @phpstan-ignore-next-line isset( $_REQUEST[ CacheFlusherPlugin::NAME_FLUSH_NOW ] ) && - // @phpstan-ignore-next-line check_admin_referer( CacheFlusherPlugin::NAME_FLUSH_NOW ) ) && ! ( - // @phpstan-ignore-next-line isset( $_REQUEST[ CacheFlusherPlugin::NAME_DOMAINFLUSH_NOW ] ) && - // @phpstan-ignore-next-line check_admin_referer( CacheFlusherPlugin::NAME_DOMAINFLUSH_NOW ) ) ) { diff --git a/inc/ThirdParty/Hostings/WPEngine.php b/inc/ThirdParty/Hostings/WPEngine.php index 216c01fad3..fd195f46f5 100644 --- a/inc/ThirdParty/Hostings/WPEngine.php +++ b/inc/ThirdParty/Hostings/WPEngine.php @@ -57,12 +57,10 @@ public function varnish_addon_title( $settings ) { */ public function clean_wpengine() { if ( method_exists( 'WpeCommon', 'purge_memcached' ) ) { - // @phpstan-ignore-next-line WpeCommon::purge_memcached(); } if ( method_exists( 'WpeCommon', 'purge_varnish_cache' ) ) { - // @phpstan-ignore-next-line WpeCommon::purge_varnish_cache(); } } diff --git a/inc/ThirdParty/Plugins/Ecommerce/WooCommerceSubscriber.php b/inc/ThirdParty/Plugins/Ecommerce/WooCommerceSubscriber.php index 06197b41cf..1e4cdcca3e 100644 --- a/inc/ThirdParty/Plugins/Ecommerce/WooCommerceSubscriber.php +++ b/inc/ThirdParty/Plugins/Ecommerce/WooCommerceSubscriber.php @@ -512,7 +512,6 @@ public function exclude_product_shipping_taxonomy( $excluded_taxonomies ) { * @return bool */ private function product_has_gallery_images() { - // @phpstan-ignore-next-line $product = wc_get_product( get_the_ID() ); if ( empty( $product ) ) { return false; @@ -530,7 +529,6 @@ public function show_empty_product_gallery_with_delayJS() { return; } - // @phpstan-ignore-next-line if ( ! is_product() ) { return; } @@ -562,7 +560,6 @@ public function show_notempty_product_gallery_with_delayJS( $exclusions = [] ): return $exclusions; } - // @phpstan-ignore-next-line if ( ! is_product() ) { return $exclusions; } @@ -616,14 +613,13 @@ public function disallow_rocket_clean_post(): void { */ public function allow_rocket_clean_post( int $product_id ): void { $urls = []; - // @phpstan-ignore-next-line + $category_list = wc_get_product_category_list( $product_id ); if ( preg_match_all( '/]*?\s+)?href=(["\'])(?.*?)\1/i', $category_list, $matches ) ) { $urls = $matches['urls']; } - // @phpstan-ignore-next-line $shop_page = get_permalink( wc_get_page_id( 'shop' ) ); if ( empty( $shop_page ) ) { diff --git a/inc/ThirdParty/Plugins/I18n/TranslatePress.php b/inc/ThirdParty/Plugins/I18n/TranslatePress.php index 9220150fcf..e55cc3b52a 100644 --- a/inc/ThirdParty/Plugins/I18n/TranslatePress.php +++ b/inc/ThirdParty/Plugins/I18n/TranslatePress.php @@ -41,7 +41,7 @@ public static function get_subscribed_events() { * @return string */ public function detect_homepage( $home_url, $url ) { - // @phpstan-ignore-next-line + $translatepress = TRP_Translate_Press::get_trp_instance(); $converter = $translatepress->get_component( 'url_converter' ); @@ -78,7 +78,6 @@ function_exists( 'trp_get_languages' ) * @return array */ public function add_langs_to_admin_bar( $langlinks ) { - // @phpstan-ignore-next-line $translatepress = TRP_Translate_Press::get_trp_instance(); $language_switcher = $translatepress->get_component( 'language_switcher' ); @@ -129,7 +128,6 @@ public function get_active_languages_uri( $urls ) { $home_url = home_url(); - // @phpstan-ignore-next-line $translatepress = TRP_Translate_Press::get_trp_instance(); $settings = $translatepress->get_component( 'settings' ); @@ -159,7 +157,6 @@ public function get_active_languages_codes( $codes ) { $codes = (array) $codes; } - // @phpstan-ignore-next-line $translatepress = TRP_Translate_Press::get_trp_instance(); $settings = $translatepress->get_component( 'settings' ); @@ -189,7 +186,6 @@ public function get_home_url_for_lang( $home_url, $lang ) { return $home_url; } - // @phpstan-ignore-next-line $translatepress = TRP_Translate_Press::get_trp_instance(); $converter = $translatepress->get_component( 'url_converter' ); $settings = $translatepress->get_component( 'settings' ); @@ -228,7 +224,6 @@ public function get_translated_post_urls( $urls, $url, $post_type, $regex ) { $urls = (array) $urls; } - // @phpstan-ignore-next-line $translatepress = TRP_Translate_Press::get_trp_instance(); $settings = $translatepress->get_component( 'settings' ); @@ -254,7 +249,6 @@ public function get_translated_post_urls( $urls, $url, $post_type, $regex ) { * @return void */ public function clear_post_languages( $post_id ) { - // @phpstan-ignore-next-line $translatepress = TRP_Translate_Press::get_trp_instance(); $converter = $translatepress->get_component( 'url_converter' ); @@ -293,7 +287,6 @@ public function clear_post_languages( $post_id ) { * @return void */ public function clear_post_after_updating_translation( $update_strings, $settings ) { - // @phpstan-ignore-next-line $translatepress = TRP_Translate_Press::get_trp_instance(); $converter = $translatepress->get_component( 'url_converter' ); diff --git a/inc/ThirdParty/Plugins/SEO/SEOPress.php b/inc/ThirdParty/Plugins/SEO/SEOPress.php index 8c8c3bb9ca..23e896bebc 100644 --- a/inc/ThirdParty/Plugins/SEO/SEOPress.php +++ b/inc/ThirdParty/Plugins/SEO/SEOPress.php @@ -31,7 +31,6 @@ public static function get_subscribed_events() { return []; } - // @phpstan-ignore-next-line if ( ! method_exists( seopress_get_service( 'SitemapOption' ), 'isEnabled' ) || 1 !== (int) seopress_get_service( 'SitemapOption' )->isEnabled() ) { return []; } diff --git a/inc/ThirdParty/Plugins/SEO/TheSEOFramework.php b/inc/ThirdParty/Plugins/SEO/TheSEOFramework.php index a83539313d..9eeba9f29c 100644 --- a/inc/ThirdParty/Plugins/SEO/TheSEOFramework.php +++ b/inc/ThirdParty/Plugins/SEO/TheSEOFramework.php @@ -66,8 +66,6 @@ public function add_tsf_sitemap_to_preload( $sitemaps ) { // The autoloader in TSF doesn't check for file_exists(). So, use version compare instead to prevent fatal errors. if ( version_compare( rocket_get_constant( 'THE_SEO_FRAMEWORK_VERSION', false ), '4.0', '>=' ) ) { // TSF 4.0+. Expect the class to exist indefinitely. - - // @phpstan-ignore-next-line $sitemap_bridge = Sitemap::get_instance(); foreach ( $sitemap_bridge->get_sitemap_endpoint_list() as $id => $data ) { @@ -80,8 +78,6 @@ public function add_tsf_sitemap_to_preload( $sitemaps ) { } } else { // Deprecated. TSF <4.0. - - // @phpstan-ignore-next-line $sitemaps[] = the_seo_framework()->get_sitemap_xml_url(); } diff --git a/inc/ThirdParty/Plugins/Security/WordFenceCompatibility.php b/inc/ThirdParty/Plugins/Security/WordFenceCompatibility.php index 099dfa3116..fef1762302 100644 --- a/inc/ThirdParty/Plugins/Security/WordFenceCompatibility.php +++ b/inc/ThirdParty/Plugins/Security/WordFenceCompatibility.php @@ -57,8 +57,6 @@ public function pop_old_ip( string $old_rucss_ip ) { } // Update whitelist. - - // @phpstan-ignore-next-line wfConfig::set( 'whitelisted', implode( ',', $whitelist ) ); } @@ -76,8 +74,6 @@ public function pop_ip_from_whitelist() { } // Update whitelist. - - // @phpstan-ignore-next-line wfConfig::set( 'whitelisted', implode( ',', $whitelist ) ); } @@ -89,8 +85,6 @@ public function pop_ip_from_whitelist() { */ private function can_pop_ip( string $ip ) { // Get all whitelists. - - // @phpstan-ignore-next-line $whitelists = wfConfig::get( 'whitelisted', '' ); // Convert to array. @@ -134,7 +128,6 @@ public function whitelist_wordfence_firewall_ips() { } foreach ( $ips as $ip ) { - // @phpstan-ignore-next-line wordfence::whitelistIP( $ip ); } } diff --git a/phpstan.neon.dist b/phpstan.neon.dist index b045c24a64..71ae4fda0b 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,37 +1,48 @@ includes: - - phar://phpstan.phar/conf/bleedingEdge.neon + - phar://phpstan.phar/conf/bleedingEdge.neon parameters: - level: 0 - checkMissingIterableValueType: false - inferPrivatePropertyTypeFromConstructor: true - paths: - # Test only the new architecture for now. - - %currentWorkingDirectory%/inc/Engine/ - - %currentWorkingDirectory%/inc/Addon/ - - %currentWorkingDirectory%/inc/ThirdParty/ - bootstrapFiles: - # Must be first - - %currentWorkingDirectory%/inc/functions/options.php - # Procedural code - - %currentWorkingDirectory%/inc/constants.php - - %currentWorkingDirectory%/inc/common/purge.php - - %currentWorkingDirectory%/inc/API/bypass.php - - %currentWorkingDirectory%/inc/3rd-party/plugins/autoptimize.php - - %currentWorkingDirectory%/inc/admin/ui/notices.php - - %currentWorkingDirectory%/inc/functions/admin.php - - %currentWorkingDirectory%/inc/functions/api.php - - %currentWorkingDirectory%/inc/functions/files.php - - %currentWorkingDirectory%/inc/functions/formatting.php - - %currentWorkingDirectory%/inc/functions/htaccess.php - - %currentWorkingDirectory%/inc/functions/i18n.php - - %currentWorkingDirectory%/inc/functions/posts.php - # Composer autoloader "exclude-from-classmap" - - %currentWorkingDirectory%/inc/classes/class-wp-rocket-requirements-check.php - - %currentWorkingDirectory%/inc/vendors/classes/class-rocket-mobile-detect.php - scanDirectories: - - %currentWorkingDirectory%/inc/Dependencies/ActionScheduler - excludePaths: - - %currentWorkingDirectory%/inc/vendors/ - # These need plugin stubs! - - %currentWorkingDirectory%/inc/classes/subscriber/third-party/ - - %currentWorkingDirectory%/inc/3rd-party/ + level: 0 + inferPrivatePropertyTypeFromConstructor: true + paths: + # Test only the new architecture for now. + - %currentWorkingDirectory%/inc/Engine/ + - %currentWorkingDirectory%/inc/Addon/ + - %currentWorkingDirectory%/inc/ThirdParty/ + - %currentWorkingDirectory%/tests/Integration/ + - %currentWorkingDirectory%/tests/Unit/ + bootstrapFiles: + # Must be first + - %currentWorkingDirectory%/inc/functions/options.php + # Procedural code + - %currentWorkingDirectory%/inc/constants.php + - %currentWorkingDirectory%/inc/common/purge.php + - %currentWorkingDirectory%/inc/API/bypass.php + - %currentWorkingDirectory%/inc/3rd-party/plugins/autoptimize.php + - %currentWorkingDirectory%/inc/admin/ui/notices.php + - %currentWorkingDirectory%/inc/functions/admin.php + - %currentWorkingDirectory%/inc/functions/api.php + - %currentWorkingDirectory%/inc/functions/files.php + - %currentWorkingDirectory%/inc/functions/formatting.php + - %currentWorkingDirectory%/inc/functions/htaccess.php + - %currentWorkingDirectory%/inc/functions/i18n.php + - %currentWorkingDirectory%/inc/functions/posts.php + # Composer autoloader "exclude-from-classmap" + - %currentWorkingDirectory%/inc/classes/class-wp-rocket-requirements-check.php + - %currentWorkingDirectory%/inc/vendors/classes/class-rocket-mobile-detect.php + scanDirectories: + - %currentWorkingDirectory%/inc/Dependencies/ActionScheduler + - %currentWorkingDirectory%/tests/Fixtures/ + - %currentWorkingDirectory%/vendor/antecedent/patchwork/ + - %currentWorkingDirectory%/vendor/wpackagist-plugin/amp/ + - %currentWorkingDirectory%/vendor/wpackagist-plugin/the-events-calendar/ + - %currentWorkingDirectory%/vendor/wpackagist-plugin/woocommerce/ + scanFiles: + - %currentWorkingDirectory%/inc/admin/upgrader.php + - %currentWorkingDirectory%/inc/admin/options.php + - %currentWorkingDirectory%/vendor/php-stubs/wordpress-stubs/wordpress-stubs.php + - %currentWorkingDirectory%/vendor/php-stubs/wordpress-tests-stubs/wordpress-tests-stubs.php + excludePaths: + - %currentWorkingDirectory%/inc/vendors/ + # These need plugin stubs! + - %currentWorkingDirectory%/inc/classes/subscriber/third-party/ + - %currentWorkingDirectory%/inc/3rd-party/ diff --git a/tests/Integration/inc/Engine/Optimization/Minify/CSS/AdminSubscriber/onUpdate.php b/tests/Integration/inc/Engine/Optimization/Minify/CSS/AdminSubscriber/onUpdate.php index 1a41de6768..45f8c507f5 100644 --- a/tests/Integration/inc/Engine/Optimization/Minify/CSS/AdminSubscriber/onUpdate.php +++ b/tests/Integration/inc/Engine/Optimization/Minify/CSS/AdminSubscriber/onUpdate.php @@ -2,8 +2,8 @@ namespace WP_Rocket\Tests\Integration\inc\Engine\Optimization\Minify\CSS\AdminSubscriber; -use WP_Rocket\Tests\Integration\TestCase; use Brain\Monkey\Functions; +use WP_Rocket\Tests\Integration\TestCase; /** * Test class covering \WP_Rocket\Engine\Optimization\Minify\CSS\AdminSubscriber::on_update @@ -11,7 +11,7 @@ * @group AdminOnly * @group MinifyAdmin */ -class Test_OnUpdate extends TestCase { +class TestOnUpdate extends TestCase { public function set_up() { parent::set_up(); @@ -19,7 +19,7 @@ public function set_up() { } public function tear_down() { - add_filter( 'wp_rocket_upgrade', 'rocket_new_upgrade' ); + add_filter( 'wp_rocket_upgrade', 'rocket_new_upgrade', 10, 2 ); parent::tear_down(); } diff --git a/tests/Integration/inc/ThirdParty/Hostings/Godaddy/GodaddyTestCase.php b/tests/Integration/inc/ThirdParty/Hostings/Godaddy/GodaddyTestCase.php index 525fb1455b..9054c91b08 100644 --- a/tests/Integration/inc/ThirdParty/Hostings/Godaddy/GodaddyTestCase.php +++ b/tests/Integration/inc/ThirdParty/Hostings/Godaddy/GodaddyTestCase.php @@ -1,5 +1,5 @@ options = $container->get('options'); - $this->options_api = $container->get('options_api'); + + $container = apply_filters( 'rocket_container', null ); + $this->options = $container->get( 'options' ); + $this->options_api = $container->get( 'options_api' ); } /** - * @dataProvider configTestData - */ - public function testShouldDoAsExpected( $config ) - { - Functions\expect('is_plugin_active')->with('')->andReturn($config['plugin_active']); - do_action('enable_cloudflare/cloudflare.php'); - $settings = $this->options_api->get('settings'); - $this->options->set_values($settings); - $this->assertSame(false, $this->options->get('do_cloudflare', false)); - } + * @dataProvider configTestData + */ + public function testShouldDoAsExpected( $config ) { + Functions\expect( 'is_plugin_active' )->andReturn( $config['plugin_active'] ); + + do_action( 'enable_cloudflare/cloudflare.php' ); + + $settings = $this->options_api->get( 'settings' ); + + $this->options->set_values( $settings ); + + $this->assertFalse( $this->options->get( 'do_cloudflare', false ) ); + } } diff --git a/tests/Integration/inc/ThirdParty/Themes/Divi/removeAssetsGenerated.php b/tests/Integration/inc/ThirdParty/Themes/Divi/removeAssetsGenerated.php index 39e23eff0a..60b931bea5 100644 --- a/tests/Integration/inc/ThirdParty/Themes/Divi/removeAssetsGenerated.php +++ b/tests/Integration/inc/ThirdParty/Themes/Divi/removeAssetsGenerated.php @@ -10,7 +10,7 @@ * * @group Themes */ -class Test_RemoveAssetsGenerated extends WPThemeTestcase { +class TestRemoveAssetsGenerated extends WPThemeTestcase { private $container; private $event; private $subscriber; @@ -49,8 +49,9 @@ public function testShouldRemoveCallback( $config, $expected ) { $this->event->add_subscriber( $this->subscriber ); - add_action( 'et_dynamic_late_assets_generated', '__return_true' ); - $this->assertTrue( has_action( 'et_dynamic_late_assets_generated' ) ); + add_filter( 'et_dynamic_late_assets_generated', '__return_true' ); + + $this->assertTrue( has_filter( 'et_dynamic_late_assets_generated' ) ); switch_theme( $config['stylesheet'] ); diff --git a/tests/Integration/inc/ThirdParty/Themes/Shoptimizer/excludeJqueryDeferjsWithCartDrawer.php b/tests/Integration/inc/ThirdParty/Themes/Shoptimizer/excludeJqueryDeferjsWithCartDrawer.php index 5a20ca42a2..36fa562b22 100644 --- a/tests/Integration/inc/ThirdParty/Themes/Shoptimizer/excludeJqueryDeferjsWithCartDrawer.php +++ b/tests/Integration/inc/ThirdParty/Themes/Shoptimizer/excludeJqueryDeferjsWithCartDrawer.php @@ -11,7 +11,7 @@ * * @group Themes */ -class Test_excludeJqueryDeferjsWithCartDrawer extends TestCase { +class TestExcludeJqueryDeferjsWithCartDrawer extends TestCase { private $container; private $event; private $subscriber; @@ -20,7 +20,7 @@ public function set_up() { parent::set_up(); $this->container = apply_filters( 'rocket_container', '' ); - $this->event = $this->container->get( 'event_manager' ); + $this->event = $this->container->get( 'event_manager' ); } public function tear_down() { @@ -33,7 +33,8 @@ public function tear_down() { * @dataProvider configTestData */ public function testShouldReturnAsExpected( $config, $expected ) { - $this->subscriber = new Shoptimizer( $this->container->get( 'options' ) ); + $this->subscriber = new Shoptimizer(); + $this->event->add_subscriber( $this->subscriber ); Functions\when( 'shoptimizer_get_option' )->justReturn( $config['option'] ); diff --git a/tests/Integration/inc/admin/rocketAfterSaveOptions.php b/tests/Integration/inc/admin/rocketAfterSaveOptions.php index 2acedd07f8..a937a50e3f 100644 --- a/tests/Integration/inc/admin/rocketAfterSaveOptions.php +++ b/tests/Integration/inc/admin/rocketAfterSaveOptions.php @@ -227,30 +227,4 @@ private function set_transient() { Functions\expect( 'set_transient' )->with( 'rocket_analytics_optin', 1 )->never(); } } - - private function silently_update_option( $new_value ) { - global $wp_filter; - - $hooks = [ - 'pre_update_option_wp_rocket_settings', - 'pre_update_option', - 'default_option_wp_rocket_settings', - 'update_option', - 'update_option_wp_rocket_settings', - 'updated_option', - ]; - - foreach ( $hooks as $hook ) { - if ( ! empty( $wp_filter[ $hook ] ) ) { - $this->hooks[ $hook ] = $wp_filter[ $hook ]; - unset( $wp_filter[ $hook ] ); - } - } - - update_option( $this->option_name, $new_value ); - - if ( $this->hooks ) { - $wp_filter = array_merge( $wp_filter, $this->hooks ); - } - } } diff --git a/tests/Integration/inc/admin/rocketFirstInstall.php b/tests/Integration/inc/admin/rocketFirstInstall.php index 8d2ae0b785..890be4e426 100644 --- a/tests/Integration/inc/admin/rocketFirstInstall.php +++ b/tests/Integration/inc/admin/rocketFirstInstall.php @@ -6,6 +6,7 @@ /** * Test class covering ::rocket_first_install + * * @group admin * @group upgrade * @group AdminOnly @@ -111,8 +112,4 @@ public function testShouldAddOption( $expected ) { $this->assertContains( $this->box_name, $user_boxes ); $this->assertNotSame( 'foobar', get_transient( $this->box_name ) ); } - - public function addProvider() { - return $this->getTestData( __DIR__, 'rocketFirstInstall' ); - } } diff --git a/tests/Unit/inc/Engine/Cache/Config/ConfigSubscriber/changeCacheRejectUriWithPermalink.php b/tests/Unit/inc/Engine/Cache/Config/ConfigSubscriber/changeCacheRejectUriWithPermalink.php index 8e0a2ea54b..ec74cf76d2 100644 --- a/tests/Unit/inc/Engine/Cache/Config/ConfigSubscriber/changeCacheRejectUriWithPermalink.php +++ b/tests/Unit/inc/Engine/Cache/Config/ConfigSubscriber/changeCacheRejectUriWithPermalink.php @@ -2,22 +2,24 @@ namespace WP_Rocket\Tests\Unit\inc\Engine\Cache\Config\ConfigSubscriber; -use Mockery; -use WP_Rocket\Tests\Unit\TestCase; use Brain\Monkey\Functions; +use Mockery; use WP_Rocket\Admin\Options_Data; use WP_Rocket\Admin\Options; use WP_Rocket\Engine\Cache\Config\ConfigSubscriber; +use WP_Rocket\Tests\Unit\TestCase; /** * Test class covering \WP_Rocket\Engine\Cache\Config\ConfigSubscriber::change_cache_reject_uri_with_permalink + * + * @group Config */ class Test_ChangeCacheRejectUriWithPermalink extends TestCase { - private $config_subscriber; + private $config_subscriber; - public function setUp() : void { + public function setUp(): void { parent::setUp(); - + $options_data = Mockery::mock( Options_Data::class ); $options = Mockery::mock( Options::class ); $this->config_subscriber = new ConfigSubscriber( $options_data, $options ); @@ -31,26 +33,25 @@ public function testShouldReturnExpected( $config, $expected ) { if ( isset( $config['permalink'] ) ) { $times = 2; + $maybe_trailing_slash = $config['permalink']['trailing_slash'] ? '/' : ''; $return_values[] = $config['value']['cache_reject_uri'][0] . $maybe_trailing_slash; if ( false !== strpos( $config['value']['cache_reject_uri'][1], 'index.php' ) || '/' === $config['value']['cache_reject_uri'][1] ) { - unset( $return_values[1] ); $times = 1; - } - else{ + } else { $return_values[] = $config['value']['cache_reject_uri'][1] . $maybe_trailing_slash; } - Functions\expect('user_trailingslashit') - ->times($times) + Functions\expect( 'user_trailingslashit' ) + ->times( $times ) ->andReturnValues( $return_values ); } - $this->assertSame( + $this->assertSame( $expected, - $this->config_subscriber->change_cache_reject_uri_with_permalink( $config['value'], $config['old_value'] ) + $this->config_subscriber->change_cache_reject_uri_with_permalink( $config['value'], $config['old_value'] ) ); } } diff --git a/tests/Unit/inc/Engine/Cache/Purge/purgeDatesArchives.php b/tests/Unit/inc/Engine/Cache/Purge/purgeDatesArchives.php index f9634c7e03..3c37ff60f8 100644 --- a/tests/Unit/inc/Engine/Cache/Purge/purgeDatesArchives.php +++ b/tests/Unit/inc/Engine/Cache/Purge/purgeDatesArchives.php @@ -2,23 +2,24 @@ namespace WP_Rocket\Tests\Unit\inc\Engine\Cache\Purge; -use Mockery; use Brain\Monkey\Functions; use WP_Rocket\Engine\Cache\Purge; -use WP_Rocket\Tests\Unit\FilesystemTestCase; use WP_Rocket\Engine\Preload\Database\Queries\Cache; +use WP_Rocket\Tests\Unit\FilesystemTestCase; /** * Test class covering \WP_Rocket\Engine\Cache\Purge::purge_dates_archives - * @group purge_actions + * + * @group purge_actions */ -class Test_PurgeDatesArchives extends FilesystemTestCase { +class TestPurgeDatesArchives extends FilesystemTestCase { + private $purge; protected $path_to_test_data = '/inc/Engine/Cache/Purge/purgeDatesArchives.php'; protected function setUp(): void { parent::setUp(); - - $query = $this->createPartialMock(Cache::class, ['query']); + + $query = $this->createPartialMock( Cache::class, [ 'query' ] ); $this->purge = new Purge( $this->filesystem, $query ); } @@ -56,13 +57,11 @@ public function testShouldCleanCache( $post, $cleaned ) { ->with( $date[0], $date[1], $date[2] ) ->andReturn( "http://example.org/{$date[0]}/{$date[1]}/{$date[2]}/" ); - Functions\when( 'wp_parse_url' )->alias( function( $url, $component = -1 ) { - return parse_url( $url, $component ); - } ); + $this->stubWpParseUrl(); $this->purge->purge_dates_archives( $post ); $this->checkEntriesDeleted( $cleaned ); $this->checkShouldNotDeleteEntries(); - } + } } diff --git a/tests/Unit/inc/Engine/Cache/Purge/purgePostTermsUrls.php b/tests/Unit/inc/Engine/Cache/Purge/purgePostTermsUrls.php index 708d21bceb..b95b242715 100644 --- a/tests/Unit/inc/Engine/Cache/Purge/purgePostTermsUrls.php +++ b/tests/Unit/inc/Engine/Cache/Purge/purgePostTermsUrls.php @@ -2,27 +2,29 @@ namespace WP_Rocket\Tests\Unit\inc\Engine\Cache\Purge; -use Brain\Monkey\Filters; -use Brain\Monkey\Functions; +use Brain\Monkey\{Filters, Functions}; use Mockery; +use WP_Post; use WP_Rocket\Engine\Cache\Purge; use WP_Rocket\Engine\Preload\Database\Queries\Cache; -use WP_Post; use WP_Rocket\Tests\Unit\FilesystemTestCase; use WP_Term; /** * Test class covering \WP_Rocket\Engine\Cache\Purge::purge_post_terms_urls - * @group Purge - * @group purge_actions + * + * @group Purge + * @group purge_actions */ class Test_PurgePostTermsUrls extends FilesystemTestCase { + private $purge; + protected $path_to_test_data = '/inc/Engine/Cache/Purge/purgePostTermsUrls.php'; public function setUp(): void { parent::setUp(); - $query = $this->createPartialMock(Cache::class, ['query']); + $query = $this->createPartialMock( Cache::class, [ 'query' ] ); $this->purge = new Purge( $this->filesystem, $query ); } @@ -53,7 +55,8 @@ public function testShouldReturnExpectedUrls( $post_data, $terms, $expected ) { ->andReturn( $taxonomies ); Filters\expectApplied( 'rocket_exclude_post_taxonomy' ) ->once(); - $urls = []; + + $urls = []; $index = 0; foreach ( $taxonomies as $type => $taxonomy ) { if ( ! $taxonomy->public ) { @@ -119,9 +122,7 @@ public function testShouldReturnExpectedUrls( $post_data, $terms, $expected ) { } Filters\expectApplied( 'rocket_post_terms_urls', $expected )->once(); - Functions\when( 'wp_parse_url' )->alias( function( $url, $component = -1 ) { - return parse_url( $url, $component ); - } ); + $this->stubWpParseUrl(); $GLOBALS['wp_rewrite'] = (object) [ 'pagination_base' => 'page' ]; @@ -158,7 +159,7 @@ private function getTerms( $type, $config ) { } foreach ( $config as $term ) { - $terms[] = (object) [ 'term_id' => rand(1, 100), 'slug' => isset( $term['name'] ) ? $term['name'] : $term['slug'], 'parent' => isset( $term['parent']) ? $term['parent'] : null ]; + $terms[] = (object) [ 'term_id' => rand( 1, 100 ), 'slug' => isset( $term['name'] ) ? $term['name'] : $term['slug'], 'parent' => isset( $term['parent']) ? $term['parent'] : null ]; } return $terms; diff --git a/tests/Unit/inc/Engine/Common/JobManager/Strategy/Strategies/ResetRetryProcess/execute.php b/tests/Unit/inc/Engine/Common/JobManager/Strategy/Strategies/ResetRetryProcess/execute.php index 24b1a36f7a..33246382ca 100644 --- a/tests/Unit/inc/Engine/Common/JobManager/Strategy/Strategies/ResetRetryProcess/execute.php +++ b/tests/Unit/inc/Engine/Common/JobManager/Strategy/Strategies/ResetRetryProcess/execute.php @@ -1,38 +1,38 @@ manager = Mockery::mock( Manager::class ); - $this->strategy = new ResetRetryProcess($this->manager); - } - - public function tearDown(): void { - parent::tearDown(); + $this->manager = Mockery::mock( Manager::class ); + $this->strategy = new ResetRetryProcess( $this->manager ); } /** * @dataProvider configTestData */ - public function testShouldBehaveAsExpected( $config, $expected ) - { + public function testShouldBehaveAsExpected( $config, $expected ) { $this->manager->shouldReceive( 'add_url_to_the_queue' ) ->once() - ->withArgs([$config['row_details']->url,$config['row_details']->is_mobile]); - - $this->strategy->execute($config['row_details'], $config['job_details']); + ->withArgs( + [ + $config['row_details']->url, + $config['row_details']->is_mobile, + ] + ); + + $this->strategy->execute( $config['row_details'], $config['job_details'] ); } } diff --git a/tests/Unit/inc/Engine/CriticalPath/Admin/Admin/enqueueAdminCpcssHeartbeatScript.php b/tests/Unit/inc/Engine/CriticalPath/Admin/Admin/enqueueAdminCpcssHeartbeatScript.php index 1872e70a61..781ddd90f0 100644 --- a/tests/Unit/inc/Engine/CriticalPath/Admin/Admin/enqueueAdminCpcssHeartbeatScript.php +++ b/tests/Unit/inc/Engine/CriticalPath/Admin/Admin/enqueueAdminCpcssHeartbeatScript.php @@ -4,22 +4,25 @@ use Brain\Monkey\Functions; use Mockery; -use WP_Rocket\Engine\CriticalPath\ProcessorService; use WP_Rocket\Engine\CriticalPath\Admin\Admin; -use WP_Rocket\Tests\Unit\TestCase; +use WP_Rocket\Engine\CriticalPath\ProcessorService; use WP_Rocket\Tests\Unit\inc\Engine\CriticalPath\Admin\AdminTrait; +use WP_Rocket\Tests\Unit\TestCase; /** * Test class covering \WP_Rocket\Engine\CriticalPath\Admin\Admin::enqueue_admin_cpcss_heartbeat_script - * @uses ::rocket_get_constant * - * @group CriticalPath - * @group CriticalPathAdmin + * @uses :rocket_get_constant + * + * @group CriticalPath + * @group CriticalPathAdmin */ -class Test_EnqueueAdminCpcssHeartbeatScript extends TestCase { +class TestEnqueueAdminCpcssHeartbeatScript extends TestCase { use AdminTrait; - public function setUp() : void { + private $admin; + + public function setUp(): void { parent::setUp(); $this->setUpMocks(); diff --git a/tests/Unit/inc/Engine/CriticalPath/CriticalCSS/processHandler.php b/tests/Unit/inc/Engine/CriticalPath/CriticalCSS/processHandler.php index b35c353271..71c548dbf9 100644 --- a/tests/Unit/inc/Engine/CriticalPath/CriticalCSS/processHandler.php +++ b/tests/Unit/inc/Engine/CriticalPath/CriticalCSS/processHandler.php @@ -14,10 +14,10 @@ /** * Test class covering \WP_Rocket\Engine\CriticalPath\CriticalCSS::process_handler * - * @group CriticalCss - * @group CriticalPath + * @group CriticalCss + * @group CriticalPath */ -class Test_ProcessHandler extends FilesystemTestCase { +class TestProcessHandler extends FilesystemTestCase { protected $path_to_test_data = '/inc/Engine/CriticalPath/CriticalCSS/processHandler.php'; private $critical_css; @@ -45,7 +45,9 @@ protected function setUp(): void { Functions\when( 'home_url' )->justReturn( 'http://example.org/' ); Functions\when( 'get_current_blog_id' )->justReturn( 1 ); - $GLOBALS['wpdb'] = $this->wpdb = new wpdb(); + $this->wpdb = new wpdb( 'dbuser', 'dbpassword', 'dbname', 'dbhost' ); + + $GLOBALS['wpdb'] = $this->wpdb; } protected function tearDown(): void { diff --git a/tests/Unit/inc/Engine/CriticalPath/DataManager/getCacheJobId.php b/tests/Unit/inc/Engine/CriticalPath/DataManager/getCacheJobId.php index 4cfeb3cbe2..cb7df674c6 100644 --- a/tests/Unit/inc/Engine/CriticalPath/DataManager/getCacheJobId.php +++ b/tests/Unit/inc/Engine/CriticalPath/DataManager/getCacheJobId.php @@ -9,11 +9,11 @@ /** * Test class covering \WP_Rocket\Engine\CriticalPath\DataManager::get_cache_job_id * - * @group CriticalPath + * @group CriticalPath */ -class Test_GetCacheJobId extends TestCase { +class TestGetCacheJobId extends TestCase { - protected function setUp() : void { + protected function setUp(): void { parent::setUp(); Functions\when( 'get_current_blog_id' )->justReturn( 1 ); @@ -32,15 +32,5 @@ public function testShouldDoExpected( $item_url, $expected, $is_mobile = false ) $actual = $data_manager->get_cache_job_id( $item_url, $is_mobile ); $this->assertSame( $expected, $actual ); - } - - public function nonMultisiteTestData() { - if ( empty( $this->config ) ) { - $this->loadConfig(); - } - - return $this->config['test_data']['non_multisite']; - } - } diff --git a/tests/Unit/inc/Engine/HealthCheck/ActionSchedulerCheck/checkOnUpdateOptions.php b/tests/Unit/inc/Engine/HealthCheck/ActionSchedulerCheck/checkOnUpdateOptions.php index 2e6850f258..c48e4939c0 100644 --- a/tests/Unit/inc/Engine/HealthCheck/ActionSchedulerCheck/checkOnUpdateOptions.php +++ b/tests/Unit/inc/Engine/HealthCheck/ActionSchedulerCheck/checkOnUpdateOptions.php @@ -30,7 +30,7 @@ protected function setUp(): void { $this->as_check = new ActionSchedulerCheck(); - $GLOBALS['wpdb'] = $this->wpdb = new wpdb(); + $GLOBALS['wpdb'] = $this->wpdb = new wpdb( 'dbuser', 'dbpassword', 'dbname', 'dbhost' ); } protected function tearDown(): void { diff --git a/tests/Unit/inc/Engine/HealthCheck/ActionSchedulerCheck/maybeRecreateAsTables.php b/tests/Unit/inc/Engine/HealthCheck/ActionSchedulerCheck/maybeRecreateAsTables.php index 55e0a57db4..6364e9b564 100644 --- a/tests/Unit/inc/Engine/HealthCheck/ActionSchedulerCheck/maybeRecreateAsTables.php +++ b/tests/Unit/inc/Engine/HealthCheck/ActionSchedulerCheck/maybeRecreateAsTables.php @@ -28,7 +28,7 @@ protected function setUp(): void { $this->as_check = new ActionSchedulerCheck(); - $GLOBALS['wpdb'] = $this->wpdb = new wpdb(); + $GLOBALS['wpdb'] = $this->wpdb = new wpdb( 'dbuser', 'dbpassword', 'dbname', 'dbhost' ); } protected function tearDown(): void { diff --git a/tests/Unit/inc/Engine/License/API/UserClient/getUserData.php b/tests/Unit/inc/Engine/License/API/UserClient/getUserData.php index d9e471aae5..bcf4f1559f 100644 --- a/tests/Unit/inc/Engine/License/API/UserClient/getUserData.php +++ b/tests/Unit/inc/Engine/License/API/UserClient/getUserData.php @@ -22,13 +22,15 @@ class GetPricingData extends TestCase { private $client; private $options; - public static function setUpBeforeClass() : void { + public static function setUpBeforeClass(): void { parent::setUpBeforeClass(); self::pathToApiCredentialsConfigFile( WP_ROCKET_TESTS_DIR . '/../env/local/' ); } public function setUp(): void { + parent::setUp(); + $this->options = Mockery::mock( Options_Data::class ); $this->client = new UserClient( $this->options ); } diff --git a/tests/Unit/inc/Engine/Optimization/GoogleFonts/Subscriber/preconnect.php b/tests/Unit/inc/Engine/Optimization/GoogleFonts/Subscriber/preconnect.php index c4ea54015b..5cf9476e1e 100644 --- a/tests/Unit/inc/Engine/Optimization/GoogleFonts/Subscriber/preconnect.php +++ b/tests/Unit/inc/Engine/Optimization/GoogleFonts/Subscriber/preconnect.php @@ -4,27 +4,30 @@ use Mockery; use Brain\Monkey\Functions; -use WPMedia\PHPUnit\Unit\TestCase; use WP_Rocket\Admin\Options_Data; use WP_Rocket\Engine\Optimization\GoogleFonts\Combine; use WP_Rocket\Engine\Optimization\GoogleFonts\CombineV2; use WP_Rocket\Engine\Optimization\GoogleFonts\Subscriber; +use WP_Rocket\Tests\Unit\TestCase; /** * Test class covering \WP_Rocket\Engine\Optimization\GoogleFonts\Subscriber::preconnect + * * @group GoogleFonts */ class Test_Preconnect extends TestCase { private $options; private $subscriber; - public function setUp() : void { + public function setUp(): void { + parent::setUp(); + $this->options = Mockery::mock( Options_Data::class ); $this->subscriber = new Subscriber( Mockery::mock( Combine::class ), Mockery::mock( CombineV2::class ), $this->options ); } /** - * @dataProvider providerTestData + * @dataProvider configTestData */ public function testShouldReturnExpectedArray( $bypass, $option_value, $urls, $relation_type, $user_logged_in, $cache_logged_user, $expected ) { Functions\when( 'rocket_bypass' )->justReturn( $bypass ); @@ -35,12 +38,12 @@ public function testShouldReturnExpectedArray( $bypass, $option_value, $urls, $r ->once() ->andReturn( $option_value ); - if ( $user_logged_in ) { - $this->options->shouldReceive( 'get' ) - ->once() - ->with( 'cache_logged_user', 0 ) - ->andReturn( $cache_logged_user ); - } + if ( $user_logged_in ) { + $this->options->shouldReceive( 'get' ) + ->once() + ->with( 'cache_logged_user', 0 ) + ->andReturn( $cache_logged_user ); + } Functions\when( 'is_user_logged_in' )->justReturn( $user_logged_in ); } else { @@ -48,14 +51,9 @@ public function testShouldReturnExpectedArray( $bypass, $option_value, $urls, $r ->never(); } - $this->assertSame( $expected, $this->subscriber->preconnect( $urls, $relation_type ) ); } - - public function providerTestData() { - return $this->getTestData( __DIR__, 'preconnect' ); - } } diff --git a/tests/Unit/inc/Engine/Optimization/Minify/CSS/AdminSubscriber/onUpdate.php b/tests/Unit/inc/Engine/Optimization/Minify/CSS/AdminSubscriber/onUpdate.php index a9cf7679ec..75c772c49c 100644 --- a/tests/Unit/inc/Engine/Optimization/Minify/CSS/AdminSubscriber/onUpdate.php +++ b/tests/Unit/inc/Engine/Optimization/Minify/CSS/AdminSubscriber/onUpdate.php @@ -3,40 +3,37 @@ namespace WP_Rocket\Tests\Unit\inc\Engine\Optimization\Minify\CSS\AdminSubscriber; use Brain\Monkey\Functions; -use Mockery; -use WP_Filesystem_Direct; -use WP_Rocket\Admin\Options_Data; use WP_Rocket\Engine\Optimization\Minify\CSS\AdminSubscriber; -use WP_Rocket\Engine\Optimization\Minify\ProcessorInterface; use WP_Rocket\Tests\Unit\TestCase; -use function Brain\Monkey\Functions; - /** * Test class covering \WP_Rocket\Engine\Optimization\Minify\CSS\AdminSubscriber::on_update + * + * @group AdminOnly + * @group Minify */ -class Test_onUpdate extends TestCase { +class TestOnUpdate extends TestCase { + /** + * @var AdminSubscriber + */ + protected $subscriber; - /** - * @var AdminSubscriber - */ - protected $subscriber; + public function setUp(): void { + parent::setUp(); - public function set_up() { - parent::set_up(); - $this->subscriber = new AdminSubscriber(); - } + $this->subscriber = new AdminSubscriber(); + } - /** - * @dataProvider configTestData - */ - public function testShouldDoAsExpected( $config, $expected ) - { - if($expected) { - Functions\expect('rocket_clean_domain'); - }else { - Functions\expect('rocket_clean_domain')->never(); + /** + * @dataProvider configTestData + */ + public function testShouldDoAsExpected( $config, $expected ) { + if ( $expected ) { + Functions\expect( 'rocket_clean_domain' ); + } else { + Functions\expect( 'rocket_clean_domain' )->never(); } - $this->subscriber->on_update('3.15', $config['old_version']); - } + + $this->subscriber->on_update( '3.15', $config['old_version'] ); + } } diff --git a/tests/Unit/inc/Engine/Optimization/RUCSS/Admin/Database/dropResourcesTable.php b/tests/Unit/inc/Engine/Optimization/RUCSS/Admin/Database/dropResourcesTable.php index 549c3cc052..3e82c62031 100644 --- a/tests/Unit/inc/Engine/Optimization/RUCSS/Admin/Database/dropResourcesTable.php +++ b/tests/Unit/inc/Engine/Optimization/RUCSS/Admin/Database/dropResourcesTable.php @@ -31,7 +31,7 @@ protected function setUp(): void { return; } - $GLOBALS['wpdb'] = new wpdb(); + $GLOBALS['wpdb'] = new wpdb( 'dbuser', 'dbpassword', 'dbname', 'dbhost' ); $this->usedCSS = $this->getMockBuilder( UsedCSS::class ) ->disableOriginalConstructor() diff --git a/tests/Unit/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/UsedCSSTrait.php b/tests/Unit/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/UsedCSSTrait.php deleted file mode 100644 index a0bf9b4c59..0000000000 --- a/tests/Unit/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/UsedCSSTrait.php +++ /dev/null @@ -1,88 +0,0 @@ -options = Mockery::mock(Options_Data::class); - $this->used_css_query = $this->createMock(UsedCSS_Query::class); - $this->api = Mockery::mock(APIClient::class); - $this->queue = Mockery::mock(QueueInterface::class); - $this->data_manager = Mockery::mock(DataManager::class); - $this->filesystem = Mockery::mock(Filesystem::class); - $this->database = Mockery::mock(Database::class); - $this->context = Mockery::mock(ContextInterface::class); - $this->optimisedContext = Mockery::mock(ContextInterface::class); - $this->strategy_factory = Mockery::mock(StrategyFactory::class); - $this->clock = Mockery::mock(WPRClock::class); - - $this->usedcss = new UsedCSS($this->options, $this->used_css_query, $this->api, $this->queue, $this->data_manager, $this->filesystem, $this->context, $this->optimisedContext, $this->strategy_factory, $this->clock); - $this->set_logger($this->usedcss); - } -} diff --git a/tests/Unit/inc/Engine/Plugin/InformationSubscriber/addPluginsToResult.php b/tests/Unit/inc/Engine/Plugin/InformationSubscriber/addPluginsToResult.php index 24eb36b5fe..217a58bb8c 100644 --- a/tests/Unit/inc/Engine/Plugin/InformationSubscriber/addPluginsToResult.php +++ b/tests/Unit/inc/Engine/Plugin/InformationSubscriber/addPluginsToResult.php @@ -15,6 +15,8 @@ class TestAddPluginsToResult extends TestCase { private $subscriber; protected function setUp(): void { + parent::setUp(); + $this->subscriber = new InformationSubscriber( [ 'plugin_file' => 'wp-rocket/wp-rocket.php', diff --git a/tests/Unit/inc/Engine/Preload/Cron/Subscriber/addInterval.php b/tests/Unit/inc/Engine/Preload/Cron/Subscriber/addInterval.php index f522b207bf..9725e829a1 100644 --- a/tests/Unit/inc/Engine/Preload/Cron/Subscriber/addInterval.php +++ b/tests/Unit/inc/Engine/Preload/Cron/Subscriber/addInterval.php @@ -2,14 +2,13 @@ namespace WP_Rocket\Tests\Unit\inc\Engine\Preload\Cron\Subscriber; +use Brain\Monkey\Filters; use Mockery; -use WP_Rocket\Engine\Common\Queue\PreloadQueueRunner; use WP_Rocket\Engine\Preload\Admin\Settings; use WP_Rocket\Engine\Preload\Controller\PreloadUrl; use WP_Rocket\Engine\Preload\Cron\Subscriber; use WP_Rocket\Engine\Preload\Database\Queries\Cache; use WP_Rocket\Tests\Unit\TestCase; -use Brain\Monkey\Filters; /** * Test class covering \WP_Rocket\Engine\Preload\Cron\Subscriber::add_interval @@ -17,39 +16,41 @@ * @group Cron * @group Preload */ -class Test_AddInterval extends TestCase -{ - protected $subscriber; - protected $query; - protected $settings; - protected $controller; - protected $queue_runner; - - protected function setUp(): void - { +class TestAddInterval extends TestCase { + private $subscriber; + private $query; + private $settings; + private $controller; + + protected function setUp(): void { parent::setUp(); - $this->query = $this->createMock(Cache::class); - $this->settings = Mockery::mock(Settings::class); - $this->controller = Mockery::mock(PreloadUrl::class); - $this->queue_runner = Mockery::mock(PreloadQueueRunner::class); - $this->subscriber = new Subscriber($this->settings, $this->query, $this->controller, $this->queue_runner); + $this->query = $this->createMock( Cache::class ); + $this->settings = Mockery::mock( Settings::class ); + $this->controller = Mockery::mock( PreloadUrl::class ); + $this->subscriber = new Subscriber( $this->settings, $this->query, $this->controller ); } /** * @dataProvider configTestData */ - public function testShouldDoAsExpected($config, $expected) { + public function testShouldDoAsExpected( $config, $expected ) { $this->stubTranslationFunctions(); - $this->settings->expects()->is_enabled()->andReturn($config['is_enabled']); - $this->assertSame($expected, $this->subscriber->add_interval($config['schedules'])); + $this->settings->expects()->is_enabled()->andReturn( $config['is_enabled'] ); + + $this->assertSame( + $expected, + $this->subscriber->add_interval( $config['schedules'] ) + ); } - public function configureInterval($config) { - if(! $config['is_enabled'] ) { + public function configureInterval( $config ) { + if( ! $config['is_enabled'] ) { return; } - Filters\expectApplied('rocket_preload_pending_jobs_cron_interval')->with()->andReturn($config['filtered_interval']); + + Filters\expectApplied( 'rocket_preload_pending_jobs_cron_interval' ) + ->andReturn( $config['filtered_interval'] ); } } diff --git a/tests/Unit/inc/Engine/Preload/Cron/Subscriber/scheduleCleanNotCommonlyUsedRows.php b/tests/Unit/inc/Engine/Preload/Cron/Subscriber/scheduleCleanNotCommonlyUsedRows.php index 571d0af111..3ea3e2c751 100644 --- a/tests/Unit/inc/Engine/Preload/Cron/Subscriber/scheduleCleanNotCommonlyUsedRows.php +++ b/tests/Unit/inc/Engine/Preload/Cron/Subscriber/scheduleCleanNotCommonlyUsedRows.php @@ -4,7 +4,6 @@ use Brain\Monkey\Functions; use Mockery; -use WP_Rocket\Engine\Common\Queue\PreloadQueueRunner; use WP_Rocket\Engine\Preload\Admin\Settings; use WP_Rocket\Engine\Preload\Controller\PreloadUrl; use WP_Rocket\Engine\Preload\Cron\Subscriber; @@ -17,28 +16,25 @@ * @group Cron * @group Preload */ -class Test_ScheduleCleanNotCommonlyUsedRows extends TestCase { +class TestScheduleCleanNotCommonlyUsedRows extends TestCase { protected $subscriber; protected $query; protected $settings; protected $controller; - protected $queue_runner; - protected function setUp(): void - { + protected function setUp(): void { parent::setUp(); - $this->query = $this->createMock(Cache::class); - $this->settings = Mockery::mock(Settings::class); - $this->controller = Mockery::mock(PreloadUrl::class); - $this->queue_runner = Mockery::mock(PreloadQueueRunner::class); - $this->subscriber = new Subscriber($this->settings, $this->query, $this->controller, $this->queue_runner); + $this->query = $this->createMock( Cache::class ); + $this->settings = Mockery::mock( Settings::class ); + $this->controller = Mockery::mock( PreloadUrl::class ); + $this->subscriber = new Subscriber( $this->settings, $this->query, $this->controller ); } /** * @dataProvider configTestData */ - public function testShouldDoAsExpected($config) { + public function testShouldDoAsExpected( $config ) { Functions\expect( 'wp_next_scheduled' ) ->once() ->with( 'rocket_preload_clean_rows_time_event' ) @@ -47,17 +43,17 @@ public function testShouldDoAsExpected($config) { $old_time = time() + 10 * MINUTE_IN_SECONDS; if ( ! $config['has_next_schedule'] ) { - Functions\expect('wp_schedule_event') + Functions\expect( 'wp_schedule_event' ) ->once() ->with( Mockery::on(function ($date) use ($old_time) { - return $date >= $old_time && $date <= time() + 10 * MINUTE_IN_SECONDS; - }), + return $date >= $old_time && $date <= time() + 10 * MINUTE_IN_SECONDS; + } ), 'weekly', 'rocket_preload_clean_rows_time_event' ); } else { - Functions\expect('wp_schedule_event')->never(); + Functions\expect( 'wp_schedule_event' )->never(); } $this->subscriber->schedule_clean_not_commonly_used_rows(); diff --git a/tests/Unit/inc/Engine/Preload/Cron/Subscriber/schedulePendingJobs.php b/tests/Unit/inc/Engine/Preload/Cron/Subscriber/schedulePendingJobs.php index cd2ab626f8..634555552d 100644 --- a/tests/Unit/inc/Engine/Preload/Cron/Subscriber/schedulePendingJobs.php +++ b/tests/Unit/inc/Engine/Preload/Cron/Subscriber/schedulePendingJobs.php @@ -2,14 +2,13 @@ namespace WP_Rocket\Tests\Unit\inc\Engine\Preload\Cron\Subscriber; +use Brain\Monkey\Functions; use Mockery; -use WP_Rocket\Engine\Common\Queue\PreloadQueueRunner; use WP_Rocket\Engine\Preload\Admin\Settings; use WP_Rocket\Engine\Preload\Controller\PreloadUrl; use WP_Rocket\Engine\Preload\Cron\Subscriber; use WP_Rocket\Engine\Preload\Database\Queries\Cache; use WP_Rocket\Tests\Unit\TestCase; -use Brain\Monkey\Functions; /** * Test class covering \WP_Rocket\Engine\Preload\Cron\Subscriber::schedule_pending_jobs @@ -17,29 +16,25 @@ * @group Cron * @group Preload */ -class Test_SchedulePendingJobs extends TestCase -{ +class TestSchedulePendingJobs extends TestCase { protected $subscriber; protected $query; protected $settings; protected $controller; - protected $queue_runner; - protected function setUp(): void - { + protected function setUp(): void { parent::setUp(); - $this->query = $this->createMock(Cache::class); - $this->settings = Mockery::mock(Settings::class); - $this->controller = Mockery::mock(PreloadUrl::class); - $this->queue_runner = Mockery::mock(PreloadQueueRunner::class); - $this->subscriber = new Subscriber($this->settings, $this->query, $this->controller, $this->queue_runner); + $this->query = $this->createMock( Cache::class ); + $this->settings = Mockery::mock( Settings::class ); + $this->controller = Mockery::mock( PreloadUrl::class ); + $this->subscriber = new Subscriber( $this->settings, $this->query, $this->controller ); } /** * @dataProvider configTestData */ - public function testShouldDoAsExpected($config) { + public function testShouldDoAsExpected( $config ) { $this->settings->shouldReceive('is_enabled')->andReturn($config['is_enabled'])->atLeast()->once(); $this->configureCheckNextSchedule($config); diff --git a/tests/Unit/inc/Engine/Preload/Cron/Subscriber/scheduleRevertOldFailedRows.php b/tests/Unit/inc/Engine/Preload/Cron/Subscriber/scheduleRevertOldFailedRows.php index 6aa240013c..3128e503ea 100644 --- a/tests/Unit/inc/Engine/Preload/Cron/Subscriber/scheduleRevertOldFailedRows.php +++ b/tests/Unit/inc/Engine/Preload/Cron/Subscriber/scheduleRevertOldFailedRows.php @@ -2,7 +2,6 @@ namespace WP_Rocket\Tests\Unit\inc\Engine\Preload\Cron\Subscriber; -use WP_Rocket\Engine\Common\Queue\PreloadQueueRunner; use WP_Rocket\Engine\Preload\Admin\Settings; use WP_Rocket\Engine\Preload\Controller\PreloadUrl; use WP_Rocket\Engine\Preload\Cron\Subscriber; diff --git a/tests/Unit/inc/ThirdParty/Hostings/HostSubscriberFactory/getSubscriber.php b/tests/Unit/inc/ThirdParty/Hostings/HostSubscriberFactory/getSubscriber.php index 06b9b82df1..b7d9389632 100644 --- a/tests/Unit/inc/ThirdParty/Hostings/HostSubscriberFactory/getSubscriber.php +++ b/tests/Unit/inc/ThirdParty/Hostings/HostSubscriberFactory/getSubscriber.php @@ -2,28 +2,22 @@ namespace WP_Rocket\Tests\Unit\inc\ThirdParty\Hostings\HostSubscriberFactory; -use Mockery; -use WP_Rocket\Engine\Cache\AdminSubscriber; -use WP_Rocket\Event_Management\Event_Manager; use WP_Rocket\ThirdParty\Hostings\HostSubscriberFactory; use WP_Rocket\Tests\Unit\TestCase; /** * Test class covering \WP_Rocket\ThirdParty\Hostings\HostSubscriberFactory::get_subscriber * - * @group Hostings - * @group ThirdParty + * @group Hostings + * @group ThirdParty */ -class Test_GetSubscriber extends TestCase { +class TestGetSubscriber extends TestCase { private $factory; protected function setUp(): void { parent::setUp(); - $this->factory = new HostSubscriberFactory( - Mockery::mock( AdminSubscriber::class ), - Mockery::mock( Event_Manager::class ) - ); + $this->factory = new HostSubscriberFactory(); } protected function tearDown(): void { @@ -55,6 +49,9 @@ public function testShouldReturnSubscriber( $host, $expected ) { break; } - $this->assertInstanceOf( $expected, $this->factory->get_subscriber()); + $this->assertInstanceOf( + $expected, + $this->factory->get_subscriber() + ); } } diff --git a/tests/Unit/inc/ThirdParty/Themes/Divi/handleSaveTemplate.php b/tests/Unit/inc/ThirdParty/Themes/Divi/handleSaveTemplate.php index 87ecc8ea6d..966f16e9a0 100644 --- a/tests/Unit/inc/ThirdParty/Themes/Divi/handleSaveTemplate.php +++ b/tests/Unit/inc/ThirdParty/Themes/Divi/handleSaveTemplate.php @@ -28,7 +28,7 @@ public static function setUpBeforeClass(): void { protected function setUp(): void { parent::setUp(); - $GLOBALS['wpdb'] = $this->wpdb = new wpdb(); + $GLOBALS['wpdb'] = $this->wpdb = new wpdb( 'dbuser', 'dbpassword', 'dbname', 'dbhost' ); } protected function tearDown(): void {