Re: Unit testing HTML_QuickForm2 forms
| From: | Alexey Borzov | Date: | Tue, 05 Jul 2011 12:47:07 +0000 |
| Subject: | Re: Unit testing HTML_QuickForm2 forms | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-31158@lists.php.net to get a copy of this message | ||
Hi Christian,
On 05.07.2011 10:27, Christian Weiske wrote:
I'm writing unit tests for my QF2 form validation rules and do it like this: $form = new My_Foo_Form(); //extends HTML_QuickForm2 $form->addBarFields(); $form->addDataSource(Unless you are doing something complex in your Rule (by complex I mean overriding methods other that validateOwner() / getJavascriptCallback() and setConfig()), you can follow the pattern we use in our unit tests: * Create a mock of Element and make its getRawValue() return 'fieldvalue' * Manually create an instance of your rule, passing the above mock as $owner * Check the return value of $rule->validate() (the method is public) For example, here is HTML_QuickForm2_Rule_RegexTest::testOptionsHandling(): $mockEl = $this->getMock('HTML_QuickForm2_Element', array('getType',new HTML_QuickForm2_DataSource_Session( array( 'fieldname' => 'fieldvalue' ) )); $this->assertFalse($form->validate()); $this->assertEquals('field needs to be a valid email', $form->getElementById('fieldID')->getError()); Is there a better/different way to do it?
'getRawValue', 'setValue', '__toString'));$mockEl->expects($this->exactly(2))->method('getRawValue')
->will($this->returnValue('foo123'));
$alpha = new HTML_QuickForm2_Rule_Regex($mockEl, 'an error', '/^[a-zA-Z]+$/');
$this->assertFalse($alpha->validate());
$alphaNum = new HTML_QuickForm2_Rule_Regex($mockEl, 'an error', '/^[a-zA-Z0-9]+$/');
$this->assertTrue($alphaNum->validate());