Permalink
Browse files

initial work on test controller and reporters

  • Loading branch information...
1 parent 21d2cb6 commit fe822aa559f9f513b25a5cb8047ace3831a7ce7f @gwoo gwoo committed Nov 18, 2009
View
6 app/config/routes.php
@@ -27,6 +27,12 @@
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'view'));
/**
+ * Connect the testing routes.
+ */
+Router::connect('/test/{:args}', array('controller' => '\lithium\test\Controller'));
+Router::connect('/test', array('controller' => '\lithium\test\Controller'));
+
+/**
* Finally, connect the default routes.
*/
Router::connect('/{:controller}/{:action}/{:id:[0-9]+}.{:type}', array('id' => null));
View
2 app/webroot/test.php
@@ -46,7 +46,7 @@
<span class="filters">
<?php
- $filters = Libraries::locate('testFilters');
+ $filters = Libraries::locate('test.filters');
$base = $_SERVER['REQUEST_URI'];
if (!strpos($base,'?')){
$base .= '?';
View
2 libraries/lithium/action/Dispatcher.php
@@ -128,7 +128,7 @@ protected static function _callable($request, $params, $options) {
if (class_exists($class)) {
return new $class(compact('request'));
}
- throw new Exception("Controller {$class} not found");
+ throw new Exception("Controller {$controller} not found");
});
}
View
6 libraries/lithium/core/Libraries.php
@@ -86,9 +86,9 @@ class Libraries {
'{:library}\extensions\sockets\{:name}',
'{:library}\{:class}\socket\{:name}' => array('libraries' => 'lithium')
),
- 'testFilters' => array(
- '{:library}\tests\filters\{:name}',
- '{:library}\test\filters\{:name}' => array('libraries' => 'lithium')
+ 'test' => array(
+ '{:library}\extensions\test\{:namespace}\{:class}\{:name}',
+ '{:library}\test\{:namespace}\{:class}\{:name}' => array('libraries' => 'lithium')
),
'tests' => array(
'{:library}\tests\{:namespace}\{:class}\{:name}Test'
View
25 libraries/lithium/test/Controller.php
@@ -0,0 +1,25 @@
+<?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
+ */
+
+namespace lithium\test;
+
+use \lithium\test\Reporter;
+use \lithium\test\Dispatcher;
+
+class Controller extends \lithium\core\Object {
+
+ public function __invoke($request, $params, $options = array()) {
+ error_reporting(E_ALL | E_STRICT | E_DEPRECATED);
+ return Reporter::run(
+ Dispatcher::process(Dispatcher::run(null, $params['args'])),
+ array('format' => 'html')
+ );
+ }
+}
+
+?>
View
36 libraries/lithium/test/Report.php
@@ -0,0 +1,36 @@
+<?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
+ */
+
+namespace lithium\test;
+
+class Report extends \lithium\core\Object {
+
+ /**
+ * Contains an instance of `lithium\test\Group`, which contains all unit tests to be executed
+ * this test run.
+ *
+ * @var 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();
+
+ protected $_startTime = null;
+
+ protected function _init() {
+ $this->_startTime = microtime(true);
+ }
+}
+
+?>
View
96 libraries/lithium/test/Reporter.php
@@ -8,8 +8,104 @@
namespace lithium\test;
+use \lithium\core\Libraries;
+use \lithium\util\Inflector;
+
+/**
+ * Reporter class to handle test report output
+ *
+ * @param class \lithium\test\Report
+ */
class Reporter extends \lithium\core\Object {
+ /**
+ * undocumented function
+ *
+ * @param string $report
+ * @param string $options
+ * @return void
+ */
+ public static function run($report, $options = array()) {
+ $defaults = array('format' => 'text');
+ $options += $defaults;
+ $reporter = Libraries::locate('test.reporter', Inflector::camelize($options['format']));
+ $reporter = new $reporter();
+ $classes = Libraries::locate('tests', null, array(
+ 'filter' => '/cases|integration|functional/'
+ ));
+ $menu = static::menu($classes);
+ return $reporter->render(compact('report', 'menu', 'classes'));
+ }
+
+ /**
+ * return menu as a string to be used as render
+ *
+ * @params array options
+ * - format: type of reporter class. eg: html default: text
+ * - recursive: if true organizes and renders as hierarchy
+ */
+ public static function menu($classes, $options = array()) {
+ $defaults = array('format' => 'text', 'recursive' => false);
+ $options += $defaults;
+ if ($options['recursive']) {
+ $data = array();
+ $assign = function(&$data, $class, $i = 0) use (&$assign) {
+ isset($data[$class[$i]]) ?: $data[] = $class[$i];
+ $end = (count($class) <= $i + 1);
+
+ if (!$end && ($offset = array_search($class[$i], $data)) !== false) {
+ $data[$class[$i]] = array();
+ unset($data[$offset]);
+ }
+ ksort($data);
+ $end ?: $assign($data[$class[$i]], $class, $i + 1);
+ };
+
+ foreach ($classes as $class) {
+ $assign($data, explode('\\', str_replace('\tests', '', $class)));
+ }
+ $classes = $data;
+ }
+ ksort($classes);
+
+ $result = null;
+ $reporter = Libraries::locate('test.reporter', Inflector::camelize($options['format']));
+ $reporter = new $reporter();
+
+ if ($options['recursive']) {
+ $menu = function ($data, $parent = null) use (&$menu, &$reporter, $result) {
+ foreach ($data as $key => $row) {
+ if (is_array($row) && is_string($key)) {
+ $key = strtolower($key);
+ $next = $parent . '\\' . $key;
+ $result .= $reporter->format('group', array(
+ 'namespace' => $next, 'name' => $key, 'menu' => $menu($row, $next)
+ ));
+ } else {
+ $next = $parent . '\\' . $key;
+ $result .= $reporter->format('case', array(
+ 'namespace' => $parent, 'name' => $row,
+ ));
+ }
+ }
+ return $format($result);
+ };
+ foreach ($classes as $library => $tests) {
+ $group = "\\{$library}\\tests";
+ $result .= $report->format(null, array('menu' => $report->format('group', array(
+ 'namespace' => $group, 'name' => $library, 'menu' => $menu($tests, $group)
+ ))));
+ }
+ return $result;
+ }
+ foreach ($classes as $test) {
+ $parts = explode('\\', $test);
+ $name = array_pop($parts);
+ $namespace = join('\\', $parts);
+ $result .= $reporter->format('case', compact('namespace', 'name'));
+ }
+ return $reporter->format(null, array('menu' => $result));
+ }
}
?>
View
38 libraries/lithium/test/reporter/Html.php
@@ -0,0 +1,38 @@
+<?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
+ */
+
+namespace lithium\test\reporter;
+
+use lithium\util\String;
+
+class Html extends \lithium\core\Object {
+
+ public function render($results) {
+ $filters = Libraries::locate('test.filters');
+ }
+
+ public function format($type, $params = array()) {
+ $defaults = array(
+ 'namespace' => null, 'name' => null, 'menu' => null
+ );
+ $params += $defaults;
+ if ($type == 'group') {
+ return '<li><a href="?'. String::insert(
+ 'group={:namespace}">{:name}</a>{:menu}</li>', $params
+ );
+ }
+ if ($type == 'case') {
+ return '<li><a href="?'. String::insert(
+ 'case={:namespace}\{:name}">{:name}</a></li>', $params
+ );
+ }
+ return String::insert('<ul>{:menu}</ul>', $params);
+ }
+}
+
+?>
View
36 libraries/lithium/test/reporter/Text.php
@@ -0,0 +1,36 @@
+<?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
+ */
+
+namespace lithium\test\reporter;
+
+use lithium\util\String;
+
+class Text extends \lithium\core\Object {
+
+ public function render($results) {
+ $filters = Libraries::locate('test.filters');
+ }
+
+ public function format($type, $params = array()) {
+ $defaults = array(
+ 'namespace' => null, 'name' => null, 'menu' => null
+ );
+ $params += $defaults;
+ if ($type == 'group') {
+ return String::insert(
+ "-group {:namespace}\n{:menu}\n", $params
+ );
+ }
+ if ($type == 'case') {
+ return String::insert("-case {:namespace}\{:name}\n", $params);
+ }
+ return String::insert("\n{:menu}\n", $params);
+ }
+}
+
+?>
View
40 libraries/lithium/test/reporter/templates/layout.html.php
@@ -0,0 +1,40 @@
+<!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 $menu ?>
+ </div>
+
+ <div style="float:left; padding: 10px; width: 75%">
+ <h2>Stats for <?php echo $title; ?></h2>
+
+ <h3>Test results</h3>
+
+ <span class="filters">
+ <?php
+ $base = $_SERVER['REQUEST_URI'];
+
+ 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>";
+
+ if ($i < count($filters) - 1) {
+ echo ' | ';
+ }
+ }
+ ?>
+ </span>
+ </div>
+ <div style="clear:both"></div>
+ </body>
+</html>
View
6 libraries/lithium/tests/cases/test/ReporterTest.php
@@ -12,8 +12,10 @@
class ReporterTest extends \lithium\test\Unit {
- public function testRender() {
-
+ public function testMenu() {
+ $expected = "\n-case lithium\core\Object\n-case lithium\core\Libraries\n\n";
+ $result = Reporter::menu(array('lithium\core\Object', 'lithium\core\Libraries'));
+ $this->assertEqual($expected, $result);
}
}
View
42 libraries/lithium/tests/cases/test/reporter/HtmlTest.php
@@ -0,0 +1,42 @@
+<?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
+ */
+
+namespace lithium\tests\cases\test\reporter;
+
+use \lithium\test\reporter\Html;
+
+class HtmlTest extends \lithium\test\Unit {
+
+ public function setUp() {
+ $this->html = new Html();
+ }
+
+ public function testFormatWithoutData() {
+ $expected = '<ul></ul>';
+ $result = $this->html->format(null);
+ $this->assertEqual($expected, $result);
+ }
+
+ public function testFormatGroup() {
+ $expected = '<li><a href="?group=lithium">Lithium</a></li>';
+ $result = Html::format('group', array(
+ 'namespace' => 'lithium', 'name' => 'Lithium', 'menu' => null
+ ));
+ $this->assertEqual($expected, $result);
+ }
+
+ public function testFormatCase() {
+ $expected = '<li><a href="?case=lithium\tests\cases\test\reporter\HtmlTest">HtmlTest</a></li>';
+ $result = $this->html->format('case', array(
+ 'namespace' => 'lithium\tests\cases\test\reporter', 'name' => 'HtmlTest', 'menu' => null
+ ));
+ $this->assertEqual($expected, $result);
+ }
+}
+
+?>
View
42 libraries/lithium/tests/cases/test/reporter/TextTest.php
@@ -0,0 +1,42 @@
+<?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
+ */
+
+namespace lithium\tests\cases\test\reporter;
+
+use \lithium\test\reporter\Text;
+
+class TextTest extends \lithium\test\Unit {
+
+ public function setUp() {
+ $this->html = new Text();
+ }
+
+ public function testFormatWithoutData() {
+ $expected = "\n\n";
+ $result = $this->html->format(null);
+ $this->assertEqual($expected, $result);
+ }
+
+ public function testFormatGroup() {
+ $expected = "-group lithium\n\n";
+ $result = $this->html->format('group', array(
+ 'namespace' => 'lithium', 'name' => 'Lithium', 'menu' => null
+ ));
+ $this->assertEqual($expected, $result);
+ }
+
+ public function testFormatCase() {
+ $expected = "-case lithium\\tests\cases\\test\\reporter\\TextTest\n";
+ $result = $this->html->format('case', array(
+ 'namespace' => 'lithium\tests\cases\test\reporter', 'name' => 'TextTest', 'menu' => null
+ ));
+ $this->assertEqual($expected, $result);
+ }
+}
+
+?>

0 comments on commit fe822aa

Please sign in to comment.