cvs: pear /HTML_QuickForm2/QuickForm2 Factory.php Node.php Rule.php /HTML_QuickForm2/QuickForm2/Rule Nonempty.php Required.php
/HTML_QuickForm2/tests/QuickForm2 AllTests.php NodeTest.php /HTML_QuickForm2/tests/QuickForm2/Rule AllTests.php NonemptyTest.php RequiredTest.php

From: Date: Thu, 05 Jul 2007 18:27:16 +0000
Subject: cvs: pear /HTML_QuickForm2/QuickForm2 Factory.php Node.php Rule.php /HTML_QuickForm2/QuickForm2/Rule Nonempty.php Required.php
/HTML_QuickForm2/tests/QuickForm2 AllTests.php NodeTest.php /HTML_QuickForm2/tests/QuickForm2/Rule AllTests.php NonemptyTest.php RequiredTest.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-47876@lists.php.net to get a copy of this message
avb Thu Jul 5 18:27:16 2007 UTC Added files: /pear/HTML_QuickForm2/QuickForm2/Rule Nonempty.php Required.php /pear/HTML_QuickForm2/tests/QuickForm2/Rule AllTests.php NonemptyTest.php RequiredTest.php Modified files: /pear/HTML_QuickForm2/QuickForm2 Factory.php Node.php Rule.php /pear/HTML_QuickForm2/tests/QuickForm2 AllTests.php NodeTest.php Log: Added "nonempty" and "required" Rules

