集成测试命令行 PHP

发布于 2024-09-27 04:45:55 字数 1048 浏览 0 评论 0原文

我有一个在命令行运行的 PHP 脚本,执行已经使用 PHPUnit 进行单元测试的类。

但是,我想验证脚本本身没有逻辑错误并且运行正常。

// require classes
require_once 'injectedClass.php';
require_once 'DBClass.php';
require_once 'taskEngine.php';

$injectedObj = new injectedClass;
$dbObj = new DBClass;

$taskRunner = new taskEngine($injectedObj, $dbObj);

$taskRunner->task1()->task2();

$taskRunner->finish();

//etc

更新的解决方案

正如 djechelon 的回答所建议的那样简单,我想太多了。解决方案是创建一个 PHPUnit 测试并将传递到 taskRunner 的变量预先分配给模拟对象。在实时脚本中,在创建真实对象之前进行简单的检查允许使用相同的脚本进行测试和生产:

test:

$injectedObj = $this->getMock('injectedClass');
$dbObj = $this->getMock('DBClass');

require_once '/path/to/live/script.php';

$this->assertTrue($taskRunner->finished);

script:

// require classes

if(!isset($injectedObj)) {
    $injectedObj = new injectedClass;
}
if(!isset($dbObj)) {
    $dbObj = new DBClass;
}

$taskRunner = new taskEngine($injectedObj, $dbObj);

// run tasks

I've got a PHP script that runs at the command line, executing classes that are already unit tested with PHPUnit.

However, I'd like to verify that the script itself has no logical errors and runs properly.

// require classes
require_once 'injectedClass.php';
require_once 'DBClass.php';
require_once 'taskEngine.php';

$injectedObj = new injectedClass;
$dbObj = new DBClass;

$taskRunner = new taskEngine($injectedObj, $dbObj);

$taskRunner->task1()->task2();

$taskRunner->finish();

//etc

Updated Solution

It is as simple as djechelon's answer suggested, I was overthinking it. The solution is to create a PHPUnit test and pre-assign the variables passed into the taskRunner to mock objects. In the live script, a simple check before creating real objects allows the same script to be used for testing and production:

test:

$injectedObj = $this->getMock('injectedClass');
$dbObj = $this->getMock('DBClass');

require_once '/path/to/live/script.php';

$this->assertTrue($taskRunner->finished);

script:

// require classes

if(!isset($injectedObj)) {
    $injectedObj = new injectedClass;
}
if(!isset($dbObj)) {
    $dbObj = new DBClass;
}

$taskRunner = new taskEngine($injectedObj, $dbObj);

// run tasks

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

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

发布评论

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

评论(1

倾城花音 2024-10-04 04:45:55

您不能为您的脚本创建 PHPUnit 测试吗?

您可以手动执行集成测试,创建一个脚本,使用一组给定的输入参数运行脚本,并将其输出与您期望的结果进行比较。

请注意先有鸡还是先有蛋的问题:您的测试脚本无法通过测试台对其本身进行测试...

无论如何,如果脚本如此简单,我不确定您是否需要测试您的脚本。一些手动运行可能就足够了......

Can't you create a PHPUnit test for your script?

You could perform an integration test by hand, creating a script that runs your script with a set of given input parameters and compare its output to what you could expect.

Beware of the chicken-and-egg problem: your testing script cannot be tested itself by a test bench...

Anyway I'm not sure you need testing your script if it's so simple. A few manual runs might suffice...

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