Skip to content
This repository has been archived by the owner on Nov 26, 2017. It is now read-only.

Commit

Permalink
Add type hinting to JFormRule. Fix an E_STRICT error in the process.
Browse files Browse the repository at this point in the history
  • Loading branch information
realityking committed Apr 17, 2012
1 parent df5218d commit e5c0ec8
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 33 deletions.
6 changes: 3 additions & 3 deletions libraries/joomla/form/field.php
Expand Up @@ -184,7 +184,7 @@ abstract class JFormField
/**
* Method to instantiate the form field object.
*
* @param object $form The form to attach to the form field object.
* @param JForm $form The form to attach to the form field object.
*
* @since 11.1
*/
Expand Down Expand Up @@ -298,10 +298,10 @@ public function setForm(JForm $form)
*
* @since 11.1
*/
public function setup($element, $value, $group = null)
public function setup(SimpleXMLElement $element, $value, $group = null)
{
// Make sure there is a valid JFormField XML element.
if (!($element instanceof SimpleXMLElement) || (string) $element->getName() != 'field')
if ((string) $element->getName() != 'field')
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/joomla/form/rule.php
Expand Up @@ -56,7 +56,7 @@ class JFormRule
* @since 11.1
* @throws UnexpectedValueException if rule is invalid.
*/
public function test($element, $value, $group = null, $input = null, $form = null)
public function test(SimpleXMLElement $element, $value, $group = null, JRegistry $input = null, JForm $form = null)
{
// Check for a valid regex.
if (empty($this->regex))
Expand Down
2 changes: 1 addition & 1 deletion libraries/joomla/form/rules/color.php
Expand Up @@ -33,7 +33,7 @@ class JFormRuleColor extends JFormRule
*
* @since 11.2
*/
public function test($element, $value, $group = null, $input = null, $form = null)
public function test(SimpleXMLElement $element, $value, $group = null, JRegistry $input = null, JForm $form = null)
{
$value = trim($value);

Expand Down
2 changes: 1 addition & 1 deletion libraries/joomla/form/rules/email.php
Expand Up @@ -41,7 +41,7 @@ class JFormRuleEmail extends JFormRule
*
* @since 11.1
*/
public function test($element, $value, $group = null, $input = null, $form = null)
public function test(SimpleXMLElement $element, $value, $group = null, JRegistry $input = null, JForm $form = null)
{
// If the field is empty and not required, the field is valid.
$required = ((string) $element['required'] == 'true' || (string) $element['required'] == 'required');
Expand Down
8 changes: 4 additions & 4 deletions libraries/joomla/form/rules/equals.php
Expand Up @@ -35,8 +35,9 @@ class JFormRuleEquals extends JFormRule
*
* @since 11.1
* @throws InvalidArgumentException
* @throws UnexpectedValueException
*/
public function test($element, $value, $group = null, $input = null, JForm $form = null)
public function test(SimpleXMLElement $element, $value, $group = null, JRegistry $input = null, JForm $form = null)
{
// Initialize variables.
$field = (string) $element['field'];
Expand All @@ -47,10 +48,9 @@ public function test($element, $value, $group = null, $input = null, JForm $form
throw new UnexpectedValueException(sprintf('$field empty in %s::test', get_class($this)));
}

// Check that a valid JForm object is given for retrieving the validation field value.
if (!($form instanceof JForm))
if (is_null($form))
{
throw new InvalidArgumentException(sprintf('%s::test(element, value, group, input, *%s*)', get_class($this), gettype($form)));
throw new InvalidArgumentException(sprintf('The value for $form must not be null in %s', get_class($this)));
}

// Test the two values against each other.
Expand Down
2 changes: 1 addition & 1 deletion libraries/joomla/form/rules/options.php
Expand Up @@ -33,7 +33,7 @@ class JFormRuleOptions extends JFormRule
*
* @since 11.1
*/
public function test($element, $value, $group = null, $input = null, $form = null)
public function test(SimpleXMLElement $element, $value, $group = null, JRegistry $input = null, JForm $form = null)
{
// Check each value and return true if we get a match
foreach ($element->option as $option)
Expand Down
4 changes: 2 additions & 2 deletions libraries/joomla/form/rules/rules.php
Expand Up @@ -33,7 +33,7 @@ class JFormRuleRules extends JFormRule
*
* @since 11.1
*/
public function test($element, $value, $group = null, $input = null, $form = null)
public function test(SimpleXMLElement $element, $value, $group = null, JRegistry $input = null, JForm $form = null)
{
// Get the possible field actions and the ones posted to validate them.
$fieldActions = self::getFieldActions($element);
Expand Down Expand Up @@ -84,7 +84,7 @@ protected function getValueActions($value)
*
* @since 11.1
*/
protected function getFieldActions($element)
protected function getFieldActions(SimpleXMLElement $element)
{
// Initialise variables.
$actions = array();
Expand Down
2 changes: 1 addition & 1 deletion libraries/joomla/form/rules/tel.php
Expand Up @@ -33,7 +33,7 @@ class JFormRuleTel extends JFormRule
*
* @since 11.1
*/
public function test($element, $value, $group = null, $input = null, $form = null)
public function test(SimpleXMLElement $element, $value, $group = null, JRegistry $input = null, JForm $form = null)
{
// If the field is empty and not required, the field is valid.
$required = ((string) $element['required'] == 'true' || (string) $element['required'] == 'required');
Expand Down
2 changes: 1 addition & 1 deletion libraries/joomla/form/rules/url.php
Expand Up @@ -35,7 +35,7 @@ class JFormRuleUrl extends JFormRule
* @link http://www.w3.org/Addressing/URL/url-spec.txt
* @see Jstring
*/
public function test($element, $value, $group = null, $input = null, $form = null)
public function test(SimpleXMLElement $element, $value, $group = null, JRegistry $input = null, JForm $form = null)
{
// If the field is empty and not required, the field is valid.
$required = ((string) $element['required'] == 'true' || (string) $element['required'] == 'required');
Expand Down
2 changes: 1 addition & 1 deletion libraries/joomla/form/rules/username.php
Expand Up @@ -33,7 +33,7 @@ class JFormRuleUsername extends JFormRule
*
* @since 11.1
*/
public function test($element, $value, $group = null, $input = null, $form = null)
public function test(SimpleXMLElement $element, $value, $group = null, JRegistry $input = null, JForm $form = null)
{
// Get the database object and a new query object.
$db = JFactory::getDBO();
Expand Down
58 changes: 41 additions & 17 deletions tests/suites/unit/joomla/form/JFormFieldTest.php
Expand Up @@ -11,7 +11,7 @@
require_once JPATH_PLATFORM.'/joomla/form/field.php';

/**
* Test class for JForm.
* Test class for JFormField.
*
* @package Joomla.UnitTest
* @subpackage Form
Expand Down Expand Up @@ -41,7 +41,9 @@ function tearDown()
}

/**
* Tests the JForm::__construct method
* Tests the JFormField::__construct method
*
* @covers JFormField::__construct
*/
public function testConstruct()
{
Expand Down Expand Up @@ -96,15 +98,17 @@ public function testConstruct()
}

/**
* Tests the JForm::__get method
* Tests the JFormField::__get method
*/
public function testGet()
{
// Tested in testSetup.
}

/**
* Tests the JForm::GetId method
* Tests the JFormField::GetId method
*
* @covers JFormField::getId
*/
public function testGetId()
{
Expand Down Expand Up @@ -138,15 +142,17 @@ public function testGetId()
}

/**
* Tests the JForm::getInput method
* Tests the JFormField::getInput method
*/
public function testGetInput()
{
// Tested in actual field types because this is an abstract method.
}

/**
* Tests the JForm::getLabel method
* Tests the JFormField::getLabel method
*
* @covers JFormField::getLabel
*/
public function testGetLabel()
{
Expand Down Expand Up @@ -211,6 +217,8 @@ public function testGetLabel()

/**
* Tests the JFormField::getTitle method
*
* @covers JFormField::getTitle
*/
public function testGetTitle()
{
Expand Down Expand Up @@ -259,7 +267,9 @@ public function testGetTitle()
}

/**
* Tests the JForm::setForm method
* Tests the JFormField::setForm method
*
* @covers JFormField::setForm
*/
public function testSetForm()
{
Expand All @@ -277,7 +287,30 @@ public function testSetForm()
}

/**
* Tests the JForm::setup method
* Test an invalid argument for the JFormField::setup method
*
* @covers JFormField::setup
* @expectedException PHPUnit_Framework_Error
*/
public function testSetupInvalidElement()
{
$form = new JFormInspector('form1');
$field = new JFormFieldInspector($form);

$wrong = 'wrong';
$this->assertThat(
$field->setup($wrong, 0),
$this->isFalse(),
'Line:'.__LINE__.' If not a form object, setup should return false.'
);

}

/**
* Tests the JFormField::setup method
*
* @covers JFormField::setup
* @covers JFormField::__get
*/
public function testSetup()
{
Expand All @@ -291,15 +324,6 @@ public function testSetup()

$field = new JFormFieldInspector($form);

// Error handling.

$wrong = 'wrong';
$this->assertThat(
$field->setup($wrong, 0),
$this->isFalse(),
'Line:'.__LINE__.' If not a form object, setup should return false.'
);

// Standard usage.

$xml = $form->getXML();
Expand Down

0 comments on commit e5c0ec8

Please sign in to comment.