From 389a78ecc453036f144b6d510cc2423f41bc9dd1 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 2 Oct 2022 15:11:24 +0000 Subject: [PATCH] Build/Test Tools: Call `wpTearDownAfterClass()` before deleting all data, instead of after. As of [35186] and [51568], there are two sets of methods used for setup/teardown in the test suite before and after a test class is run: * `set_up_before_class()` / `tear_down_after_class()` * `wpSetUpBeforeClass()` / `wpTearDownAfterClass()`. (Note the `wp` prefix, these are WordPress' own methods and are not the same as the native PHPUnit `setUpBeforeClass()` / `tearDownAfterClass()` methods.) The main difference is that `wpSetUpBeforeClass()` receives the `$factory` argument for ease of use, and both `wpSetUpBeforeClass()` and `wpTearDownAfterClass()` don't need to call `self::commit_transaction()`. Many tests use the `wpTearDownAfterClass()` method to clean up posts, users, roles, etc. created via `wpSetUpBeforeClass()`. However, due to [source:tags/6.0/tests/phpunit/includes/abstract-testcase.php?marks=88-95#L82 how the method was previously called], this cleanup happened after all data is **already deleted** from the database. This could cause some confusion when refactoring tests. For example: {{{ public static function wpTearDownAfterClass() { $GLOBALS['_wp_additional_image_sizes'] = self::$_sizes; } public static function tear_down_after_class() { wp_delete_attachment( self::$large_id, true ); parent::tear_down_after_class(); } }}} At a glance, it seems like these two methods can be combined: {{{ public static function wpTearDownAfterClass() { wp_delete_attachment( self::$large_id, true ); $GLOBALS['_wp_additional_image_sizes'] = self::$_sizes; } }}} However, that would not work as expected: by the time `wp_delete_attachment()` runs, the attachment ID is no longer in the database, so it returns early, leaving some files in the `uploads` directory. By calling `wpTearDownAfterClass()` in `WP_UnitTestCase_Base::tear_down_after_class()` before deleting all data, instead of after, we ensure that both of these methods have access to the same data and can be used interchangeably to perform cleanup as necessary. Additionally, this commit moves the calls to parent methods in `WP_UnitTestCase_Base`: * `parent::set_up_before_class()` to be the first thing called in `::set_up_before_class()` * `parent::tear_down_after_class()` to be the last thing called in `::tear_down_after_class()` This does not have any effect in practice, but brings consistency with how these methods are called in the test suite. Follow-up to [35186], [35225], [35242], [38398], [39626], [49001], [51568]. Props ironprogrammer, SergeyBiryukov. Fixes #55918. See #55652. git-svn-id: https://develop.svn.wordpress.org/trunk@54366 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/includes/abstract-testcase.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index 1a60c630f1..38f846e541 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -62,13 +62,13 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { public static function set_up_before_class() { global $wpdb; + parent::set_up_before_class(); + $wpdb->suppress_errors = false; $wpdb->show_errors = true; $wpdb->db_connect(); ini_set( 'display_errors', 1 ); - parent::set_up_before_class(); - $class = get_called_class(); if ( method_exists( $class, 'wpSetUpBeforeClass' ) ) { @@ -82,18 +82,18 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { * Runs the routine after all tests have been run. */ public static function tear_down_after_class() { - parent::tear_down_after_class(); - - _delete_all_data(); - self::flush_cache(); - $class = get_called_class(); if ( method_exists( $class, 'wpTearDownAfterClass' ) ) { call_user_func( array( $class, 'wpTearDownAfterClass' ) ); } + _delete_all_data(); + self::flush_cache(); + self::commit_transaction(); + + parent::tear_down_after_class(); } /**