最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - PHPUnit Testing and woocommerce Constant

programmeradmin5浏览0评论

I tried to perform a test on the WC_VERSION constant.

The main class :

class WC_Custom_Variable_Products_Dependencies {

    /** minimum WooCommerce version required by this plugin */
    const MINIMUM_WC_VERSION = '3.7.9';

    public function __construct() {

        add_action( 'admin_init', [$this, 'check_wc_version']);
    }

    protected function check_wc_version() {

         return  version_compare(WC_VERSION, self::MINIMUM_WC_VERSION , '>=');
    }
}

The test class :

 require_once 'includes/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();
     }

     /**
     * Tests the protected functions 
     * 
     * @param Object $method
     * @return bool
     */
    private function makeReflection($method) {

        $reflection = new ReflectionClass('WC_Custom_Variable_Products_Dependencies');
        $protectedMethod = $reflection->getMethod($method);
        $protectedMethod->setAccessible(true);

        return $protectedMethod->invokeArgs($this->class_instance, ['']);
    }
    public function test_check_wc_version() {

        $result = $this->makeReflection('check_wc_version');
        $this->assertTrue($result);
    }
}

And PHPunit return : Use of undefined constant WC_VERSION - assumed 'WC_VERSION' does this mean in the test, Woocommerce is not loaded?

I tried to perform a test on the WC_VERSION constant.

The main class :

class WC_Custom_Variable_Products_Dependencies {

    /** minimum WooCommerce version required by this plugin */
    const MINIMUM_WC_VERSION = '3.7.9';

    public function __construct() {

        add_action( 'admin_init', [$this, 'check_wc_version']);
    }

    protected function check_wc_version() {

         return  version_compare(WC_VERSION, self::MINIMUM_WC_VERSION , '>=');
    }
}

The test class :

 require_once 'includes/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();
     }

     /**
     * Tests the protected functions 
     * 
     * @param Object $method
     * @return bool
     */
    private function makeReflection($method) {

        $reflection = new ReflectionClass('WC_Custom_Variable_Products_Dependencies');
        $protectedMethod = $reflection->getMethod($method);
        $protectedMethod->setAccessible(true);

        return $protectedMethod->invokeArgs($this->class_instance, ['']);
    }
    public function test_check_wc_version() {

        $result = $this->makeReflection('check_wc_version');
        $this->assertTrue($result);
    }
}

And PHPunit return : Use of undefined constant WC_VERSION - assumed 'WC_VERSION' does this mean in the test, Woocommerce is not loaded?

Share Improve this question edited Jan 13, 2020 at 11:51 Younes.D asked Jan 13, 2020 at 8:59 Younes.DYounes.D 1629 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

return defined('WC_VERSION'); should fix since defined() accepts a string as parameter. https://www.php/manual/en/function.defined.php

If you use: require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php'); adjusting your path if the installation is in a subrdir you load all the WP environment including active plugins etc

发布评论

评论列表(0)

  1. 暂无评论