Skip to content

Commit

Permalink
Moving some functions to Util and adding reporting when only excluded…
Browse files Browse the repository at this point in the history
… files are supplied
  • Loading branch information
IsaacChapman committed Jun 1, 2015
1 parent 6dadf6c commit 98af39a
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 103 deletions.
21 changes: 16 additions & 5 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,23 @@ public static function run(array $args, $exit = true)

// Ensure there are files to test
if (!count($config->testFiles)) {
if ($exit) {
echo("No test files found.\n");
self::usage();
exit(2);
if (count($config->excludeFiles)) {
echo ("Only <exclude/> designated files specified/\n");
$stripPath = getenv('TDDIUM_REPO_ROOT') ? getenv('TDDIUM_REPO_ROOT') : getcwd();
SolanoLabs_PHPUnit_Util::writeOutputFile($config->outputFile, $stripPath, $config->testFiles, $config->excludeFiles);
if ($exit) {
exit(0);
} else {
return true;
}
} else {
return false;
if ($exit) {
echo("No test files found.\n");
self::usage();
exit(2);
} else {
return false;
}
}
}

Expand Down
98 changes: 1 addition & 97 deletions src/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,103 +255,7 @@ private function addTestCase($testcase)
*/
public function flush()
{
$this->convertOutputToUTF8();
if (file_exists($this->outputFile)) {
// If the output file exists, add to it
$json = json_decode(file_get_contents($this->outputFile), true);
if (is_null($json) || !isset($json['byfile']) || !is_array($json['byfile'])) {
// JSON could not be read
echo("### ERROR: JSON data could not be read from " . $this->outputFile . "\n");
$jsonData = array('byfile' => $this->files);
} else {
$jsonData = array('byfile' => array_merge($json['byfile'], $this->files));
}
} else {
// Output file doesn't exist, create fresh.
$jsonData = array('byfile' => $this->files);
}

if (count($this->excludeFiles)) {
$jsonData = array('byfile' => array_merge($jsonData['byfile'], $this->generateExcludeFileNotices()));
}

$file = fopen($this->outputFile, 'w');
if (!defined('JSON_PRETTY_PRINT')) { define('JSON_PRETTY_PRINT', 128); } // JSON_PRETTY_PRINT available since PHP 5.4.0
fwrite($file, json_encode($jsonData, JSON_PRETTY_PRINT));
fclose($file);
}

/**
* Create output for files that were excluded in the XML config
*/
private function generateExcludeFileNotices()
{
$skipFiles = array();
foreach($this->excludeFiles as $file) {
$shortFilename = $file;
if (0 === strpos($file, $this->stripPath)) {
$shortFilename = substr($file, strlen($this->stripPath) + 1);
}
// Can we inspect the file?
try {
$fileMethods = array();
$declaredClasses = get_declared_classes();
PHPUnit_Util_Fileloader::checkAndLoad($file);
$newClasses = array_diff(get_declared_classes(), $declaredClasses);
foreach($newClasses as $className) {
$class = new ReflectionClass($className);
if ($class->implementsInterface('PHPUnit_Framework_Test')) {
$methods = $class->getMethods();
foreach ($methods as $method) {
if (0 === strpos($method->name, 'test')) {
$fileMethod = array('id' => $className . '::' . $method->name,
'address' => $className . '::' . $method->name,
'status' => 'skip',
'stderr' => 'Skipped Test File: ' . $shortFilename . "\n" . 'Excluded by <exclude/> in configuration',
'stdout' => '',
'time' => 0,
'traceback' => array());
$fileMethods[] = $fileMethod;
}
}
}
}
if (count($fileMethods)) {
$skipFiles[$shortFilename] = $fileMethods;
} else {
$skipFiles[$shortFilename] = array(array('id' => $shortFilename,
'address' => $shortFilename,
'status' => 'skip',
'stderr' => 'Skipped Test File: ' . $shortFilename . "\n" . 'Excluded by <exclude/> and no test methods found',
'stdout' => '',
'time' => 0,
'traceback' => array()));
}
} catch (Exception $e) {
$skipFiles[$shortFilename] = array(array('id' => $shortFilename,
'address' => $shortFilename,
'status' => 'skip',
'stderr' => 'Skipped Test File: ' . $shortFilename . "\n" . 'Excluded by <exclude/> in configuration and could not inspect file:' . "\n" . $e->getMessage(),
'stdout' => '',
'time' => 0,
'traceback' => array()));
}

}
return $skipFiles;
}

/**
* Convert to utf8
*/
private function convertOutputToUTF8()
{
array_walk_recursive($this->files, function (&$input) {
if (is_string($input)) {
$input = PHPUnit_Util_String::convertToUtf8($input);
}
});
unset($input);
SolanoLabs_PHPUnit_Util::writeOutputFile($this->outputFile, $this->stripPath, $this->files, $this->excludeFiles);
}
}

Expand Down
117 changes: 116 additions & 1 deletion src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,120 @@ public static function toAbsolutePath($path, $useIncludePath = false)
}

return $path;
}
}

/**
* Create output for files that were excluded in the XML config
*
* @param array $excludeFiles
* @param string $stripPath
*/
public static function generateExcludeFileNotices($excludeFiles, $stripPath)
{
$skipFiles = array();
foreach($excludeFiles as $file) {
$shortFilename = $file;
if (0 === strpos($file, $stripPath)) {
$shortFilename = substr($file, strlen($stripPath) + 1);
}
// Can we inspect the file?
try {
$fileMethods = array();
$declaredClasses = get_declared_classes();
PHPUnit_Util_Fileloader::checkAndLoad($file);
$newClasses = array_diff(get_declared_classes(), $declaredClasses);
foreach($newClasses as $className) {
$class = new ReflectionClass($className);
if ($class->implementsInterface('PHPUnit_Framework_Test')) {
$methods = $class->getMethods();
foreach ($methods as $method) {
if (0 === strpos($method->name, 'test')) {
$fileMethod = array('id' => $className . '::' . $method->name,
'address' => $className . '::' . $method->name,
'status' => 'skip',
'stderr' => 'Skipped Test File: ' . $shortFilename . "\n" . 'Excluded by <exclude/> in configuration',
'stdout' => '',
'time' => 0,
'traceback' => array());
$fileMethods[] = $fileMethod;
}
}
}
}
if (count($fileMethods)) {
$skipFiles[$shortFilename] = $fileMethods;
} else {
$skipFiles[$shortFilename] = array(array('id' => $shortFilename,
'address' => $shortFilename,
'status' => 'skip',
'stderr' => 'Skipped Test File: ' . $shortFilename . "\n" . 'Excluded by <exclude/> and no test methods found',
'stdout' => '',
'time' => 0,
'traceback' => array()));
}
} catch (Exception $e) {
$skipFiles[$shortFilename] = array(array('id' => $shortFilename,
'address' => $shortFilename,
'status' => 'skip',
'stderr' => 'Skipped Test File: ' . $shortFilename . "\n" . 'Excluded by <exclude/> in configuration and could not inspect file:' . "\n" . $e->getMessage(),
'stdout' => '',
'time' => 0,
'traceback' => array()));
}

}
return $skipFiles;
}

/**
* Write output file.
*
* @param string $outputFile
* @param string $stripPath
* @param array $files
* @param array $excludeFiles
*/
public static function writeOutputFile($outputFile, $stripPath, $files = array(), $excludeFiles = array())
{
$files = self::convertOutputToUTF8($files);
if (file_exists($outputFile)) {
// If the output file exists, add to it
$json = json_decode(file_get_contents($outputFile), true);
if (is_null($json) || !isset($json['byfile']) || !is_array($json['byfile'])) {
// JSON could not be read
echo("### ERROR: JSON data could not be read from " . $outputFile . "\n");
$jsonData = array('byfile' => $files);
} else {
$jsonData = array('byfile' => array_merge($json['byfile'], $files));
}
} else {
// Output file doesn't exist, create fresh.
$jsonData = array('byfile' => $files);
}

if (count($excludeFiles)) {
$jsonData = array('byfile' => array_merge($jsonData['byfile'], self::generateExcludeFileNotices($excludeFiles, $stripPath)));
}

$file = fopen($outputFile, 'w');
if (!defined('JSON_PRETTY_PRINT')) { define('JSON_PRETTY_PRINT', 128); } // JSON_PRETTY_PRINT available since PHP 5.4.0
fwrite($file, json_encode($jsonData, JSON_PRETTY_PRINT));
fclose($file);
}

/**
* Convert to utf8
*
* @param array $array
*/
public static function convertOutputToUTF8($array)
{
array_walk_recursive($array, function (&$input) {
if (is_string($input)) {
$input = PHPUnit_Util_String::convertToUtf8($input);
}
});
unset($input);
return $array;
}
}

0 comments on commit 98af39a

Please sign in to comment.