如何进行依赖于 ~real~ POST/GET 数据的 PHPUnit 测试? [英] How to make a PHPUnit test that depends on ~real~ POST/GET data?

查看:21
本文介绍了如何进行依赖于 ~real~ POST/GET 数据的 PHPUnit 测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含 filter_input 函数的 PHP 类,以使我们的开发人员的生活更轻松.
要验证带有 urlnameage 字段的 HTML 表单,代码如下:

I've created a PHP class that envelopes filter_input functions to make our developer's life easier.
To validate an HTML form with url, name and age fields, the code would be like that:

$post = Filter::POST();
if ($post->validate_string('name') && $post->validate_integer('age')) {
    $url = $post->sanitize_url('url');
}

它与以下内容相同:

if (filter_input(INPUT_POST,'name',FILTER_UNSAFE_RAW) && filter_input(INPUT_POST,'age',FILTER_VALIDATE_INTEGER)) {
    $url = filter_input(INPUT_POST,'url',FILTER_SANITIZE_URL);
}

好吧,我认为代码已经完成,现在我想为它创建一个 PHPUnit 测试.

Well, I think the code is done and now I would like to create a PHPUnit test for it.

问题是我不知道如何在 PHPUnit 方法上伪造 GET/POST 数据,不适用于这种情况.
我不需要在 $_POST 中插入值,我需要真实"数据,因为 filter_input 使用脚本接收的数据,而不是实际的 <代码>$_POST 超全局.

The problem is that I have no idea on how to fake GET/POST data on a PHPUnit method, not for this case.
I don't need to insert values in $_POST, I need "real" data on it, because filter_input works with the data the script received, not with the actual $_POST superglobal.

我尝试使用以下 PHPT 测试和 PHPUnit 方法来实现这一点,但完全没有成功:

I've tried using the following PHPT test and PHPUnit method to achieve this, with no success at all:

--TEST--
Generates POST and GET data to be used in FilterTest.php
--POST--
name=Igor&age=20
--GET--
name=Igor&age=19
--FILE--
<?php
echo $_POST['nome'].' = '.$_POST['idade'];
?>
--EXPECT--
Igor = 20

<小时>

public function testPhpt() {
 $phpt = new PHPUnit_Extensions_PhptTestCase('FilterTest_data.phpt', array('cgi' => 'php-cgi'));
 $result = $phpt->run();
 $this->assertTrue($result->wasSuccessful());
}

编辑

原始代码:http://pastebin.com/fpw2fpxM
用于初始测试的代码:http://pastebin.com/vzxsBQWm
(对不起葡萄牙语,我知道用英语编码会更好,但这是我工作的地方的工作方式.如果您真的认为确实需要,我可以翻译代码).

EDIT

Original code: http://pastebin.com/fpw2fpxM
Code used for initial testing: http://pastebin.com/vzxsBQWm
(sorry for the portuguese, I know it would be better to code in english, but it's how things work here where I work. If you really think it's really needed, I can translate the code).

知道我可以做些什么来测试这个类吗?

Any idea on what can I do to test this class?

推荐答案

您不能伪造原始 POST 数据.但问题在于您的代码:它不是可单元测试的.而不是:

You can't fake raw POST data. But the problem lies in your code: it's not unit-testable. Instead of:

if (filter_input(INPUT_POST,'name',FILTER_UNSAFE_RAW) && filter_input(INPUT_POST,'age', FILTER_VALIDATE_INTEGER)) {
    $url = filter_input(INPUT_POST,'url',FILTER_SANITIZE_URL);
}

如果你有:

if (filter_var($data['name'], FILTER_UNSAFE_RAW) && filter_var($data['age'], FILTER_VALIDATE_INT)) {
    $url = filter_var($data['url'], FILTER_SANITIZE_URL);
}
// where $data is a copy of $_POST in that case

将使您的代码可单元测试,并与您之前的代码一样做同样的事情.

Would render your code unit testable and amount to the same thing as your previous code did.

P.S.:FILTER_VALIDATE_INTEGER 无效.正确的常量是 FILTER_VALIDATE_INT

P.S.: FILTER_VALIDATE_INTEGER is not valid. The proper constant for this is FILTER_VALIDATE_INT

这篇关于如何进行依赖于 ~real~ POST/GET 数据的 PHPUnit 测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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