public function testNegatesResult() { $mockEl = $this->getMock('HTML_QuickForm2_Element', array('getType', 'getRawValue', 'setValue', '__toString')); $mockEl->expects($this->atLeastOnce())->method('getRawValue')->will($this->returnValue('foo')); $inArrayOne = new HTML_QuickForm2_Rule_NotCallback($mockEl, 'an error', array('callback' => 'in_array', 'arguments' => array(array('foo', 'bar', 'baz')))); $inArrayTwo = HTML_QuickForm2_Factory::createRule('notcallback', $mockEl, 'an error', array('callback' => 'in_array', 'arguments' => array(array('one', 'two', 'three')))); $this->assertFalse($inArrayOne->validate()); $this->assertTrue($inArrayTwo->validate()); }
public function testConfigOverridesOptions() { $mockEl = $this->getMock('HTML_QuickForm2_Element', array('getType', 'getValue', 'setValue', '__toString')); $mockEl->expects($this->atLeastOnce())->method('getValue')->will($this->returnValue('foo')); HTML_QuickForm2_Factory::registerRule('inarray-override', 'HTML_QuickForm2_Rule_Callback', null, array('callback' => 'in_array', 'arguments' => array(array('foo', 'bar', 'baz')))); $rule1 = HTML_QuickForm2_Factory::createRule('inarray-override', $mockEl, 'an error', array('callback' => array($this, 'checkNotFoo'))); $rule2 = HTML_QuickForm2_Factory::createRule('inarray-override', $mockEl, 'an error', array('arguments' => array(array('one', 'two', 'three')))); $this->assertTrue($rule1->validate()); $this->assertTrue($rule2->validate()); }
public function testConfigOverridesOptions() { $mockEl = $this->getMock('HTML_QuickForm2_Element', array('getType', 'getValue', 'setValue', '__toString')); $mockEl->expects($this->once())->method('getValue')->will($this->returnValue('foo')); HTML_QuickForm2_Factory::registerRule('regex-override', 'HTML_QuickForm2_Rule_Regex', null, '/^[a-zA-Z]+$/'); $override = HTML_QuickForm2_Factory::createRule('regex-override', $mockEl, 'an error', '/^[0-9]+$/'); $this->assertTrue($override->validate()); }
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); }
/** * 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 $type Rule type * @param string $message Message to display if validation fails * @param mixed $options Configuration 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); }
public function testConfigOverridesOptions() { $mockEl = $this->getMock('HTML_QuickForm2_Element', array('getType', 'getValue', 'setValue', '__toString')); $mockEl->expects($this->atLeastOnce())->method('getValue')->will($this->returnValue('foo')); HTML_QuickForm2_Factory::registerRule('compare-override', 'HTML_QuickForm2_Rule_Compare', null, array('operator' => '===', 'operand' => 'foo')); $rule1 = HTML_QuickForm2_Factory::createRule('compare-override', $mockEl, '...', array('operator' => '===', 'operand' => 'bar')); $rule2 = HTML_QuickForm2_Factory::createRule('compare-override', $mockEl, '...', array('operator' => '!==', 'operand' => 'foo')); $this->assertTrue($rule1->validate()); $this->assertTrue($rule2->validate()); }
public function testCreateRuleValid() { $mockNode = $this->getMock('HTML_QuickForm2_Node', $this->nodeAbstractMethods); HTML_QuickForm2_Factory::registerRule('fakerule', 'FakeRule', dirname(__FILE__) . '/_files/FakeRule.php'); /* @var $rule FakeRule */ $rule = HTML_QuickForm2_Factory::createRule('fakerule', $mockNode, 'An error message', 'Some options'); $this->assertInstanceOf('FakeRule', $rule); $this->assertSame($mockNode, $rule->owner); $this->assertEquals('An error message', $rule->getMessage()); $this->assertEquals('Some options', $rule->getConfig()); }