如何进行依赖于〜real〜POST/GET数据的PHPUnit测试? [英] How to make a PHPUnit test that depends on ~real~ POST/GET data?
问题描述
我创建了一个PHP类,该类封装了filter_input函数,以使我们的开发人员的生活更轻松.
要使用url
,name
和age
字段验证HTML表单,代码应如下所示:
$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测试.
问题是我不知道如何伪造PHPUnit方法上的GET/POST数据,而不是这种情况.
我不需要在$_POST
中插入值,我需要在其上的真实"数据,因为filter_input
适用于脚本收到的数据,而不适用于实际的$_POST
超全局变量.
我尝试使用以下PHPT测试和PHPUnit方法来实现此目标,但完全没有成功:
--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
(对不起葡萄牙语,我知道用英语编码会更好,但是这是我在这里工作的地方.如果您真的认为确实需要,我可以翻译代码). /p>
关于我可以做什么来测试该课程的任何想法?
您不能伪造原始POST数据.但是问题出在您的代码中:它不是可单元测试的.代替:
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
将使您的代码单元可测试,并且与您先前的代码所做的相同.
P.S.:FILTER_VALIDATE_INTEGER无效.正确的常量是FILTER_VALIDATE_INT
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');
}
It would be the same as:
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);
}
Well, I think the code is done and now I would like to create a PHPUnit test for it.
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.
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());
}
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?
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 you had:
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 is not valid. The proper constant for this is FILTER_VALIDATE_INT
这篇关于如何进行依赖于〜real〜POST/GET数据的PHPUnit测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!