diff options
author | Dries Buytaert <dries@buytaert.net> | 2008-04-20 18:34:43 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2008-04-20 18:34:43 +0000 |
commit | ffc0e93c4eb0555a08f0b58bed0735416e6ba41f (patch) | |
tree | 7f741d0b40124633c5b337dc7210711bfcd4b876 /modules/simpletest/dumper.php | |
parent | af474609e3e80db9ba1d16b9ad2eae89775f51c8 (diff) | |
download | brdo-ffc0e93c4eb0555a08f0b58bed0735416e6ba41f.tar.gz brdo-ffc0e93c4eb0555a08f0b58bed0735416e6ba41f.tar.bz2 |
- Added a test framework to Drupal along with a first batch of tests for
Drupal core! This is an important milestone for the project so enable
the module and check it out ... :)
Thanks to Rok Žlender, Károly Négyesi, Jimmy Berry, Kevin Bridges, Charlie
Gordon, Douglas Hubler, Miglius Alaburda, Andy Kirkham, Dimitri13, Kieran
Lal, Moshe Weitzman, and the many other people that helped with testing
over the past years and that drove this home.
It all works but it is still rough around the edges (i.e. documentation
is still being written, the coding style is not 100% yet, a number of
tests still fail) but we spent the entire weekend working on it in Paris
and made a ton of progress. The best way to help and to get up to speed,
is to start writing and contributing some tests ... as well as fixing
some of the failures.
For those willing to help with improving the test framework, here are
some next steps and issues to resolve:
- How to best approach unit tests and mock functions?
- How to test drupal_mail() and drupal_http_request()?
- How to improve the admin UI so we have a nice progress bar?
- How best to do code coverage?
- See http://g.d.o/node/10099 for more ...
Diffstat (limited to 'modules/simpletest/dumper.php')
-rw-r--r-- | modules/simpletest/dumper.php | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/modules/simpletest/dumper.php b/modules/simpletest/dumper.php new file mode 100644 index 000000000..e2995cb21 --- /dev/null +++ b/modules/simpletest/dumper.php @@ -0,0 +1,80 @@ +<?php +// $Id$ + +/** + * Displays variables as text and does diffs. + */ +class SimpleDumper { + /** + * Renders a variable in a shorter form than print_r(). + * + * @param mixed $value Variable to render as a string. + * + * @return string Human readable string form. + * @access public + */ + function describeValue($value) { + $type = $this->getType($value); + switch ($type) { + case "Null": + return "NULL"; + + case "Bool": + return "Boolean: ". ($value ? "true" : "false"); + + case "Array": + return "Array: ". count($value) ." items"; + + case "Object": + return "Object: of ". get_class($value); + + case "String": + return "String: ". $this->clipString($value, 200); + + default: + return "$type: $value"; + } + } + + /** + * Gets the string representation of a type. + * @param mixed $value Variable to check against. + * @return string Type. + * @access public + */ + function getType($value) { + if (!isset($value)) { + return "Null"; + } + $functions = array('bool', 'string', 'integer', 'float', 'array', 'resource', 'object'); + foreach ($functions as $function) { + $function_name = 'is_'. $function; + if ($function_name($value)) { + return ucfirst($function); + } + } + return "Unknown"; + } + + /** + * Clips a string to a maximum length. + * @param string $value String to truncate. + * @param integer $size Minimum string size to show. + * @param integer $position Centre of string section. + * @return string Shortened version. + * @access public + */ + function clipString($value, $size, $position = 0) { + $length = strlen($value); + if ($length <= $size) { + return $value; + } + $position = min($position, $length); + $start = ($size / 2 > $position ? 0 : $position - $size / 2); + if ($start + $size > $length) { + $start = $length - $size; + } + $value = substr($value, $start, $size); + return ($start > 0 ? "..." : "") . $value . ($start + $size < $length ? "..." : ""); + } +} |