Skip to content

Instantly share code, notes, and snippets.

@chdemko
Created February 20, 2012 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chdemko/1868665 to your computer and use it in GitHub Desktop.
Save chdemko/1868665 to your computer and use it in GitHub Desktop.
Fix unit test for JForm
public function testValidateField()
{
$form = new JFormInspector('form1');
$this->assertThat(
$form->load(JFormDataHelper::$validateFieldDocument),
$this->isTrue(),
'Line:'.__LINE__.' XML string should load successfully.'
);
$xml = $form->getXML();
// Test error handling.
try
{
$result = $form->validateField('wrong');
$this->fail('Line:'.__LINE__.' Passing a non-JXmlElement should return an exception.');
}
catch (InvalidArgumentException $result)
{
$this->assertThat(
$result->getMessage(),
$this->equalTo('JLIB_FORM_ERROR_VALIDATE_FIELD'),
'Line:'.__LINE__.' The correct exception should be returned.'
);
}
try
{
$field = array_pop($xml->xpath('fields/field[@name="missingrule"]'));
$result = $form->validateField($field, null, 'value');
$this->fail('Line:'.__LINE__.' Having a missing validation rule should return an exception.');
}
catch (InvalidArgumentException $result)
{
$this->assertThat(
$result->getMessage(),
$this->equalTo('JLIB_FORM_VALIDATE_FIELD_RULE_MISSING'),
'Line:'.__LINE__.' The correct exception should be returned.'
);
}
try
{
$field = array_pop($xml->xpath('fields/field[@name="boolean"]'));
$result = $form->validateField($field);
$this->fail('Line:'.__LINE__.' A failed validation should return an exception.');
}
catch (RuntimeException $result)
{
$this->assertThat(
$result->getMessage(),
$this->equalTo('JLIB_FORM_VALIDATE_FIELD_INVALID'),
'Line:'.__LINE__.' The correct exception should be returned.'
);
}
try
{
$field = array_pop($xml->xpath('fields/field[@name="required"]'));
$result = $form->validateField($field);
$this->fail('Line:'.__LINE__.' A required field missing a value should return an exception.');
}
catch (RuntimeException $result)
{
$this->assertThat(
$result->getMessage(),
$this->equalTo('JLIB_FORM_VALIDATE_FIELD_REQUIRED'),
'Line:'.__LINE__.' The correct exception should be returned.'
);
}
// Test general usage.
$field = array_pop($xml->xpath('fields/field[@name="boolean"]'));
$this->assertThat(
$form->validateField($field, null, 'true'),
$this->isTrue(),
'Line:'.__LINE__.' A field with a passing validate attribute set should return true.'
);
$field = array_pop($xml->xpath('fields/field[@name="optional"]'));
$this->assertThat(
$form->validateField($field),
$this->isTrue(),
'Line:'.__LINE__.' A field without required set should return true.'
);
$field = array_pop($xml->xpath('fields/field[@name="required"]'));
$this->assertThat(
$form->validateField($field, null, 'value'),
$this->isTrue(),
'Line:'.__LINE__.' A required field with a value should return true.'
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment