Dispatcher.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\test;
  9. use lithium\util\Set;
  10. use lithium\core\Libraries;
  11. use lithium\core\Environment;
  12. /**
  13. * The Lithium Test Dispatcher
  14. *
  15. * This Dispatcher is used exclusively for the purpose of running, organizing and compiling
  16. * statistics for the built-in Lithium test suite.
  17. */
  18. class Dispatcher extends \lithium\core\StaticObject {
  19. /**
  20. * Composed classes used by the Dispatcher.
  21. *
  22. * @var array Key/value array of short identifier for the fully-namespaced class.
  23. */
  24. protected static $_classes = array(
  25. 'group' => 'lithium\test\Group',
  26. 'report' => 'lithium\test\Report'
  27. );
  28. /**
  29. * Runs a test group or a specific test file based on the passed
  30. * parameters.
  31. *
  32. * @param string $group If set, this test group is run. If not set, a group test may
  33. * also be run by passing the 'group' option to the $options parameter.
  34. * @param array $options Options array for the test run. Valid options are:
  35. * - `'case'`: The fully namespaced test case to be run.
  36. * - `'group'`: The fully namespaced test group to be run.
  37. * - `'filters'`: An array of filters that the test output should be run through.
  38. * - `'format'`: The format of the template to use, defaults to `'txt'`.
  39. * - `'reporter'`: The reporter to use.
  40. * @return array A compact array of the title, an array of the results, as well
  41. * as an additional array of the results after the $options['filters']
  42. * have been applied.
  43. * @filter
  44. */
  45. public static function run($group = null, array $options = array()) {
  46. $defaults = array(
  47. 'title' => $group,
  48. 'filters' => array(),
  49. 'format' => 'txt',
  50. 'reporter' => null
  51. );
  52. $options += $defaults;
  53. $isCase = is_string($group) && preg_match('/Test$/', $group);
  54. $items = ($isCase) ? array(new $group()) : (array) $group;
  55. $options['filters'] = Set::normalize($options['filters']);
  56. $group = static::_group($items);
  57. $report = static::_report($group, $options);
  58. return static::_filter(__FUNCTION__, compact('report'), function($self, $params, $chain) {
  59. $environment = Environment::get();
  60. Environment::set('test');
  61. $params['report']->run();
  62. Environment::set($environment);
  63. return $params['report'];
  64. });
  65. }
  66. /**
  67. * Creates the group class based
  68. *
  69. * @see lithium\test\Dispatcher::$_classes
  70. * @param array $data Array of cases or groups.
  71. * @return object Group object constructed with `$data`.
  72. */
  73. protected static function _group($data) {
  74. $group = Libraries::locate('test', static::$_classes['group']);
  75. $class = static::_instance($group, compact('data'));
  76. return $class;
  77. }
  78. /**
  79. * Creates the test report class based on either the passed test case or the
  80. * passed test group.
  81. *
  82. * @see lithium\test\Dispatcher::$_classes
  83. * @param string $group
  84. * @param array $options Options array passed from Dispatcher::run(). Should contain
  85. * one of 'case' or 'group' keys.
  86. * @return object Group object constructed with the test case or group passed in $options.
  87. */
  88. protected static function _report($group, $options) {
  89. $report = Libraries::locate('test', static::$_classes['report']);
  90. $class = static::_instance($report, compact('group') + $options);
  91. return $class;
  92. }
  93. }
  94. ?>