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');
}
: Bu aynı gibi olacaktır
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);
}
Eh, ben kod yapılır düşünüyorum ve şimdi bunun için bir PHPUnit test oluşturmak istiyoruz.
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.
Ben hiç başarı ile, bunu başarmak için aşağıdaki PHPT testi ve PHPUnit yöntemi kullanarak denedim:
--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).
Ben bu sınıfı test etmek için ne yapabilirim üzerinde herhangi bir fikir?