[wp-trac] [WordPress Trac] #51752: Enable the static variable in wp_get_environment_type() to be reset

WordPress Trac noreply at wordpress.org
Tue Nov 10 21:11:05 UTC 2020


#51752: Enable the static variable in wp_get_environment_type() to be reset
----------------------------+-----------------------------
 Reporter:  stevegrunwell   |      Owner:  (none)
     Type:  enhancement     |     Status:  new
 Priority:  normal          |  Milestone:  Awaiting Review
Component:  Bootstrap/Load  |    Version:  5.5
 Severity:  normal          |   Keywords:
  Focuses:                  |
----------------------------+-----------------------------
 While the static `$current_env` variable in `wp_get_environment_type()` is
 a good choice performance-wise (especially since this ''generally'' won't
 change), it's become a pain-point when writing automated tests.

 For example, imagine we have two PHPUnit test classes: `StagingTest` and
 `ProductionTest`, responsible for testing the difference in a plugin's
 behavior in staging and production environments, respectively:

 {{{#!php
 <?php

 /**
  * Tests the behavior in a STAGING environment.
  */
 class StagingTest extends TestCase {

     public function setUp() {
         putenv('WP_ENVIRONMENT_TYPE=staging');

         // Continue the WordPress bootstrapping.
         parent::setUp();
     }

     /**
      * @test
      */
     public function it_should_put_the_plugin_in_staging_mode() {
         $this->assertTrue( myplugin_is_staging_mode() );
     }
 }

 /**
  * Tests the behavior in a PRODUCTION environment.
  */
 class ProductionTest extends TestCase {

     public function setUp() {
         putenv('WP_ENVIRONMENT_TYPE=production');

         // Continue the WordPress bootstrapping.
         parent::setUp();
     }

     /**
      * @test
      */
     public function it_should_not_put_the_plugin_in_staging_mode() {
         $this->assertFalse( myplugin_is_staging_mode() );
     }
 }
 }}}

 Unfortunately, one (or both) of these test classes would need to be run in
 separate PHP processes or risk whichever one ran first being the
 `$current_env` for the duration of the test suite.

 To get around this, I propose a `$use_cache` parameter for the function;
 it would default to `true`, but a `false` value would cause `$current_env`
 variable to be re-calculated.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/51752>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list