* @license PHP License * @package WB * @subpackage unittest */ require_once dirname(__FILE__) . '/../rule.php'; /** * Unit Test * * * * @version 0.1.0 * @package WB * @subpackage unittest */ class TestCasePat_Forms_Rule_InOther extends TestCasePat_Forms_Rule { protected $formDef = array( 'me' => array( 'type' => 'String', 'attributes' => array( 'title' => 'meself', 'required' => 'no', 'minlenght' => 1, 'maxlength' => 250 ) ), 'other' => array( 'type' => 'SwitchGroup', 'attributes' => array( 'title' => 'the others', 'required' => 'no', 'values' => array( array('value' => 'foo', 'label' => 'Foo'), array('value' => 'bar', 'label' => 'Bar'), array('value' => 'IN', 'label' => 'This is in other!'), ) ) ) ); public function testLoad() { WBClass::load('patForms'); $rule = patForms::createRule('InOther'); /** @var $rule WBPAT_Forms_Rule */ $this->assertIsA($rule, 'WBPAT_Forms_Rule'); $this->assertIsA($rule, 'patForms_Rule'); $this->assertIsA($rule, 'patForms_Rule_InOther'); $config = array( 'in' => 'yes', 'other' => 'other' ); $rule->setConfig($config); $this->getForm(); } /** * load class and start */ public function testInOtherValid() { // validate form $values = array( 'me' => 'IN', 'other' => array('IN', 'bar') ); $this->checkOtherValid($values); } public function testInOtherInvalid() { // validate form $values = array( 'me' => 'NOTIN', 'other' => array('IN', 'bar') ); $this->checkOtherInvalid($values); } public function testNotInOthersValid() { // validate form $values = array( 'me' => 'NOTIN', 'other' => array('IN', 'bar') ); $this->checkOtherValid($values, false); } public function testNotInOthersInvalid() { // validate form $values = array( 'me' => 'IN', 'other' => array('IN', 'bar') ); $this->checkOtherInvalid($values, false); } protected function checkOtherValid($values, $in = true) { $form = $this->getForm(); $this->prepareForm($form, $in); $_POST = $values; $form->setSubmitted(true); $valid = $form->validateForm(true); $this->assertTrue($valid); // there must be zero erros $errors = $form->getValidationErrors(); if (is_array($errors)) { $this->assertEqual(count($errors), 0); if (count($errors)) { echo '
' . var_export($errors, true) . "\n"; } } else { $this->assertFalse($errors); } } protected function checkOtherInvalid($values, $in = true) { $form = $this->getForm(); $this->prepareForm($form, $in); $_POST = $values; $form->setSubmitted(true); $valid = $form->validateForm(true); $this->assertFalse($valid); $errors = $form->getValidationErrors(); $this->assertTrue(is_array($errors)); $this->assertEqual(count($errors), 1); // validate that there is exactly on error for tested form element $this->assertTrue(isset($errors['me'])); $this->assertEqual(count($errors['me']), 1); } /** * prepare form inclusive form rule * * @param patForms $form * @param bool $in */ protected function prepareForm($form, $in = true) { $other = $form->getElementByName('other'); /** @var $other patForms_Element */ $rule = patForms::createRule('InOther'); /** @var $rule WBPAT_Forms_Rule */ $config = array( 'in' => 'yes', 'other' => $other ); if (!$in) { $config['in'] = 'no'; } $rule->setConfig($config); // add rule to element $me = $form->getElementByName('me'); /** @var $me patForms_Element */ $me->addRule($rule); } /** * create form object * * @return patForms $form */ protected function getForm() { $params = array(); $params['elements'] = $this->formDef; $form = WBClass::create('patForms', $params); $this->assertIsA($form, 'patForms'); return $form; } } ?>