【问题标题】:Unit Testing action hook单元测试动作钩子
【发布时间】:2020-01-10 01:52:25
【问题描述】:

我想对一个类进行单元测试,我的目标是:我想通过函数来​​检查插件是否被激活:is_plugin_active

class WC_Custom_Variable_Products_Dependencies {
    public function __construct() {
          add_action( 'admin_init', [$this, 'check_environment']);
    }
    public function check_environment(){
          return is_plugin_active( 
                    'woocommerce-custom-variable-products/woocommerce-custom-variable-products.php' 
                 );
    }
}

分类测试:

require_once 'class-wc-custom-variable-products-dependencies.php';
class WC_Custom_Variable_Products_DependenciesTest extends WP_UnitTestCase {

    public function setUp() {
        parent::setUp();

        $this->class_instance = new WC_Custom_Variable_Products_Dependencies();
    }

    public function test_check_environment(){

        $result = $this->class_instance->check_environment();
        $this->assertTrue($result);
    }

断言总是返回 False 。

我的插件被激活,如果我从浏览器执行函数is_plugin_active返回True:

add_action('admin_init', function(){

     var_dump(is_plugin_active( 
                        'woocommerce-custom-variable-products/woocommerce-custom-variable-products.php' 
    ));
});

我认为admin_init 钩子没有在测试中执行。是真是假?

【问题讨论】:

    标签: wordpress unit-testing phpunit


    【解决方案1】:

    我找到了原因。这是解决方案:您必须在tests / bootstrap.php 文件中激活插件:

    $GLOBALS[ 'wp_tests_options' ] = array(
            'active_plugins' => array(
                    'YOUR-PLUGIN/YOUR-PLUGIN.php'
            )
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-25
      • 1970-01-01
      • 1970-01-01
      • 2021-10-04
      • 2023-04-01
      • 2021-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多