Skip to content

Commit

Permalink
Merge pull request #880 from BlaineSch/feature/complexityFixes
Browse files Browse the repository at this point in the history
Feature/complexity fixes
  • Loading branch information
nateabele committed Apr 12, 2013
2 parents fab503b + 85a9953 commit e789069
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 35 deletions.
24 changes: 19 additions & 5 deletions test/Mocker.php
Expand Up @@ -258,12 +258,22 @@ class Mocker {
' return $this->mocker->$name = $value;', ' return $this->mocker->$name = $value;',
'}', '}',
), ),
'isset' => array(
'public function __isset($name) {',
' return isset($this->mocker->$name);',
'}',
),
'unset' => array(
'public function __unset($name) {',
' unset($this->mocker->$name);',
'}',
),
'constructor' => array( 'constructor' => array(
'{:modifiers} function __construct({:args}) {', '{:modifiers} function __construct({:args}) {',
' $args = compact({:stringArgs});', ' $args = compact({:stringArgs});',
' array_push($args, $this);', ' array_push($args, $this);',
' foreach ($this as $key => $value) {', ' foreach (get_class_vars(get_class($this)) as $key => $value) {',
' if (!in_array($key, $this->_safeVars)) {', ' if (isset($this->{$key}) && !in_array($key, $this->_safeVars)) {',
' unset($this->$key);', ' unset($this->$key);',
' }', ' }',
' }', ' }',
Expand Down Expand Up @@ -429,6 +439,8 @@ public static function create($mockee) {
'reference' => $getByReference ? '&' : '', 'reference' => $getByReference ? '&' : '',
)); ));
$mock .= self::_dynamicCode('mock', 'set'); $mock .= self::_dynamicCode('mock', 'set');
$mock .= self::_dynamicCode('mock', 'isset');
$mock .= self::_dynamicCode('mock', 'unset');
$mock .= self::_dynamicCode('mock', 'applyFilter', array( $mock .= self::_dynamicCode('mock', 'applyFilter', array(
'static' => $staticApplyFilter ? 'static' : '', 'static' => $staticApplyFilter ? 'static' : '',
)); ));
Expand Down Expand Up @@ -595,10 +607,12 @@ public static function mergeResults($results, $secondary) {
* to remove all the current filters for the given method. * to remove all the current filters for the given method.
* @return void * @return void
*/ */
public static function applyFilter($class, $method, $filter = null) { public static function applyFilter($class, $method = null, $filter = null) {
if ($class === false) {
return static::$_methodFilters = array();
}
if ($method === false) { if ($method === false) {
static::$_methodFilters[$class] = array(); return static::$_methodFilters[$class] = array();
return;
} }
foreach ((array) $method as $m) { foreach ((array) $method as $m) {
if (!isset(static::$_methodFilters[$class][$m]) || $filter === false) { if (!isset(static::$_methodFilters[$class][$m]) || $filter === false) {
Expand Down
36 changes: 36 additions & 0 deletions test/Unit.php
Expand Up @@ -624,6 +624,42 @@ public function assertException($expected, $closure, $message = '{:message}') {
} }
} }


/**
* Assert that the code passed in a closure does not throw an exception matching the passed
* expected exception.
*
* The value passed to `exepected` is either an exception class name or the expected message.
*
* @param mixed $expected A string indicating what the error text is not expected to be. This
* can be an exact string, a /-delimited regular expression, or true, indicating
* that any error text is acceptable.
* @param closure $closure A closure containing the code that should throw the exception.
* @param string $message
* @return boolean
*/
public function assertNotException($expected, $closure, $message = '{:message}') {
try {
$closure();
} catch (Exception $e) {
$class = get_class($e);
$eMessage = $e->getMessage();
if (is_a($e, $expected)) {
$result = $class;
return $this->assert(false, $message, compact('expected', 'result'));
}
if ($eMessage === $expected) {
$result = $eMessage;
return $this->assert(false, $message, compact('expected', 'result'));
}
if (Validator::isRegex($expected) && preg_match($expected, $eMessage)) {
$result = $eMessage;
return $this->assert(false, $message, compact('expected', 'result'));
}
}
$message = sprintf('Exception "%s" was not expected.', $expected);
return $this->assert(true, $message, compact('expected', 'result'));
}

/** /**
* Assert Cookie data is properly set in headers. * Assert Cookie data is properly set in headers.
* *
Expand Down
26 changes: 18 additions & 8 deletions test/filter/Complexity.php
Expand Up @@ -8,9 +8,6 @@


namespace lithium\test\filter; namespace lithium\test\filter;


use lithium\analysis\Parser;
use lithium\analysis\Inspector;

/** /**
* Calculates the cyclomatic complexity of class methods, and shows worst-offenders and statistics. * Calculates the cyclomatic complexity of class methods, and shows worst-offenders and statistics.
*/ */
Expand All @@ -20,30 +17,43 @@ class Complexity extends \lithium\test\Filter {
* The list of tokens which represent the starting point of a code branch. * The list of tokens which represent the starting point of a code branch.
*/ */
protected static $_include = array( protected static $_include = array(
'T_CASE', 'T_DEFAULT', 'T_CATCH', 'T_IF', 'T_FOR', 'T_CASE', 'T_CATCH', 'T_IF', 'T_FOR',
'T_FOREACH', 'T_WHILE', 'T_DO', 'T_ELSEIF' 'T_FOREACH', 'T_WHILE', 'T_DO', 'T_ELSEIF'
); );


protected static $_classes = array(
'parser' => 'lithium\analysis\Parser',
'inspector' => 'lithium\analysis\Inspector',
);

/** /**
* Takes an instance of an object (usually a Collection object) containing test * Takes an instance of an object (usually a Collection object) containing test
* instances. Introspects the test subject classes to extract cyclomatic complexity data. * instances. Introspects the test subject classes to extract cyclomatic complexity data.
* *
* @param object $report Instance of Report which is calling apply. * @param object $report Instance of Report which is calling apply.
* @param array $tests The test to apply this filter on * @param array $tests The test to apply this filter on
* @param array $options Not used. * @param array $options Additional options to overwrite dependencies.
* - `'classes'` _array_: Overwrite default classes array.
* @return object Returns the instance of `$tests`. * @return object Returns the instance of `$tests`.
*/ */
public static function apply($report, $tests, array $options = array()) { public static function apply($report, $tests, array $options = array()) {
$results = array(); $results = array();
$options += array(
'classes' => array(),
);
$classes = $options['classes'] + static::$_classes;
$inspector = $classes['inspector'];
$parser = $classes['parser'];

foreach ($tests->invoke('subject') as $class) { foreach ($tests->invoke('subject') as $class) {
$results[$class] = array(); $results[$class] = array();


if (!$methods = Inspector::methods($class, 'ranges', array('public' => false))) { if (!$methods = $inspector::methods($class, 'ranges', array('public' => false))) {
continue; continue;
} }
foreach ($methods as $method => $lines) { foreach ($methods as $method => $lines) {
$lines = Inspector::lines($class, $lines); $lines = $inspector::lines($class, $lines);
$branches = Parser::tokenize(join("\n", (array) $lines), array( $branches = $parser::tokenize(join("\n", (array) $lines), array(
'include' => static::$_include 'include' => static::$_include
)); ));
$results[$class][$method] = count($branches) + 1; $results[$class][$method] = count($branches) + 1;
Expand Down
10 changes: 10 additions & 0 deletions tests/cases/test/MockerTest.php
Expand Up @@ -20,6 +20,10 @@ public function setUp() {
Mocker::register(); Mocker::register();
} }


public function tearDown() {
Mocker::applyFilter(false);
}

public function testAutoloadRegister() { public function testAutoloadRegister() {
Mocker::register(); Mocker::register();
$registered = spl_autoload_functions(); $registered = spl_autoload_functions();
Expand Down Expand Up @@ -431,6 +435,12 @@ public function testMagicCallGetStoredResultsWhenCalledIndirectly() {
$this->assertCount(2, $results['__call']); $this->assertCount(2, $results['__call']);
} }


public function testDoesNotThrowExceptionWhenMockingIterator() {
$this->assertNotException('Exception', function() {
return new \lithium\util\collection\Mock;
});
}

} }


?> ?>
24 changes: 24 additions & 0 deletions tests/cases/test/UnitTest.php
Expand Up @@ -1589,6 +1589,30 @@ public function testAssertStringStartsWithFalse() {
), $result['data']); ), $result['data']);
} }


public function testAssertNotExceptionPassesWithNoException() {
$this->assertTrue($this->test->assertNotException('Exception', function() {
return false;
}));
}

public function testAssertNotExceptionPassesWithWrongException() {
$this->assertTrue($this->test->assertNotException('FooException', function() {
throw new \Exception('Foo');
}));
}

public function testAssertNotExceptionFailsWithExactException() {
$this->assertFalse($this->test->assertNotException('lithium\action\DispatchException', function() {
throw new \lithium\action\DispatchException('Foo');
}));
}

public function testAssertNotExceptionFailsWithBaseException() {
$this->assertFalse($this->test->assertNotException('Exception', function() {
throw new \lithium\action\DispatchException('Foo');
}));
}

} }


?> ?>

0 comments on commit e789069

Please sign in to comment.