I was wondering if somebody could let me know if I've got the right idea with the following test, and perhaps offer some pointers. Please note that I am writing this test for a class that was previously written.
The Form class:
The tests:PHP Code:<?php
/**
*******************************************************
* wildcat.forms.Form
* Base form class
*******************************************************
* @author Luke Redpath
* @version 1.0
* @copyright Copyright © Luke Redpath 2005
*******************************************************
*/
if (!defined('WILDCAT_PATH')) {
define('WILDCAT_PATH', '../../classes/');
}
/**
* Form
* @package wildcat.forms
*/
class Form
{
// MEMBERS
protected $_id;
protected $_method;
protected $_action;
protected $_fieldsets;
protected $_isValid;
protected $_formErrors;
private $_successView;
private $_failedView;
// CONSTRUCTOR
function __construct($id, $action, $method)
{
$this->_id = $id;
$this->_method = $method;
$this->_action = $action;
$this->_fieldsets = array();
$this->_formErrors = array();
}
// MANIPULATORS
function createFieldset($id, $legend='')
{
require_once(WILDCAT_PATH.'forms/Fieldset.class.php');
$fs = new Fieldset($this->_id.'_'.$id, $legend);
$this->_fieldsets[] = $fs;
return $fs;
}
function display()
{
// check for fieldsets
if(count($this->_fieldsets)==0) {
throw new Exception('Form error: form ['.$this->_id .'] contains no fieldsets.');
}
$form_html = '<form id="'.$this->_id.'" method="'.$this->_method.'" action="'.$this->_action.'">';
foreach($this->_fieldsets as $fieldset) {
$form_html .= $fieldset->render();
}
$form_html .= '</form>';
return $form_html;
}
function isSubmitted()
{
if(isset($_REQUEST['submit_'.$this->_id])) {
// form is submitted so populate the form
// with submitted values and validate
$this->repopulateForm();
$this->validate();
return true;
} else {
return false;
}
}
function isValid()
{
return $this->_isValid;
}
private function validate()
{
$this->_isValid = true;
// loop through fields and check they are valid
foreach($this->_fieldsets as $fieldset) {
foreach($fieldset->getFields() as $field) {
if(!$field->isValid()) {
$this->_formErrors[] = array('fieldLabel' => $field->getLabel(),
'errMsg' => $field->getError());
$field->setValue('');
}
}
}
if(!empty($this->_formErrors)) {
$this->_isValid = false;
}
}
private function repopulateForm()
{
foreach($this->_fieldsets as $fieldset) {
foreach($fieldset->getFields() as $field) {
$field->setValue($_REQUEST[$field->getName()]);
}
}
}
function displayFormData()
{
$dataHTML = '';
$data = $this->getAllDataFields();
foreach($data as $field) {
$dataHTML .= '<strong>'.$field->getLabel().'</strong>: '.$field->getValue().'<br />';
}
return $dataHTML;
}
function getAllDataFields()
{
$tmpFieldsArray = Array();
foreach($this->_fieldsets as $fieldset) {
$tmpFieldsArray = array_merge($tmpFieldsArray, $fieldset->getFields());
}
// remove the submit button
unset($tmpFieldsArray['submit_'.$this->_id]);
return $tmpFieldsArray;
}
function process($processor)
{
$processor->run($this->getAllDataFields());
}
// ACCESSORS
function getErrors()
{
return $this->_formErrors;
}
function setSuccessView(FormView $view)
{
$this->_successView = $view;
}
function setFailedView(FormView $view)
{
$this->_failedView = $view;
}
function getSuccessView()
{
return $this->_successView;
}
function getFailedView()
{
return $this->_failedView;
}
function getFormId()
{
return $this->_id;
}
}
?>
Thanks a lot.PHP Code:<?php
/**
*******************************************************
* tests.wildcat.Form
* Form test
*******************************************************
* @author Luke Redpath
* @version 1.0
* @copyright Copyright © Luke Redpath 2005
*******************************************************
*/
require_once('../TestBase.inc.php');
require_once(SIMPLE_TEST.'simpletest/unit_tester.php');
require_once(SIMPLE_TEST.'simpletest/reporter.php');
require_once('../../classes/forms/Form.class.php');
class test_Form extends UnitTestCase
{
function __construct()
{
$this->UnitTestCase('wildcat.forms.Form Test');
}
function createSampleForm()
{
$form = new Form('testform', 'index.php', 'post');
$fs1 = $form->createFieldset('fs1', 'Some Fieldset');
$fs1->addField('textfield', 'Text Field', 'text', true);
return $form;
}
function submitSampleForm($form, $valid = true)
{
if($valid) {
// create some valid form data
$_REQUEST['textfield'] = 'Sample Data';
} else {
// create some invalid form data
// (in this case an empty required field)
$_REQUEST['textfield'] = '';
}
$_REQUEST['submit_'.$form->getFormId()] = 'true';
}
function clearRequest($formId)
{
unset($_REQUEST['textfield']);
unset($_REQUEST['submit_'.$formId]);
}
function testCreatingForm()
{
$form = new Form('testform', 'index.php', 'post');
$this->assertFalse($form->isSubmitted());
}
function testDisplayingForm()
{
// create an empty form
$form = new Form('testform', 'index.php', 'post');
// display() should throw an exception here
try {
$this->assertTrue(is_string($form->display()));
} catch(Exception $e) {
$this->pass();
}
// create a fieldset
$fs1 = $form->createFieldset('fs1', 'Some Fieldset');
// display() should still throw an exception
try {
$this->assertTrue(is_string($form->display()));
} catch(Exception $e) {
$this->pass();
}
// add a field to the fieldset
$fs1->addField('textfield', 'Text Field', 'text', true);
// display() should return the form HTML here
try {
$this->assertTrue(is_string($form->display()));
} catch(Exception $e) {
$this->fail('Form threw an exception, expected the form HTML');
}
}
function testSubmittingForm()
{
$form = $this->createSampleForm();
$this->assertFalse($form->isSubmitted());
// submit the form
$this->submitSampleForm($form);
$this->assertTrue($form->isSubmitted());
// clear the request for other tests
$this->clearRequest($form->getFormId());
}
function testValidatingForm()
{
$form = $this->createSampleForm();
// should be invalid as it hasn't yet been submitted
$this->assertFalse($form->isValid());
// submit with invalid data
$this->submitSampleForm($form, false);
$this->assertTrue($form->isSubmitted());
$this->assertFalse($form->isValid());
$this->clearRequest($form->getFormId());
// submit some valid data
$form2 = $this->createSampleForm();
$this->submitSampleForm($form2);
$this->assertTrue($form2->isSubmitted());
$this->assertTrue($form2->isValid());
}
}
$test = new test_Form();
$test->run(new HtmlReporter());
?>





I don't like that, tests are supposed to run red the very first time they're run, seems I'm getting addicted to the red bar instead of the green one



Bookmarks