使用php cli创建测试环境

发布于 2024-07-10 14:31:14 字数 357 浏览 7 评论 0原文

我想在控制台模式下使用 php 并创建一个环境来测试我的功能。

我不想每次想要测试某个功能时都被迫使用网络浏览器并创建一个新文件。

我想访问控制台中的函数,然后返回结果。

我该怎么做呢?

更新:

也许我对此解释得很糟糕。 我只想看看函数的返回结果是什么。

也许我必须学习单元测试,但目前我只想要一个交互式控制台,它允许我一一测试所有功能。

就我而言,我必须加载 WordPress 函数(我知道如何使用常规 .php 文件,然后使用浏览器来解析该文件),但如果可以的话,我不知道从命令行使用 php。

I want to use php in console mode and create an environment to test my functions.

I do not want to be forced to use a web browser and create a new file each time I want to test a function.

I want to access the function in the console and then it return the result.

How do I do this?

Update:

Perhaps I have explained this badly. I only want to see what result the function's return.

Maybe I have to learn unit testing but for the moment I only want an interactive console which allows me to test all functions one by one.

In my case I have to load the wordpress functions (I know how do it with a regular .php file and then a browser to parse the file) but i don't if it is possible to do it with php from the command line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

这个俗人 2024-07-17 14:31:14

我过去使用过 phpsh ,发现它非常有用。 一旦启动它,您将需要chdir()到您的文件所在的位置,然后显然require()任何包含您需要测试的函数的文件。 然后,您可以通过在 shell 中输入函数调用来测试它们,例如 var_dump(some_function(1, 2));

I have used phpsh in the past and found it very useful. Once you start it you will need to chdir() to where your files are and then obviously require() any files containing functions you need to test. You can then just test your function calls by typing them into the shell e.g. var_dump(some_function(1, 2));

西瑶 2024-07-17 14:31:14

我想你必须更具体地说明到底是什么类型的功能。 Wordpress 不提供类似的开箱即用的功能,大多数 PHP 应用程序也不会。

我还认为,当此类应用程序没有考虑到此类环境而开发时,您就是在自找麻烦。

这是一个尝试从functions.php调用“current_time()”的示例,我必须做的尝试才意识到它不会以这种方式工作:


php -r 'require "functions.php"; var_dump(current_time("mysql"));'

给出


Fatal error: Call to undefined function apply_filters() in functions.php on line 346

尝试


php -r 'require "functions.php"; require "plugin.php"; var_dump(current_time("mysql"));'

给出


Fatal error: Call to undefined function wp_cache_get() in functions.php on line 351

尝试


php -r 'require "functions.php"; require "plugin.php"; require "cache.php"; var_dump(current_time("mysql"));'

给出


Fatal error: Call to a member function get() on a non-object in cache.php on line 93

查看源中的最后一个错误我看到


 function wp_cache_get($id, $flag = '') {
     global $wp_object_cache;

     return $wp_object_cache->get($id, $flag);
 }

使用全局变量使得在其他环境中进行测试成为 PITA(如果不是不可能的话)。

如果这不是您想要做的,那么您的问题必须更加具体/详细。

I guess you've to be more specific what kind of functions exactly. Wordpress does not provide something like that out of the box, most PHP apps won't.

I also think you're calling for trouble here when such apps aren't developed in mind for such environments.

Here's an example trying to call "current_time()" from functions.php and the attempts I had to do just to realize it won't work that way:


php -r 'require "functions.php"; var_dump(current_time("mysql"));'

gives


Fatal error: Call to undefined function apply_filters() in functions.php on line 346

Trying


php -r 'require "functions.php"; require "plugin.php"; var_dump(current_time("mysql"));'

gives


Fatal error: Call to undefined function wp_cache_get() in functions.php on line 351

Trying


php -r 'require "functions.php"; require "plugin.php"; require "cache.php"; var_dump(current_time("mysql"));'

gives


Fatal error: Call to a member function get() on a non-object in cache.php on line 93

Looking at the last error in the source I see


 function wp_cache_get($id, $flag = '') {
     global $wp_object_cache;

     return $wp_object_cache->get($id, $flag);
 }

Using global variables makes testing in other environments a PITA if not impossible.

If this is not what you're trying to do, you've to be more specific/detailed in your question.

清泪尽 2024-07-17 14:31:14

您将需要阅读一般意义上的“单元测试”,然后尝试将它们应用到 PHP 中。

您正在使用的框架(如果有)、代码风格以及您想要运行的测试将决定您需要使用的确切方法。 只有首先理解单元测试的概念并将其实施到您的编码最佳实践中,您才能在这方面取得进展。

You are going to want to read up on "Unit Testing" in a generic sense and then try and apply them to PHP.

The framework you are using (if any), the style of code, and the tests you want to run are going to determine the exact methods you need to use. Only by first understanding the concept of Unit Tests and implementing them into your coding best-practices will you be able to make progress in this regard.

笑饮青盏花 2024-07-17 14:31:14

怎么样:

php -a

如果你用 readline 支持来编译 php,它会更奇特。

How about:

php -a

And if you compile php with readline support it'll be more fancy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文