This Dispatcher is used exclusively for the purpose of running, organizing and compiling statistics for the built-in Lithium test suite.
Inheritance: extends lithium\core\StaticObject
Exemplo n.º 1
0
 /**
  * Main method.
  *
  * @param string $path Absolute path to file or directory.
  * @return boolean
  */
 public function run()
 {
     $path = $this->request->action;
     if (!($path = realpath($path))) {
         $this->error('Not a valid path.');
         return false;
     }
     if (!($library = $this->_library($path))) {
         $this->error("No library registered for path `{$path}`.");
         return false;
     }
     $classes = Libraries::find($library, array('recursive' => true, 'exclude' => '/tests|resources|webroot|index$|^app\\\\config|^app\\\\views|Exception$/'));
     $tests = array();
     foreach (Group::all() as $test) {
         $class = preg_replace('/(tests\\\\[a-z]+\\\\|Test$)/', null, $test);
         $tests[$class] = $test;
     }
     foreach ($classes as $class) {
         $coverage = null;
         if ($hasTest = isset($tests[$class])) {
             $report = Dispatcher::run($tests[$class], array('reporter' => 'console', 'format' => 'txt', 'filters' => array('Coverage')));
             $coverage = $report->results['filters']['lithium\\test\\filter\\Coverage'];
             $coverage = isset($coverage[$class]) ? $coverage[$class]['percentage'] : null;
         }
         $this->out(sprintf('%10s | %7s | %s', $hasTest ? 'has test' : 'no test', is_numeric($coverage) ? sprintf('%.2f%%', $coverage) : 'n/a', $class));
     }
 }
Exemplo n.º 2
0
 /**
  * Magic method to make Controller callable.
  *
  * @see lithium\action\Dispatcher::_callable()
  * @param object $request A \lithium\action\Request object.
  * @param array $dispatchParams Array of params after being parsed by router.
  * @param array $options Some basic options for this controller.
  * @return string
  */
 public function __invoke($request, $dispatchParams, array $options = array())
 {
     $dispatchParamsDefaults = array('args' => array());
     $dispatchParams += $dispatchParamsDefaults;
     $defaults = array('reporter' => 'html', 'format' => 'html');
     $options += (array) $request->query + $defaults;
     $params = compact('request', 'dispatchParams', 'options');
     set_time_limit(0);
     return $this->_filter(__METHOD__, $params, function ($self, $params) {
         $request = $params['request'];
         $options = $params['options'];
         $params = $params['dispatchParams'];
         $group = join('\\', (array) $params['args']);
         if ($group === "all") {
             $group = Group::all();
             $options['title'] = 'All Tests';
         }
         $report = Dispatcher::run($group, $options);
         $filters = Libraries::locate('test.filter');
         $menu = Libraries::locate('tests', null, array('filter' => '/cases|integration|functional/', 'exclude' => '/mocks/'));
         sort($menu);
         $result = compact('request', 'report', 'filters', 'menu');
         return $report->render('layout', $result);
     });
 }
Exemplo n.º 3
0
 /**
  * Base method for gathering data
  *
  * @param string $request
  * @param string $params
  * @param string $options
  * @return array request, group, report, filters, classes, menu
  */
 protected function _data($request, $params, $options)
 {
     $group = '\\' . join('\\', $request->args);
     $report = Dispatcher::run($group, $request->query + array('reporter' => 'html'));
     $filters = Libraries::locate('test.filter', null, array('exclude' => '/Base$/'));
     $classes = Libraries::locate('tests', null, array('filter' => '/cases|integration|functional/', 'exclude' => '/mocks/'));
     $menu = $report->reporter->menu($classes, array('request' => $request, 'tree' => true));
     return compact('request', 'group', 'report', 'filters', 'classes', 'menu');
 }
Exemplo n.º 4
0
 public function testRunGroupWithString()
 {
     $report = Dispatcher::run('\\lithium\\tests\\mocks\\test');
     $expected = '\\lithium\\tests\\mocks\\test';
     $result = $report->title;
     $this->assertEqual($expected, $result);
     $expected = new Collection(array('data' => array(new \lithium\tests\mocks\test\cases\MockSkipThrowsException(), new \lithium\tests\mocks\test\cases\MockTest(), new \lithium\tests\mocks\test\cases\MockTestErrorHandling())));
     $result = $report->group->tests();
     $this->assertEqual($expected, $result);
     $expected = 'testNothing';
     $result = $report->results['group'][1][0]['method'];
     $this->assertEqual($expected, $result);
     $expected = 'pass';
     $result = $report->results['group'][1][0]['result'];
     $this->assertEqual($expected, $result);
 }
Exemplo n.º 5
0
 /**
  * Runs tests. Will provide a list of available tests if none are given.
  * Test cases should be given in dot notation.
  *
  * Case example:
  * {{{
  * lithium test --case=lithium.tests.cases.core.ObjectTest
  * }}}
  *
  * Group example:
  * {{{
  * lithium test --group=lithium.tests.cases.core
  * }}}
  *
  * @return void
  */
 public function run()
 {
     $this->header('Test');
     if ($this->_getTests() != true) {
         return 0;
     }
     error_reporting(E_ALL | E_STRICT | E_DEPRECATED);
     $run = $this->case ?: $this->group;
     $run = '\\' . str_replace('.', '\\', $run);
     $this->out(sprintf('Running `%s`... ', $run), false);
     $report = Dispatcher::run($run, array('filters' => $this->filters, 'reporter' => 'console', 'format' => 'txt'));
     $this->out('done.', 2);
     $this->out('{:heading1}Results{:end}', 0);
     $this->out($report->render('stats'));
     foreach ($report->filters() as $filter => $options) {
         $data = $report->results['filters'][$filter];
         $this->out($report->render($options['name'], compact('data')));
     }
     $this->hr();
     $this->nl();
 }
