Make WordPress Core

Changeset 53948


Ignore:
Timestamp:
08/27/2022 12:30:08 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Explicitly declare all properties created in set_up() methods of various test classes.

Dynamic (non-explicitly declared) properties are deprecated as of PHP 8.2 and are expected to become a fatal error in PHP 9.0.

In each of the cases included in this commit, one or more properties are dynamically created in the set_up() method of the test class. This commit explicitly declares these properties.

As these properties are being declared on test classes, they are marked as private. Even though the original dynamic property was public, this should not be considered a backward compatibility break as this only involves test classes.

Notes:

  • As these properties receive assignments during test runs or a one-time assignment, but the assignment uses a function call or variable access, these properties cannot be changed to class constants, but they should be declared explicitly as properties on the class.
  • In Tests_Theme_CustomHeader, the $customize_manager property is given a default value of null, same as it was already being reset to null in the tear_down() method.
  • In Tests_Privacy_wpPrivacyProcessPersonalDataExportPage and Tests_Privacy_wpPrivacyGeneratePersonalDataExportFile classes, the property name had a leading _ underscore. This is an outdated PHP 4 practice to indicate a private property. As PHP 4 is no longer supported, the property has been renamed to $orig_error_level.
  • Along the same lines, in Tests_Menu_Walker_Nav_Menu, the property name also had a leading _ underscore. The property has been renamed to $orig_wp_nav_menu_max_depth.
  • In the Tests_Shortcode class, three properties were already being (re)set in the set_up() method, while three others were being set for most tests via the shortcode_test_shortcode_tag() method or in the tests themselves. It is ensured now that all six properties are given their initial null value in the set_up() method and are explicitly declared.

Additionally:

  • In the Tests_User_Session class, the set_up() method is incorrect. No assertions should be executed in fixture methods, but the set_up() method contains two assertions. This has not been addressed yet as it is outside the scope of this commit, but should be addressed at a later point in time.

Follow-up to [12/tests], [218/tests], [374/tests], [384/tests], [986/tests], [1106/tests], [1239/tests], [28704], [29221], [29347], [32648], [36519], [37953], [38832], [40142], [40825], [43584], [43768], [44786], [45141], [53557], [53558], [53850], [53851], [53852], [53853], [53854], [53856], [53916], [53935], [53936], [53937], [53938], [53942], [53945].

Props jrf.
See #56033.

