Thanks for the tip, I just needed to get simpletest into the correct folder.

So I started to look at it last night and have started to add test where some were missing. I've attached my changes so if you feel that these are useful then I can continue to potter along in this area. I would also like to add a web interface to the tests, so I'll play with something along those lines as well.

I needed to add encapsulation methods to the ValidatorManager so that I could properly test the ValidatorManagerTest.

Also the naming convention in the test files doesn't quite match the convention i.e. the method name is testregisterNames() rather than testRegisterNames() is the intentional, or should I change them?

graeme.



Mike Vincent wrote:
On 6/14/05, Graeme <[EMAIL PROTECTED]> wrote:
  
 [EMAIL PROTECTED] agavi]$ phing test
 
 ... and when I run phing test...
 
 Buildfile: /home/graeme/Development/Web/agavi/build.xml
 
 agavi > test:
 [EMAIL PROTECTED] agavi]$ 
 
 Essentially no output, I'm doing something wrong...
 
 I have been doing a little work on a web interface for the tests. So I
could potter away at that, if you'd like.
    

Looks like you need to install simpletest, and/or make it accessible
via your include path.
see http://sourceforge.net/projects/simpletest

I have this fixed and will be committing shortly.

-Mike

  
_controller = new MockController($this); $this->_controller->dispatch(); $this->_context = $this->_controller->getContext(); $this->_vm = new ValidatorManager(); $this->_vm->initialize($this->_context); } public function tearDown() { $this->_controller = null; $this->_vm = null; $this->_vmProphlyaxis = null; $this->_context->cleanSlate(); $this->_context = null; } public function testinitialize() { $this->assertIsA($this->_vm, 'ValidatorManager'); } public function testregisterName() { // Clear the validator & check that names // 1) is an empty array $this->_vm->clear(); $this->assertEqual($this->_vm->getNames(), array(), 'name cleared'); // Register a name check that names: // 1) is an array // 2) is not an empty array // 3) the name has been registered // 4) - 9) register entries are set up correctly $name = "My Test #1"; $this->_vm->registerName($name); $names = $this->_vm->getNames(); $this->assertIsA($names,'Array', 'name registered'); $this->assertNotEqual($names, array(), 'names established'); $this->assertIsA($names[$name], 'Array', 'name registered'); $this->assertNull($names[$name]['group'], 'group entry default'); $this->assertFalse($names[$name]['is_file'], 'is_file entry default'); $this->assertTrue($names[$name]['required'], 'required entry default'); $this->assertEqual($names[$name]['required_msg'], 'Required', 'required_msg entry default'); $this->assertTrue($names[$name]['validation_status'], 'validation_status entry default'); $this->assertEqual($names[$name]['validators'], array(), 'validators entry default'); // Register an optional name check that names: // 1) is an array // 2) is not an empty array // 3) the name has been registered // 4) - 9) register entries are set up correctly $name = "My Test #2"; $this->_vm->registerName($name,false,'Optional',null,null,false); $names = $this->_vm->getNames(); $this->assertIsA($names,'Array', 'name registered'); $this->assertNotEqual($names, array(), 'names established'); $this->assertIsA($names[$name], 'Array', 'name registered'); $this->assertNull($names[$name]['group'], 'group entry default'); $this->assertFalse($names[$name]['is_file'], 'is_file entry default'); $this->assertFalse($names[$name]['required'], 'required entry default'); $this->assertEqual($names[$name]['required_msg'], 'Optional', 'required_msg entry default'); $this->assertTrue($names[$name]['validation_status'], 'validation_status entry default'); $this->assertEqual($names[$name]['validators'], array(), 'validators entry default'); // Register a required child name $nameParent = "Apa"; $nameChild = "My Chils Test #1"; $this->_vm->registerName($nameChild,true,'Required',$nameParent,null,false); // check that names: // 1) is an array // 2) is not an empty array // 3) the name has been registered // 4) - 9) register entries are set up correctly $names = $this->_vm->getNames(); $this->assertIsA($names,'Array', 'name registered'); $this->assertNotEqual($names[$nameParent], array(), 'names established'); $this->assertIsA($names[$nameParent][$nameChild], 'Array', 'name registered'); $this->assertNull($names[$nameParent][$nameChild]['group'], 'group entry default'); $this->assertFalse($names[$nameParent][$nameChild]['is_file'], 'is_file entry default'); $this->assertTrue($names[$nameParent][$nameChild]['required'], 'required entry default'); $this->assertEqual($names[$nameParent][$nameChild]['required_msg'], 'Required', 'required_msg entry default'); $this->assertTrue($names[$nameParent][$nameChild]['validation_status'], 'validation_status entry default'); $this->assertEqual($names[$nameParent][$nameChild]['validators'], array(), 'validators entry default'); // Register a second optional child name $nameParent = "Apa"; $nameChild = "My Child Test #2"; $this->_vm->registerName($nameChild,false,'Optional',$nameParent,null,false); // check that names: // 1) is an array // 2) is not an empty array // 3) the name has been registered // 4) - 9) register entries are set up correctly // 10) the parent array contains two entries $names = $this->_vm->getNames(); $this->assertIsA($names,'Array', 'name registered'); $this->assertNotEqual($names[$nameParent], array(), 'names established'); $this->assertIsA($names[$nameParent][$nameChild], 'Array', 'name registered'); $this->assertNull($names[$nameParent][$nameChild]['group'], 'group entry default'); $this->assertFalse($names[$nameParent][$nameChild]['is_file'], 'is_file entry default'); $this->assertFalse($names[$nameParent][$nameChild]['required'], 'required entry'); $this->assertEqual($names[$nameParent][$nameChild]['required_msg'], 'Optional', 'required_msg entry'); $this->assertTrue($names[$nameParent][$nameChild]['validation_status'], 'validation_status entry default'); $this->assertEqual($names[$nameParent][$nameChild]['validators'], array(), 'validators entry default'); $this->assertEqual(count($names[$nameParent]), 3, 'parent array has _is_parent + 2 entries'); $this->fail('Need to add group'); } // end of testregisterName() public function testregisterValidator() { $this->fail('Incomplete Test'); } public function testexecute() { $this->fail('Incomplete Test'); } public function testclear() { // Check that the groups and names arrays have been cleared. $this->_vm->clear(); $this->assertEqual($this->_vm->getGroups(), array(), 'groups cleared'); $this->assertEqual($this->_vm->getNames(), array(), 'names cleared'); } // end of testClear() } _dv = new DateValidator(); $this->_good_date = '2005-05-05'; $this->_bad_date = 'BAD DATE'; } public function testexecute() { $good = array( '2005-05-05', '2005-05-05 21:00:00', 'March 31st, 2005', '5/3/2005', '15/3/2005', 'Today', 'Now', 'Today + 1', 'Monday', 'Next week', 'yesterday', 'tomorrow', 'pm', 'am' ); $bad = array( '15032005', 'This is a bad date.', 'afternoon', '15/3/2005', '9999999' ); $error = ''; foreach ($good as &$value) { $this->assertTrue($this->_dv->execute($value, $error),"'$value'"); } foreach ($bad as &$value) { $this->assertFalse($this->_dv->execute($value, $error),"'$value'"); } } } ?> groups = null; $this->groups = array(); $this->names = null; $this->names = array(); } // ------------------------------------------------------------------------- /** * Encapsulation method to return the groups array * * @return array of validator groups, or NULL if none have been registered * * @author Graeme Foster * @since 1.0.0 */ public function getGroups() { return $this->groups; } // end of getGroups() // ------------------------------------------------------------------------- /** * Encapsulation method to return the names array * * @return array of validator names, or NULL if none have been registered * * @author Graeme Foster * @since 1.0.0 */ public function getNames() { return $this->names; } // end of getNames() // ------------------------------------------------------------------------- /** * Execute all validators and determine the validation status. * * @return bool true, if validation completed successfully, otherwise false. * * @author Sean Kerr ([EMAIL PROTECTED]) * @since 0.9.0 */ public function execute () { $retval = true; $pass = 1; while (true) { foreach ($this->names as $name => &$data) { if (isset($data['_is_parent'])) { foreach ($data as $subname => &$subdata) { if ($subname == '_is_parent') { // this isn't an actual index, but more of a flag continue; } if ($subdata['validation_status'] == true && !$this->validate($subname, $subdata, $name)) { // validation failed $retval = false; } } } else { // single parameter if ($data['validation_status'] == true && !$this->validate($name, $data, null)) { // validation failed $retval = false; } } } if (count($this->groups) == 0 || $pass == 2) { break; } // increase our pass indicator $pass++; } return $retval; } // ------------------------------------------------------------------------- /** * Initialize this validator manager. * * @param Context A context instance. * * @return void * * @author Sean Kerr ([EMAIL PROTECTED]) * @since 0.9.0 */ public function initialize ($context) { $this->request = $context->getRequest(); } // ------------------------------------------------------------------------- /** * Register a file or parameter. * * @param string A file or parameter name. * @param bool The required status. * @param string A required error message. * @param string A parent array * @param string A group name. * @param bool An unused(?) parameter, apparently meant to determine if the name is a parameter or filename. * * @author Sean Kerr ([EMAIL PROTECTED]) * @since 0.9.0 */ public function registerName ($name, $required = true, $message = 'Required', $parent = null, $group = null, $isFile = false) { $entry = array(); $entry['group'] = null; $entry['is_file'] = $isFile; $entry['required'] = $required; $entry['required_msg'] = $message; $entry['validation_status'] = true; $entry['validators'] = array(); if ($parent != null) { // this parameter has a parent array if (!isset($this->names[$parent])) { // create the parent array $this->names[$parent] = array('_is_parent' => true); } // register this parameter $this->names[$parent][$name] =& $entry; } else { // no parent // register this parameter $this->names[$name] =& $entry; } if ($group != null) { // set group if (!isset($this->groups[$group])) { // create our group $this->groups[$group] = array('_force' => false); } // add this file/parameter name to the group $this->groups[$group][] = $name; // add a reference back to the group array to the file/param array $entry['group'] =& $this->groups[$group]; } } // ------------------------------------------------------------------------- /** * Register a validator for a file or parameter. * * @param string A file or parameter name. * @param Validator A validator implementation instance. * @param string A parent array name. * * @return void * * @author Sean Kerr ([EMAIL PROTECTED]) * @since 0.9.0 */ public function registerValidator ($name, $validator, $parent = null) { if ($parent != null) { // this parameter has a parent $this->names[$parent][$name]['validators'][] = $validator; } else { // no parent $this->names[$name]['validators'][] = $validator; } } // ------------------------------------------------------------------------- /** * Validate a file or parameter. * * @param string A file or parameter name. * @param array Data associated with the file or parameter. * @param string A parent name. * * @return bool true, if validation completes successfully, otherwise false. * * @author Sean Kerr ([EMAIL PROTECTED]) * @since 0.9.0 */ private function validate (&$name, &$data, $parent) { // get defaults $error = null; $errorName = null; $force = ($data['group'] != null) ? $data['group']['_force'] : true; $retval = true; $value = null; // get our parameter value if ($parent == null) { // normal file/parameter $errorName = $name; if ($data['is_file']) { // file $value =& $this->request->getFile($name); } else { // parameter $value =& $this->request->getParameter($name); } } else { // we have a parent $errorName = $parent . '{' . $name . '}'; if ($data['is_file']) { // file $parent =& $this->request->getFile($parent); } else { // parameter $parent =& $this->request->getParameter($parent); } if ($parent != null && isset($parent[$name])) { $value =& $parent[$name]; } } // now for the dirty work if ($value == null || strlen($value) == 0) { if (!$data['required'] || !$force) { // we don't have to validate it $retval = true; } else { // it's empty! $error = $data['required_msg']; $retval = false; } } else { // time for the fun $error = null; // get group force status if ($data['group'] != null) { // we set this because we do have a value for a parameter in // this group $data['group']['_force'] = true; $force = true; } if (count($data['validators']) > 0) { // loop through our validators foreach ($data['validators'] as $validator) { if (!$validator->execute($value, $error)) { $retval = false; } } } } if (!$retval) { // set validation status $data['validation_status'] = false; // set the request error $this->request->setError($errorName, $error); } return $retval; } } ?>
_______________________________________________
agavi-dev mailing list
[EMAIL PROTECTED]
http://labworkz.com/cgi-bin/mailman/listinfo/agavi-dev

Reply via email to