Exemplo n.º 6
0
 /**
  * Runs tests. Will provide a list of available tests if none are given.
  * Test cases should be given in dot notation.
  *
  * Case example:
  * {{{
  * lithium test --case=lithium.tests.cases.core.ObjectTest
  * }}}
  *
  * Group example:
  * {{{
  * lithium test --group=lithium.tests.cases.core
  * }}}
  *
  * @return void
  */
 public function run()
 {
     $this->header('Test');
     if ($this->_getTests() != true) {
         return 0;
     }
     $startBenchmark = microtime(true);
     error_reporting(E_ALL | E_STRICT | E_DEPRECATED);
     if (!empty($this->case)) {
         $this->case = '\\' . str_replace('.', '\\', $this->case);
     } elseif (!empty($this->group)) {
         $this->group = '\\' . str_replace('.', '\\', $this->group);
     }
     $run = $this->case ?: $this->group;
     $this->nl();
     $this->out(sprintf('Running `%s`... ', $run), false);
     $report = Dispatcher::run($run, array('filters' => $this->filters, 'reporter' => 'console'));
     $this->out('done.', 2);
     if ($output = $report->filters()) {
         $this->out($output, 2);
     }
     $this->out($report->stats());
     $this->nl();
 }
Exemplo n.º 7
0
 /**
  * Runs tests given a path to a directory or file containing tests. The path to the
  * test(s) may be absolute or relative to the current working directory.
  *
  * {{{
  * li3 test lithium/tests/cases/core/ObjectTest.php
  * li3 test lithium/tests/cases/core
  * }}}
  *
  * If you are in the working directory of an application or plugin and wish to run all tests,
  * simply execute the following:
  *
  * {{{
  * li3 test tests/cases
  * }}}
  *
  * If you are in the working directory of an application and wish to run a plugin, execute one
  * of the following:
  *
  * {{{
  * li3 test libraries/<plugin>/tests/cases
  * li3 test <plugin>/tests/cases
  * }}}
  *
  * @param string $path Absolute or relative path to tests.
  * @return boolean Will exit with status `1` if one or more tests failed otherwise with `0`.
  */
 public function run($path = null)
 {
     if (!($path = $this->_path($path))) {
         return false;
     }
     $handlers = $this->_handlers;
     if (!isset($handlers[$this->format]) || !is_callable($handlers[$this->format])) {
         $this->error(sprintf('No handler for format `%s`... ', $this->format));
         return false;
     }
     $filters = $this->filters ? array_map('trim', explode(',', $this->filters)) : array();
     $params = compact('filters') + array('reporter' => 'console', 'format' => $this->format);
     $runner = function () use($path, $params) {
         error_reporting(E_ALL | E_STRICT | E_DEPRECATED);
         return Dispatcher::run($path, $params);
     };
     $report = $handlers[$this->format]($runner, $path);
     $stats = $report->stats();
     return $stats['success'];
 }
Exemplo n.º 8
0
 /**
  * Runs tests given a path to a directory or file containing tests. The path to the
  * test(s) may be absolute or relative to the current working directory.
  *
  * {{{
  * li3 test lithium/tests/cases/core/ObjectTest.php
  * li3 test lithium/tests/cases/core
  * }}}
  *
  * If you are in the working directory of an application or plugin and wish to run all tests,
  * simply execute the following:
  *
  * {{{
  * li3 test tests/cases
  * }}}
  *
  * If you are in the working directory of an application and wish to run a plugin, execute one
  * of the following:
  *
  * {{{
  * li3 test libraries/<plugin>/tests/cases
  * li3 test <plugin>/tests/cases
  * }}}
  *
  *
  * This will run `<library>/tests/cases/<package>/<class>Test.php`:
  *
  * {{{
  * li3 test <library>/<package>/<class>.php
  * }}}
  *
  * @param string $path Absolute or relative path to tests or a file which
  *                     corresponding test should be run.
  * @return boolean Will exit with status `1` if one or more tests failed otherwise with `0`.
  */
 public function run($path = null)
 {
     if (!($path = $this->_path($path))) {
         return false;
     }
     if (!preg_match('/(tests|Test\\.php)/', $path)) {
         if (!($path = Unit::get($path))) {
             $this->error('Cannot map path to test path.');
             return static::EXIT_NO_TEST;
         }
     }
     $handlers = $this->_handlers;
     if (!isset($handlers[$this->format]) || !is_callable($handlers[$this->format])) {
         $this->error(sprintf('No handler for format `%s`... ', $this->format));
         return false;
     }
     $filters = $this->filters ? array_map('trim', explode(',', $this->filters)) : array();
     $params = compact('filters') + array('format' => $this->format);
     $runner = function ($options = array()) use($path, $params) {
         error_reporting(E_ALL | E_STRICT | E_DEPRECATED);
         return Dispatcher::run($path, $params + $options);
     };
     $report = $handlers[$this->format]($runner, $path);
     $stats = $report->stats();
     return $stats['success'];
 }