Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactoring how filters are handled

  • Loading branch information...
commit 8ac8a367f57a8455f783a45bc955c37c95972a94 1 parent 3f9a944
@gwoo gwoo authored
View
17 libraries/lithium/console/command/Test.php
@@ -76,17 +76,10 @@ public function run() {
if ($this->_getTests() != true) {
return 0;
}
- $startBenchmark = microtime(true);
-
error_reporting(E_ALL | E_STRICT | E_DEPRECATED);
- if (!empty($this->case)) {
- $this->case = '\\' . str_replace('.', '\\', $this->case);
- } elseif (!empty($this->group)) {
- $this->group = '\\' . str_replace('.', '\\', $this->group);
- }
$run = $this->case ?: $this->group;
- $this->nl();
+ $run = '\\' . str_replace('.', '\\', $run);
$this->out(sprintf('Running `%s`... ', $run), false);
$report = Dispatcher::run($run, array(
@@ -95,12 +88,12 @@ public function run() {
'format' => 'txt'
));
$this->out('done.', 2);
-
- $this->hr();
+ $this->out('{:heading1}Results{:end}', 0);
$this->out($report->render('stats'));
- if ($filterResults = $report->filters("\n")) {
- $this->out($filterResults);
+ foreach ($report->filters() as $filter => $options) {
+ $data = $report->results['filters'][$filter];
+ $this->out($report->render($options['name'], compact('data')));
}
$this->hr();
View
76 libraries/lithium/test/Report.php
@@ -28,14 +28,6 @@ class Report extends \lithium\core\Object {
public $group = null;
/**
- * An array of fully-namespaced class names representing the filters to be applied to this test
- * group.
- *
- * @var array
- */
- public $filters = array();
-
- /**
* Title of the group being run
*
* @var string
@@ -57,6 +49,14 @@ class Report extends \lithium\core\Object {
public $timer = array('start' => null, 'end' => null);
/**
+ * An array key on fully-namespaced class names of the filter with options to be
+ * applied for the filter as the value
+ *
+ * @var array
+ */
+ protected $_filters = array();
+
+ /**
* Construct Report Object
*
* @param array $config Options array for the test run. Valid options are:
@@ -81,7 +81,6 @@ public function __construct(array $config = array()) {
*/
protected function _init() {
$this->group = $this->_config['group'];
- $this->filters = $this->_config['filters'];
$this->title = $this->_config['title'] ?: $this->_config['title'];
}
@@ -92,34 +91,15 @@ protected function _init() {
*/
public function run() {
$tests = $this->group->tests();
- $filters = array();
- foreach ($this->filters as $filter => $options) {
- if (!strpos($filter, "\\")) {
- $filter = ucwords($filter);
- }
- if (!$class = Libraries::locate('test.filter', $filter)) {
- throw new Exception("{$class} is not a valid test filter.");
- }
- $options = isset($options['apply']) ? $options['apply'] : array();
- $tests = $class::apply($this, $tests, $options) ?: $tests;
- $filters[] = compact('class', 'options');
+ foreach ($this->filters() as $filter => $options) {
+ $this->results['filters'][$filter] = array();
+ $tests = $filter::apply($this, $tests, $options['apply']) ?: $tests;
}
$this->results['group'] = $tests->run();
- foreach ($filters as $filter) {
- if (isset($filter['options']['analyze'])) {
- $filter['options'] = $filter['options']['analyze'];
- } else {
- $filter['options'] = array();
- }
- if (!isset($this->results['filters'][$filter['class']])) {
- $this->results['filters'][$filter['class']] = array();
- }
- $this->results['filters'][$filter['class']] = $filter['class']::analyze(
- $this,
- $filter['options']
- );
+ foreach ($this->filters() as $filter => $options) {
+ $this->results['filters'][$filter] = $filter::analyze($this, $options['analyze']);
}
}
@@ -132,9 +112,6 @@ public function run() {
* @return void
*/
public function collect($class, $results) {
- if (!isset($this->results['filters'][$class])) {
- $this->results['filters'][$class] = array();
- }
$this->results['filters'][$class][] = $results;
}
@@ -220,21 +197,20 @@ public function render($template, $data = array()) {
});
}
- /**
- * Loops through and renders each filter that currently has stored results
- *
- * @param string $separator Optional separator string to join the templates on
- * @return string|boolean
- */
- public function filters($separator = null) {
- $output = false;
- foreach ($this->results['filters'] as $filter => $analysis) {
- $filterClass = explode("\\", $filter);
- $filterClass = array_pop($filterClass);
- $output .= $separator . $this->render(strtolower($filterClass), compact('analysis'));
+ public function filters(array $filters = array()) {
+ if (!empty($this->_filters) && empty($filters)) {
+ return $this->_filters;
}
-
- return $output;
+ $filters += (array) $this->_config['filters'];
+ $results = array();
+ foreach ($filters as $filter => $options) {
+ if (!$class = Libraries::locate('test.filter', $filter)) {
+ throw new Exception("{$class} is not a valid test filter.");
+ }
+ $options['name'] = strtolower(join('', array_slice(explode("\\", $class), -1)));
+ $results[$class] = $options + array('apply' => array(), 'analyze' => array());
+ }
+ return $this->_filters = $results;
}
}
View
5 libraries/lithium/test/templates/console/affected.txt.php
@@ -1,7 +1,6 @@
+{:heading2}Affected Tests{:end}
<?php
- echo "===Additional Affected Tests===\n";
-
- foreach ($analysis as $class => $test) {
+ foreach ($data as $class => $test) {
if ($test) {
echo "{$test}\n";
}
View
14 libraries/lithium/test/templates/console/complexity.txt.php
@@ -1,18 +1,16 @@
+{:heading2}Cyclomatic Complexity{:end}
<?php
-echo "===Cyclomatic Complexity===\n";
-
-foreach (array_slice($analysis['max'], 0, 10) as $method => $count) {
+foreach (array_slice($data['max'], 0, 10) as $method => $count) {
if ($count <= 7) {
continue;
}
echo "Worst Offender\n\t{$method} - {$count}\n";
}
-echo "Class Averages\n";
-foreach (array_slice($analysis['class'], 0, 10) as $class => $count) {
+?>
+{:heading3}Class Averages{:end}
+<?php
+foreach (array_slice($data['class'], 0, 10) as $class => $count) {
echo "\t{$class} - ";
echo round($count, 2) . "\n";
}
-
-echo "\n";
-
?>
View
5 libraries/lithium/test/templates/console/coverage.txt.php
@@ -1,7 +1,6 @@
+{:heading2}Code Coverage{:end}
<?php
- echo "===Code Coverage===\n";
-
- foreach ($analysis as $class => $coverage) {
+ foreach ($data as $class => $coverage) {
echo ($coverage['percentage'] >= 85 ? "{:success}" : "{:error}");
echo "{$class}{:end}: ";
echo count($coverage['covered']) . " of " . count($coverage['executable']);
View
7 libraries/lithium/test/templates/console/profiler.txt.php
@@ -1,7 +1,6 @@
+{:heading2}Benchmarks{:end}
<?php
- echo "===Benchmarks===\n";
-
- foreach ($analysis['totals'] as $title => $data) {
- echo "{$title}: {$data['formatter']($data['value'])}\n";
+ foreach ($data['totals'] as $title => $result) {
+ echo "{$title}: {$result['formatter']($result['value'])}\n";
}
?>
View
1  libraries/lithium/test/templates/console/stats.txt.php
@@ -9,6 +9,7 @@
echo "{$fails} " . ($fails == 1 ? 'fail' : 'fails');
echo " and {$exceptions} ";
echo ($exceptions == 1 ? 'exception' : 'exceptions') . ($success ? '{:end}' : '') . "\n";
+
foreach ((array) $stats['errors'] as $error) {
if ($error['result'] == 'fail') {
echo "\n{:error}Assertion '{$error['assertion']}' failed in ";
View
4 libraries/lithium/test/templates/html/affected.html.php
@@ -1,7 +1,7 @@
-<h3>Additional Affected Tests</h3>
+<h3>Affected Tests</h3>
<ul class="metrics">
-<?php foreach ($analysis as $class => $test): ?>
+<?php foreach ($data as $class => $test): ?>
<?php if ($test): ?>
<li><?php echo $test ?></li>
<?php endif ?>
View
4 libraries/lithium/test/templates/html/complexity.html.php
@@ -1,6 +1,6 @@
<h3>Cyclomatic Complexity</h3>
<table class="metrics"><tbody>
-<?php foreach (array_slice($analysis['max'], 0, 10) as $method => $count): ?>
+<?php foreach (array_slice($data['max'], 0, 10) as $method => $count): ?>
<?php
if ($count <= 7) {
continue;
@@ -14,7 +14,7 @@
<tr>
<th colspan="2">Class Averages</th>
</tr>
-<?php foreach (array_slice($analysis['class'], 0, 10) as $class => $count): ?>
+<?php foreach (array_slice($data['class'], 0, 10) as $class => $count): ?>
<tr>
<td class="metric-name"><?php echo $class ?></th>
<td class="metric"><?php echo round($count, 2) ?></td>
View
2  libraries/lithium/test/templates/html/coverage.html.php
@@ -1,4 +1,4 @@
-<?php foreach ($analysis as $class => $coverage): ?>
+<?php foreach ($data as $class => $coverage): ?>
<h4 class="coverage">
<?php echo $class ?>:
<?php echo count($coverage['covered']) ?> of <?php echo count($coverage['executable']) ?>
View
5 libraries/lithium/test/templates/html/layout.html.php
@@ -56,7 +56,10 @@ function($class) use ($request) {
<?php
echo $report->render("stats");
- echo $report->filters();
+ foreach ($report->filters() as $filter => $options) {
+ $data = $report->results['filters'][$filter];
+ echo $report->render($options['name'], compact('data'));
+ }
?>
</div>
</article>
View
4 libraries/lithium/test/templates/html/profiler.html.php
@@ -1,10 +1,10 @@
<h3>Benchmarks</h3>
<table class="metrics"><tbody>
-<?php foreach ($analysis['totals'] as $title => $data): ?>
+<?php foreach ($data['totals'] as $title => $result): ?>
<tr>
<td class="metric-name"><?php echo $title ?></td>
- <td class="metric"><?php echo $data['formatter']($data['value']) ?></td>
+ <td class="metric"><?php echo $result['formatter']($result['value']) ?></td>
</tr>
<?php endforeach ?>
</tbody></table>
View
56 libraries/lithium/tests/cases/test/ReportTest.php
@@ -10,7 +10,6 @@
use \lithium\test\Report;
use \lithium\test\Group;
-//use \lithium\test\filter\Complexity;
class ReportTest extends \lithium\test\Unit {
@@ -34,6 +33,24 @@ public function testInit() {
$this->assertEqual($expected, $result);
}
+ public function testFilters() {
+ $report = new Report(array(
+ 'title' => '\lithium\tests\mocks\test\MockFilterClassTest',
+ 'group' => new Group(
+ array('items' => array('\lithium\tests\mocks\test\MockFilterClassTest'))
+ ),
+ 'filters' => array("Complexity" => ""),
+ 'format' => 'html',
+ 'reporter' => 'html'
+ ));
+
+ $expected = array('lithium\test\filter\Complexity' => array(
+ 'name' => 'complexity', 'apply' => array(), 'analyze' => array()
+ ));
+ $result = $report->filters();
+ $this->assertEqual($expected, $result);
+ }
+
public function testStats() {
$report = new Report(array(
'title' => '\lithium\tests\mocks\test\MockUnitTest',
@@ -44,24 +61,10 @@ public function testStats() {
$expected = 1;
$result = $report->stats();
$this->assertEqual($expected, $result['count']['asserts']);
-
$this->assertEqual($expected, $result['count']['passes']);
-
$this->assertTrue($result['success']);
}
- public function testSingleFilter() {
- $report = new Report(array(
- 'title' => '\lithium\tests\mocks\test\MockFilterClassTest',
- 'group' => new Group(array('items' => array('\lithium\tests\mocks\test\MockFilterClassTest'))),
- 'filters' => array("Complexity" => "")
- ));
- $report->run();
-
- $class = 'lithium\test\filter\Complexity';
- $this->assertNotEqual(null, $report->results['filters'][$class]);
- }
-
public function testRender() {
$report = new Report(array(
'title' => '\lithium\tests\mocks\test\MockUnitTest',
@@ -71,26 +74,23 @@ public function testRender() {
));
$report->run();
- $output = $report->render("stats");
-
- $this->assertPattern("/1 \/ 1 passes, 0 fails and 0 exceptions/", $output);
+ $result = $report->render("stats");
+ $this->assertPattern("/1 \/ 1 passes, 0 fails and 0 exceptions/", $result);
}
- public function testFilters() {
+ public function testSingleFilter() {
$report = new Report(array(
'title' => '\lithium\tests\mocks\test\MockFilterClassTest',
- 'group' => new Group(
- array('items' => array('\lithium\tests\mocks\test\MockFilterClassTest'))
- ),
- 'filters' => array("Complexity" => ""),
- 'format' => 'html',
- 'reporter' => 'html'
+ 'group' => new Group(array(
+ 'items' => array('\lithium\tests\mocks\test\MockFilterClassTest')
+ )),
+ 'filters' => array("Complexity" => "")
));
$report->run();
- $output = $report->filters();
-
- $this->assertPattern("/<h3>Cyclomatic Complexity<\/h3>/", $output);
+ $class = 'lithium\test\filter\Complexity';
+ $result = $report->results['filters'][$class];
+ $this->assertTrue(isset($report->results['filters'][$class]));
}
}
View
25 libraries/lithium/tests/integration/test/FilterTest.php
@@ -24,18 +24,17 @@ public function setUp() {
}
public function testSingleTest() {
- $this->report->filters = array("Coverage" => "");
+ $this->report->filters(array("Coverage" => ""));
$this->report->run();
$expected = 40;
- $result = $this->report->results['filters'];
- $percentage = $result['lithium\test\filter\Coverage'];
- $percentage = $percentage['lithium\tests\mocks\test\MockFilterClass'];
- $percentage = $percentage['percentage'];
+ $filter = $this->report->results['filters']['lithium\test\filter\Coverage'];
+ $data = $filter['lithium\tests\mocks\test\MockFilterClass'];
+ $result = $data['percentage'];
- $this->assertEqual($expected, $percentage);
+ $this->assertEqual($expected, $result);
}
public function testSingleTestWithMultipleFilters() {
@@ -61,7 +60,7 @@ public function testSingleTestWithMultipleFilters() {
)
));
- $report->filters = $filters;
+ $report->filters($filters);
$report->run();
@@ -69,8 +68,10 @@ public function testSingleTestWithMultipleFilters() {
$expected = 40;
$result = $report->results['filters'];
+
$this->assertTrue(isset($result['lithium\test\filter\Coverage']),
- "Filter(s): '" .join(array_keys($filters), ", ") . "' returned no Coverage results."
+ "Filter(s): '" . join(array_keys($filters), ", ") . "'"
+ . "returned no Coverage results."
);
$percentage = $result['lithium\test\filter\Coverage'];
$percentage = $percentage['lithium\tests\mocks\test\MockFilterClass'];
@@ -87,7 +88,6 @@ public function testSingleTestWithMultipleFilters() {
*/
private function power_perms($arr) {
-
$power_set = $this->power_set($arr);
$result = array();
foreach($power_set as $set) {
@@ -98,12 +98,11 @@ private function power_perms($arr) {
}
private function power_set($in,$minLength = 1) {
-
$count = count($in);
$members = pow(2,$count);
$return = array();
for ($i = 0; $i < $members; $i++) {
- $b = sprintf("%0".$count."b",$i);
+ $b = sprintf("%0" . $count . "b", $i);
$out = array();
for ($j = 0; $j < $count; $j++) {
if ($b{$j} == '1') $out[] = $in[$j];
@@ -122,7 +121,7 @@ private function factorial($int){
return 1;
}
- for($f = 2; $int-1 > 1; $f *= $int--);
+ for($f = 2; $int - 1 > 1; $f *= $int--){}
return $f;
}
@@ -150,7 +149,7 @@ private function perm($arr, $nth = null) {
private function perms($arr) {
$p = array();
- for ($i=0; $i < $this->factorial(count($arr)); $i++) {
+ for ($i = 0; $i < $this->factorial(count($arr)); $i++) {
$p[] = $this->perm($arr, $i);
}
return $p;
Please sign in to comment.
Something went wrong with that request. Please try again.