Permalink
Browse files

Merge pull request #880 from BlaineSch/feature/complexityFixes

Feature/complexity fixes
  • Loading branch information...
2 parents fab503b + 85a9953 commit e78906918fb8ffab45f77a5c6d292295dca5e902 @nateabele nateabele committed Apr 12, 2013
View
@@ -258,12 +258,22 @@ class Mocker {
' 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(
'{:modifiers} function __construct({:args}) {',
' $args = compact({:stringArgs});',
' array_push($args, $this);',
- ' foreach ($this as $key => $value) {',
- ' if (!in_array($key, $this->_safeVars)) {',
+ ' foreach (get_class_vars(get_class($this)) as $key => $value) {',
+ ' if (isset($this->{$key}) && !in_array($key, $this->_safeVars)) {',
' unset($this->$key);',
' }',
' }',
@@ -429,6 +439,8 @@ public static function create($mockee) {
'reference' => $getByReference ? '&' : '',
));
$mock .= self::_dynamicCode('mock', 'set');
+ $mock .= self::_dynamicCode('mock', 'isset');
+ $mock .= self::_dynamicCode('mock', 'unset');
$mock .= self::_dynamicCode('mock', 'applyFilter', array(
'static' => $staticApplyFilter ? 'static' : '',
));
@@ -595,10 +607,12 @@ public static function mergeResults($results, $secondary) {
* to remove all the current filters for the given method.
* @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) {
- static::$_methodFilters[$class] = array();
- return;
+ return static::$_methodFilters[$class] = array();
}
foreach ((array) $method as $m) {
if (!isset(static::$_methodFilters[$class][$m]) || $filter === false) {
View
@@ -625,6 +625,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.
*
* The value passed to `exepected` is an array of the cookie data, with at least the key and
View
@@ -8,9 +8,6 @@
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.
*/
@@ -20,30 +17,43 @@ class Complexity extends \lithium\test\Filter {
* The list of tokens which represent the starting point of a code branch.
*/
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'
);
+ protected static $_classes = array(
+ 'parser' => 'lithium\analysis\Parser',
+ 'inspector' => 'lithium\analysis\Inspector',
+ );
+
/**
* Takes an instance of an object (usually a Collection object) containing test
* instances. Introspects the test subject classes to extract cyclomatic complexity data.
*
* @param object $report Instance of Report which is calling apply.
* @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`.
*/
public static function apply($report, $tests, array $options = array()) {
$results = array();
+ $options += array(
+ 'classes' => array(),
+ );
+ $classes = $options['classes'] + static::$_classes;
+ $inspector = $classes['inspector'];
+ $parser = $classes['parser'];
+
foreach ($tests->invoke('subject') as $class) {
$results[$class] = array();
- if (!$methods = Inspector::methods($class, 'ranges', array('public' => false))) {
+ if (!$methods = $inspector::methods($class, 'ranges', array('public' => false))) {
continue;
}
foreach ($methods as $method => $lines) {
- $lines = Inspector::lines($class, $lines);
- $branches = Parser::tokenize(join("\n", (array) $lines), array(
+ $lines = $inspector::lines($class, $lines);
+ $branches = $parser::tokenize(join("\n", (array) $lines), array(
'include' => static::$_include
));
$results[$class][$method] = count($branches) + 1;
@@ -20,6 +20,10 @@ public function setUp() {
Mocker::register();
}
+ public function tearDown() {
+ Mocker::applyFilter(false);
+ }
+
public function testAutoloadRegister() {
Mocker::register();
$registered = spl_autoload_functions();
@@ -431,6 +435,12 @@ public function testMagicCallGetStoredResultsWhenCalledIndirectly() {
$this->assertCount(2, $results['__call']);
}
+ public function testDoesNotThrowExceptionWhenMockingIterator() {
+ $this->assertNotException('Exception', function() {
+ return new \lithium\util\collection\Mock;
+ });
+ }
+
}
?>
@@ -1589,6 +1589,30 @@ public function testAssertStringStartsWithFalse() {
), $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');
+ }));
+ }
+
}
?>
Oops, something went wrong.

0 comments on commit e789069

Please sign in to comment.