Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 1038

Přidáno uživatelem Ondřej Fibich před více než 13 roky(ů)

Pokracovani prace na unit testech, implementace modelu.
Oprava chyby v memebers.

Zobrazit rozdíly:

freenetis/branches/unit_tester/application/vendors/unit_tester/generate_unit_config.pl
$model_code = join("", <$fh>);
# For each method
while ($model_code =~ m/function\s+(\w+)\s*\((.*)\)\s*{/g)
# regex:
# group(1): Function specification: private, static, etc.
# group(2): Function name
# group(3): Args of function
# m/function\s+(\w+)\s*\((.*)\)\s*{/g
while ($model_code =~ m/((?:[a-z0-9_*]+\s+)*)function\s+(\w+)\s*\((.*?)\)\s*{/g)
{
my $model_name = $1;
my $auto_generation = $xml_reader->{model}->{"acosca"}->{autogenerate};
#print ">>>>'$auto_generation'\n" if defined $auto_generation;
print ">>>>'$auto_generation'\n" if defined $auto_generation;
#my $auto_generation_on = defined $auto_generation and ($auto_generation == "on");
# Throw up private and protected methods
next if ($1 =~ /(private)|(protected)/);
# Throw up special methods
next if ($1 =~ /^__/);
next if ($2 =~ /^__/);
# Add method
$xml_writer->startTag("method", "name" => $model_name);
$xml_writer->startTag("method", "name" => $2);
# Get args
my @attributes = split(/,/, $2);
my @attributes = split(/,/, $3);
my $i = 0;
# Add group of attributes
freenetis/branches/unit_tester/application/models/member.php
* @return string containing concatenation of member_id and 5 digits of its crc16
*/
public function make_variable_symbol($member_id) {
return $member_id . sprintf("%5d", crc16($member_id));
return $member_id . sprintf("%5d", $this->crc16($member_id));
}
/**
* @author Tomas Dulik
freenetis/branches/unit_tester/application/controllers/unit_tester.php
<?php
/** File with XML config generated by generate_unit_config.pl at same path */
define("filename", APPPATH . "vendors/unit_tester/unit_testing_config.xml");
/**
* Exception handler for disabling Kohana handler
* @param Exception $exception
*/
function unit_tester_exception_handler($exception)
{
throw $exception;
}
/**
* Error handler for disabling Kohana handler
* @param Exception $exception
*/
function unit_tester_error_handler($errno, $errstr, $errfile, $errline)
{
throw new Exception($errstr, $errno);
}
/**
* Unit tester controller, test models, helpers and controllers.
* Invoke testers and displays results.
*
* @author Ondřej Fibich
* @version 1.0
*/
class Unit_tester_Controller extends Controller
{
/** Type for all test */
const ALL = 15;
/** Type for test of models */
const MODELS = 1;
/** Type for test of constrollers */
const CONTROLLERS = 2;
/** Type for test of helpers */
const HELPERS = 4;
/**
* Invokes test suites and displays results
*
* @param type $type One of ALL, MODELS, CONTROLLERS, HELPERS
* which can be combined using bit or
*/
public function index($type = self::ALL)
{
// overload Kohana error handlers
set_error_handler("unit_tester_error_handler");
set_exception_handler('unit_tester_exception_handler');
// errors
$results = array
(
"models" => array
(
"title" => "Models",
"errors" => null
),
"controllers" => array
(
"title" => "Controllers",
"errors" => null
),
"helpers" => array
(
"title" => "Helpers",
"errors" => null
),
);
/* testing */
// test models
if ($type & self::MODELS)
{
$results["models"]["errors"] = $this->models();
}
// test controllers
if ($type & self::CONTROLLERS)
{
}
// test helpers
if ($type & self::HELPERS)
{
}
/* Display results */
$view = new View("unit_tester");
$view->title = "Unit tester controller";
$view->results = $results;
$view->render(TRUE);
}
/**
* Model testing utility.
* Check parse errors and exception throws of models.
* @return array Errors with keys obj, type, error
*/
private function models()
{
$errors = array();
$f = false;
$xml_dom = new DOMDocument("1.0", "UTF-8");
// open file
if (($f = file_exists(filename)) === false)
{
echo "Cannot find file: `" . filename . "`\n";
echo "Run unit_testing_config.pl\n";
exit(1);
}
// read whole file
$source = file_get_contents(filename);
// parse file
if (!$xml_dom->loadXML($source))
{
echo "Cannot parse config file: `" . filename . "`\n";
exit(2);
}
$models = $xml_dom->getElementsByTagName("model");
// each model
foreach ($models as $model)
{
$model_name = $model->getAttribute('name');
$model_file = APPPATH . "models/" . $model_name . EXT;
/* File exist catcher */
if (!file_exists($model_file))
{
$errors[] = array
(
"obj" => $model_name,
"type" => "FILE ERROR on model",
"error" => "Cannot find model file: `" . $model_file . "`\n"
);
// next model
continue;
}
/* Syntax error catcher */
$output = null;
$return_var = null;
exec("php -l " . $model_file . " 2>&1", $output, $return_var);
if ($return_var != 0)
{
$errors[] = array
(
"obj" => $model_name,
"type" => "PARSE ERROR (" . $return_var . ") in model",
"error" => join("", $output)
);
// next model
continue;
}
/* Load model */
$class_name = strtoupper(substr($model_name, 0, 1)) . substr($model_name, 1) . "_Model";
if (!class_exists($class_name))
{
$errors[] = array
(
"obj" => $class_name,
"type" => "EXCEPTION ERROR in model",
"error" => "Model does not exists."
);
// next model
continue;
}
try
{
$obj = new $class_name;
}
catch (Exception $e)
{
$errors[] = array
(
"obj" => $class_name,
"type" => "EXCEPTION ERROR during loading model",
"error" => $e->__toString()
);
// next model
continue;
}
/* Check model methods */
$methods = $model->getElementsByTagName("method");
foreach ($methods as $method)
{
$method_name = $method->getAttribute("name");
$attributes = array();
/* Get attributes */
$attrs = $method->getElementsByTagName("attributes")
->item(0)
->getElementsByTagName("attribute");
foreach ($attrs as $attr)
{
$attr_name = $attr->getAttribute("name");
$attributes[$attr_name] = $attr->getAttribute("default_value");
if ($attributes[$attr_name] === "array()")
{
$attributes[$attr_name] = array();
}
}
/* Call for all inputs */
$inputs = $method->getElementsByTagName("values")
->item(0)
->getElementsByTagName("input");
foreach ($inputs as $input)
{
/* Get params */
$paramsarray = array_values($attributes);
$i = 0;
if ($input->hasChildNodes())
{
$params = $input->getElementsByTagName("param");
foreach ($params as $param)
{
$paramsarray[$i] = $param->getAttribute("value");
if ($paramsarray[$i] === "array()")
{
$paramsarray[$i] = array();
}
$i++;
}
}
/* Call method */
try
{
call_user_func_array(array($obj, $method_name), $paramsarray);
}
catch (Exception $e)
{
$errors[] = array
(
"obj" => $class_name . "#" . $method_name,
"type" => "EXCEPTION ERROR in model during call on line " . $e->getLine(),
"error" => $e->getMessage()
);
// next
continue;
}
}
}
}
return $errors;
}
}
freenetis/branches/unit_tester/application/views/unit_tester.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><?php echo $title ?></title>
<style type="text/css">
/* <![CDATA[ */
* {padding:0;margin:0;border:0;}
body {background:#eee;font-family:sans-serif;font-size:85%;}
h1,h2,h3,h4 {margin-bottom:0.5em;padding:0.2em 0;border-bottom:solid 1px #ccc;color:#911;}
h1 {font-size:2em;}
h2 {font-size:1.5em;}
h3 {font-size:1.2em;}
h3 {font-size:1.0em;}
p,pre {margin-bottom:0.5em;}
strong {color:#700;}
#wrap {width:90%;margin:2em auto;padding:0.5em 1em;background:#fff;border:solid 1px #ddd;border-bottom:solid 2px #aaa;}
#stats {margin:0;padding-top: 0.5em;border-top:solid 1px #ccc;font-size:0.8em;text-align:center;color:#555;}
.message {margin:1em;padding:0.5em;background:#dfdfdf;border:solid 1px #999;overflow: auto}
.error {font-size: 14px; color: red}
.green {font-size: 14px; color: green}
table {width: 100%; border: 1px solid #ccc;}
table th {background-color: #ccc; text-align: left;}
/* ]]> */
</style>
</head>
<body>
<div id="wrap">
<h1><?php echo $title ?></h1>
<table>
<tr>
<th>Models</th>
<th>Controllers</th>
<th>Helpers</th>
</tr>
<tr>
<td><?php echo (empty($results["models"]["errors"]) ?
"<p class=\"green\">OK" :
"<p class=\"error\">" . count($results["models"]["errors"]) . " Errors") ?></p></td>
<td><?php echo (empty($results["controllers"]["errors"]) ?
"<p class=\"green\">OK" :
"<p class=\"error\">" . count($results["controllers"]["errors"]) . " Errors") ?></p></td>
<td><?php echo (empty($results["helpers"]["errors"]) ?
"<p class=\"green\">OK" :
"<p class=\"error\">" . count($results["helpers"]["errors"]) . " dErrors") ?></p></td>
</tr>
</table>
<?php foreach ($results as $result): ?>
<?php if (!empty($result["errors"])): ?>
<h2><?php echo $result["title"] ?></h2>
<?php foreach ($result["errors"] as $error): ?>
<h3><?php echo $error["obj"] ?></h3>
<h4><?php echo $error["type"] ?></h4>
<pre class="message"><?php echo $error["error"] ?></pre>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
<p id="stats"><?php echo Kohana::lang('core.stats_footer') ?></p>
</div>
</body>
</html>

Také k dispozici: Unified diff