首页 > 解决方案 > 在functions.php中错误WordPress测试Phpunit调用函数

问题描述

我正在使用 PhpUnit 进行测试,调用 functions.php 的函数

我有一个函数可以验证它是否是一个页面并且 SLUG 模板等于一个页面

function my_scripts() {

    var_dump (get_page_template_slug());

    if (is_page() && get_page_template_slug() == 'page-script.php') {

    ........
    }
}

在我的测试中,我有这个代码

public function test_my_scripts(){

    $page_id = $this->factory->post->create(
        array(
            'post_type' => 'page',
        )
    );
    add_post_meta( $page_id , '_wp_page_template', 'page-script.php' );
    add_filter( 'page_template', 'custom_page_template' );

    var_dump(get_page_template_slug($page_id));

    my_scripts();

}

我测试的 var_dump 的结果是

字符串(15)“页面脚本.php”

以及functions.php的功能

布尔(假)

并且不要输入条件 IF。

如何确定从页面调用函数和 slug 是否指示进入条件?

谢谢

标签: wordpressphpunit

解决方案


我找到了解决方案,将它放在调用函数 my_scripts(); 之前。

$this->go_to(get_permalink($page_id));

推荐阅读