Ok, in the wonderfull world of PhpUnit tests I wanted to test a user registration process with Captcha enabled. But, what do you put in the Captcha field since it’s an automated test ? Well, reading the session appears to be the simplest way to get this information. Here’s a simple function I wrote, suggested by Matthew Weier O’Phinney, that will allow you simply to get the Captcha word while in your php tests :

  1. private function testGetCaptcha()
  2. {
  3. $this->dispatch(‘/user/registration’);
  4. foreach ($_SESSION as $key=>$value)
  5. {
  6. if(ereg(“Zend_Form_Captcha.*”, $key))
  7. return  $value[‘word’];
  8. }
  9. }

And here you go… It’s a quick and dirty solution since this will only work if you have a unique Zend_Form_Captcha to test for. Feel free to write a better solution !