Location:
trunk/tests/phpunit/tests
Files:
22 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/canonical/https.php

    r52010 r53948  
    77 */
    88class Tests_Canonical_HTTPS extends WP_Canonical_UnitTestCase {
     9
     10    /**
     11     * Dummy HTTP URL.
     12     *
     13     * @var string
     14     */
     15    private $http = '';
     16
     17    /**
     18     * Dummy HTTPS URL.
     19     *
     20     * @var string
     21     */
     22    private $https = '';
     23
    924    public function set_up() {
    1025        parent::set_up();
  • trunk/tests/phpunit/tests/customize/manager.php

    r53855 r53948  
    3333     */
    3434    protected static $subscriber_user_id;
     35
     36    /**
     37     * Path to test file 1.
     38     *
     39     * @var string
     40     */
     41    private $test_file;
     42
     43    /**
     44     * Path to test file 2.
     45     *
     46     * @var string
     47     */
     48    private $test_file2;
    3549
    3650    /**
  • trunk/tests/phpunit/tests/feed/atom.php

    r53815 r53948  
    1414    public static $category;
    1515
     16    private $post_count;
     17    private $excerpt_only;
     18
    1619    /**
    1720     * Setup a new user and attribute some posts.
  • trunk/tests/phpunit/tests/feed/rss2.php

    r53815 r53948  
    1515    public static $post_date;
    1616
     17    private $post_count;
     18    private $excerpt_only;
     19
    1720    /**
    1821     * Setup a new user and attribute some posts.
  • trunk/tests/phpunit/tests/includes/factory.php

    r52654 r53948  
    22
    33class TestFactoryFor extends WP_UnitTestCase {
     4
     5    /**
     6     * @var WP_UnitTest_Factory_For_Term
     7     */
     8    private $category_factory;
     9
    410    public function set_up() {
    511        parent::set_up();
  • trunk/tests/phpunit/tests/menu/walker-nav-menu-edit.php

    r52010 r53948  
    66 */
    77class Tests_Menu_Walker_Nav_Menu_Edit extends WP_UnitTestCase {
     8
     9    /**
     10     * @var \Walker_Nav_Menu_Edit
     11     */
     12    private $walker;
     13
    814    protected $_wp_nav_menu_max_depth;
    915
  • trunk/tests/phpunit/tests/menu/walker-nav-menu.php

    r52248 r53948  
    1212
    1313    /**
     14     * Original nav menu max depth.
     15     *
     16     * @var int
     17     */
     18    private $orig_wp_nav_menu_max_depth;
     19
     20    /**
    1421     * Setup.
    1522     */
     
    2330        $this->walker = new Walker_Nav_Menu();
    2431
    25         $this->_wp_nav_menu_max_depth = $_wp_nav_menu_max_depth;
     32        $this->orig_wp_nav_menu_max_depth = $_wp_nav_menu_max_depth;
    2633    }
    2734
     
    3239        global $_wp_nav_menu_max_depth;
    3340
    34         $_wp_nav_menu_max_depth = $this->_wp_nav_menu_max_depth;
     41        $_wp_nav_menu_max_depth = $this->orig_wp_nav_menu_max_depth;
    3542        parent::tear_down();
    3643    }
  • trunk/tests/phpunit/tests/meta.php

    r52010 r53948  
    66class Tests_Meta extends WP_UnitTestCase {
    77    protected $updated_mids = array();
     8
     9    /**
     10     * @var \WP_User
     11     */
     12    private $author;
     13
     14    private $meta_id;
     15    private $delete_meta_id;
    816
    917    public function set_up() {
  • trunk/tests/phpunit/tests/pomo/noopTranslations.php

    r52010 r53948  
    55 */
    66class Tests_POMO_NOOPTranslations extends WP_UnitTestCase {
     7
     8    /**
     9     * NOOP translations object.
     10     *
     11     * @var NOOP_Translations
     12     */
     13    private $noop;
     14
     15    /**
     16     * Single translation entry object.
     17     *
     18     * @var Translation_Entry
     19     */
     20    private $entry;
     21
     22    /**
     23     * Multi translation entries object.
     24     *
     25     * @var Translation_Entry
     26     */
     27    private $plural_entry;
     28
    729    public function set_up() {
    830        parent::set_up();
  • trunk/tests/phpunit/tests/post.php

    r53787 r53948  
    1010    protected static $grammarian_id;
    1111
     12    private $post_ids = array();
     13
    1214    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    1315        self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
  • trunk/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php

    r53454 r53948  
    4646
    4747    /**
     48     * Original error level.
     49     *
     50     * @var int
     51     */
     52    private $orig_error_level;
     53
     54    /**
    4855     * Create fixtures that are shared by multiple test cases.
    4956     *
     
    8087
    8188        // Suppress warnings from "Cannot modify header information - headers already sent by".
    82         $this->_error_level = error_reporting();
    83         error_reporting( $this->_error_level & ~E_WARNING );
     89        $this->orig_error_level = error_reporting();
     90        error_reporting( $this->orig_error_level & ~E_WARNING );
    8491    }
    8592
     
    93100    public function tear_down() {
    94101        $this->remove_exports_dir();
    95         error_reporting( $this->_error_level );
     102        error_reporting( $this->orig_error_level );
    96103        parent::tear_down();
    97104    }
  • trunk/tests/phpunit/tests/privacy/wpPrivacyProcessPersonalDataExportPage.php

    r51568 r53948  
    140140     */
    141141    public $_export_data_grouped_fetched_within_callback;
     142
     143    /**
     144     * Original error level.
     145     *
     146     * @var int
     147     */
     148    private $orig_error_level;
    142149
    143150    /**
     
    210217
    211218        // Suppress warnings from "Cannot modify header information - headers already sent by".
    212         $this->_error_level = error_reporting();
    213         error_reporting( $this->_error_level & ~E_WARNING );
     219        $this->orig_error_level = error_reporting();
     220        error_reporting( $this->orig_error_level & ~E_WARNING );
    214221    }
    215222
     
    220227     */
    221228    public function tear_down() {
    222         error_reporting( $this->_error_level );
     229        error_reporting( $this->orig_error_level );
    223230
    224231        parent::tear_down();
  • trunk/tests/phpunit/tests/rest-api/rest-autosaves-controller.php

    r53921 r53948  
    2525    protected static $child_page_id;
    2626    protected static $child_draft_page_id;
     27
     28    private $post_autosave;
    2729
    2830    protected function set_post_data( $args = array() ) {
  • trunk/tests/phpunit/tests/rest-api/rest-controller.php

    r53760 r53948  
    1111 */
    1212class WP_Test_REST_Controller extends WP_Test_REST_TestCase {
     13
     14    /**
     15     * @var WP_REST_Request
     16     */
     17    private $request;
    1318
    1419    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  • trunk/tests/phpunit/tests/rest-api/rest-revisions-controller.php

    r51568 r53948  
    1616    protected static $editor_id;
    1717    protected static $contributor_id;
     18
     19    private $total_revisions;
     20    private $revisions;
     21    private $revision_1;
     22    private $revision_id1;
     23    private $revision_2;
     24    private $revision_id2;
     25    private $revision_3;
     26    private $revision_id3;
    1827
    1928    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  • trunk/tests/phpunit/tests/rest-api/rest-settings-controller.php

    r53921 r53948  
    1414    protected static $administrator;
    1515    protected static $author;
     16
     17    /**
     18     * @var WP_REST_Settings_Controller
     19     */
     20    private $endpoint;
    1621
    1722    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  • trunk/tests/phpunit/tests/rest-api/rest-users-controller.php

    r53936 r53948  
    2525
    2626    protected static $site;
     27
     28    /**
     29     * @var WP_REST_Users_Controller
     30     */
     31    private $endpoint;
    2732
    2833    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  • trunk/tests/phpunit/tests/rewrite/numericSlugs.php

    r52389 r53948  
    77class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase {
    88    private $old_current_user;
     9    private $author_id;
    910
    1011    public function set_up() {
  • trunk/tests/phpunit/tests/shortcode.php

    r52010 r53948  
    77    protected $shortcodes = array( 'test-shortcode-tag', 'footag', 'bartag', 'baztag', 'dumptag', 'hyphen', 'hyphen-foo', 'hyphen-foo-bar', 'url', 'img' );
    88
     9    private $atts    = null;
     10    private $content = null;
     11    private $tagname = null;
     12
     13    private $filter_atts_out   = null;
     14    private $filter_atts_pairs = null;
     15    private $filter_atts_atts  = null;
     16
    917    public function set_up() {
    1018        parent::set_up();
     
    1422        }
    1523
    16         $this->atts    = null;
    17         $this->content = null;
    18         $this->tagname = null;
    19 
     24        $this->atts              = null;
     25        $this->content           = null;
     26        $this->tagname           = null;
     27        $this->filter_atts_out   = null;
     28        $this->filter_atts_pairs = null;
     29        $this->filter_atts_atts  = null;
    2030    }
    2131
  • trunk/tests/phpunit/tests/theme/customHeader.php

    r53741 r53948  
    88
    99    protected static $header_video_id;
     10
     11    private $customize_manager = null;
    1012
    1113    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  • trunk/tests/phpunit/tests/user/session.php

    r52010 r53948  
    77 */
    88class Tests_User_Session extends WP_UnitTestCase {
     9
     10    /**
     11     * @var WP_User_Meta_Session_Tokens
     12     */
     13    private $manager;
    914
    1015    public function set_up() {
  • trunk/tests/phpunit/tests/walker.php

    r52010 r53948  
    99class Tests_Walker extends WP_UnitTestCase {
    1010
     11    /**
     12     * @var Walker
     13     */
     14    private $walker;
     15
    1116    public function set_up() {
    1217        parent::set_up();
Note: See TracChangeset for help on using the changeset viewer.