WordPress插件phpunit具有依赖项的测试 [英] WordPress plugin phpunit tests with dependencies

查看:107
本文介绍了WordPress插件phpunit具有依赖项的测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怀疑这将是一个重复的问题或答案很简单的问题,但是我搜索了所有我能想到的没有结果的词.

I suspect this would be a duplicate question or something with a very simple answer, but I've searched on every term I can come up with without any result.

这真的很简单; 如何对依赖于其他插件的WordPress插件进行单元测试?

我目前正在使用WooCommerce插件,该插件将包含一个支付网关,该网关必须扩展WooCommerce插件中包含的WC_Payment_Gateway.因此,我需要WordPress 加载并激活 WooCommerce.我试过了

I'm currently working on a WooCommerce plugin which will contain a payment gateway, which has to extend the WC_Payment_Gateway included in the WooCommerce plugin. So I need WordPress to load and activate WooCommerce. I tried;

  1. 通常只包含/plugins/woocommerce/woocommerce.php.由于尚未创建WooCommerce表,因此出现数据库错误.
  2. "muplugins_loaded"动作上运行activate_plugin( '<woocommerce plugin path>' );.出现与上述相同的错误.
  3. 克隆WooCommerce开发人员.回购,并在woocommerce中包含test/bootstrap.php.给我一个PHP致命错误:wp-content/plugins/woocommerce/tmp/wordpress-tests-lib/includes/functions.php): failed to open stream: No such file or directory in /home/...
  4. 正在运行/plugins/woocommerce/tests/bin/install.sh.给我PHP Fatal error: Cannot redeclare tests_add_filter() (previously declared in ...
  5. 以上各项的组合
  1. manually just including /plugins/woocommerce/woocommerce.php. Gives me db errors as WooCommerce tables has not been created.
  2. running activate_plugin( '<woocommerce plugin path>' ); on the "muplugins_loaded" action. Gives same error as above.
  3. Cloning WooCommerce dev. repo and including test/bootstrap.php in woocommerce. Gives me a PHP fatal error: wp-content/plugins/woocommerce/tmp/wordpress-tests-lib/includes/functions.php): failed to open stream: No such file or directory in /home/...
  4. Running /plugins/woocommerce/tests/bin/install.sh. Gives me PHP Fatal error: Cannot redeclare tests_add_filter() (previously declared in ...
  5. Combinations of the above

有人知道我在做什么错吗?我真的不想放弃单元测试,但目前似乎是唯一的选择,因为我的插件中有些类已经致命,例如,键入提示付款网关,这显然会迫使自动加载器获得它.

Anyone know what I'm doing wrong? I really don't want to drop unit tests but currently seems like the only option as I'm getting fatal b/c some classes in my plugin i.e. type hint the payment gateway which obviously forces the autoloader to get it.

先谢谢您了:)

推荐答案

我花了几个小时试图弄清楚这一点,并最终实现了目标.将此答案留给其他正在寻找的人.

I just spent hours trying to figure this out, and got there eventually. Leaving this answer here for anyone else that's looking.

该指南很好地说明了此过程: https://wptheming.com/2016/01/unit-tests-for-woocommerce-extensions/

This guide explains the process quite well: https://wptheming.com/2016/01/unit-tests-for-woocommerce-extensions/

基本上,您可以将整个woocommerce测试套件复制到扩展文件夹中,并对引导文件中的初始化进行一些调整,以便一起加载woocommerce和扩展.

Basically you can copy the entire woocommerce test suite into your extension folder and make a few adjustments to the initialization in the bootstrap file, so that you load up woocommerce and your extension together.

这篇关于WordPress插件phpunit具有依赖项的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