Skip to content

Commit

Permalink
Moved test stat reporting into template and completely removed Html R…
Browse files Browse the repository at this point in the history
…eporter from test suite [#6 state:resolved]
  • Loading branch information
indiefan authored and gwoo committed Mar 13, 2010
1 parent 1c956bf commit a00543d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
29 changes: 13 additions & 16 deletions libraries/lithium/test/Report.php
Expand Up @@ -27,13 +27,6 @@ class Report extends \lithium\core\Object {
*/
public $group = null;

/**
* Contains an instance of a test reporter, which contains the format to be displayed.
*
* @var object
*/
public $reporter = null;

/**
* Contains the format for the results to be rendered in
*
Expand Down Expand Up @@ -82,7 +75,6 @@ public function __construct(array $config = array()) {
'title' => null,
'group' => null,
'filters' => array(),
'reporter' => 'text',
'format' => 'txt'
);
parent::__construct((array) $config + $defaults);
Expand All @@ -94,12 +86,6 @@ public function __construct(array $config = array()) {
* @return void
*/
protected function _init() {
$class = Inflector::camelize($this->_config['reporter']);

if (!$reporter = Libraries::locate('test.reporter', $class)) {
throw new Exception("{$class} is not a valid reporter");
}
$this->reporter = new $reporter();
$this->group = $this->_config['group'];
$this->filters = $this->_config['filters'];
$this->title = $this->_config['title'] ?: $this->_config['title'];
Expand Down Expand Up @@ -163,7 +149,7 @@ public function collect($class, $results) {
*/
public function stats() {
$results = (array) $this->results['group'];
return $this->reporter->stats(array_reduce($results, function($stats, $result) {
$stats = array_reduce($results, function($stats, $result) {
$stats = (array) $stats + array(
'asserts' => 0,
'passes' => array(),
Expand All @@ -180,6 +166,9 @@ public function stats() {
$result = $response['result'];

if (in_array($result, array('fail', 'exception'))) {
$response = array_merge(
array('class' => 'unknown', 'method' => 'unknown'), $response
);
$stats['errors'][] = $response;
}
unset($response['file'], $response['result']);
Expand All @@ -192,7 +181,15 @@ public function stats() {
}
}
return $stats;
}));
});

$count = array_map(
function($value) { return is_array($value) ? count($value) : $value; },
$stats
);
$success = $count['passes'] === $count['asserts'] && $count['errors'] === 0;

return compact("stats", "count", "success");
}

/**
Expand Down
4 changes: 1 addition & 3 deletions libraries/lithium/test/templates/layout.html.php
Expand Up @@ -34,9 +34,7 @@ function($class) use ($request) {
$filters
)); ?>
</span>
<?php
echo $report->stats();
?>
<?= $report->render("stats", $report->stats()) ?>
<?php foreach ($report->results['filters'] as $filter => $data): ?>
<?= $report->render(
strtolower(array_pop(explode("\\", $filter))),
Expand Down
2 changes: 1 addition & 1 deletion libraries/lithium/test/templates/menu.html.php
Expand Up @@ -3,7 +3,7 @@
$prev = array();
$current = null;
?>
<ul><li><a href="<?= $base ?>/test/lithium/tests">lithium</a></li>
<ul><li><a href="<?= $base ?>/test/lithium/tests">Run All Tests</a></li>
<?php foreach ($menu as $test): ?>
<?php
$case = array_pop($path = explode("\\", $test));
Expand Down
33 changes: 33 additions & 0 deletions libraries/lithium/test/templates/stats.html.php
@@ -0,0 +1,33 @@
<div class="test-result test-result-<?= ($success ? 'success' : 'fail') ?>">
<?= $count['passes'] ?> / <?= $count['asserts'] ?> passes, <?= $count['fails'] ?>
<?= ((intval($count['fails']) == 1) ? ' fail' : ' fails') ?> and <?= $count['exceptions'] ?>
<?= ((intval($count['exceptions']) == 1) ? ' exception' : ' exceptions') ?>
</div>

<?php foreach ((array) $stats['errors'] as $error): ?>
<?php if ($error['result'] == 'fail'): ?>
<div class="test-assert test-assert-failed">
Assertion '<?= $error['assertion'] ?>' failed in
<?= $error['class'] ?>::<?= $error['method']?>() on line
<?= $error['line'] ?>:
<span class="content"><?= $error['message'] ?></span>
</div>
<?php elseif ($error['result'] == 'exception'): ?>
<div class="test-exception">
Exception thrown in <?= $error['class'] ?>::<?= $error['method'] ?>()
on line <?= $error['line'] ?>:
<span class="content"><?= $error['message'] ?></span>
<?php if (isset($error['trace']) && !empty($error['trace'])): ?>
Trace: <span class="trace"><?= $error['trace'] ?></span>
<?php endif ?>
</div>
<?php endif ?>
<?php endforeach ?>

<?php foreach ((array) $stats['skips'] as $skip): ?>
<div class="test-skip">
Skip <?= $skip['trace'][1]['class'] ?>::<?= $skip['trace'][1]['function'] ?>()
on line <?= $skip['trace'][1]['line'] ?>:
<span class="content"><?= $skip['message'] ?></span>
</div>
<?php endforeach ?>

0 comments on commit a00543d

Please sign in to comment.