Simplify and speedup WC_Tests_REST_System_Status tests

This implements two changes to WC_Tests_REST_System_Status test class:

- It simplifies the tests by setting the user in the setUp method
instead of in each test.
- Improves the performance of the tests by creating a new admin user
only once when the class is instantiated instead of once for every
single test that is executed.
This commit is contained in:
Rodrigo Primo 2020-01-08 15:06:53 -03:00 committed by Claudio Sanches
parent bc0acf60b8
commit 0cfbc27a27
1 changed files with 23 additions and 22 deletions

View File

@ -14,16 +14,34 @@
class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
/**
* Setup our test server.
* User variable.
*
* @var WP_User
*/
public function setUp() {
parent::setUp();
$this->endpoint = new WC_REST_System_Status_Controller();
$this->user = $this->factory->user->create(
protected static $user;
/**
* Setup once before running tests.
*
* @param object $factory Factory object.
*/
public static function wpSetUpBeforeClass( $factory ) {
self::$user = $factory->user->create(
array(
'role' => 'administrator',
)
);
}
/**
* Setup our test server.
*/
public function setUp() {
parent::setUp();
wp_set_current_user( self::$user );
$this->endpoint = new WC_REST_System_Status_Controller();
// Callback used by WP_HTTP_TestCase to decide whether to perform HTTP requests or to provide a mocked response.
$this->http_responder = array( $this, 'mock_http_responses' );
@ -57,7 +75,6 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
* @since 3.5.0
*/
public function test_get_system_status_info_returns_root_properties() {
wp_set_current_user( $this->user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
$data = $response->get_data();
@ -76,7 +93,6 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
* @since 3.5.0
*/
public function test_get_system_status_info_environment() {
wp_set_current_user( $this->user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
$data = $response->get_data();
$environment = (array) $data['environment'];
@ -97,7 +113,6 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
*/
public function test_get_system_status_info_database() {
global $wpdb;
wp_set_current_user( $this->user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
$data = $response->get_data();
$database = (array) $data['database'];
@ -115,8 +130,6 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
* @since 3.5.0
*/
public function test_get_system_status_info_active_plugins() {
wp_set_current_user( $this->user );
$actual_plugins = array( 'hello.php' );
update_option( 'active_plugins', $actual_plugins );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
@ -135,7 +148,6 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
* @since 3.5.0
*/
public function test_get_system_status_info_theme() {
wp_set_current_user( $this->user );
$active_theme = wp_get_theme();
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
@ -152,8 +164,6 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
* @since 3.5.0
*/
public function test_get_system_status_info_settings() {
wp_set_current_user( $this->user );
$term_response = array();
$terms = get_terms( 'product_type', array( 'hide_empty' => 0 ) );
foreach ( $terms as $term ) {
@ -176,8 +186,6 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
* @since 3.5.0
*/
public function test_get_system_status_info_security() {
wp_set_current_user( $this->user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
$data = $response->get_data();
$settings = (array) $data['security'];
@ -193,7 +201,6 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
* @since 3.5.0
*/
public function test_get_system_status_info_pages() {
wp_set_current_user( $this->user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
$data = $response->get_data();
$pages = $data['pages'];
@ -226,8 +233,6 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
* @since 3.5.0
*/
public function test_get_system_tools() {
wp_set_current_user( $this->user );
$tools_controller = new WC_REST_System_Status_Tools_Controller();
$raw_tools = $tools_controller->get_tools();
@ -303,8 +308,6 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
* @since 3.5.0
*/
public function test_get_system_tool() {
wp_set_current_user( $this->user );
$tools_controller = new WC_REST_System_Status_Tools_Controller();
$raw_tools = $tools_controller->get_tools();
$raw_tool = $raw_tools['recount_terms'];
@ -359,8 +362,6 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
* @since 3.5.0
*/
public function test_execute_system_tool() {
wp_set_current_user( $this->user );
$tools_controller = new WC_REST_System_Status_Tools_Controller();
$raw_tools = $tools_controller->get_tools();
$raw_tool = $raw_tools['recount_terms'];