Exemple #1
0
 /**
  * Run this test.
  *
  * @param xTestRunner $testRunner Test runner.
  * @return boolean
  */
 public function run(xTestRunner $testRunner)
 {
     $this->testRunner = $testRunner;
     $this->resultPrinter->testHeader($this);
     $reflection = new \ReflectionClass(get_class($this));
     $methods = $reflection->getMethods();
     $passed = $count = 0;
     if (method_exists($this, 'init')) {
         try {
             $this->init();
         } catch (\Exception $exception) {
             echo '<tr style="color:white; background:red;"><td>init() failed</td><td><pre>' . $exception . '</pre></td></tr></table>';
             return false;
         }
     }
     foreach ($methods as $method) {
         if (substr($method->name, 0, 4) == 'test') {
             $this->result = null;
             $test = $method->name;
             $name = substr($test, 4);
             if (count($_GET) && isset($_GET[$name]) && $_GET[$name] !== '') {
                 continue;
             }
             $this->testRunner->startCoverageCollector($test);
             if (method_exists($this, 'setup')) {
                 $this->setup();
             }
             $exception = null;
             try {
                 $this->{$test}();
             } catch (\Exception $exception) {
             }
             try {
                 $this->assertException($exception);
             } catch (xTestException $subException) {
             }
             if (is_null($this->result) && !($exception instanceof xTestException && $exception->getCode() == xTestException::FAIL)) {
                 $this->result = (string) $exception;
             }
             $count++;
             if ($this->result === true) {
                 $passed++;
             }
             if (method_exists($this, 'teardown')) {
                 $this->teardown();
             }
             $this->setExpectedException(null, '', null);
             $this->testRunner->stopCoverageCollector();
             $this->resultPrinter->testCaseResult($method, $this->getResultColor(), $this->getResultMessage());
         }
     }
     $this->resultPrinter->testFooter($this, $count, $passed);
     return $passed == $count;
 }
 /**
  * Runs a suite of unit tests
  *
  * @param string $directory Directory with tests.
  * @param string $suffix    Test file suffix.
  * @throws \Exception When invalid test found.
  * @return boolean
  */
 public function run($directory, $suffix)
 {
     $this->resultPrinter->suiteHeader($this, $directory . '/*' . $suffix);
     $passed = true;
     $facade = new \File_Iterator_Facade();
     $old_handler = set_error_handler(array($this, 'handlePHPErrors'));
     foreach ($facade->getFilesAsArray($directory, $suffix) as $path) {
         $test = (require $path);
         if (!$test instanceof xTest) {
             throw new \Exception("'{$path}' is not a valid unit test");
         }
         $test->setResultPrinter($this->resultPrinter);
         $passed = $passed && $test->run($this);
     }
     if ($old_handler) {
         set_error_handler($old_handler);
     } else {
         restore_error_handler();
     }
     $this->resultPrinter->createCodeCoverageReport($this->coverage);
     $this->resultPrinter->suiteFooter($this);
     return $passed;
 }