-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(WPLoader) add global state control support, fix #673
- Loading branch information
Showing
8 changed files
with
731 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,6 +82,10 @@ When used in this mode, the module supports the following configuration paramete | |
* `WP_HTTP_BLOCK_EXTERNAL` - the `WP_HTTP_BLOCK_EXTERNAL` constant value to use when loading WordPress. If | ||
the `wpRootFolder` path points at a configured installation, containing the `wp-config.php` file, then the value of | ||
the constant in the configuration file will be used, else it will be randomly generated. | ||
* `backupGlobals` - a boolean value to indicate if the global environment should be backed up before each test. Defaults to `true`. The globals' backup involves serialization of the global state, plugins or themes that define classes developed to prevent serialization of the global state will cause the tests to fail. Set this parameter to `false` to disable the global environment backup, or use a more refined approach setting the `backupGlobalsExcludeList` parameter below. Note that a test case that is explicitly setting the `backupGlobals` property will override this configuration parameter. | ||
* `backupGlobalsExcludeList` - a list of global variables to exclude from the global environment backup. The list must be in the form of array, and it will be merged to the list of globals excluded by default. | ||
* `backupStaticAttributes` - a boolean value to indicate if static attributes of classes should be backed up before each test. Defaults to `true`. The static attributes' backup involves serialization of the global state, plugins or themes that define classes developed to prevent serialization of the global state will cause the tests to fail. Set this parameter to `false` to disable the static attributes backup, or use a more refined approanch setting the `backupStaticAttributesExcludeList` parameter below. Note that a test case that is explicitly setting the `backupStaticAttributes` property will override this configuration parameter. | ||
* `backupStaticAttributesExcludeList` - a list of classes to exclude from the static attributes backup. The list must be in the form of map from class names to the array of method names to exclude from the backup. See an example below. | ||
|
||
This is an example of an integration suite configured to use the module: | ||
|
||
|
@@ -153,6 +157,70 @@ modules: | |
theme: twentytwentythree | ||
``` | ||
The follow example configuration prevents the backup of globals and static attributes in all the tests of the suite that are not explicitly overriding the `backupGlobals` and `backupStaticAttributes` properties: | ||
|
||
```yaml | ||
actor: IntegrationTester | ||
bootstrap: _bootstrap.php | ||
modules: | ||
enabled: | ||
- \Helper\Integration | ||
- lucatume\WPBrowser\Module\WPLoader: | ||
wpRootFolder: /var/wordpress | ||
dbUrl: sqlite:///var/wordpress/wp-tests.sqlite | ||
dump: | ||
- tests/_data/products.sql | ||
- tests/_data/users.sql | ||
- tests/_data/orders.sql | ||
tablePrefix: test_ | ||
domain: wordpress.test | ||
adminEmail: [email protected] | ||
title: 'Integration Tests' | ||
plugins: | ||
- hello.php | ||
- woocommerce/woocommerce.php | ||
- my-plugin/my-plugin.php | ||
theme: twentytwentythree | ||
backupGlobals: false | ||
backupStaticAttributes: false | ||
``` | ||
|
||
The following configuration prevents the backup of *some* globals and static attributes: | ||
|
||
```yaml | ||
actor: IntegrationTester | ||
bootstrap: _bootstrap.php | ||
modules: | ||
enabled: | ||
- \Helper\Integration | ||
- lucatume\WPBrowser\Module\WPLoader: | ||
wpRootFolder: /var/wordpress | ||
dbUrl: sqlite:///var/wordpress/wp-tests.sqlite | ||
dump: | ||
- tests/_data/products.sql | ||
- tests/_data/users.sql | ||
- tests/_data/orders.sql | ||
tablePrefix: test_ | ||
domain: wordpress.test | ||
adminEmail: [email protected] | ||
title: 'Integration Tests' | ||
plugins: | ||
- hello.php | ||
- woocommerce/woocommerce.php | ||
- my-plugin/my-plugin.php | ||
theme: twentytwentythree | ||
backupGlobalsExcludeList: | ||
- my_plugin_will_explode_on_wakeup | ||
- another_problematic_global | ||
backupStaticAttributesExcludeList: | ||
- MyPlugin\MyClass: | ||
- instance | ||
- anotherStaticAttributeThatWillExplodeOnWakeup | ||
- AnotherPlugin\AnotherClass: | ||
- instance | ||
- yetAnotherStaticAttributeThatWillExplodeOnWakeup | ||
``` | ||
|
||
### Handling a custom site structure | ||
|
||
If you're working on a site project using a custom file structure, e.g. [Bedrock][4], you should | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
use lucatume\WPBrowser\TestCase\WPTestCase; | ||
|
||
class BackupControlTestCaseStore | ||
{ | ||
public static $staticAttribute = 'initial_value'; | ||
public static $staticAttributeTwo = 'initial_value'; | ||
public static $staticAttributeThree = 'initial_value'; | ||
public static $staticAttributeFour = 'initial_value'; | ||
} | ||
|
||
class BackupControlTestCaseStoreTwo | ||
{ | ||
public static $staticAttribute = 'initial_value'; | ||
public static $staticAttributeTwo = 'initial_value'; | ||
public static $staticAttributeThree = 'initial_value'; | ||
public static $staticAttributeFour = 'initial_value'; | ||
} | ||
|
||
class BackupControlTestCase extends WPTestCase | ||
{ | ||
/** | ||
* Override the method to avoid issues with Codeception specific meta data. | ||
*/ | ||
protected function _setUp() | ||
{ | ||
$this->_before(); | ||
} | ||
|
||
public function testBackupGlobalsIsFalse(): void | ||
{ | ||
$this->assertFalse($this->backupGlobals); | ||
} | ||
|
||
public function testBackupGlobalsIsTrue(): void | ||
{ | ||
$this->assertTrue($this->backupGlobals); | ||
} | ||
|
||
public function testWillUpdateTheValueOfGlobalVar(): void | ||
{ | ||
global $_wpbrowser_test_global_var; | ||
$_wpbrowser_test_global_var = 'updated_value'; | ||
$this->assertTrue(true); // Useless assertion to avoid the test to be marked as risky. | ||
} | ||
|
||
public function testWillAlterStoreStaticAttribute(): void | ||
{ | ||
BackupControlTestCaseStore::$staticAttribute = 'updated_value'; | ||
BackupControlTestCaseStore::$staticAttributeTwo = 'updated_value'; | ||
BackupControlTestCaseStore::$staticAttributeThree = 'updated_value'; | ||
BackupControlTestCaseStore::$staticAttributeFour = 'updated_value'; | ||
BackupControlTestCaseStoreTwo::$staticAttribute = 'updated_value'; | ||
BackupControlTestCaseStoreTwo::$staticAttributeTwo = 'updated_value'; | ||
BackupControlTestCaseStoreTwo::$staticAttributeThree = 'updated_value'; | ||
BackupControlTestCaseStoreTwo::$staticAttributeFour = 'updated_value'; | ||
$this->assertTrue(true); // Useless assertion to avoid the test to be marked as risky. | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
tests/_data/files/BackupControlTestCaseOverridingTestCase.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
use lucatume\WPBrowser\TestCase\WPTestCase; | ||
|
||
class BackupControlTestCaseOverridingStore | ||
{ | ||
public static $staticAttribute = 'initial_value'; | ||
} | ||
|
||
class BackupControlTestCaseOverridingTestCase extends WPTestCase | ||
{ | ||
protected $backupGlobals = false; | ||
protected $backupStaticAttributes = false; | ||
|
||
/** | ||
* Override the method to avoid issues with Codeception specific meta data. | ||
*/ | ||
protected function _setUp() | ||
{ | ||
$this->_before(); | ||
} | ||
|
||
public function testBackupGlobalsIsFalse(): void | ||
{ | ||
$this->assertFalse($this->backupGlobals); | ||
} | ||
|
||
public function testWillAlterStoreStaticAttribute(): void | ||
{ | ||
BackupControlTestCaseOverridingStore::$staticAttribute = 'updated_value'; | ||
$this->assertTrue(true); // Useless assertion to avoid the test to be marked as risky. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.