Send parameter to PHP Unit on the command line(在命令行向 PHP 单元发送参数)
问题描述
我想使用命令行向 PHP 单元发送一个参数.
I want to send a parameter to PHP Unit using the command line.
例如
./phpunit --foo='bar' AllTests
我该怎么做?
我能够实现目标的最接近的方法是使用以下内容:
The closest I was able to achieve my objective is using the following:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
<php>
<env name="foo" value="bar"/>
</php>
</phpunit>
然后我可以使用 $_ENV['foo']
访问该变量.
I can then access the variable using $_ENV['foo']
.
但是,我想使用命令行发送这个变量.
However, I want to send this variable using the command line.
推荐答案
使用 phpunit.xml
显然是针对常量环境变量,使用它来将更改的参数传递给测试有点开销.
Using phpunit.xml
is obviously for constant environmental variables and using it for passing changing parameters to tests is a bit of an overhead.
您可以执行以下操作之一(请参阅向 PHPUnit 传递参数讨论):
What you can do is one of the following (see Passing parameters to PHPUnit for the discussion):
示例: FOO=bar ./phpunit AllTests
优点:微不足道.
缺点:取决于环境;需要记住变量的名称(如果有很多,那就不那么简单了);没有关于支持/必要参数的明显文档.
Cons: Depends on environment; requires remembering names of the variables (which is not that simple if there are many); no obvious documentation on supported/necessary parameters available.
例子: ./phpunit AllTests bar
优点:微不足道;独立于环境;对 PHPUnit 参数没有限制.
Pros: Trivial; independent from environment; no limitations to PHPUnit parameters.
缺点:如果有多个参数会很痛苦,特别是如果它们中的大多数是可选的;没有关于预期参数的明显文档.
Cons: Will be a pain if there are more than several arguments, especially if most of them are optional; no obvious documentation for expected arguments.
示例: <代码>.run.sh AllTests bar 其中 run.sh
查看提供的参数并将它们导出到环境中.
Example: . run.sh AllTests bar
where run.sh
looks at the provided arguments and exports them into the environment.
优点:实施起来或多或少是微不足道的;添加预期参数列表的文档;添加错误处理(例如,如果 bar
是必需参数,但未提供).
Pros: Still more or less trivial to implement; adds documentation of the expected argument list; adds error handling (e.g. in case bar
is a required parameter, but is not provided).
缺点: runner 内部的 PHPUnit 参数是硬编码的;取决于环境.
Cons: PHPUnit parameters inside runner are hardcoded; dependent on the environment.
示例: ./phpunit --foo='bar' AllTests
优点:完全符合您的要求.
缺点:实施起来并不简单;需要分叉,这使得它强烈依赖于当前 PHPUnit 版本的 CLI.
Cons: Not so trivial to implement; requires forking which makes it strongly dependent on CLI of the current PHPUnit version.
示例: run.sh --foo=bar --coverage-html=baz
其中 run.sh
调用了一些 run.php
依次通过解析器运行命令参数,构建用于运行测试的命令并执行此操作.
Example: run.sh --foo=bar --coverage-html=baz
where run.sh
calls some run.php
which in its turn runs command arguments through parser, builds the command for running tests and does that.
优点:现在您可以做任何您喜欢的事情并添加您需要的任何参数.您可以实现自己的记录器,可以在多线程中运行测试等.
Pros: Now you can do whatever you like and add any parameters you need. You can implement your own logger, you can run tests in multithread etc.
缺点:难以实施;有时需要维护;强烈依赖于 PHPUnit CLI.
Cons: Hard to implement; sometimes needs maintenance; is strongly dependent on PHPUnit CLI.
这篇关于在命令行向 PHP 单元发送参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在命令行向 PHP 单元发送参数
基础教程推荐
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01