public function error(LimeError $error)
 {
     $this->errors++;
     $message = sprintf("%s: %s\n(in %s on line %s)", $error->getType(), $error->getMessage(), $this->stripBaseDir($error->getFile()), $error->getLine());
     $this->printer->printLargeBox($message, LimePrinter::ERROR);
     $this->printer->printLine('Exception trace:', LimePrinter::COMMENT);
     $this->printTrace(null, $error->getFile(), $error->getLine());
     foreach ($error->getTrace() as $trace) {
         // hide the part of the trace that is responsible for getting the
         // annotations to work
         if (strpos($trace['function'], '__lime_annotation_') === 0 && !$this->options['verbose']) {
             break;
         }
         if (array_key_exists('class', $trace)) {
             $method = sprintf('%s%s%s()', $trace['class'], $trace['type'], $trace['function']);
         } else {
             $method = sprintf('%s()', $trace['function']);
         }
         if (array_key_exists('file', $trace)) {
             $this->printTrace($method, $trace['file'], $trace['line']);
         } else {
             $this->printTrace($method);
         }
     }
     $this->printer->printLine('');
 }
 public function handleException(Exception $exception)
 {
     if (!is_null($this->exceptionExpectation)) {
         $this->exception = $exception;
     } else {
         $this->output->error(LimeError::fromException($exception));
     }
     return true;
 }
Esempio n. 3
0
 public function endTest()
 {
     $time = microtime(true) - $this->startTime;
     $this->startTime = 0;
     if ($this->skipped) {
         $this->output->skip($this->comment, $this->class, $time, $this->skipped[0], $this->skipped[1], $this->skipped[2]);
         return;
     }
     if (!is_null($this->exceptionExpectation) && !$this->aborted) {
         $expected = $this->exceptionExpectation->getException();
         $file = $this->exceptionExpectation->getFile();
         $line = $this->exceptionExpectation->getLine();
         if (is_string($expected)) {
             $actual = is_object($this->exception) ? get_class($this->exception) : 'none';
             $message = sprintf('A "%s" was thrown', $expected);
         } else {
             $actual = $this->exception;
             $message = sprintf('A "%s" was thrown', get_class($expected));
         }
         try {
             $this->is($actual, $expected, $message);
         } catch (LimeConstraintException $e) {
             $this->failed = true;
             $this->error = LimeError::fromException($e, $file, $line, array());
         }
     }
     if (is_null($this->exceptionExpectation) && !is_null($this->exception)) {
         $this->failed = true;
         $this->error = LimeError::fromException($this->exception);
     }
     if (!$this->failed) {
         try {
             foreach ($this->mocks as $mock) {
                 $mock->verify();
             }
             $this->output->pass($this->comment, $this->class, $time, $this->file, $this->line);
         } catch (LimeMockException $e) {
             $this->failed = true;
             // suppress trace
             $this->error = LimeError::fromException($e, '', '', array());
         }
     }
     if ($this->failed) {
         $this->output->fail($this->comment, $this->class, $time, $this->file, $this->line, $this->error);
     }
 }
