main() public static method

public static main ( boolean $exit = true )
$exit boolean
 /**
  * Runs PHPUnit.
  *
  * @return void
  */
 public function run()
 {
     require_once 'PHPUnit/Autoload.php';
     /** @var string */
     define('PHPUnit_MAIN_METHOD', 'PHPUnit_TextUI_Command::main');
     PHPUnit_TextUI_Command::main();
 }
 /**
  * Should run tests in browser
  * @param null $filterClass
  */
 public function runAction($filterClass = null)
 {
     // Make sure PHPUnit is autoloaded
     require_once 'PHPUnit/Autoload.php';
     set_time_limit(3600);
     $version = \PHPUnit_Runner_Version::id();
     $kernel_dir = $this->container->getParameter('kernel.root_dir');
     chdir($kernel_dir);
     // This will force the printer class to be autoloaded by Symfony, before PHPUnit tries to (and does not) find it
     $printerClass = 'Goutte\\DoodleBundle\\Tools\\PHPUnit\\HtmlResultPrinter';
     if (!class_exists($printerClass)) {
         $printerClass = false;
     }
     $argv = array();
     $argv[] = 'phpunit';
     if ($filterClass) {
         $argv[] = '--filter';
         $argv[] = $filterClass;
     }
     if (version_compare($version, "3.6.0") >= 0) {
         if ($printerClass) {
             $argv[] = '--printer';
             $argv[] = $printerClass;
         }
         $_SERVER['argv'] = $argv;
         \PHPUnit_TextUI_Command::main(true);
     } else {
         ob_end_clean();
         echo '<pre>';
         $_SERVER['argv'] = $argv;
         \PHPUnit_TextUI_Command::main(false);
         echo '</pre>';
         exit;
     }
 }
Example #3
0
 /**
  * Runs PHPUnit.
  *
  * @return void
  */
 public function run()
 {
     // Store current TYPO3 configuration and set the default one
     // This is needed as the configuration might include closures which cannot be backed up
     $globalBackup = $this->removeClosures($GLOBALS['TYPO3_CONF_VARS']);
     // Run unit tests
     /** @var string */
     define('PHPUnit_MAIN_METHOD', 'PHPUnit_TextUI_Command::main');
     PHPUnit_TextUI_Command::main();
     // Restore configuration
     $GLOBALS['TYPO3_CONF_VARS'] = array_merge($GLOBALS['TYPO3_CONF_VARS'], $globalBackup);
 }
Example #4
0
$ok = false;
if (class_exists('PHPUnit_TextUI_Command')) {
    echo "PHPUnit already present\n";
    $ok = true;
} else {
    foreach (array(stream_resolve_include_path('phpunit.phar'), stream_resolve_include_path('phpunit-old.phar'), 'PHPUnit/Runner/Version.php', 'PHPUnit/Autoload.php') as $includePath) {
        if ($includePath === false) {
            // stream_resolve_include_path can return false
            continue;
        }
        \MediaWiki\suppressWarnings();
        include_once $includePath;
        \MediaWiki\restoreWarnings();
        if (class_exists('PHPUnit_TextUI_Command')) {
            $ok = true;
            echo "Using PHPUnit from {$includePath}\n";
            break;
        }
    }
}
if (!$ok) {
    echo "Couldn't find a usable PHPUnit.\n";
    exit(1);
}
$puVersion = PHPUnit_Runner_Version::id();
if ($puVersion !== '@package_version@' && version_compare($puVersion, '3.7.0', '<')) {
    echo "PHPUnit 3.7.0 or later required; you have {$puVersion}.\n";
    exit(1);
}
PHPUnit_TextUI_Command::main();
Example #5
0
<?php

