/**
  * Run the tests
  *
  * This method will run the tests with the correct Reporter. It will run 
  * grouped tests if asked to and filter results. It also has support for 
  * running coverage report. 
  *
  */
 public function run()
 {
     $testGroup = $this->testGroup;
     if (PHP_SAPI === 'cli') {
         require_once dirname(__FILE__) . '/DoctrineTest/Reporter/Cli.php';
         $reporter = new DoctrineTest_Reporter_Cli();
         $argv = $_SERVER['argv'];
         array_shift($argv);
         $options = $this->parseOptions($argv);
     } else {
         require_once dirname(__FILE__) . '/DoctrineTest/Reporter/Html.php';
         $options = $_GET;
         if (isset($options['filter'])) {
             if (!is_array($options['filter'])) {
                 $options['filter'] = explode(',', $options['filter']);
             }
         }
         if (isset($options['group'])) {
             if (!is_array($options['group'])) {
                 $options['group'] = explode(',', $options['group']);
             }
         }
         $reporter = new DoctrineTest_Reporter_Html();
     }
     //replace global group with custom group if we have group option set
     if (isset($options['group'])) {
         $testGroup = new GroupTest('Doctrine Custom Test', 'custom');
         foreach ($options['group'] as $group) {
             if (isset($this->groups[$group])) {
                 $testGroup->addTestCase($this->groups[$group]);
             } else {
                 if (class_exists($group)) {
                     $testGroup->addTestCase(new $group());
                 } else {
                     die($group . " is not a valid group or doctrine test class\n ");
                 }
             }
         }
     }
     if (isset($options['ticket'])) {
         $testGroup = new GroupTest('Doctrine Custom Test', 'custom');
         foreach ($options['ticket'] as $ticket) {
             $class = 'Doctrine_Ticket_' . $ticket . '_TestCase';
             $testGroup->addTestCase(new $class());
         }
     }
     $filter = '';
     if (isset($options['filter'])) {
         $filter = $options['filter'];
     }
     //show help text
     if (isset($options['help'])) {
         $availableGroups = sort(array_keys($this->groups));
         echo "Doctrine test runner help\n";
         echo "===========================\n";
         echo " To run all tests simply run this script without arguments. \n";
         echo "\n Flags:\n";
         echo " -coverage will generate coverage report data that can be viewed with the cc.php script in this folder. NB! This takes time. You need xdebug to run this\n";
         echo " -group <groupName1> <groupName2> <className1> Use this option to run just a group of tests or tests with a given classname. Groups are currently defined as the variable name they are called in this script.\n";
         echo " -filter <string1> <string2> case insensitive strings that will be applied to the className of the tests. A test_classname must contain all of these strings to be run\n";
         echo "\nAvailable groups:\n " . implode(', ', $availableGroups) . "\n";
         die;
     }
     //generate coverage report
     if (isset($options['coverage'])) {
         /*
         * The below code will not work for me (meus). It would be nice if 
         * somebody could give it a try. Just replace this block of code 
         * with the one below
         *
         define('PHPCOVERAGE_HOME', dirname(dirname(__FILE__)) . '/vendor/spikephpcoverage');
                     require_once PHPCOVERAGE_HOME . '/CoverageRecorder.php';
                     require_once PHPCOVERAGE_HOME . '/reporter/HtmlCoverageReporter.php';
         
                     $covReporter = new HtmlCoverageReporter('Doctrine Code Coverage Report', '', 'coverage2');
         
                     $includePaths = array('../lib');
                     $excludePaths = array();
                     $cov = new CoverageRecorder($includePaths, $excludePaths, $covReporter);
         
                     $cov->startInstrumentation();
                     $ret = $testGroup->run($reporter, $filter);
                     $cov->stopInstrumentation();
         
                     $cov->generateReport();
                     $covReporter->printTextSummary();
                     return $ret;
         */
         xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
         $ret = $testGroup->run($reporter, $filter);
         $result['coverage'] = xdebug_get_code_coverage();
         xdebug_stop_code_coverage();
         file_put_contents(dirname(__FILE__) . '/coverage/coverage.txt', serialize($result));
         require_once dirname(__FILE__) . '/DoctrineTest/Coverage.php';
         $coverageGeneration = new DoctrineTest_Coverage();
         $coverageGeneration->generateReport();
         return $ret;
         // */
     }
     if (array_key_exists('only-failed', $options)) {
         $testGroup->onlyRunFailed(true);
     }
     $result = $testGroup->run($reporter, $filter);
     global $startTime;
     $endTime = time();
     $time = $endTime - $startTime;
     if (PHP_SAPI === 'cli') {
         echo "\nTests ran in " . $time . " seconds and used " . memory_get_peak_usage() / 1024 . " KB of memory\n\n";
     } else {
         echo "<p>Tests ran in " . $time . " seconds and used " . memory_get_peak_usage() / 1024 . " KB of memory</p>";
     }
     return $result;
 }