test.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php namespace System;
  2. class Test {
  3. /**
  4. * All of the test results.
  5. *
  6. * @var array
  7. */
  8. public static $results = array();
  9. /**
  10. * Total number of tests being run.
  11. *
  12. * @var int
  13. */
  14. public static $total = 0;
  15. /**
  16. * Total number of passed tests.
  17. *
  18. * @var int
  19. */
  20. public static $passed = 0;
  21. /**
  22. * Run a test suite.
  23. *
  24. * @param string $suite
  25. * @param array $tests
  26. * @return void
  27. */
  28. public static function run($suite, $tests)
  29. {
  30. static::$total = static::$total + count($tests);
  31. // -----------------------------------------------------
  32. // Run each test in the suite.
  33. // -----------------------------------------------------
  34. foreach ($tests as $name => $test)
  35. {
  36. if ( ! is_callable($test))
  37. {
  38. throw new \Exception("Test [$name] in suite [$suite] is not callable.");
  39. }
  40. static::$passed = ($result = call_user_func($test)) ? static::$passed + 1 : static::$passed;
  41. static::$results[$suite][] = array('name' => $name, 'result' => $result);
  42. }
  43. }
  44. /**
  45. * Get the test report view.
  46. *
  47. * @return View
  48. */
  49. public static function report()
  50. {
  51. return View::make('test/report')
  52. ->bind('results', static::$results)
  53. ->bind('passed', static::$passed)
  54. ->bind('total', static::$total);
  55. }
  56. }