* @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_Fraction extends TestCasePat_Forms_Rule { protected $formDef = array( 'hour' => array( 'type' => 'Number', 'attributes' => array( 'title' => 'hour', 'required' => 'yes', 'min' => 0.25, 'max' => 25 ) ), ); public function testLoad() { WBClass::load('patForms'); $rule = patForms::createRule('Fraction'); /** @var $rule WBPAT_Forms_Rule */ $this->assertIsA($rule, 'WBPAT_Forms_Rule'); $this->assertIsA($rule, 'patForms_Rule'); $this->assertIsA($rule, 'patForms_Rule_Fraction'); $config = array( 'factor' => '4', 'add' => '0' ); $rule->setConfig($config); $this->getForm(); } /** * load class and start */ public function testValid() { // validate form $this->checkValid(array('hour' => '0.25')); $this->checkValid(array('hour' => '0.5')); $this->checkValid(array('hour' => '0.75')); $this->checkValid(array('hour' => '1.0')); $this->checkValid(array('hour' => '1.25')); $this->checkValid(array('hour' => '1.5')); $this->checkValid(array('hour' => '1.75')); $this->checkValid(array('hour' => '4.0')); } public function testValid2() { // validate form $this->checkValid(array('hour' => '0.5'), '2'); $this->checkValid(array('hour' => '1.0'), '2'); $this->checkValid(array('hour' => '1.5'), '2'); } public function testValid5() { // validate form $this->checkValid(array('hour' => '0.4'), '5'); $this->checkValid(array('hour' => '0.6'), '5'); $this->checkValid(array('hour' => '0.8'), '5'); $this->checkValid(array('hour' => '1.2'), '5'); } public function testInvalid() { // validate form $this->checkInvalid(array('hour' => '0.3')); $this->checkInvalid(array('hour' => '1.1')); $this->checkInvalid(array('hour' => '1.2')); } public function testInvalid2() { // validate form $this->checkInvalid(array('hour' => '0.3'), '2'); $this->checkInvalid(array('hour' => '0.6'), '2'); $this->checkInvalid(array('hour' => '0.9'), '2'); } protected function checkValid($values, $parts = '4') { $form = $this->getForm(); $this->prepareForm($form, $parts); $_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 checkInvalid($values, $parts = '4') { $form = $this->getForm(); $this->prepareForm($form, $parts); $_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['hour'])); $this->assertEqual(count($errors['hour']), 1); } /** * prepare form inclusive form rule * * @param patForms $form * @param bool $in */ protected function prepareForm($form, $factor = '4') { $rule = patForms::createRule('Fraction'); /** @var $rule WBPAT_Forms_Rule */ $config = array( 'factor' => $factor, 'add' => '0' ); $rule->setConfig($config); // add rule to element $el = $form->getElementByName('hour'); /** @var $el patForms_Element */ $el->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; } } ?>