Mocking away captcha field when testing zend Form
I want to test my form which I have created with Zend_Form. Now the problem is that I have a captcha field within my form. And I was told by one of the Zend Framework Guru's that I should mock away the validation of the captcha field a mock object.
I have read this http://www.phpunit.de/manual/3.0/en/mock-objects.html on the PHPUnit 开发者_高级运维manual. But I really don't understand how I can mock away a validator.
Does anyone have experience with this? Can anyone help me out?
Thanks
More information:
I want my test not to break because it can't test the captcha field. So I need to find out some way the post will not be stopped because of the captcha field.
public function testCanSubmitContactForm(){
$mock = $this->getMock('Zend_Form_Element_Captcha', array(), '', false);
$mock->expects($this->once()->method("isValid")->will($this->resturnValue(true)));
$this->request->setMethod('post')
->setPost(array(
'email' => 'someemail@adres.com',
'comment' => 'Testing the bladiebla contact form'
'captcha' => ''//no idea
));
$this->dispach('/contact');
}
Here is how my Zend_Form generated form looks like:
<form id="contact-form" enctype="application/x-www-form-urlencoded" method="post" action="/index/contact"><dl class="zend_form">
<dt id="email-label"><label for="email" class="required">Uw E-mailadres</label></dt>
<dd id="email-element">
<input type="text" name="email" id="email" value="" /></dd>
<dt id="comment-label"><label for="comment" class="required">Stel hieronder uw vraag of geef je commentaar op</label></dt>
<dd id="comment-element">
<textarea name="comment" id="comment" cols="71" rows="24"></textarea></dd>
<dt id="captcha-input-label"><label for="captcha-input" class="required">SPAM Beveiliging</label></dt>
<dd id="captcha-element">
<pre> _ _ __ __ ______ ______ ______ ___
| \ / || \ \\/ // /_ _// /_ _// /_____// / _ \\
| \/ || \ ` // `-| |,- -| ||- `____ ` | / \ ||
| . . || | || | || _| ||_ /___// | \_/ ||
|_|\/|_|| |_|| |_|| /_____// `__ ` \___//
`-` `-` `-`' `-`' `-----` /_// `---`
`-`
</pre>
<input type="hidden" name="captcha[id]" value="a4957b2dbfea79d8bd654428f6eb0a2c" id="captcha-id" />
<input type="text" name="captcha[input]" id="captcha-input" value="" />
<p class="description">Voer de 6 letters in die hierboven getoond worden. Deze vraag wordt gebruikt om te testen of u een menselijke bezoeker bent teneinde spam-inzendingen te vermijden.</p></dd>
<dt id="submit-label"> </dt><dd id="submit-element">
<input type="submit" name="submit" id="submit" value="Nu Versturen" /></dd></dl></form>
I'll just fire away, let me know if this is what you want:
From: http://framework.zend.com/manual/en/zend.form.standardElements.html
// Concrete instance:
$element->setCaptcha(new Zend_Captcha_Figlet());
You can't unit test this since you can't do dependency injection (as with many parts of zend framework as you sure have figured out) so you need some way to inject that into your form builder.
public function setCaptcha(Zend_Captcha $cap) { // override the default cap }
or if your application allows for it pass it in the constructor (would be better anyways but can be hard with zf)
Then you have your standard mock:
$mock = $this->getMock("Zend_Form_Element_Captcha", array(), array(), '', false /*don't call original constructor'*/);
$mock->expects($this->once())->method("isValid")->will($this->returnValue(true)); // Not sure about the method name ;)
And add that in instead of the default captcha element.
Not sure what part of the problem you where having trouble with so i went over everything a little
You just have to separate the captcha element from the form, e.g. create:
public function addCaptcha()
{
$this->addElement()...
}
protected $_enableCaptcha = true;
public function disableCaptcha()
{
$this->_enableCaptcha = false;
}
public function init()
{
// ...
if ($this->_enableCaptcha) {
$this->addCaptcha();
}
}
Then in your test just disable the CAPTCHA.
Maybe Zend Form or Captcha Element already has some method to do so.
You may also create base form without captcha and test it and next subclass the form and add captcha element.
For testing you can use this condition in your form:
if (!\Zend_Session::$_unitTestEnabled) {
$this->addElement($captcha);
}
精彩评论