Skip to content

Commit

Permalink
Added Tests, renamed validateInput() to validate()
Browse files Browse the repository at this point in the history
  • Loading branch information
FranzWegener committed Aug 19, 2016
1 parent 8e27a1d commit ecae1b9
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/FormElementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ public function setValidators(array $validators);
public function getValidators();

/**
* This function is normally called by Form::validateInput() during backend form validation
* This function is normally called by Form::validate() during backend form validation
* it validates against the Validators that are set
* @return boolean
*/
public function validateInput($input);
public function validate($input);

/**
* Renders the HTML of the FormElement
Expand Down
4 changes: 2 additions & 2 deletions src/FormElements/AbstractFormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ public function setValidators(array $validators)
}

/**
* This function is normally called by Form::validateInput() during backend form validation
* This function is normally called by Form::validate() during backend form validation
* it validates against the Validators that are set
* @return boolean
*/
public function validateInput($input)
public function validate($input)
{
foreach ($this->validators as $validator){
if (!$validator->validate($input)) return false;
Expand Down
9 changes: 9 additions & 0 deletions src/FormElements/CustomHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@
*/
class CustomHtml extends AbstractFormElement implements \Quantumforms\FormElementInterface
{
/**
* @var Contains the FormElement's custom HTML
*/
protected $formElement;

public function __construct($name)
{
parent::__construct($name);
$this->value = '';
}

/**
* {value} will be replaced with set value or empty string
* @return string
Expand Down
2 changes: 1 addition & 1 deletion src/FormInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function setHtmlAfter($string);
* @param array $request
* @return boolean
*/
public function validateInput(array $request);
public function validate(array $request);
/**
* Populates the form fields with the key-value-pairs in the $formValues array
* @param array $formValues
Expand Down
6 changes: 3 additions & 3 deletions src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ public function renderJavascript()
}
/**
* (non-PHPdoc)
* @see \QuantumForms\FormInterface::validateInput()
* @see \QuantumForms\FormInterface::validate()
*/
public function validateInput(array $request)
public function validate(array $request)
{
$errors = [];
foreach ($this->formElements as $element)
{
$result = $element->validateInput($request[$element->getName()]);
$result = $element->validate($request[$element->getName()]);
if (!$result) $errors[] = $element->getName();
}
if (!empty($errors)) return $errors;
Expand Down
3 changes: 3 additions & 0 deletions tests/FormElements/CheckboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public function testRendering()
$this->assertTrue(isset($attributes['data-attrib']), 'data-attrib tag not set');
$this->assertTrue(isset($attributes['data-attrib']) && $attributes['data-attrib'] == 'attributeValue');

$this->assertTrue($this->element->setValue(true) instanceof Checkbox);
$html = $this->element->render();
$this->standardAssertions($html);
}
/**
*
Expand Down
4 changes: 4 additions & 0 deletions tests/FormElements/CustomHtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public function testRendering()
$this->assertEquals($html, $testPrefixedHtml.$testHtml.$testSuffixedHtml);
$this->assertEquals($this->element->getName(), 'test');

$this->element->setHtml('<div>{value}</div>');
$this->assertTrue($this->element->setValue('123Test') instanceof CustomHtml);
$this->assertTrue(is_string($this->element->render()));
$this->assertTrue(strpos($this->element->render(), '123Test') !== false);
}
/**
*
Expand Down
4 changes: 3 additions & 1 deletion tests/FormElements/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public function testRendering()
$this->assertTrue($this->element->setValidators([]) instanceof Input);
$this->assertTrue($this->element->addValidator(new Integer()) instanceof Input);

$this->assertEquals($this->element->validateInput('123'), true);
$this->assertEquals($this->element->validate('123'), true);
$this->assertTrue($this->element->setValue('123') instanceof Input);
$this->assertTrue(is_string($this->element->render()));
}

/**
Expand Down
8 changes: 6 additions & 2 deletions tests/FormElements/RadiobuttonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class RadiobuttonTest extends \PHPUnit_Framework_TestCase
{
/**
* @var QuantumForms\FormElementInterface
* @var FormElementInterface
*/
public $element;
/**
Expand Down Expand Up @@ -57,7 +57,11 @@ public function testRendering()
foreach ($matches[0] as $match){
$this->standardAssertions($match);
}

$this->assertTrue($this->element->setValue('unknown') instanceof Radiobutton);
$html = $this->element->render();

$this->assertEquals(count($this->options)+2, preg_match_all('/\<input.+?\/\>/', $html, $matches));
$this->assertEquals(count($this->options)+2, count($matches[0]));
}
/**
*
Expand Down
2 changes: 2 additions & 0 deletions tests/FormElements/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public function testRendering()
foreach ($matches[0] as $match){
$this->standardAssertionsOptions($match);
}
$this->assertTrue($this->element->setValue('unknown') instanceof Select);
$this->assertTrue(is_string($this->element->render()));
}

public function testException()
Expand Down
4 changes: 4 additions & 0 deletions tests/FormElements/SubmitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ protected function standardAssertions($html)
$this->assertTrue(isset($attributes['type']), 'type isset');
$this->assertTrue(isset($attributes['type']) && $attributes['type'] == 'submit', 'type is wrong');
$this->assertEquals($this->element->getName(), 'test');

$this->assertEquals($this->element->validate('123'), true);
$this->assertTrue($this->element->setValue('123') instanceof Submit);
$this->assertTrue(is_string($this->element->render()));
}

}
4 changes: 2 additions & 2 deletions tests/FormElements/TextAreaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public function testRendering()
$this->assertTrue($this->element->setValidators([]) instanceof TextArea);
$this->assertTrue($this->element->addValidator(new IsEmpty()) instanceof TextArea);

$this->assertEquals($this->element->validateInput(''), true);
$this->assertEquals($this->element->validateInput('default'), false);
$this->assertEquals($this->element->validate(''), true);
$this->assertEquals($this->element->validate('default'), false);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/FormElements/TextInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ protected function standardAssertions($html)
$this->assertTrue(isset($attributes['type']), 'type isset');
$this->assertTrue(isset($attributes['type']) && $attributes['type'] == 'text', 'type is wrong');
$this->assertTrue($this->element->getName() == 'test', 'name is wrong');

$this->assertEquals($this->element->validate('123'), true);
$this->assertTrue($this->element->setValue('123') instanceof TextInput);
$this->assertTrue(is_string($this->element->render()));
}

}
4 changes: 3 additions & 1 deletion tests/Forms/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ public function testValidation()
$this->assertTrue($element->addValidator(new Integer()) instanceof FormElementInterface);
$this->assertTrue($this->element->addElement($element, 'test') instanceof FormInterface);
$this->assertTrue($this->element->addElement($element) instanceof FormInterface);
$this->assertTrue($this->element->validateInput(['test'=>123]));
$this->assertTrue($this->element->validate(['test'=>123])===true );
$this->assertTrue($this->element->setJqueryAvailable(false) instanceof FormInterface);
$this->assertTrue(is_string($this->element->renderJavascript()));
$this->assertTrue($this->element->setJqueryAvailable(true) instanceof FormInterface);
$this->assertTrue(is_string($this->element->renderJavascript()));
$this->assertTrue($this->element->populate(['test'=>123]) instanceof FormInterface);
$this->assertTrue(is_string($this->element->renderHtml()));
}
}

0 comments on commit ecae1b9

Please sign in to comment.