Permalink
Browse files

test suite refactor is "working" still needs more cleanup and organiz…

…ation
  • Loading branch information...
1 parent 7cdd71a commit 81d6725937b705437d3153cbe5f68a8788249f69 @gwoo gwoo committed Nov 19, 2009
View
@@ -1,147 +0,0 @@
-<?php
-/**
- * Lithium: the most rad php framework
- *
- * @copyright Copyright 2009, Union of RAD (http://union-of-rad.org)
- * @license http://opensource.org/licenses/bsd-license.php The BSD License
- */
-
-use \lithium\core\Libraries;
-use \lithium\test\Group;
-use \lithium\test\Dispatcher;
-use \lithium\util\Inflector;
-use \lithium\util\reflection\Inspector;
-
-$startBenchmark = microtime(true);
-
-error_reporting(E_ALL | E_STRICT | E_DEPRECATED);
-
-require dirname(__DIR__) . '/config/bootstrap.php';
-$core = dirname(dirname(__DIR__)) . '/libraries/lithium';
-
-$testRun = Dispatcher::run(null, $_GET);
-$stats = Dispatcher::process($testRun['results']);
-
-$show_included_files = isset($_GET['files']) ? $_GET['files']: false;
-$show_no_test_classes = isset($_GET['classes']) ? $_GET['classes'] : false;
-?>
-<!doctype html>
-<html>
- <head>
- <title>Lithium Unit Test Dashboard</title>
- <link rel="stylesheet" href="css/debug.css" />
- </head>
- <body class="test-dashboard">
- <h1>Lithium Unit Test Dashboard</h1>
-
- <div style="float: left; padding: 10px 0 20px 20px; width: 20%;">
- <h2><a href="?group=\">Tests</a></h2>
- <?php echo Dispatcher::menu('html'); ?>
- </div>
-
- <div style="float:left; padding: 10px; width: 75%">
- <h2>Stats for <?php echo $testRun['title']; ?></h2>
-
- <h3>Test results</h3>
-
- <span class="filters">
- <?php
- $filters = Libraries::locate('test.filters');
- $base = $_SERVER['REQUEST_URI'];
- if (!strpos($base,'?')){
- $base .= '?';
- }
- foreach ($filters as $i => $class) {
- $url = $base . "&amp;filters[]={$class}";
- $name = join('', array_slice(explode("\\", $class), -1));
- $key = Inflector::underscore($name);
-
- echo "<a class=\"{$key}\" href=\"{$url}\">{$name}</a>";
- echo ' | ';
- }
- echo "<a href=\"{$base}&amp;files=".($show_included_files?0:1)."\">Included files</a>";
- echo ' | ';
- echo "<a href=\"{$base}&amp;classes=".($show_no_test_classes?0:1)."\">No Test Classes</a>";
- ?>
- </span>
-
- <?php
- $passes = count($stats['passes']);
- $fails = count($stats['fails']);
- $errors = count($stats['errors']);
- $exceptions = count($stats['exceptions']);
- $success = ($passes === $stats['asserts'] && $errors === 0);
-
- echo '<div class="test-result test-result-' . ($success ? 'success' : 'fail') . '"';
- echo ">{$passes} / {$stats['asserts']} passes, {$fails} ";
- echo ((intval($stats['fails']) == 1) ? 'fail' : 'fails') . " and {$exceptions} ";
- echo ((intval($exceptions) == 1) ? 'exceptions' : 'exceptions');
- echo '</div>';
-
- foreach ((array)$stats['errors'] as $error) {
- switch ($error['result']) {
- case 'fail':
- $error += array('class' => 'unknown', 'method' => 'unknown');
- echo '<div class="test-assert test-assert-failed">';
- echo "Assertion '{$error['assertion']}' failed in ";
- echo "{$error['class']}::{$error['method']}() on line ";
- echo "{$error['line']}: ";
- echo "<span class=\"content\">{$error['message']}</span>";
- break;
- case 'exception':
- echo '<div class="test-exception">';
- echo "Exception thrown in {$error['class']}::{$error['method']}() ";
- echo "on line {$error['line']}: ";
- echo "<span class=\"content\">{$error['message']}</span>";
- if (isset($error['trace']) && !empty($error['trace'])) {
- echo "Trace:<span class=\"trace\">{$error['trace']}</span>";
- }
- break;
- }
- echo '</div>';
- }
-
- foreach ((array)$testRun['filters'] as $class => $data) {
- echo $class::output('html', $data);
- }
- $classes = array();
- $tests = Group::all(array('transform' => true));
- $options = array(
- 'recursive' => true,
- 'filter' => false,
- 'exclude' => '/\w+Test$|Mock+\w|webroot|index$|^app\\\\config|^\w+\\\\views\/|\./'
- );
- $none = Libraries::find('lithium', $options);
- $classes = array_diff($none, $tests);
- sort($classes);
-
- if ($show_no_test_classes) : ?>
- <h3>Classes with no test case (<?php echo count($classes); ?>)</h3>
- <ul class="classes">
- <?php
- foreach ($classes as $class) {
- echo "<li>{$class}</li>";
- }
- ?>
- </ul>
- <?php
- endif;
- if ($show_included_files) :
- ?>
- <h3>Included files (<?php echo count(get_included_files()); ?>)</h3>
- <ul class="files">
- <?php
- $base = dirname(dirname($core));
- $files = str_replace($base, '', get_included_files());
- sort($files);
-
- foreach ($files as $file) {
- echo "<li>{$file}</li>";
- }
- ?>
- </ul>
- <?php endif; ?>
- </div>
- <div style="clear:both"></div>
- </body>
-</html>
@@ -67,42 +67,22 @@ public function run() {
str_replace('lithium.tests.cases.', '', $this->group)
);
}
- $testRun = Dispatcher::run(null, array(
+ $report = Dispatcher::run(null, array(
'case' => $this->case, 'group' => $this->group,
'filters' => $this->filters
));
- $stats = Dispatcher::process($testRun['results']);
-
$this->header('Included Files');
$base = dirname(dirname(dirname(dirname(__DIR__))));
$files = str_replace($base, '', get_included_files());
sort($files);
$this->out($files);
- $passes = count($stats['passes']);
- $fails = count($stats['fails']);
- $exceptions = count($stats['exceptions']);
-
- $this->header($testRun['title']);
- $this->out("{$passes} / {$stats['asserts']} passes");
- $this->out(($stats['fails'] === 1) ? "{$fails} fail" : "{$fails} fails");
- $this->out("{$exceptions} exceptions");
+ $this->header($report->title);
- foreach ((array)$stats['fails'] as $fail) {
- $this->out("Assertion '{$fail['assertion']}' failed in");
- $this->out("{$fail['class']}::{$fail['method']}() on line {$fail['line']}");
- $this->out($fail['message']);
- }
-
- foreach ((array)$testRun['filters'] as $class => $data) {
- $this->out($class::output('text', $data));
- }
+ $this->out($report->stats());
- $this->header('Benchmarking');
- $this->out("Time: " . number_format(microtime(true) - $startBenchmark, 4) . 's');
- $this->out("Peak Memory: " . number_format((memory_get_peak_usage() / 1024), 3) . 'k');
- $this->out("Current Memory: " . number_format((memory_get_usage() / 1024), 3) . 'k');
+ $this->out($report->filters());
$again = $this->in("Would you like to run this test again?", array(
'choices' => array('y', 'n'),
@@ -145,7 +125,9 @@ public function missing() {
*/
protected function _getTests() {
while (empty($this->case) && empty($this->group)) {
- $tests = Libraries::find(true, array('filter' => '/\w+Test$/', 'recursive' => true));
+ $tests = Libraries::locate('tests', null, array(
+ 'filter' => '/cases|integration|functional/'
+ ));
$tests = str_replace('\\', '.',
str_replace('lithium\tests\cases\\', '', $tests)
);
@@ -363,6 +363,9 @@ public static function path($class, $options = array()) {
* @see lithium\core\Libraries::add()
*/
public static function locate($type, $name = null, $options = array()) {
+ $defaults = array('type' => 'class');
+ $options += $defaults;
+
if (is_object($name) || strpos($name, '\\') !== false) {
return $name;
}
@@ -449,12 +452,8 @@ protected static function _locateDeferred($defer, $paths, $params, $options = ar
}
$params['library'] = $library;
$class = str_replace('\\*', '', String::insert($pathTemplate, $params));
- $file = Libraries::path($class, $options);
- if (file_exists($file)) {
- if (isset($options['format']) && $options['format'] == 'file') {
- return $file;
- }
- return $class;
+ if (file_exists($file = Libraries::path($class, $options))) {
+ return ($options['type'] === 'file') ? $file : $class;
}
}
}
@@ -17,29 +17,26 @@
*
*/
class Controller extends \lithium\core\Object {
-
+
/**
* undocumented function
*
- * @param string $request
- * @param string $params
- * @param string $options
+ * @param string $request
+ * @param string $params
+ * @param string $options
* @return void
*/
public function __invoke($request, $params, $options = array()) {
error_reporting(E_ALL | E_STRICT | E_DEPRECATED);
- $report = Reporter::run(
- Dispatcher::run(null, $request->query),
- array('format' => 'html')
- );
+ $report = Dispatcher::run(null, $request->query + array('reporter' => 'html'));
$filters = Libraries::locate('test.filters');
$classes = Libraries::locate('tests', null, array(
'filter' => '/cases|integration|functional/'
));
- $menu = Reporter::menu($classes, array('format' => 'html', 'tree' => true));
-
+ $menu = $report->reporter->menu($classes, array('format' => 'html', 'tree' => true));
+
$template = Libraries::locate('test.reporter.templates', 'layout', array(
- 'filter' => false, 'format' => 'file', 'suffix' => '.html.php',
+ 'filter' => false, 'type' => 'file', 'suffix' => '.html.php',
));
include($template);
}
@@ -50,7 +50,7 @@ public static function run($group = null, $options = array()) {
'case' => null,
'group' => null,
'filters' => array(),
- 'reporter' => 'html'
+ 'reporter' => 'text'
);
$options += $defaults;
@@ -61,47 +61,6 @@ public static function run($group = null, $options = array()) {
}
/**
- * Processes the aggregated results from the test cases and compiles some
- * basic statistics.
- *
- * @param array $results An array of results as returned by Dispatcher::run().
- * @return array Array of results. Data includes aggregated values for
- * passes, fails, exceptions, errors, and assertions.
- */
- public static function process($results) {
- return array_reduce((array)$results, function($stats, $result) {
- $stats = (array)$stats + array(
- 'asserts' => 0,
- 'passes' => array(),
- 'fails' => array(),
- 'exceptions' => array(),
- 'errors' => array()
- );
- $result = empty($result[0]) ? array($result) : $result;
-
- foreach ($result as $response) {
- if (empty($response['result'])) {
- continue;
- }
- $result = $response['result'];
-
- if (in_array($result, array('fail', 'exception'))) {
- $stats['errors'][] = $response;
- }
- unset($response['file'], $response['result']);
-
- if (in_array($result, array('pass', 'fail'))) {
- $stats['asserts']++;
- }
- if (in_array($result, array('pass', 'fail', 'exception'))) {
- $stats[Inflector::pluralize($result)][] = $response;
- }
- }
- return $stats;
- });
- }
-
- /**
* Creates the test report class based on either the passed test case or the
* passed test group.
*
@@ -122,30 +81,6 @@ protected static function _report($options) {
return $report;
}
- /**
- * Runs the given tests through the applicable filters.
- *
- * @param object $group The test Group object, which contains the test cases.
- * @param array $filters The filters to be applied to the test cases.
- * @return array An array of two elements, the first being the results of the test
- * run, the second being the results of the filtered test run.
- */
- protected static function _execute($group, $filters) {
- $tests = $group->tests();
- $filterResults = array();
-
- foreach ($filters as $filter => $options) {
- $options = isset($options['apply']) ? $options['apply'] : array();
- $tests = $filter::apply($tests, $options);
- }
- $results = $tests->run();
-
- foreach ($filters as $filter => $options) {
- $options = isset($options['analyze']) ? $options['analyze'] : array();
- $filterResults[$filter] = $filter::analyze($results, $options);
- }
- return array($results, $filterResults);
- }
}
?>
Oops, something went wrong.

0 comments on commit 81d6725

Please sign in to comment.