cvs: pear /HTML_QuickForm2/QuickForm2 Factory.php Node.php Rule.php /HTML_QuickForm2/tests/QuickForm2 FactoryTest.php RuleTest.php
/HTML_QuickForm2/tests/QuickForm2/_files FakeRule.php

From: Date: Wed, 04 Jul 2007 14:58:17 +0000
Subject: cvs: pear /HTML_QuickForm2/QuickForm2 Factory.php Node.php Rule.php /HTML_QuickForm2/tests/QuickForm2 FactoryTest.php RuleTest.php
/HTML_QuickForm2/tests/QuickForm2/_files FakeRule.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-47855@lists.php.net to get a copy of this message
avb Wed Jul 4 14:58:17 2007 UTC Added files: /pear/HTML_QuickForm2/tests/QuickForm2/_files FakeRule.php Modified files: /pear/HTML_QuickForm2/QuickForm2 Factory.php Node.php Rule.php /pear/HTML_QuickForm2/tests/QuickForm2 FactoryTest.php RuleTest.php Log: Added Rule-related methods to Factory createRule() and better addRule() in Node Error message is set only if whole validation chain fails, not the first rule in it. Previous behaviour was counter-intuitive

http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/QuickForm2/Factory.php?r1=1.15&r2=1.16&diff_format=u Index: pear/HTML_QuickForm2/QuickForm2/Factory.php diff -u pear/HTML_QuickForm2/QuickForm2/Factory.php:1.15 pear/HTML_QuickForm2/QuickForm2/Factory.php:1.16 --- pear/HTML_QuickForm2/QuickForm2/Factory.php:1.15 Sat Jun 30 17:39:41 2007 +++ pear/HTML_QuickForm2/QuickForm2/Factory.php Wed Jul 4 14:58:17 2007 @@ -5,7 +5,7 @@ * PHP version 5 * * LICENSE: - * + * * Copyright (c) 2006, 2007, Alexey Borzov <avb@php.net>, * Bertrand Mansion <golgote@mamasam.com> * All rights reserved. @@ -17,9 +17,9 @@ * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the + * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * * The names of the authors may not be used to endorse or promote products + * * The names of the authors may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS @@ -39,12 +39,12 @@ * @author Alexey Borzov <avb@php.net> * @author Bertrand Mansion <golgote@mamasam.com> * @license http://opensource.org/licenses/bsd-license.php New BSD License - * @version CVS: $Id: Factory.php,v 1.15 2007/06/30 17:39:41 avb Exp $ + * @version CVS: $Id: Factory.php,v 1.16 2007/07/04 14:58:17 avb Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ /** - * Exception classes for HTML_QuickForm2 + * Exception classes for HTML_QuickForm2 */ require_once 'HTML/QuickForm2/Exception.php'; @@ -63,7 +63,7 @@ class HTML_QuickForm2_Factory { /** - * List of element types known to Factory + * List of element types known to Factory * @var array */ protected static $elementTypes = array( @@ -97,9 +97,16 @@ 'HTML/QuickForm2/Element/Textarea.php') ); + /** + * List of registered rules + * @var array + */ + protected static $registeredRules = array( + ); + /** - * Checks whether the file exists in the include path + * Checks whether the file exists in the include path * * @param string file name * @return bool @@ -114,6 +121,14 @@ return false; } + /** + * Tries to load a given class from a given file + * + * @param string Class name to load + * @param string Name of the file (supposedly) containing the given class + * @throws HTML_QuickForm2_NotFoundException If the file either can't be + * loaded or doesn't contain the given class + */ protected static function loadClass($className, $includeFile) { if (empty($includeFile)) { @@ -124,7 +139,7 @@ throw new HTML_QuickForm2_NotFoundException("File '$includeFile' was not found"); } // Do not silence the errors with @, parse errors will not be seen - include_once $includeFile; + include_once $includeFile; // Still no class? if (!class_exists($className, false)) { throw new HTML_QuickForm2_NotFoundException( @@ -167,10 +182,10 @@ * @param array Element-specific data (passed to element's constructor) * @return HTML_QuickForm2_Node A created element * @throws HTML_QuickForm2_InvalidArgumentException If type name is unknown - * @throws HTML_QuickForm2_NotFoundException If class for the element can - * not be found and/or loaded from file + * @throws HTML_QuickForm2_NotFoundException If class for the element can + * not be found and/or loaded from file */ - public static function createElement($type, $name = null, $attributes = null, + public static function createElement($type, $name = null, $attributes = null, array $data = array()) { $type = strtolower($type); @@ -183,5 +198,77 @@ } return new $className($name, $attributes, $data); } + + + /** + * Registers a new rule type + * + * @param string Rule type name (treated case-insensitively) + * @param string Class name + * @param string File containing the class, leave empty if class already loaded + * @param mixed Configuration data for rules of the given type + */ + public static function registerRule($type, $className, $includeFile = null, + $config = null) + { + self::$registeredRules[strtolower($type)] = array($className, $includeFile, $config); + } + + + /** + * Returns configuration data for rules of the given type + * + * @param string Rule type name (treated case-insensitively) + * @return mixed Configuration data (set when registering the rule) + * @throws HTML_QuickForm2_InvalidArgumentException If rule type is unknown + */ + public static function getRuleConfig($type) + { + $type = strtolower($type); + if (!isset(self::$registeredRules[$type])) { + throw new HTML_QuickForm2_InvalidArgumentException("Rule '$type' is not known"); + } else { + return self::$registeredRules[$type][2]; + } + } + + + /** + * Checks whether a rule type is known to Factory + * + * @param string Rule type name (treated case-insensitively) + * @return bool + */ + public static function isRuleRegistered($type) + { + return isset(self::$registeredRules[strtolower($type)]); + } + + + /** + * Creates a new Rule of the given type + * + * @param string Rule type name (treated case-insensitively) + * @param HTML_QuickForm2_Node Element to validate by the rule + * @param string Message to display if validation fails + * @param mixed Additional data for the rule + * @return HTML_QuickForm2_Rule A created Rule + * @throws HTML_QuickForm2_InvalidArgumentException If rule type is unknown + * @throws HTML_QuickForm2_NotFoundException If class for the rule + * can't be found and/or loaded from file + */ + public static function createRule($type, HTML_QuickForm2_Node $owner, + $message = '', $options = null) + { + $type = strtolower($type); + if (!isset(self::$registeredRules[$type])) { + throw new HTML_QuickForm2_InvalidArgumentException("Rule '$type' is not known"); + } + list($className, $includeFile) = self::$registeredRules[$type]; + if (!class_exists($className, false)) { + self::loadClass($className, $includeFile); + } + return new $className($owner, $message, $options, $type); + } } ?> http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/QuickForm2/Node.php?r1=1.7&r2=1.8&diff_format=u Index: pear/HTML_QuickForm2/QuickForm2/Node.php diff -u pear/HTML_QuickForm2/QuickForm2/Node.php:1.7 pear/HTML_QuickForm2/QuickForm2/Node.php:1.8 --- pear/HTML_QuickForm2/QuickForm2/Node.php:1.7 Sat Jun 30 17:39:41 2007 +++ pear/HTML_QuickForm2/QuickForm2/Node.php Wed Jul 4 14:58:17 2007 @@ -5,7 +5,7 @@ * PHP version 5 * * LICENSE: - * + * * Copyright (c) 2006, 2007, Alexey Borzov <avb@php.net>, * Bertrand Mansion <golgote@mamasam.com> * All rights reserved. @@ -17,9 +17,9 @@ * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the + * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * * The names of the authors may not be used to endorse or promote products + * * The names of the authors may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS @@ -39,17 +39,17 @@ * @author Alexey Borzov <avb@php.net> * @author Bertrand Mansion <golgote@mamasam.com> * @license http://opensource.org/licenses/bsd-license.php New BSD License - * @version CVS: $Id: Node.php,v 1.7 2007/06/30 17:39:41 avb Exp $ + * @version CVS: $Id: Node.php,v 1.8 2007/07/04 14:58:17 avb Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ /** - * HTML_Common2 - base class for HTML elements + * HTML_Common2 - base class for HTML elements */ require_once 'HTML/Common2.php'; /** - * Exception classes for HTML_QuickForm2 + * Exception classes for HTML_QuickForm2 */ require_once 'HTML/QuickForm2/Exception.php'; @@ -92,7 +92,7 @@ protected $persistent = false; /** - * Element containing current + * Element containing current * @var HTML_QuickForm2_Container */ protected $container = null; @@ -144,7 +144,7 @@ */ protected static function generateId($elementName) { - $tokens = strlen($elementName)? + $tokens = strlen($elementName)? explode('[', str_replace(']', '', $elementName)): array('qfauto'); $container =& self::$ids; @@ -158,7 +158,7 @@ $token = 0; } else { $keys = array_keys($container); - $token = end($keys); + $token = end($keys); while (isset($container[$token])) { $token++; } @@ -229,7 +229,7 @@ /** - * Returns the element's id + * Returns the element's id * * @return string */ @@ -239,9 +239,9 @@ /** * Sets the elements id * - * Please note that elements should always have an id in QuickForm2 and + * Please note that elements should always have an id in QuickForm2 and * therefore it will not be possible to remove the element's id or set it to - * an empty value. If id is not explicitly given, it will be autogenerated. + * an empty value. If id is not explicitly given, it will be autogenerated. * * @param string Element's id, will be autogenerated if not given * @return HTML_QuickForm_Node @@ -314,13 +314,13 @@ /** * Changes the element's persistent freeze behaviour * - * If persistent freeze is on, the element's value will be kept (and + * If persistent freeze is on, the element's value will be kept (and * submitted) in a hidden field when the element is frozen. * - * @param bool New value for "persistent freeze". If omitted, the + * @param bool New value for "persistent freeze". If omitted, the * method will not set anything, just return the current - * value of the flag. - * @return bool Old value of "persistent freeze" flag + * value of the flag. + * @return bool Old value of "persistent freeze" flag */ public function persistentFreeze($persistent = null) { @@ -334,11 +334,11 @@ /** * Adds the link to the element containing current - * + * * @param HTML_QuickForm2_Container Element containing the current one, - * null if the link should really be + * null if the link should really be * removed (if removing from container) - * @throws HTML_QuickForm2_InvalidArgumentException If trying to set a + * @throws HTML_QuickForm2_InvalidArgumentException If trying to set a * child of an element as its container */ protected function setContainer(HTML_QuickForm2_Container $container = null) @@ -364,7 +364,7 @@ /** - * Returns the element containing current + * Returns the element containing current * * @return HTML_QuickForm2_Container|null */ @@ -395,15 +395,58 @@ /** * Adds a validation rule * - * @param HTML_QuickForm2_Rule Validation rule - * @return HTML_QuickForm2_Rule The added rule - */ - public function addRule(HTML_QuickForm2_Rule $rule) - { + * @param HTML_QuickForm2_Rule|string Validation rule or rule type + * @param string Message to display if validation fails + * @param mixed Additional data for the rule + * @return HTML_QuickForm2_Rule The added rule + * @throws HTML_QuickForm2_InvalidArgumentException if $rule is of a + * wrong type or rule name isn't registered with Factory + * @throws HTML_QuickForm2_NotFoundException if class for a given rule + * name cannot be found + */ + public function addRule($rule, $message = '', $options = null) + { + if ($rule instanceof HTML_QuickForm2_Rule) { + $rule->setOwner($this); + } elseif (is_string($rule)) { + $rule = HTML_QuickForm2_Factory::createRule($rule, $this, $message, $options); + } else { + throw new HTML_QuickForm2_InvalidArgumentException( + 'addRule() expects either a rule type or ' . + 'a HTML_QuickForm2_Rule instance' + ); + } + $this->rules[] = $rule; return $rule; } + + /** + * Creates a validation rule + * + * This method is mostly useful when when chaining several rules together + * via {@link HTML_QuickForm2_Rule::and_()} and {@link HTML_QuickForm2_Rule::or_()} + * methods: + * <code> + * $first->addRule('nonempty', 'Fill in either first or second field') + * ->or_($second->createRule('nonempty')); + * </code> + * + * @param string Rule type + * @param string Message to display if validation fails + * @param mixed Additional data for the rule + * @return HTML_QuickForm2_Rule The created rule + * @throws HTML_QuickForm2_InvalidArgumentException If rule type is unknown + * @throws HTML_QuickForm2_NotFoundException If class for the rule + * can't be found and/or loaded from file + */ + public function createRule($type, $message = '', $options = null) + { + return HTML_QuickForm2_Factory::createRule($type, $this, $message, $options); + } + + /** * Performs the server-side validation * http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/QuickForm2/Rule.php?r1=1.1&r2=1.2&diff_format=u Index: pear/HTML_QuickForm2/QuickForm2/Rule.php diff -u pear/HTML_QuickForm2/QuickForm2/Rule.php:1.1 pear/HTML_QuickForm2/QuickForm2/Rule.php:1.2 --- pear/HTML_QuickForm2/QuickForm2/Rule.php:1.1 Sun May 13 21:30:35 2007 +++ pear/HTML_QuickForm2/QuickForm2/Rule.php Wed Jul 4 14:58:17 2007 @@ -5,7 +5,7 @@ * PHP version 5 * * LICENSE: - * + * * Copyright (c) 2006, 2007, Alexey Borzov <avb@php.net>, * Bertrand Mansion <golgote@mamasam.com> * All rights reserved. @@ -17,9 +17,9 @@ * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the + * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * * The names of the authors may not be used to endorse or promote products + * * The names of the authors may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS @@ -39,7 +39,7 @@ * @author Alexey Borzov <avb@php.net> * @author Bertrand Mansion <golgote@mamasam.com> * @license http://opensource.org/licenses/bsd-license.php New BSD License - * @version CVS: $Id: Rule.php,v 1.1 2007/05/13 21:30:35 avb Exp $ + * @version CVS: $Id: Rule.php,v 1.2 2007/07/04 14:58:17 avb Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ @@ -73,19 +73,29 @@ * Additional data for the rule * @var mixed */ - protected $config; + protected $options; /** * Rules chained to this via "and" and "or" operators * * The contents can be described as "disjunctive normal form", where an outer * array represents a disjunction of conjunctive clauses represented by inner - * arrays. + * arrays. * * @var array */ protected $chainedRules = array(array()); + /** + * Type that was provided to Factory when creating this Rule instance + * + * Used to get the common configuration data for the Rules of that type from + * Factory. + * + * @var string + */ + protected $registeredType = null; + /** * Class constructor @@ -93,23 +103,29 @@ * @param HTML_QuickForm2_Node Element to validate * @param string Error message to display if validation fails * @param mixed Additional data for the rule + * @param string Type that was provided to Factory when + * creating this Rule instance, shouldn't + * be set if instantiating the Rule object + * manually. */ - public function __construct(HTML_QuickForm2_Node $owner, $message = '', $config = null) + public function __construct(HTML_QuickForm2_Node $owner, $message = '', + $options = null, $registeredType = null) { $this->setOwner($owner); $this->setMessage($message); - $this->setConfig($config); + $this->setOptions($options); + $this->registeredType = $registeredType; } /** - * Sets the configuration data for the rule + * Sets additional configuration data for the rule * * @param mixed Rule configuration data (rule-dependent) * @return HTML_QuickForm2_Rule */ - public function setConfig($config) + public function setOptions($options) { - $this->config = $config; + $this->options = $options; return $this; } @@ -118,9 +134,9 @@ * * @return mixed */ - public function getConfig() + public function getOptions() { - return $this->config; + return $this->options; } /** @@ -157,9 +173,9 @@ } /** - * Adds a rule to the chain with an "and" operator + * Adds a rule to the chain with an "and" operator * - * Evaluation is short-circuited, next rule will not be evaluated if the + * Evaluation is short-circuited, next rule will not be evaluated if the * previous one returns false. The method is named this way because "and" is * a reserved word in PHP. * @@ -173,9 +189,9 @@ } /** - * Adds a rule to the chain with an "or" operator + * Adds a rule to the chain with an "or" operator * - * Evaluation is short-circuited, next rule will not be evaluated if the + * Evaluation is short-circuited, next rule will not be evaluated if the * previous one returns true. The method is named this way because "or" is * a reserved word in PHP. * @@ -199,11 +215,7 @@ public function validate() { $globalValid = false; - if (!($localValid = $this->checkValue($this->owner->getValue())) && - strlen($this->message) && !$this->owner->getError()) - { - $this->owner->setError($this->message); - } + $localValid = $this->checkValue($this->owner->getValue()); foreach ($this->chainedRules as $item) { foreach ($item as $multiplier) { if (!$localValid) { @@ -217,6 +229,9 @@ } $localValid = true; } + if (!$globalValid && strlen($this->message) && !$this->owner->getError()) { + $this->owner->setError($this->message); + } return $globalValid; } http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/tests/QuickForm2/FactoryTest.php?r1=1.9&r2=1.10&diff_format=u Index: pear/HTML_QuickForm2/tests/QuickForm2/FactoryTest.php diff -u pear/HTML_QuickForm2/tests/QuickForm2/FactoryTest.php:1.9 pear/HTML_QuickForm2/tests/QuickForm2/FactoryTest.php:1.10 --- pear/HTML_QuickForm2/tests/QuickForm2/FactoryTest.php:1.9 Sat Jun 30 17:39:41 2007 +++ pear/HTML_QuickForm2/tests/QuickForm2/FactoryTest.php Wed Jul 4 14:58:17 2007 @@ -5,7 +5,7 @@ * PHP version 5 * * LICENSE: - * + * * Copyright (c) 2006, 2007, Alexey Borzov <avb@php.net>, * Bertrand Mansion <golgote@mamasam.com> * All rights reserved. @@ -17,9 +17,9 @@ * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the + * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * * The names of the authors may not be used to endorse or promote products + * * The names of the authors may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS @@ -39,7 +39,7 @@ * @author Alexey Borzov <avb@php.net> * @author Bertrand Mansion <golgote@mamasam.com> * @license http://opensource.org/licenses/bsd-license.php New BSD License - * @version CVS: $Id: FactoryTest.php,v 1.9 2007/06/30 17:39:41 avb Exp $ + * @version CVS: $Id: FactoryTest.php,v 1.10 2007/07/04 14:58:17 avb Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ @@ -53,6 +53,10 @@ */ require_once 'PHPUnit/Framework/TestCase.php'; +/** + * Element class + */ +require_once 'HTML/QuickForm2/Node.php'; /** * Unit test for HTML_QuickForm2_Factory class @@ -121,12 +125,120 @@ public function testCreateElementValid() { HTML_QuickForm2_Factory::registerElement('fakeelement', 'FakeElement', dirname(__FILE__) . '/_files/FakeElement.php'); - $el = HTML_QuickForm2_Factory::createElement('fakeelement', + $el = HTML_QuickForm2_Factory::createElement('fakeelement', 'fake', 'attributes', array('options' => '', 'label' => 'fake label')); $this->assertType('FakeElement', $el); $this->assertEquals('fake', $el->name); $this->assertEquals(array('options' => '', 'label' => 'fake label'), $el->data); $this->assertEquals('attributes', $el->attributes); } + + public function testNotRegisteredRule() + { + $this->assertFalse(HTML_QuickForm2_Factory::isRuleRegistered('foo_' . mt_rand())); + } + + public function testRuleNameCaseInsensitive() + { + HTML_QuickForm2_Factory::registerRule('fOo', 'RuleClassname'); + $this->assertTrue(HTML_QuickForm2_Factory::isRuleRegistered('FOO')); + $this->assertTrue(HTML_QuickForm2_Factory::isRuleRegistered('foo')); + } + + public function testGetRuleConfig() + { + HTML_QuickForm2_Factory::registerRule('foobar', 'FooBar', null, 'Some config'); + $this->assertEquals('Some config', HTML_QuickForm2_Factory::getRuleConfig('foobar')); + + try { + $config = HTML_QuickForm2_Factory::getRuleConfig('foobar_' . mt_rand()); + } catch (HTML_QuickForm2_InvalidArgumentException $e) { + $this->assertRegexp('/Rule(.*)is not known/', $e->getMessage()); + return; + } + $this->fail('Expected HTML_QuickForm2_InvalidArgumentException was not thrown'); + } + + public function testCreateNotRegisteredRule() + { + $mockNode = $this->getMock( + 'HTML_QuickForm2_Node', array('updateValue', 'getId', 'getName', + 'getType', 'getValue', 'setId', 'setName', 'setValue', '__toString') + ); + try { + $rule = HTML_QuickForm2_Factory::createRule('foo2', $mockNode); + } catch (HTML_QuickForm2_InvalidArgumentException $e) { + $this->assertRegexp('/Rule(.*)is not known/', $e->getMessage()); + return; + } + $this->fail('Expected HTML_QuickForm2_InvalidArgumentException was not thrown'); + } + + public function testCreateRuleNonExistingClass() + { + $mockNode = $this->getMock( + 'HTML_QuickForm2_Node', array('updateValue', 'getId', 'getName', + 'getType', 'getValue', 'setId', 'setName', 'setValue', '__toString') + ); + HTML_QuickForm2_Factory::registerRule('foo3', 'NonexistentClass'); + try { + $rule = HTML_QuickForm2_Factory::createRule('foo3', $mockNode); + } catch (HTML_QuickForm2_NotFoundException $e) { + $this->assertRegexp('/Class(.*)does not exist and no file to load/', $e->getMessage()); + return; + } + $this->fail('Expected HTML_QuickForm2_NotFoundException was not thrown'); + } + + public function testCreateRuleNonExistingFile() + { + $mockNode = $this->getMock( + 'HTML_QuickForm2_Node', array('updateValue', 'getId', 'getName', + 'getType', 'getValue', 'setId', 'setName', 'setValue', '__toString') + ); + HTML_QuickForm2_Factory::registerRule('foo4', 'NonexistentClass', 'NonexistentFile.php'); + try { + $rule = HTML_QuickForm2_Factory::createRule('foo4', $mockNode); + } catch (HTML_QuickForm2_NotFoundException $e) { + $this->assertRegexp('/File(.*)was not found/', $e->getMessage()); + return; + } + $this->fail('Expected HTML_QuickForm2_NotFoundException was not thrown'); + } + + public function testCreateRuleInvalidFile() + { + $mockNode = $this->getMock( + 'HTML_QuickForm2_Node', array('updateValue', 'getId', 'getName', + 'getType', 'getValue', 'setId', 'setName', 'setValue', '__toString') + ); + HTML_QuickForm2_Factory::registerRule('foo5', 'NonexistentClass', dirname(__FILE__) . '/_files/InvalidFile.php'); + try { + $rule = HTML_QuickForm2_Factory::createRule('foo5', $mockNode); + } catch (HTML_QuickForm2_NotFoundException $e) { + $this->assertRegexp('/Class(.*)was not found within file(.*)/', $e->getMessage()); + return; + } + $this->fail('Expected HTML_QuickForm2_NotFoundException was not thrown'); + } + + public function testCreateRuleValid() + { + $mockNode = $this->getMock( + 'HTML_QuickForm2_Node', array('updateValue', 'getId', 'getName', + 'getType', 'getValue', 'setId', 'setName', 'setValue', '__toString') + ); + HTML_QuickForm2_Factory::registerRule( + 'fakerule', 'FakeRule', dirname(__FILE__) . '/_files/FakeRule.php' + ); + $rule = HTML_QuickForm2_Factory::createRule( + 'fakerule', $mockNode, 'An error message', 'Some options' + ); + $this->assertType('FakeRule', $rule); + $this->assertSame($mockNode, $rule->owner); + $this->assertEquals('An error message', $rule->getMessage()); + $this->assertEquals('Some options', $rule->getOptions()); + $this->assertEquals('fakerule', $rule->registeredType); + } } ?> \ No newline at end of file http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/tests/QuickForm2/RuleTest.php?r1=1.1&r2=1.2&diff_format=u Index: pear/HTML_QuickForm2/tests/QuickForm2/RuleTest.php diff -u pear/HTML_QuickForm2/tests/QuickForm2/RuleTest.php:1.1 pear/HTML_QuickForm2/tests/QuickForm2/RuleTest.php:1.2 --- pear/HTML_QuickForm2/tests/QuickForm2/RuleTest.php:1.1 Sun May 13 21:30:35 2007 +++ pear/HTML_QuickForm2/tests/QuickForm2/RuleTest.php Wed Jul 4 14:58:17 2007 @@ -5,7 +5,7 @@ * PHP version 5 * * LICENSE: - * + * * Copyright (c) 2006, 2007, Alexey Borzov <avb@php.net>, * Bertrand Mansion <golgote@mamasam.com> * All rights reserved. @@ -17,9 +17,9 @@ * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the + * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * * The names of the authors may not be used to endorse or promote products + * * The names of the authors may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS @@ -38,7 +38,7 @@ * @package HTML_QuickForm2 * @author Alexey Borzov <avb@php.net> * @license http://opensource.org/licenses/bsd-license.php New BSD License - * @version CVS: $Id: RuleTest.php,v 1.1 2007/05/13 21:30:35 avb Exp $ + * @version CVS: $Id: RuleTest.php,v 1.2 2007/07/04 14:58:17 avb Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ @@ -53,7 +53,7 @@ require_once 'PHPUnit/Framework/TestCase.php'; /** - * Class for <input type="text" /> elements + * Class for <input type="text" /> elements * * We need a subclass of Node that can be instantiated */ @@ -66,8 +66,8 @@ { protected function checkValue($value) { - // It just returns whatever value was passed to setConfig() - return $this->config; + // It just returns whatever value was passed to setOptions() + return $this->options; } } @@ -76,15 +76,15 @@ */ class HTML_QuickForm2_RuleTest extends PHPUnit_Framework_TestCase { - public function testSetAndGetConfig() + public function testSetAndGetOptions() { $rule = new HTML_QuickForm2_Rule_ImplConst( new HTML_QuickForm2_Element_InputText('foo'), 'a message', 'bar' ); - $this->assertEquals('bar', $rule->getConfig()); + $this->assertEquals('bar', $rule->getOptions()); - $this->assertSame($rule, $rule->setConfig('baz')); - $this->assertEquals('baz', $rule->getConfig()); + $this->assertSame($rule, $rule->setOptions('baz')); + $this->assertEquals('baz', $rule->getOptions()); } public function testSetAndGetMessage() @@ -116,7 +116,7 @@ $elTest = new HTML_QuickForm2_Element_InputText('testAndOr'); $ruleAnd = new HTML_QuickForm2_Rule_ImplConst($elTest, 'a message', true); $ruleAnd->and_(new HTML_QuickForm2_Rule_ImplConst($elTest, 'a message', false)); - $this->assertFalse($ruleAnd->validate()); + $this->assertFalse($ruleAnd->validate()); $ruleOr = new HTML_QuickForm2_Rule_ImplConst($elTest, 'a message', false); $ruleOr->or_(new HTML_QuickForm2_Rule_ImplConst($elTest, 'a message', true)); @@ -179,5 +179,21 @@ $ruleFalse->or_($ruleOrFalse); $ruleFalse->validate(); } + + public function testSetErrorOnlyOnChainFailure() + { + $elTest = new HTML_QuickForm2_Element_InputText('valid'); + $chain = new HTML_QuickForm2_Rule_ImplConst($elTest, 'bogus error', false); + $chain->or_(new HTML_QuickForm2_Rule_ImplConst($elTest, '', true)); + + $this->assertTrue($chain->validate()); + $this->assertEquals('', $elTest->getError()); + + $chain2 = new HTML_QuickForm2_Rule_ImplConst($elTest, 'genuine error', false); + $chain2->or_(new HTML_QuickForm2_Rule_ImplConst($elTest, '', false)); + + $this->assertFalse($chain2->validate()); + $this->assertEquals('genuine error', $elTest->getError()); + } } ?> http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/tests/QuickForm2/_files/FakeRule.php?view=markup&rev=1.1 Index: pear/HTML_QuickForm2/tests/QuickForm2/_files/FakeRule.php +++ pear/HTML_QuickForm2/tests/QuickForm2/_files/FakeRule.php <?php /** * Base class for HTML_QuickForm2 rules */ require_once 'HTML/QuickForm2/Rule.php'; /** * Class used to test creation of rules via HTML_QuickForm2_Factory::createRule() */ class FakeRule extends HTML_QuickForm2_Rule { public $owner; public $registeredType; protected function checkValue($value) { return true; } } ?>
« previous php.pear.cvs (#47855) next »