http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/QuickForm2/Factory.php?r1=1.16&r2=1.17&diff_format=u Index: pear/HTML_QuickForm2/QuickForm2/Factory.php diff -u pear/HTML_QuickForm2/QuickForm2/Factory.php:1.16 pear/HTML_QuickForm2/QuickForm2/Factory.php:1.17 --- pear/HTML_QuickForm2/QuickForm2/Factory.php:1.16 Wed Jul 4 14:58:17 2007 +++ pear/HTML_QuickForm2/QuickForm2/Factory.php Thu Jul 5 18:27:16 2007 @@ -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: Factory.php,v 1.16 2007/07/04 14:58:17 avb Exp $ + * @version CVS: $Id: Factory.php,v 1.17 2007/07/05 18:27:16 avb Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ @@ -102,6 +102,10 @@ * @var array */ protected static $registeredRules = array( + 'nonempty' => array('HTML_QuickForm2_Rule_Nonempty', + 'HTML/QuickForm2/Rule/Nonempty.php'), + 'required' => array('HTML_QuickForm2_Rule_Required', + 'HTML/QuickForm2/Rule/Required.php') ); http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/QuickForm2/Node.php?r1=1.8&r2=1.9&diff_format=u Index: pear/HTML_QuickForm2/QuickForm2/Node.php diff -u pear/HTML_QuickForm2/QuickForm2/Node.php:1.8 pear/HTML_QuickForm2/QuickForm2/Node.php:1.9 --- pear/HTML_QuickForm2/QuickForm2/Node.php:1.8 Wed Jul 4 14:58:17 2007 +++ pear/HTML_QuickForm2/QuickForm2/Node.php Thu Jul 5 18:27:16 2007 @@ -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: Node.php,v 1.8 2007/07/04 14:58:17 avb Exp $ + * @version CVS: $Id: Node.php,v 1.9 2007/07/05 18:27:16 avb Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ @@ -448,6 +448,22 @@ /** + * Checks whether an element is required + * + * @return boolean + */ + public function isRequired() + { + foreach ($this->rules as $rule) { + if ($rule instanceof HTML_QuickForm2_Rule_Required) { + return true; + } + } + return false; + } + + + /** * Performs the server-side validation * * @return boolean Whether the element is valid http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/QuickForm2/Rule.php?r1=1.2&r2=1.3&diff_format=u Index: pear/HTML_QuickForm2/QuickForm2/Rule.php diff -u pear/HTML_QuickForm2/QuickForm2/Rule.php:1.2 pear/HTML_QuickForm2/QuickForm2/Rule.php:1.3 --- pear/HTML_QuickForm2/QuickForm2/Rule.php:1.2 Wed Jul 4 14:58:17 2007 +++ pear/HTML_QuickForm2/QuickForm2/Rule.php Thu Jul 5 18:27:16 2007 @@ -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.2 2007/07/04 14:58:17 avb Exp $ + * @version CVS: $Id: Rule.php,v 1.3 2007/07/05 18:27:16 avb Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ @@ -181,9 +181,16 @@ * * @param HTML_QuickForm2_Rule * @return HTML_QuickForm2_Rule first rule in the chain (i.e. $this) + * @throws HTML_QuickForm2_InvalidArgumentException when trying to add + * a "required" rule to the chain */ public function and_(HTML_QuickForm2_Rule $next) { + if ($next instanceof HTML_QuickForm2_Rule_Required) { + throw new HTML_QuickForm2_InvalidArgumentException( + 'and_(): Cannot add a "required" rule' + ); + } $this->chainedRules[count($this->chainedRules) - 1][] = $next; return $this; } @@ -197,9 +204,16 @@ * * @param HTML_QuickForm2_Rule * @return HTML_QuickForm2_Rule first rule in the chain (i.e. $this) + * @throws HTML_QuickForm2_InvalidArgumentException when trying to add + * a "required" rule to the chain */ public function or_(HTML_QuickForm2_Rule $next) { + if ($next instanceof HTML_QuickForm2_Rule_Required) { + throw new HTML_QuickForm2_InvalidArgumentException( + 'or_(): Cannot add a "required" rule' + ); + } $this->chainedRules[] = array($next); return $this; } http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/tests/QuickForm2/AllTests.php?r1=1.12&r2=1.13&diff_format=u Index: pear/HTML_QuickForm2/tests/QuickForm2/AllTests.php diff -u pear/HTML_QuickForm2/tests/QuickForm2/AllTests.php:1.12 pear/HTML_QuickForm2/tests/QuickForm2/AllTests.php:1.13 --- pear/HTML_QuickForm2/tests/QuickForm2/AllTests.php:1.12 Fri Jun 29 14:42:55 2007 +++ pear/HTML_QuickForm2/tests/QuickForm2/AllTests.php Thu Jul 5 18:27:16 2007 @@ -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: AllTests.php,v 1.12 2007/06/29 14:42:55 mansion Exp $ + * @version CVS: $Id: AllTests.php,v 1.13 2007/07/05 18:27:16 avb Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ @@ -60,6 +60,7 @@ require_once dirname(__FILE__) . '/Container/AllTests.php'; require_once dirname(__FILE__) . '/DataSource/AllTests.php'; require_once dirname(__FILE__) . '/RuleTest.php'; +require_once dirname(__FILE__) . '/Rule/AllTests.php'; class QuickForm2_AllTests { @@ -81,6 +82,7 @@ $suite->addTest(QuickForm2_Element_AllTests::suite()); $suite->addTest(QuickForm2_Container_AllTests::suite()); $suite->addTest(QuickForm2_DataSource_AllTests::suite()); + $suite->addTest(QuickForm2_Rule_AllTests::suite()); return $suite; } http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/tests/QuickForm2/NodeTest.php?r1=1.6&r2=1.7&diff_format=u Index: pear/HTML_QuickForm2/tests/QuickForm2/NodeTest.php diff -u pear/HTML_QuickForm2/tests/QuickForm2/NodeTest.php:1.6 pear/HTML_QuickForm2/tests/QuickForm2/NodeTest.php:1.7 --- pear/HTML_QuickForm2/tests/QuickForm2/NodeTest.php:1.6 Sat Jun 30 17:39:41 2007 +++ pear/HTML_QuickForm2/tests/QuickForm2/NodeTest.php Thu Jul 5 18:27:16 2007 @@ -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: NodeTest.php,v 1.6 2007/06/30 17:39:41 avb Exp $ + * @version CVS: $Id: NodeTest.php,v 1.7 2007/07/05 18:27:16 avb Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ @@ -200,5 +200,11 @@ $this->assertFalse($manyRules->validate()); $this->assertEquals('some error', $manyRules->getError()); } + + public function testElementIsNotRequiredByDefault() + { + $node = new HTML_QuickForm2_NodeImpl(); + $this->assertFalse($node->isRequired()); + } } ?> http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/QuickForm2/Rule/Nonempty.php?view=markup&rev=1.1 Index: pear/HTML_QuickForm2/QuickForm2/Rule/Nonempty.php +++ pear/HTML_QuickForm2/QuickForm2/Rule/Nonempty.php <?php /** * Rule checking that the field is not empty * * PHP version 5 * * LICENSE: * * Copyright (c) 2006, 2007, Alexey Borzov <avb@php.net>, * Bertrand Mansion <golgote@mamasam.com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * 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 * documentation and/or other materials provided with the distribution. * * 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 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @category HTML * @package HTML_QuickForm2 * @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: Nonempty.php,v 1.1 2007/07/05 18:27:16 avb Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ /** * Base class for HTML_QuickForm2 rules */ require_once 'HTML/QuickForm2/Rule.php'; /** * Rule checking that the field is not empty * * Handles both simple form fields and file uploads * * @category HTML * @package HTML_QuickForm2 * @author Alexey Borzov <avb@php.net> * @author Bertrand Mansion <golgote@mamasam.com> * @version Release: @package_version@ */ class HTML_QuickForm2_Rule_Nonempty extends HTML_QuickForm2_Rule { protected function checkValue($value) { if (!$this->owner instanceof HTML_QuickForm2_Element_InputFile) { return (bool)strlen($value); } else { return isset($value['error']) && (UPLOAD_ERR_OK == $value['error']); } } } ?> http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/QuickForm2/Rule/Required.php?view=markup&rev=1.1 Index: pear/HTML_QuickForm2/QuickForm2/Rule/Required.php +++ pear/HTML_QuickForm2/QuickForm2/Rule/Required.php <?php /** * Rule for required elements * * PHP version 5 * * LICENSE: * * Copyright (c) 2006, 2007, Alexey Borzov <avb@php.net>, * Bertrand Mansion <golgote@mamasam.com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * 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 * documentation and/or other materials provided with the distribution. * * 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 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @category HTML * @package HTML_QuickForm2 * @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: Required.php,v 1.1 2007/07/05 18:27:16 avb Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ /** * Rule checking that the form field is not empty */ require_once 'HTML/QuickForm2/Rule/Nonempty.php'; /** * Rule for required elements * * The main difference from "nonempty" Rule is that * - elements to which this Rule is attached will be considered required * ({@link HTML_QuickForm2_Node::isRequired()} will return true for them) and * marked accordingly when outputting the form * - this Rule can only be added directly to the element and other Rules can * only be added to it via and_() method * * @category HTML * @package HTML_QuickForm2 * @author Alexey Borzov <avb@php.net> * @author Bertrand Mansion <golgote@mamasam.com> * @version Release: @package_version@ */ class HTML_QuickForm2_Rule_Required extends HTML_QuickForm2_Rule_Nonempty { /** * Disallows adding a rule to the chain with an "or" operator * * Required rules are different from all others because they affect the * visual representation of an element ("* denotes required field"). * Therefore we cannot allow chaining other rules to these via or_(), since * this will effectively mean that the field is not required anymore and the * visual difference is bogus. * * @param HTML_QuickForm2_Rule * @throws HTML_QuickForm2_Exception */ public function or_(HTML_QuickForm2_Rule $next) { throw new HTML_QuickForm2_Exception( 'or_(): Cannot add a rule to "required" rule' ); } } ?> http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/tests/QuickForm2/Rule/AllTests.php?view=markup&rev=1.1 Index: pear/HTML_QuickForm2/tests/QuickForm2/Rule/AllTests.php +++ pear/HTML_QuickForm2/tests/QuickForm2/Rule/AllTests.php <?php /** * Unit tests for HTML_QuickForm2 package * * PHP version 5 * * LICENSE: * * Copyright (c) 2006, 2007, Alexey Borzov <avb@php.net>, * Bertrand Mansion <golgote@mamasam.com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * 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 * documentation and/or other materials provided with the distribution. * * 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 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @category HTML * @package HTML_QuickForm2 * @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: AllTests.php,v 1.1 2007/07/05 18:27:16 avb Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'QuickForm2_Rule_AllTests::main'); } require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once dirname(__FILE__) . '/NonemptyTest.php'; require_once dirname(__FILE__) . '/RequiredTest.php'; class QuickForm2_Rule_AllTests { public static function main() { PHPUnit_TextUI_TestRunner::run(self::suite()); } public static function suite() { $suite = new PHPUnit_Framework_TestSuite('HTML_QuickForm2 package - QuickForm2 - Rule'); $suite->addTestSuite('HTML_QuickForm2_Rule_NonemptyTest'); $suite->addTestSuite('HTML_QuickForm2_Rule_RequiredTest'); return $suite; } } if (PHPUnit_MAIN_METHOD == 'QuickForm2_Rule_AllTests::main') { QuickForm2_Rule_AllTests::main(); } ?> http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/tests/QuickForm2/Rule/NonemptyTest.php?view=markup&rev=1.1 Index: pear/HTML_QuickForm2/tests/QuickForm2/Rule/NonemptyTest.php +++ pear/HTML_QuickForm2/tests/QuickForm2/Rule/NonemptyTest.php <?php /** * Unit tests for HTML_QuickForm2 package * * PHP version 5 * * LICENSE: * * Copyright (c) 2006, 2007, Alexey Borzov <avb@php.net>, * Bertrand Mansion <golgote@mamasam.com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * 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 * documentation and/or other materials provided with the distribution. * * 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 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @category HTML * @package HTML_QuickForm2 * @author Alexey Borzov <avb@php.net> * @license http://opensource.org/licenses/bsd-license.php New BSD License * @version CVS: $Id: NonemptyTest.php,v 1.1 2007/07/05 18:27:16 avb Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ /** * PHPUnit2 Test Case */ require_once 'PHPUnit/Framework/TestCase.php'; /** * Rule checking that the form field is not empty */ require_once 'HTML/QuickForm2/Rule/Nonempty.php'; /** * Class for <input type="file" /> elements */ require_once 'HTML/QuickForm2/Element/InputFile.php'; /** * Unit test for HTML_QuickForm2_Rule_Nonempty class */ class HTML_QuickForm2_Rule_NonemptyTest extends PHPUnit_Framework_TestCase { function testValidateGenericElement() { $mockValid = $this->getMock('HTML_QuickForm2_Element', array('getType', 'getValue', 'setValue', '__toString')); $mockValid->expects($this->once())->method('getValue') ->will($this->returnValue('a string')); $rule = new HTML_QuickForm2_Rule_Nonempty($mockValid, 'an error'); $this->assertTrue($rule->validate()); $this->assertEquals('', $mockValid->getError()); $mockInvalid = $this->getMock('HTML_QuickForm2_Element', array('getType', 'getValue', 'setValue', '__toString')); $mockInvalid->expects($this->once())->method('getValue') ->will($this->returnValue('')); $rule2 = new HTML_QuickForm2_Rule_Nonempty($mockInvalid, 'an error'); $this->assertFalse($rule2->validate()); $this->assertEquals('an error', $mockInvalid->getError()); } function testValidateInputFileElement() { $mockValid = $this->getMock('HTML_QuickForm2_Element_InputFile', array('getValue')); $mockValid->expects($this->once())->method('getValue') ->will($this->returnValue(array( 'name' => 'goodfile.php', 'type' => 'application/octet-stream', 'tmp_name' => '/tmp/foobar', 'error' => UPLOAD_ERR_OK, 'size' => 1234 ))); $rule = new HTML_QuickForm2_Rule_Nonempty($mockValid, 'an error'); $this->assertTrue($rule->validate()); $this->assertEquals('', $mockValid->getError()); $mockInvalid = $this->getMock('HTML_QuickForm2_Element_InputFile', array('getValue')); $mockInvalid->expects($this->once())->method('getValue') ->will($this->returnValue(array( 'name' => '', 'type' => '', 'tmp_name' => '', 'error' => UPLOAD_ERR_NO_FILE, 'size' => 0 ))); $rule2 = new HTML_QuickForm2_Rule_Nonempty($mockInvalid, 'an error'); $this->assertFalse($rule2->validate()); $this->assertEquals('an error', $mockInvalid->getError()); } } ?> http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/tests/QuickForm2/Rule/RequiredTest.php?view=markup&rev=1.1 Index: pear/HTML_QuickForm2/tests/QuickForm2/Rule/RequiredTest.php +++ pear/HTML_QuickForm2/tests/QuickForm2/Rule/RequiredTest.php <?php /** * Unit tests for HTML_QuickForm2 package * * PHP version 5 * * LICENSE: * * Copyright (c) 2006, 2007, Alexey Borzov <avb@php.net>, * Bertrand Mansion <golgote@mamasam.com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * 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 * documentation and/or other materials provided with the distribution. * * 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 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @category HTML * @package HTML_QuickForm2 * @author Alexey Borzov <avb@php.net> * @license http://opensource.org/licenses/bsd-license.php New BSD License * @version CVS: $Id: RequiredTest.php,v 1.1 2007/07/05 18:27:16 avb Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ /** * PHPUnit2 Test Case */ require_once 'PHPUnit/Framework/TestCase.php'; /** * Rule for required form fields */ require_once 'HTML/QuickForm2/Rule/Required.php'; /** * Element class */ require_once 'HTML/QuickForm2/Node.php'; /** * Unit test for HTML_QuickForm2_Rule_Required class */ class HTML_QuickForm2_Rule_RequiredTest extends PHPUnit_Framework_TestCase { public function testMakesElementRequired() { $mockNode = $this->getMock( 'HTML_QuickForm2_Node', array('updateValue', 'getId', 'getName', 'getType', 'getValue', 'setId', 'setName', 'setValue', '__toString') ); $mockNode->addRule(new HTML_QuickForm2_Rule_Required($mockNode, 'element is required')); $this->assertTrue($mockNode->isRequired()); } public function testMustBeFirstInChain() { $mockNode = $this->getMock( 'HTML_QuickForm2_Node', array('updateValue', 'getId', 'getName', 'getType', 'getValue', 'setId', 'setName', 'setValue', '__toString') ); $rule = $mockNode->addRule($this->getMock('HTML_QuickForm2_Rule', array('checkValue'), array($mockNode, 'some message'))); try { $rule->and_(new HTML_QuickForm2_Rule_Required($mockNode, 'element is required')); } catch (HTML_QuickForm2_InvalidArgumentException $e) { $this->assertRegexp('/Cannot add a "required" rule/', $e->getMessage()); try { $rule->or_(new HTML_QuickForm2_Rule_Required($mockNode, 'element is required')); } catch (HTML_QuickForm2_InvalidArgumentException $e) { $this->assertRegexp('/Cannot add a "required" rule/', $e->getMessage()); return; } } $this->fail('Expected HTML_QuickForm2_InvalidArgumentException was not thrown'); } public function testCannotAppendWithOr_() { $mockNode = $this->getMock( 'HTML_QuickForm2_Node', array('updateValue', 'getId', 'getName', 'getType', 'getValue', 'setId', 'setName', 'setValue', '__toString') ); $required = new HTML_QuickForm2_Rule_Required($mockNode, 'element is required'); try { $required->or_($this->getMock('HTML_QuickForm2_Rule', array('checkValue'), array($mockNode, 'some message'))); } catch (HTML_QuickForm2_Exception $e) { $this->assertRegexp('/Cannot add a rule to "required" rule/', $e->getMessage()); return; } $this->fail('Expected HTML_QuickForm2_Exception was not thrown'); } } ?>
« previous php.pear.cvs (#47876) next »