require_once __DIR__ . '/vendor/autoload.php';
use SPE\Core\TestController;
use SPE\CKlein\Reports\ConsolidatedReport;
try {
    PHPUnit_TextUI_Command::main(false);
} catch (Exception $e) {
}
if (TestController::isTestSuccessful()) {
    echo "generate consolidation report";
    $consolidatedReport = new ConsolidatedReport($argv);
    $consolidatedReport->generate();
} else {
    echo "in shutdown";
    TestController::shutdown();
}
Example #6
0
 /**
  * Run Both Unit-test and Code-coverage analysist
  *
  * @param  string Path to Test (Juriya idiomatic style directory structure is required)
  * @return stream
  */
 public static function runTest($target = '')
 {
     $paths = explode(PATH_SEPARATOR, get_include_path());
     $pwd = $_SERVER['PWD'];
     if (!$target) {
         self::out('--test command missed target folder');
     } else {
         // Find configuration definition
         $configuration = '';
         $configurationExists = FALSE;
         if (file_exists($pwd . DIRECTORY_SEPARATOR . 'phpunit.xml')) {
             $configuration = $pwd . DIRECTORY_SEPARATOR . 'phpunit.xml';
             $configurationExists = TRUE;
         } elseif (($path = $pwd . DIRECTORY_SEPARATOR . $target) && is_dir($path)) {
             if (($testSrc = $path . DIRECTORY_SEPARATOR . 'Tests') && is_dir($testSrc)) {
                 $configuration = $testSrc . DIRECTORY_SEPARATOR . 'phpunit.xml';
             }
         } else {
             foreach ($paths as $path) {
                 if (($testSrc = $path . DIRECTORY_SEPARATOR . $target . DIRECTORY_SEPARATOR . 'Tests') && is_dir($testSrc)) {
                     $configuration = $testSrc . DIRECTORY_SEPARATOR . 'phpunit.xml';
                     break 1;
                 }
             }
         }
         // Garbage collection
         $coveragePhp = '/tmp/coverage.php';
         file_exists($coveragePhp) and File::delete($coveragePhp);
         // Try to read the configuration
         try {
             $configurationInstance = \PHPUnit_Util_Configuration::getInstance($configuration);
         } catch (\Exception $e) {
             print $e->getMessage() . "\n";
             exit(\PHPUnit_TextUI_TestRunner::FAILURE_EXIT);
         }
         $configurationArray = $configurationInstance->getPHPUnitConfiguration();
         // Validate test suites before start processing furthermore
         $testDirectory = new \DirectoryIterator(str_replace('phpunit.xml', '', $configuration));
         $validTestSuite = FALSE;
         while ($testDirectory->valid()) {
             if ($testDirectory->isFile() && strpos($testDirectory->getFilename(), 'Test.php') !== FALSE) {
                 $validTestSuite = TRUE;
             }
             $testDirectory->next();
         }
         if (!$validTestSuite) {
             $errorText = 'ERROR: ' . I18n::translate('command_testsuite_not_found');
             if ($configurationArray['colors'] === TRUE) {
                 $errorText = Utility::getColoredString($errorText, 'black', 'red');
             }
             self::out($errorText);
             exit(\PHPUnit_TextUI_TestRunner::FAILURE_EXIT);
         }
         // Reset server arguments
         $_SERVER['argv'] = array('--configuration', $configuration, '--coverage-php', $coveragePhp);
         // Preparing reports
         $strippedText = 'Generating code coverage report in PHP format ... done';
         $reportTestTitle = 'PHPUnit Report : ';
         self::out($configurationArray['colors'] ? Utility::getColoredString($reportTestTitle, 'yellow') : $reportTestTitle);
         $reportCcTitle = 'CodeCoverage Report : ';
         $reportCcTitle = $configurationArray['colors'] ? Utility::getColoredString($reportCcTitle, 'yellow') : $reportCcTitle;
         // Get phpunit report
         ob_start();
         $exitCode = \PHPUnit_TextUI_Command::main(FALSE);
         $out = ob_get_clean();
         $report = substr($out, strpos($out, "\n\n") + 1);
         $unitTestReport = str_replace($strippedText, $reportCcTitle, $report);
         $codeCoverage = @unserialize(File::read($coveragePhp));
         $codeCoverageData = $codeCoverage->getData();
         $codeCoverageRoot = $codeCoverage->getReport();
         $codeCoverageNodes = $codeCoverageRoot->getChildNodes();
         // Generate code coverage report
         $classCoverage = array();
         $overallCoverage = array('totalClasses' => 0, 'totalCoveredClasses' => 0, 'totalMethods' => 0, 'totalCoveredMethods' => 0, 'totalLines' => 0, 'totalCoveredLines' => 0);
         // Closure for calculate the level
         $calculate = function ($percentages) {
             $level = 'gray';
             if ($percentages >= 75) {
                 $level = 'green';
             } elseif ($percentages < 75 && $percentages >= 50) {
                 $level = 'yellow';
             } elseif ($percentages < 50 && $percentages >= 25) {
                 $level = 'magenta';
             } else {
                 $level = 'red';
             }
             return $level;
         };
         $collectFromNode = function ($node) {
             $classes = array_merge($node->getClasses(), $node->getTraits());
             $coverage = $node->getCoverageData();
             $lines = array();
             $ignoredLines = $node->getIgnoredLines();
             foreach ($classes as $className => $class) {
                 $classStatements = 0;
                 $coveredClassStatements = 0;
                 $coveredMethods = 0;
                 foreach ($class['methods'] as $methodName => $method) {
                     $methodCount = 0;
                     $methodLines = 0;
                     $methodLinesCovered = 0;
                     for ($z = $method['startLine']; $z <= $method['endLine']; $z++) {
                         if (isset($ignoredLines[$z])) {
                             continue;
                         }
                         $add = TRUE;
                         $count = 0;
                         if (isset($coverage[$z])) {
                             if ($coverage[$z] !== NULL) {
                                 $classStatements++;
                                 $methodLines++;
                             } else {
                                 $add = FALSE;
                             }
                             $count = count($coverage[$z]);
                             if ($count > 0) {
                                 $coveredClassStatements++;
                                 $methodLinesCovered++;
                             }
                         } else {
                             $add = FALSE;
                         }
                         $methodCount = max($methodCount, $count);
                         if ($add) {
                             $lines[$z] = array('count' => $count, 'type' => 'stmt');
                         }
                     }
                     if ($methodCount > 0) {
                         $coveredMethods++;
                     }
                 }
                 if (!empty($class['package']['namespace'])) {
                     $namespace = '\\' . $class['package']['namespace'] . '\\';
                 } elseif (!empty($class['package']['fullPackage'])) {
                     $namespace = '@' . $class['package']['fullPackage'] . '\\';
                 } else {
                     $namespace = '';
                 }
                 return array('namespace' => $namespace, 'className' => $className, 'methodsCovered' => $coveredMethods, 'methodCount' => count($class['methods']), 'statementsCovered' => $coveredClassStatements, 'statementCount' => $classStatements);
             }
         };
         for ($i = 0, $max = count($codeCoverageNodes); $i < $max; $i++) {
             $item = $codeCoverageNodes[$i];
             if ($item instanceof \PHP_CodeCoverage_Report_Node_File) {
                 array_push($classCoverage, $collectFromNode($item));
             } elseif ($item instanceof \PHP_CodeCoverage_Report_Node_Directory) {
                 foreach ($item->getChildNodes() as $itemNode) {
                     array_push($classCoverage, $collectFromNode($itemNode));
                 }
             } else {
                 continue;
             }
         }
         ksort($classCoverage);
         $codeCoverageReport = '';
         for ($x = 0, $max = count($classCoverage); $x < $max; $x++) {
             $classInfo = $classCoverage[$x];
             $fullQualifiedPath = $classInfo['namespace'] . $classInfo['className'];
             if ($classInfo['statementsCovered'] != 0) {
                 // Increase overallCoverage
                 $overallCoverage['totalClasses']++;
                 $overallCoverage['totalMethods'] = $overallCoverage['totalMethods'] + $classInfo['methodCount'];
                 $overallCoverage['totalCoveredMethods'] = $overallCoverage['totalCoveredMethods'] + $classInfo['methodsCovered'];
                 $overallCoverage['totalLines'] = $overallCoverage['totalLines'] + $classInfo['statementCount'];
                 $overallCoverage['totalCoveredLines'] = $overallCoverage['totalCoveredLines'] + $classInfo['statementsCovered'];
                 // Build item Code-coverage main report
                 $ccPath = $fullQualifiedPath;
                 $ccMethods = str_pad(' Methods(' . $classInfo['methodsCovered'] . '/' . $classInfo['methodCount'] . ')', 18, ' ');
                 $ccLines = str_pad(' Lines(' . $classInfo['statementsCovered'] . '/' . $classInfo['statementCount'] . ')', 18, ' ');
                 // Calculate the percentage
                 $ccMethodsPercentages = round((int) $classInfo['methodsCovered'] / (int) $classInfo['methodCount'] * 100, 2);
                 $ccLinesPercentages = round((int) $classInfo['statementsCovered'] / (int) $classInfo['statementCount'] * 100, 2);
                 // Normalize the pad
                 $ccMethodsPercentagesText = $ccMethods . ': ' . str_pad($ccMethodsPercentages . '% ', 8, ' ', STR_PAD_LEFT);
                 $ccLinesPercentagesText = $ccLines . ': ' . str_pad($ccLinesPercentages . '% ', 8, ' ', STR_PAD_LEFT);
                 // Increase classed covered for 100% classes
                 if ($ccMethodsPercentages == 100 && $ccLinesPercentages == 100) {
                     $overallCoverage['totalCoveredClasses']++;
                 }
                 // Colorize if necessary
                 if ($configurationArray['colors'] === TRUE) {
                     $ccPath = Utility::getColoredString($ccPath, 'light_cyan');
                     $ccMethodsLevel = $calculate($ccMethodsPercentages);
                     $ccLinesLevel = $calculate($ccLinesPercentages);
                     $ccMethodsPercentagesText = Utility::getColoredString($ccMethodsPercentagesText, 'black', $ccMethodsLevel);
                     $ccLinesPercentagesText = Utility::getColoredString($ccLinesPercentagesText, 'black', $ccLinesLevel);
                 }
                 $codeCoverageReport .= PHP_EOL . $ccPath . PHP_EOL . $ccMethodsPercentagesText . PHP_EOL . $ccLinesPercentagesText . PHP_EOL;
             }
         }
         // Finalize Code-coverage report
         if ($overallCoverage['totalCoveredClasses'] > 0) {
             $overallPercentage = round((int) $overallCoverage['totalCoveredClasses'] / (int) $overallCoverage['totalClasses'] * 100, 2);
         } else {
             $overallPercentage = 0;
         }
         if ($overallPercentage >= 75) {
             $overallPercentageText = 'OK';
         } elseif ($overallPercentage < 75 && $overallPercentage >= 50) {
             $overallPercentageText = 'MEDIUM';
         } else {
             $overallPercentageText = 'LOW';
         }
         $overallCoverageReport = $overallPercentageText . ' (';
         $overallCoverageReport .= $overallPercentage;
         $overallCoverageReport .= '% classes, ';
         if ($overallCoverage['totalCoveredMethods'] > 0) {
             $overallCoverageReport .= round((int) $overallCoverage['totalCoveredMethods'] / (int) $overallCoverage['totalMethods'] * 100, 2);
         } else {
             $overallCoverageReport .= 0;
         }
         $overallCoverageReport .= '% methods, ';
         if ($overallCoverage['totalCoveredLines'] > 0) {
             $overallCoverageReport .= round((int) $overallCoverage['totalCoveredLines'] / (int) $overallCoverage['totalLines'] * 100, 2);
         } else {
             $overallCoverageReport .= 0;
         }
         $overallCoverageReport .= '% lines were covered)';
         // Colorize if necessary
         if ($configurationArray['colors'] === TRUE) {
             $coverageStatus = $calculate($overallPercentage);
             $overallCoverageReport = Utility::getColoredString($overallCoverageReport, 'black', $coverageStatus);
         }
         $codeCoverageReport .= PHP_EOL . $overallCoverageReport . PHP_EOL;
         // Output all tests reports
         self::out($unitTestReport . $codeCoverageReport);
         exit($exitCode);
     }
 }
Example #7
0
 /**
  * Execute the command
  * 
  * @return void
  */
 public function fire()
 {
     $_SERVER['argv'] = array_slice($_SERVER['argv'], 2);
     \PHPUnit_TextUI_Command::main(false);
 }
Example #8
0
 public static function main($exit = TRUE)
 {
     return parent::main($exit);
 }
Example #9
0
 /**
  * Runs PHPUnit.
  *
  * @return void
  */
 public function run()
 {
     /** @var string */
     define('PHPUnit_MAIN_METHOD', 'PHPUnit_TextUI_Command::main');
     PHPUnit_TextUI_Command::main();
 }