/**
  * Called when a test run starts.
  *
  * @param   unittest.TestSuite suite
  */
 public function testRunStarted(TestSuite $suite)
 {
     $this->sum = $suite->numTests();
     $this->cur = 0;
 }
Ejemplo n.º 2
0
 /**
  * Runs suite
  *
  * @param   string[] args
  * @return  int exitcode
  */
 public function run(array $args)
 {
     if (!$args) {
         return $this->usage();
     }
     // Setup suite
     $suite = new TestSuite();
     // Parse arguments
     $sources = new Vector();
     $listener = TestListeners::$DEFAULT;
     $arguments = [];
     $colors = null;
     try {
         for ($i = 0, $s = sizeof($args); $i < $s; $i++) {
             if ('-v' == $args[$i]) {
                 $listener = TestListeners::$VERBOSE;
             } else {
                 if ('-q' == $args[$i]) {
                     $listener = TestListeners::$QUIET;
                 } else {
                     if ('-cp' == $args[$i]) {
                         foreach (explode(PATH_SEPARATOR, $this->arg($args, ++$i, 'cp')) as $element) {
                             ClassLoader::registerPath($element, null);
                         }
                     } else {
                         if ('-e' == $args[$i]) {
                             $arg = ++$i < $s ? $args[$i] : '-';
                             if ('-' === $arg) {
                                 $sources->add(new EvaluationSource(Streams::readAll($this->in->getStream())));
                             } else {
                                 $sources->add(new EvaluationSource($this->arg($args, $i, 'e')));
                             }
                         } else {
                             if ('-l' == $args[$i]) {
                                 $arg = $this->arg($args, ++$i, 'l');
                                 $class = XPClass::forName(strstr($arg, '.') ? $arg : 'xp.unittest.' . ucfirst($arg) . 'Listener');
                                 $arg = $this->arg($args, ++$i, 'l');
                                 if ('-?' == $arg || '--help' == $arg) {
                                     return $this->listenerUsage($class);
                                 }
                                 $output = $this->streamWriter($arg);
                                 $instance = $suite->addListener($class->newInstance($output));
                                 // Get all @arg-annotated methods
                                 $options = [];
                                 foreach ($class->getMethods() as $method) {
                                     if ($method->hasAnnotation('arg')) {
                                         $arg = $method->getAnnotation('arg');
                                         if (isset($arg['position'])) {
                                             $options[$arg['position']] = $method;
                                         } else {
                                             $name = isset($arg['name']) ? $arg['name'] : strtolower(preg_replace('/^set/', '', $method->getName()));
                                             $short = isset($arg['short']) ? $arg['short'] : $name[0];
                                             $options[$name] = $options[$short] = $method;
                                         }
                                     }
                                 }
                                 $option = 0;
                             } else {
                                 if ('-o' == $args[$i]) {
                                     if (isset($options[$option])) {
                                         $name = '#' . ($option + 1);
                                         $method = $options[$option];
                                     } else {
                                         $name = $this->arg($args, ++$i, 'o');
                                         if (!isset($options[$name])) {
                                             $this->err->writeLine('*** Unknown listener argument ' . $name . ' to ' . nameof($instance));
                                             return 2;
                                         }
                                         $method = $options[$name];
                                     }
                                     $option++;
                                     if (0 == $method->numParameters()) {
                                         $pass = [];
                                     } else {
                                         $pass = $this->arg($args, ++$i, 'o ' . $name);
                                     }
                                     try {
                                         $method->invoke($instance, $pass);
                                     } catch (TargetInvocationException $e) {
                                         $this->err->writeLine('*** Error for argument ' . $name . ' to ' . $instance->getClassName() . ': ' . $e->getCause()->toString());
                                         return 2;
                                     }
                                 } else {
                                     if ('-?' == $args[$i] || '--help' == $args[$i]) {
                                         return $this->usage();
                                     } else {
                                         if ('-a' == $args[$i]) {
                                             $arguments[] = $this->arg($args, ++$i, 'a');
                                         } else {
                                             if ('-w' == $args[$i]) {
                                                 $this->arg($args, ++$i, 'w');
                                             } else {
                                                 if ('--color' == substr($args[$i], 0, 7)) {
                                                     $remainder = (string) substr($args[$i], 7);
                                                     if (!array_key_exists($remainder, self::$cmap)) {
                                                         throw new IllegalArgumentException('Unsupported argument for --color (must be <empty>, "on", "off", "auto" (default))');
                                                     }
                                                     $colors = self::$cmap[$remainder];
                                                 } else {
                                                     if (strstr($args[$i], '.ini')) {
                                                         $sources->add(new PropertySource(new Properties($args[$i])));
                                                     } else {
                                                         if (strstr($args[$i], \xp::CLASS_FILE_EXT)) {
                                                             $sources->add(new ClassFileSource(new File($args[$i])));
                                                         } else {
                                                             if (strstr($args[$i], '.**')) {
                                                                 $sources->add(new PackageSource(Package::forName(substr($args[$i], 0, -3)), true));
                                                             } else {
                                                                 if (strstr($args[$i], '.*')) {
                                                                     $sources->add(new PackageSource(Package::forName(substr($args[$i], 0, -2))));
                                                                 } else {
                                                                     if (false !== ($p = strpos($args[$i], '::'))) {
                                                                         $sources->add(new ClassSource(XPClass::forName(substr($args[$i], 0, $p)), substr($args[$i], $p + 2)));
                                                                     } else {
                                                                         if (is_dir($args[$i])) {
                                                                             $sources->add(new FolderSource(new Folder($args[$i])));
                                                                         } else {
                                                                             $sources->add(new ClassSource(XPClass::forName($args[$i])));
                                                                         }
                                                                     }
                                                                 }
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     } catch (Throwable $e) {
         $this->err->writeLine('*** ', $e->getMessage());
         \xp::gc();
         return 2;
     }
     if ($sources->isEmpty()) {
         $this->err->writeLine('*** No tests specified');
         return 2;
     }
     // Set up suite
     $l = $suite->addListener($listener->newInstance($this->out));
     if ($l instanceof ColorizingListener) {
         $l->setColor($colors);
     }
     foreach ($sources as $source) {
         try {
             $tests = $source->testCasesWith($arguments);
             foreach ($tests as $test) {
                 $suite->addTest($test);
             }
         } catch (NoSuchElementException $e) {
             $this->err->writeLine('*** Warning: ', $e->getMessage());
             continue;
         } catch (IllegalArgumentException $e) {
             $this->err->writeLine('*** Error: ', $e->getMessage());
             return 2;
         } catch (MethodNotImplementedException $e) {
             $this->err->writeLine('*** Error: ', $e->getMessage(), ': ', $e->method, '()');
             return 2;
         }
     }
     // Run it!
     if (0 == $suite->numTests()) {
         return 3;
     } else {
         $r = $suite->run();
         return $r->failureCount() > 0 ? 1 : 0;
     }
 }
 /**
  * Called when a test run starts.
  *
  * @param   unittest.TestSuite suite
  */
 public function testRunStarted(\unittest\TestSuite $suite)
 {
     $this->out->writeLine('===> Running test suite (', $suite->numTests(), ' test(s))');
 }
 /**
  * Called when a test run starts.
  *
  * @param   unittest.TestSuite suite
  */
 public function testRunStarted(\unittest\TestSuite $suite)
 {
     $this->sum = $suite->numTests();
     $this->cur = 0;
     $this->stats = array('failed' => 0, 'errored' => 0, 'warned' => 0, 'skipped' => 0, 'notrun' => 0);
     $this->status = true;
 }