5 * @author Geoffrey Sneddon <geoffers@gmail.com>
6 * @version $Id: unit_test2.php 16 2007-08-08 14:52:36Z gsnedders $
7 * @license http://www.opensource.org/licenses/zlib-license.php zlib/libpng license
8 * @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
9 * @copyright Copyright © 2007, Geoffrey Sneddon
21 * Sets whether this class is a unit test or not
61 * Number of tests passed
69 * Number of tests failed
77 * Set the test name to the class name by default, replacing "_" with " "
81 $this->name = str_replace('_', ' ', get_class($this));
85 * Whether this class is a test
93 return (bool) $this->test;
109 * Number of tests passed
117 return (int) $this->passes;
121 * Number of tests failed
129 return (int) $this->fails;
133 * Total number of tests
141 return $this->passes() + $this->fails();
160 * First method called when running the test
162 * This isn't defined as abstract as it's optional
171 * Set Unit_Test2::$data
175 * @see Unit_Test2::$data
182 * Set Unit_Test2::$expected
186 * @see Unit_Test2::$expected
193 * Actually run the test (should set Unit_Test::$result)
197 * @see Unit_Test2::$result
204 * Check whether the result is valid (should call Unit_Test2::pass() or Unit_Test2::fail())
208 * @see Unit_Test2::$expected
209 * @see Unit_Test2::$result
241 class Unit_Test2_Group
244 * Unit Test Group Name
257 var $tests = array(array());
260 * Number of tests passed
268 * Number of tests failed
276 * Time taken to run tests
284 * Create Unit Test Group
287 * @param string $name Unit Test Group Name
289 function Unit_Test2_Group($name)
295 * Unit Test Group Name
307 * Number of tests passed
315 return (int) $this->passes;
319 * Number of tests failed
327 return (int) $this->fails;
331 * Total number of tests
339 return $this->passes() + $this->fails();
351 return (float) $this->time;
355 * Add a test (a Unit_Test2 child, or a Unit_Test2_Group)
358 * @param object $test Test to add
362 $this->tests[$test->name()][] = $test;
369 * @param string $name Test name
371 function remove($name)
373 unset($this->tests[$name]);
377 * Load tests in folder
379 * This loads all the Unit_Test2 classes within files with the same
380 * extension as this file within the specified folder
383 * @param string $folder Folder name
385 function load_folder($folder)
387 static $extension = null;
390 $extension = pathinfo(__FILE__, PATHINFO_EXTENSION);
392 $files = Unit_Test2_Files::get_files($folder);
393 $count_classes = count(get_declared_classes());
394 foreach ($files as $file)
396 if (is_file($file) && pathinfo($file, PATHINFO_EXTENSION) === $extension)
401 $classes = array_slice(get_declared_classes(), $count_classes);
402 foreach ($classes as $class)
404 if ($this->is_subclass_of($class, 'Unit_Test2'))
407 if ($class->is_test())
423 $start_time = $this->microtime(true);
424 foreach ($this->tests as $tests)
426 foreach ($tests as $test)
428 if ($this->is_a($test, 'Unit_Test2') || $this->is_a($test, 'Unit_Test2_Group'))
431 $this->passes += $test->passes();
432 $this->fails += $test->fails();
436 $this->time = $this->microtime(true) - $start_time;
441 * Executed before the tests are executed
451 * Executed after the tests are executed
461 * Re-implementation of PHP 5.0.3's is_subclass_of()
464 * @param mixed $object
465 * @param string $class_name
467 function is_subclass_of($object, $class_name)
469 if (func_num_args() != 2)
471 trigger_error('Wrong parameter count for SimplePie_Misc::is_subclass_of()', E_USER_WARNING);
475 if (version_compare(phpversion(), '5.0.3', '>=') || is_object($object))
477 return is_subclass_of($object, $class_name);
479 else if (is_string($object) && is_string($class_name))
481 if (class_exists($object))
483 if (class_exists($class_name))
485 $class_name = strtolower($class_name);
486 while ($object = strtolower(get_parent_class($object)))
488 if ($object == $class_name)
497 trigger_error('Unknown class passed as parameter', E_USER_WARNNG);
505 * Re-implementation of PHP 4.2.0's is_a()
508 * @param object $object The tested object
509 * @param string $class_name The class name
510 * @return bool Returns true if the object is of this class or has this class as one of its parents, false otherwise
512 function is_a($object, $class_name)
514 if (function_exists('is_a'))
516 return is_a($object, $class_name);
518 elseif (!is_object($object))
522 elseif (get_class($object) == strtolower($class_name))
528 return is_subclass_of($object, $class_name);
533 * Re-implementation of PHP 5's microtime()
536 * @param bool $get_as_float
538 function microtime($get_as_float = false)
542 if (is_float($time = microtime(true)))
548 list($user, $sec) = explode(' ', $time);
549 return ((float) $user + (float) $sec);
554 // PHP6 will likely return a float by default, so explicitly pass false (this is just ignored under PHP < 5)
555 return microtime(false);
565 class Unit_Test2_Files
568 * Get a list of files/folders within $dir
572 * @param string $dir Folder to get listing for
575 function get_files($dir)
578 if ($dh = opendir($dir))
580 while (($file = readdir($dh)) !== false)
582 if (substr($file, 0, 1) != '.')
584 $files[] = "$dir/$file";
588 usort($files, array(__CLASS__, 'sort_files'));
589 foreach ($files as $file)
593 array_splice($files, array_search($file, $files), 0, Unit_Test2_Files::get_files($file));
601 * Sort files/folders with files listed before inner folders
605 * @param string $a File/folder 1
606 * @param string $b File/folder 2
609 function sort_files($a, $b)
611 if (is_dir($a) && is_dir($b))
613 return strnatcmp($a, $b);
625 return strnatcmp($a, $b);