Esempio n. 4
0
 /**
  * Tests a given set of labels.
  *
  * Packages may given with a leading "+" or "-". The tested files are:
  *
  *    * all files that are in all of the labels without leading "+" or "-"
  *    * all files that are in any label with a leading "+"
  *    * no files that are in any label with a leading "-"
  *
  * @param  array $labels  The label names
  * @return integer        The return value of the command (0 if successful)
  */
 protected function test(array $labels, array $options)
 {
     // don't load configuration in the constructor because then --init does
     // not work!
     $configuration = LimeConfiguration::read(getcwd());
     if ($configuration->getLegacyMode()) {
         LimeAutoloader::enableLegacyMode();
     }
     if (isset($options['processes'])) {
         $configuration->setProcesses($options['processes']);
     }
     if (isset($options['suffix'])) {
         $configuration->setSuffix($options['suffix']);
     }
     if (isset($options['output'])) {
         $configuration->setTestOutput($options['output']);
         $configuration->setSuiteOutput($options['output']);
     }
     if (isset($options['color'])) {
         $configuration->setForceColors(true);
     }
     if (isset($options['verbose'])) {
         $configuration->setVerbose(true);
     }
     if (isset($options['serialize'])) {
         $configuration->setSerialize(true);
     }
     if (isset($options['test'])) {
         $fileName = $options['test'];
         if (!is_readable($fileName)) {
             $loader = new LimeLoader($configuration);
             $files = $loader->getFilesByName($options['test']);
             if (count($files) == 0) {
                 throw new Exception("No tests are registered in the test suite! Please add your tests in lime.config.php.");
             } else {
                 if (count($files) > 1) {
                     $paths = array();
                     foreach ($files as $file) {
                         $paths[] = $file->getPath();
                     }
                     throw new Exception(sprintf("The name \"%s\" is ambiguous:\n  - %s\nPlease launch the test with the full file path.", $labels[0], implode("\n  - ", $paths)));
                 }
             }
             $fileName = $files[0]->getPath();
         } else {
             $fileName = realpath($fileName);
         }
         $configuration->getTestOutput()->focus($fileName);
         try {
             if ($configuration->getAnnotationSupport()) {
                 $support = new LimeAnnotationSupport($fileName);
                 $result = $support->execute();
             } else {
                 $result = $this->includeTest($fileName);
             }
             // xUnit compatibility
             $class = basename($fileName, '.php');
             if (class_exists($class) && is_subclass_of($class, 'LimeTestCase')) {
                 $test = new $class($configuration);
                 return $test->run();
             } else {
                 return $result;
             }
         } catch (Exception $e) {
             $configuration->getTestOutput()->error(LimeError::fromException($e));
             return 1;
         }
     } else {
         $loader = new LimeLoader($configuration);
         $harness = new LimeHarness($configuration, $loader);
         $files = $loader->getFilesByLabels($labels);
         if (count($files) == 0) {
             throw new Exception("No tests are registered in the test suite! Please add your tests in lime.config.php.");
         }
         return $harness->run($files) ? 0 : 1;
     }
 }
Esempio n. 5
0
 private function printError(LimeError $error)
 {
     $message = sprintf("%s: %s", $error->getType(), $error->getMessage());
     if ($error->getFile()) {
         $message .= sprintf("\n(in %s on line %s)", $this->stripBaseDir($error->getFile()), $error->getLine());
     }
     if ($error->getType() == 'Warning' || $error->getType() == 'Notice') {
         $lineStyle = LimePrinter::WARNING;
     } else {
         $lineStyle = LimePrinter::ERROR;
     }
     $this->printer->printLargeBox($message, $lineStyle);
     if (is_readable($error->getFile())) {
         $this->printer->printLine('Source code:', LimePrinter::COMMENT);
         $file = fopen($error->getFile(), 'r');
         $indentation = strlen($error->getLine() + 5) + 4;
         $i = 1;
         $l = $error->getLine() - 2;
         while ($i < $l) {
             fgets($file);
             ++$i;
         }
         while ($i < $l + 5) {
             $line = rtrim(fgets($file), "\n");
             $line = '  ' . $i . '. ' . wordwrap($line, 80 - $indentation, "\n" . str_repeat(' ', $indentation));
             $lines = explode("\n", $line);
             $style = $i == $error->getLine() ? $lineStyle : null;
             foreach ($lines as $line) {
                 $this->printer->printLine(str_pad($line, 80, ' '), $style);
             }
             ++$i;
         }
         fclose($file);
         $this->printer->printLine('');
     }
     if (count($error->getInvocationTrace()) > 0) {
         $this->printer->printLine('Invocation trace:', LimePrinter::COMMENT);
         foreach ($error->getInvocationTrace() as $i => $trace) {
             $this->printer->printLine('  ' . ($i + 1) . ') ' . $trace);
         }
         $this->printer->printLine('');
     }
     if (count($error->getTrace()) > 0) {
         $this->printer->printLine('Exception trace:', LimePrinter::COMMENT);
         $this->printTrace(null, $error->getFile(), $error->getLine());
         foreach ($error->getTrace() as $trace) {
             if (array_key_exists('class', $trace)) {
                 $method = sprintf('%s%s%s()', $trace['class'], $trace['type'], $trace['function']);
             } else {
                 $method = sprintf('%s()', $trace['function']);
             }
             if (array_key_exists('file', $trace)) {
                 $this->printTrace($method, $trace['file'], $trace['line']);
             } else {
                 $this->printTrace($method);
             }
         }
         $this->printer->printLine('');
     }
 }
 public function error(LimeError $error)
 {
     $this->errors++;
     $this->failedFiles[] = $error->getFile();
     $this->output->error($error);
 }