Beispiel #1
0
 /**
  * @covers Phramework\Testphase\Testphase::getVersion
  */
 public function testGetVersion()
 {
     $version = Testphase::getVersion();
     $this->assertInternalType('string', $version);
     /*$this->assertRegExp(
           '/^1\.[1-9]*[0-9]?\.[1-9]*[0-9]?(:?\-[a-zA-Z0-9]+)?$/',
           $version,
           'Validates againts 1.x.x versions'
       );*/
 }
Beispiel #2
0
 /**
  * Invoke scripts
  * @return integer Returns indicate how the script exited.
  * Normal exit is generally represented by a 0 return.
  */
 public function invoke()
 {
     $arguments = $this->arguments;
     if (($serverHost = $arguments->{'server-host'}) !== null) {
         $this->server = new Server($serverHost, $arguments->{'server-root'});
         $this->server->start();
     }
     //Include bootstrap file if set
     if ($bootstrapFile = $arguments->bootstrap) {
         require $bootstrapFile;
     }
     echo 'testphase v' . Testphase::getVersion() . PHP_EOL;
     if ($arguments->help) {
         echo 'Help:' . PHP_EOL;
         $printer = new ConsoleOptionPrinter();
         echo $printer->render(static::getArgumentSpecifications());
         return 0;
     } elseif ($arguments->debug) {
         echo 'Enabled options: ' . PHP_EOL;
         foreach ($arguments as $key => $spec) {
             echo $spec;
         }
     }
     try {
         $testParserCollection = $this->getTestParserCollection();
     } catch (\Exception $e) {
         echo $e->getMessage();
         return $this->stop(1);
     }
     //Statistics object
     $stats = (object) ['tests' => count($testParserCollection), 'success' => 0, 'error' => 0, 'failure' => 0, 'ignore' => 0, 'incomplete' => 0, 'errors' => []];
     $testIndex = 0;
     foreach ($testParserCollection as $test) {
         //Check if subdir argument is set
         if (isset($arguments->subdir) && $arguments->subdir !== null) {
             //If so check if file name passes the given pattern
             //Remove base dir from filename
             $cleanFilename = trim(str_replace($arguments->dir, '', $test->getFilename()), '/');
             $match = false;
             //Check if file name matches any of the subdir patterns
             foreach ($arguments->subdir as $pattern) {
                 $pattern = '@' . $pattern . '@';
                 if (!!preg_match($pattern, $cleanFilename)) {
                     $match = $match || true;
                     break;
                 }
             }
             if (!$match) {
                 //Ignore
                 $stats->ignore += 1;
                 if ($arguments->verbose) {
                     echo sprintf('I %s', $test->getFilename()) . PHP_EOL;
                 } else {
                     echo 'I';
                 }
                 continue;
             }
         }
         $meta = $test->getMeta();
         if (isset($meta->ignore) && $meta->ignore) {
             if ($arguments->verbose) {
                 echo sprintf('I %s', $test->getFilename()) . PHP_EOL;
             } else {
                 echo 'I';
             }
             $stats->ignore += 1;
             continue;
         }
         if (isset($meta->incomplete) && $meta->incomplete !== false) {
             $stats->incomplete += 1;
         }
         try {
             //Complete test's testphase collection
             $test->createTest();
         } catch (\Exception $e) {
             echo sprintf('Unable to create test from file "%s" %s With message: "%s"', $test->getFilename(), PHP_EOL, $e->getMessage()) . PHP_EOL;
             return $this->stop(1);
         }
         $testphaseCollection = $test->getTest();
         //Include number of additional testphase collections
         $stats->tests += count($testphaseCollection) - 1;
         //Iterate though test parser's testphase collection
         foreach ($testphaseCollection as $testphase) {
             try {
                 $testphase->run(function ($responseStatusCode, $responseHeaders, $responseBody, $responseBodyObject = null) use($test, $arguments) {
                     //todo move to TestParser
                     $export = $test->getExport();
                     //Fetch all test exports and add them as globals
                     foreach ($export as $key => $value) {
                         $path = explode('.', $value);
                         $pathValue = $responseBodyObject;
                         foreach ($path as $p) {
                             //@todo implement array index
                             $arrayIndex = 0;
                             if (is_array($pathValue)) {
                                 $pathValue = $pathValue[$arrayIndex]->{$p};
                             } else {
                                 $pathValue = $pathValue->{$p};
                             }
                         }
                         Globals::set($key, $pathValue);
                     }
                     if ($arguments->debug) {
                         echo 'Response Status Code:' . PHP_EOL;
                         echo $responseStatusCode . PHP_EOL;
                         echo 'Response Headers:' . PHP_EOL;
                         print_r($responseHeaders);
                         echo PHP_EOL;
                         echo 'Response Body:' . PHP_EOL;
                         echo json_encode($responseBodyObject, JSON_PRETTY_PRINT) . PHP_EOL;
                     }
                 });
                 //Echo successful char
                 if ($arguments->verbose) {
                     echo sprintf('. %s', $test->getFilename()) . PHP_EOL;
                 } else {
                     echo '.';
                 }
                 $stats->success += 1;
             } catch (UnsetGlobalException $e) {
                 //Error message
                 $message = $e->getMessage();
                 $message = sprintf(self::colored('Test "%s" failed with message', 'red') . PHP_EOL . ' %s' . PHP_EOL, $test->getFilename(), $message);
                 //push message to error message
                 $stats->errors[] = $message;
                 //Echo unsuccessful char
                 if ($arguments->verbose) {
                     echo sprintf('F %s', $test->getFilename()) . PHP_EOL;
                 } else {
                     echo 'F';
                 }
                 //print if immediate
                 if ($arguments->immediate) {
                     echo PHP_EOL . $message . PHP_EOL;
                 }
                 //Error message
                 $stats->error += 1;
             } catch (\Exception $e) {
                 //Error message
                 $message = $e->getMessage();
                 if ($arguments->debug) {
                     $message .= PHP_EOL . $testphase->getResponseBody();
                 }
                 $message = sprintf(self::colored('Test "%s" failed with message', 'red') . PHP_EOL . ' %s' . PHP_EOL, $test->getFilename(), $message);
                 if (get_class($e) == IncorrectParametersException::class) {
                     $message .= 'Incorrect:' . PHP_EOL . json_encode($e->getParameters(), JSON_PRETTY_PRINT) . PHP_EOL;
                 } elseif (get_class($e) == MissingParametersException::class) {
                     $message .= 'Missing:' . PHP_EOL . json_encode($e->getParameters(), JSON_PRETTY_PRINT) . PHP_EOL;
                 }
                 //push message to error message
                 $stats->errors[] = $message;
                 //Echo unsuccessful char
                 if ($arguments->verbose) {
                     echo sprintf('F %s', $test->getFilename()) . PHP_EOL;
                 } else {
                     echo 'F';
                 }
                 //print if immediate
                 if ($arguments->immediate) {
                     echo PHP_EOL . $message . PHP_EOL;
                 }
                 $stats->failure += 1;
             }
             ++$testIndex;
             //Show only 80 characters per line
             if (!($testIndex % 79)) {
                 echo PHP_EOL;
             }
         }
     }
     echo PHP_EOL;
     if ($arguments->{'show-globals'}) {
         echo 'Globals:' . PHP_EOL;
         echo Globals::toString() . PHP_EOL;
     }
     //don't print if immediate is true
     if (!$arguments->immediate && !empty($stats->errors)) {
         echo 'Errors:' . PHP_EOL;
         foreach ($stats->errors as $e) {
             echo $e . PHP_EOL;
         }
     }
     echo 'Complete!' . PHP_EOL;
     echo 'Tests:' . $stats->tests . ', ';
     Binary::output('Successful: ' . $stats->success, 'green');
     echo ', ';
     Binary::output('Ignored: ' . $stats->ignore, 'yellow');
     echo ', ';
     Binary::output('Incomplete: ' . $stats->incomplete, 'yellow');
     echo ', ';
     Binary::output('Error: ' . $stats->error, 'red');
     echo ', ';
     Binary::output('Failure: ' . $stats->failure . PHP_EOL, 'red');
     echo 'Memory usage: ' . (int) (memory_get_usage(true) / 1048576) . ' MB' . PHP_EOL;
     echo 'Elapsed time: ' . (time() - $_SERVER['REQUEST_TIME']) . ' s' . PHP_EOL;
     if ($stats->error > 0) {
         return $this->stop(1);
     }
     if ($stats->failure > 0) {
         return $this->stop(2);
     }
     return $this->stop(0);
 }