Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I usually have a data provider to test my input filters.</p> <p>Here's an example input filter with two really simple fields:</p> <pre class="lang-php prettyprint-override"><code>use Zend\InputFilter\InputFilter; use Zend\InputFilter\Input; use Zend\I18n\Validator\Alnum; class MyInputFilter extends InputFilter { public function __construct() { $name = new Input('name'); $name-&gt;setRequired(false)-&gt;setAllowEmpty(true); $this-&gt;add($name); $nickname = new Input('nickname'); $nickname-&gt;getValidatorChain()-&gt;attach(new Alnum()); $this-&gt;add($nickname); } } </code></pre> <p>And here's a test class for it:</p> <pre class="lang-php prettyprint-override"><code>class MyInputFilterTest extends \PHPUnit_Framework_TestCase { public function setUp() { $this-&gt;inputFilter = new MyInputFilter(); } /** @dataProvider validatedDataProvider */ public function testValidation($data, $valid) { $this-&gt;inputFilter-&gt;setData($data); $this-&gt;assertSame($valid, $this-&gt;inputFilter-&gt;isValid()); } public function validatedDataProvider() { return array( array( array(), false ), array( array('name' =&gt; '', 'nickname' =&gt; 'Ocramius'), true ), array( array('name' =&gt; 'Test', 'nickname' =&gt; 'Ocramius'), true ), array( array('name' =&gt; 'Test', 'nickname' =&gt; 'Hax$or'), false ), ); } } </code></pre> <p>This is a very simple example, but I am basically throwing different datasets at the filter and checking what is relevant to me (in this case checking that data is valid or invalid).</p> <p>If your filter applies transformations on the data, you may also want to check what the output of <code>$inputFilter-&gt;getValues()</code> is.</p> <p>If the error messages are relevant to you, you can also check <code>$inputFilter-&gt;getMessages()</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload