Skip to content

Commit

Permalink
Recreate branch for PHPUnit 3.1 based upon tag for PHPUnit 3.1.5.
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Aug 3, 2007
2 parents e2a0da3 + 416744c commit 4f8a921
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 565 deletions.
36 changes: 16 additions & 20 deletions PHPUnit/Framework/Assert.php
Expand Up @@ -604,45 +604,41 @@ public static function assertFileNotExists($filename, $message = '')
*
* @param boolean $condition
* @param string $message
* @throws PHPUnit_Framework_AssertionFailedError
* @throws PHPUnit_Framework_ComparisonFailure
* @throws InvalidArgumentException
* @access public
* @static
*/
public static function assertTrue($condition, $message = '')
{
if ($condition !== TRUE) {
throw new PHPUnit_Framework_AssertionFailedError(
sprintf(
'%sFailed asserting that %s is true.',

$message != '' ? $message . "\n" : '',
PHPUnit_Util_Type::toString($condition)
)
);
if (!is_bool($condition)) {
throw new InvalidArgumentException;
}

$constraint = new PHPUnit_Framework_Constraint_IsIdentical(TRUE);

self::assertThat($condition, $constraint, $message);
}

/**
* Asserts that a condition is false.
*
* @param boolean $condition
* @param string $message
* @throws PHPUnit_Framework_AssertionFailedError
* @throws PHPUnit_Framework_ComparisonFailure
* @throws InvalidArgumentException
* @access public
* @static
*/
public static function assertFalse($condition, $message = '')
{
if ($condition !== FALSE) {
throw new PHPUnit_Framework_AssertionFailedError(
sprintf(
'%sFailed asserting that %s is false.',

$message != '' ? $message . "\n" : '',
PHPUnit_Util_Type::toString($condition)
)
);
if (!is_bool($condition)) {
throw new InvalidArgumentException;
}

$constraint = new PHPUnit_Framework_Constraint_IsIdentical(FALSE);

self::assertThat($condition, $constraint, $message);
}

/**
Expand Down
117 changes: 0 additions & 117 deletions PHPUnit/Util/Class.php
Expand Up @@ -64,7 +64,6 @@ class PHPUnit_Util_Class
{
protected static $buffer = array();
protected static $fileClassMap = array();
protected static $noc = array();

/**
* Starts the collection of loaded classes.
Expand Down Expand Up @@ -216,121 +215,5 @@ public static function getMethodSource($className, $methodName)

return $result;
}

/**
* Returns the Cyclomatic Complexity Number (CCN) for a method.
* This is also known as the McCabe metric.
*
* Each method has a minimum value of 1 per default. For each of the
* following PHP keywords/statements this value gets incremented by one:
*
* - if
* - for
* - foreach
* - while
* - case
* - catch
* - AND, &&
* - OR, ||
*
* Note that 'else', 'default', and 'finally' don't increment the value
* any further. On the other hand, a simple method with a 'switch'
* statement and a huge block of 'case 'statements can have a surprisingly
* high value (still it has the same value when converting a 'switch'
* block to an equivalent sequence of 'if' statements).
*
* @param string $className
* @param string $methodName
* @return integer
* @access public
* @static
* @since Method available since Release 3.1.6
*/
public static function getCCN($className, $methodName)
{
$source = self::getMethodSource($className, $methodName);
$tokens = token_get_all('<?php' . $source . '?>');
$ccn = 1;

foreach ($tokens as $i => $token) {
if (is_string($token)) {
continue;
}

list ($token, $value) = $token;

switch ($token) {
case T_IF:
case T_FOR:
case T_FOREACH:
case T_WHILE:
case T_CASE:
case T_CATCH:
case T_BOOLEAN_AND:
case T_LOGICAL_AND:
case T_BOOLEAN_OR:
case T_LOGICAL_OR: {
$ccn++;
}
break;
}
}

return $ccn;
}

/**
* Returns the Depth of Inheritance Tree (DIT) for a class.
*
* @param string $className
* @return integer
* @access public
* @static
* @since Method available since Release 3.1.6
*/
public static function getDIT($className)
{
return count(self::getHierarchy($className));
}

/**
* Returns the Number of Children (NOC) for a class.
*
* @param string $className
* @param boolean $clearCache
* @return integer
* @access public
* @static
* @since Method available since Release 3.1.6
*/
public static function getNOC($className, $clearCache = FALSE)
{
if ($clearCache) {
self::$noc = array();
}

if (empty(self::$noc)) {
foreach (get_declared_classes() as $_className) {
$class = new ReflectionClass($_className);
$parent = $class->getParentClass();

if ($parent !== FALSE) {
$parentName = $parent->getName();

if (isset(self::$noc[$parentName])) {
self::$noc[$parentName]++;
} else {
self::$noc[$parentName] = 1;
}
}
}
}

if (isset(self::$noc[$className])) {
return self::$noc[$className];
} else {
return 0;
}
}
}
?>
8 changes: 4 additions & 4 deletions PHPUnit/Util/Filter.php
Expand Up @@ -122,7 +122,7 @@ class PHPUnit_Util_Filter
*/
public static function addDirectoryToFilter($directory, $suffix = '.php', $group = 'DEFAULT')
{
foreach (self::getIterator($directory, $suffix) as $file) {
foreach ($this->getIterator($directory, $suffix) as $file) {
self::addFileToFilter($file->getPathName(), $group);
}
}
Expand Down Expand Up @@ -161,7 +161,7 @@ public static function addFileToFilter($filename, $group = 'DEFAULT')
*/
public static function removeDirectoryFromFilter($directory, $suffix = '.php', $group = 'DEFAULT')
{
foreach (self::getIterator($directory, $suffix) as $file) {
foreach ($this->getIterator($directory, $suffix) as $file) {
self::removeFileFromFilter($file->getPathName(), $group);
}
}
Expand Down Expand Up @@ -199,7 +199,7 @@ public static function removeFileFromFilter($filename, $group = 'DEFAULT')
*/
public static function addDirectoryToWhitelist($directory, $suffix = '.php')
{
foreach (self::getIterator($directory, $suffix) as $file) {
foreach ($this->getIterator($directory, $suffix) as $file) {
self::addFileToWhitelist($file->getPathName());
}
}
Expand Down Expand Up @@ -235,7 +235,7 @@ public static function addFileToWhitelist($filename)
*/
public static function removeDirectoryFromWhitelist($directory, $suffix = '.php')
{
foreach (self::getIterator($directory, $suffix) as $file) {
foreach ($this->getIterator($directory, $suffix) as $file) {
self::removeFileFromWhitelist($file->getPathName());
}
}
Expand Down
52 changes: 13 additions & 39 deletions PHPUnit/Util/Log/CodeCoverage/Database.php
Expand Up @@ -49,7 +49,6 @@
require_once 'PHPUnit/Util/CodeCoverage.php';
require_once 'PHPUnit/Util/Filesystem.php';
require_once 'PHPUnit/Util/Filter.php';
require_once 'PHPUnit/Util/SourceFile.php';

PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');

Expand Down Expand Up @@ -102,14 +101,10 @@ public function storeCodeCoverage(PHPUnit_Framework_TestResult $result, $revisio
$this->dbh->beginTransaction();

foreach ($files as $file) {
$sourceFile = new PHPUnit_Util_SourceFile($file);
$filename = str_replace($commonPath, '', $file);
$fileId = FALSE;
$lines = $sourceFile->getLines();
$loc = $sourceFile->getLoc();
$cloc = $sourceFile->getCloc();
$ncloc = $sourceFile->getNcloc();
$hash = md5_file($file);
$filename = str_replace($commonPath, '', $file);
$fileId = FALSE;
$lines = file($file);
$numLines = count($lines);

$stmt = $this->dbh->prepare(
'SELECT code_file_id
Expand All @@ -129,18 +124,17 @@ public function storeCodeCoverage(PHPUnit_Framework_TestResult $result, $revisio
unset($stmt);

if ($fileId == 0) {
$hash = md5_file($file);

$stmt = $this->dbh->prepare(
'INSERT INTO code_file
(code_file_name, code_file_md5, revision, loc, cloc, ncloc)
VALUES(:filename, :hash, :revision, :loc, :cloc, :ncloc);'
(code_file_name, code_file_md5, revision)
VALUES(:filename, :hash, :revision);'
);

$stmt->bindParam(':filename', $filename, PDO::PARAM_STR);
$stmt->bindParam(':hash', $hash, PDO::PARAM_STR);
$stmt->bindParam(':revision', $revision, PDO::PARAM_INT);
$stmt->bindParam(':loc', $loc, PDO::PARAM_INT);
$stmt->bindParam(':cloc', $cloc, PDO::PARAM_INT);
$stmt->bindParam(':ncloc', $ncloc, PDO::PARAM_INT);
$stmt->execute();

$fileId = $this->dbh->lastInsertId();
Expand All @@ -149,36 +143,30 @@ public function storeCodeCoverage(PHPUnit_Framework_TestResult $result, $revisio
$stmt = $this->dbh->prepare(
'INSERT INTO code_class
(code_file_id, code_class_name,
code_class_start_line, code_class_end_line,
code_class_dit, code_class_wmc)
VALUES(:fileId, :className, :startLine, :endLine, :dit, 0);'
code_class_start_line, code_class_end_line)
VALUES(:fileId, :className, :startLine, :endLine);'
);

foreach ($classes as $class) {
$className = $class->getName();
$startLine = $class->getStartLine();
$endLine = $class->getEndLine();
$dit = PHPUnit_Util_Class::getDIT( $className );

$stmt->bindParam(':fileId', $fileId, PDO::PARAM_INT);
$stmt->bindParam(':className', $className, PDO::PARAM_STR);
$stmt->bindParam(':startLine', $startLine, PDO::PARAM_INT);
$stmt->bindParam(':endLine', $endLine, PDO::PARAM_INT);
$stmt->bindParam(':dit', $dit, PDO::PARAM_INT);
$stmt->execute();

$classId = $this->dbh->lastInsertId();

$stmt2 = $this->dbh->prepare(
'INSERT INTO code_method
(code_class_id, code_method_name,
code_method_start_line, code_method_end_line,
code_method_ccn)
VALUES(:classId, :methodName, :startLine, :endLine, :ccn);'
code_method_start_line, code_method_end_line)
VALUES(:classId, :methodName, :startLine, :endLine);'
);

$wmc = 0;

foreach ($class->getMethods() as $method) {
if ($class->getName() != $method->getDeclaringClass()->getName()) {
continue;
Expand All @@ -187,31 +175,17 @@ public function storeCodeCoverage(PHPUnit_Framework_TestResult $result, $revisio
$methodName = $method->getName();
$startLine = $method->getStartLine();
$endLine = $method->getEndLine();
$ccn = PHPUnit_Util_Class::getCCN($className, $methodName);

$stmt2->bindParam(':classId', $classId, PDO::PARAM_INT);
$stmt2->bindParam(':methodName', $methodName, PDO::PARAM_STR);
$stmt2->bindParam(':startLine', $startLine, PDO::PARAM_INT);
$stmt2->bindParam(':endLine', $endLine, PDO::PARAM_INT);
$stmt2->bindParam(':ccn', $ccn, PDO::PARAM_INT);
$stmt2->execute();

$wmc += $ccn;
}

unset($stmt2);
}

$stmt = $this->dbh->prepare(
'UPDATE code_class
SET code_class_wmc = :wmc
WHERE code_class_id = :classId;'
);

$stmt->bindParam(':classId', $classId, PDO::PARAM_INT);
$stmt->bindParam(':wmc', $wmc, PDO::PARAM_INT);
$stmt->execute();

$stmt = $this->dbh->prepare(
'INSERT INTO code_line
(code_file_id, code_line_number, code_line,
Expand Down Expand Up @@ -253,7 +227,7 @@ public function storeCodeCoverage(PHPUnit_Framework_TestResult $result, $revisio
VALUES(:testId, :lineId);'
);

for ($lineNumber = 1; $lineNumber <= $loc; $lineNumber++) {
for ($lineNumber = 1; $lineNumber <= $numLines; $lineNumber++) {
$coveringTests = PHPUnit_Util_CodeCoverage::getCoveringTests(
$codeCoverage, $file, $lineNumber
);
Expand Down

0 comments on commit 4f8a921

Please sign in to comment.