1. sfTestUnitTask.class.php
  2. /** * Launches unit tests. * * @package symfony * @subpackage task * @author Fabien Potencier * @version SVN: $Id: sfTestUnitTask.class.php 25036 2009-12-07 19:41:58Z Kris.Wallsmith $ */
  3. class sfTestUnitTask extends sfTestBaseTask
  4. {
  5. /**
  6. * @see sfTask
  7. */
  8. protected function configure()
  9. {
  10. $this->addArguments(array(
  11. new sfCommandArgument('name', sfCommandArgument::OPTIONAL | sfCommandArgument::IS_ARRAY, 'The test name'),
  12. ));
  13. $this->addOptions(array(
  14. new sfCommandOption('xml', null, sfCommandOption::PARAMETER_REQUIRED, 'The file name for the JUnit compatible XML log file'),
  15. ));
  16. $this->namespace = 'test';
  17. $this->name = 'unit';
  18. $this->briefDescription = 'Launches unit tests';
  19. $this->detailedDescription = <<<EOF
  20. The [test:unit|INFO] task launches unit tests:
  21. [./symfony test:unit|INFO]
  22. The task launches all tests found in [test/unit|COMMENT].
  23. If some tests fail, you can use the [--trace|COMMENT] option to have more
  24. information about the failures:
  25. [./symfony test:unit -t|INFO]
  26. You can launch unit tests for a specific name:
  27. [./symfony test:unit strtolower|INFO]
  28. You can also launch unit tests for several names:
  29. [./symfony test:unit strtolower strtoupper|INFO]
  30. The task can output a JUnit compatible XML log file with the [--xml|COMMENT]
  31. options:
  32. [./symfony test:unit --xml=log.xml|INFO]
  33. EOF;
  34. }
  35. /**
  36. * @see sfTask
  37. */
  38. protected function execute($arguments = array(), $options = array())
  39. {
  40. if (count($arguments['name']))
  41. {
  42. $files = array();
  43. foreach ($arguments['name'] as $name)
  44. {
  45. $finder = sfFinder::type('file')->follow_link()->name(basename($name).'Test.php');
  46. $files = array_merge($files, $finder->in(sfConfig::get('sf_test_dir').'/unit/'.dirname($name)));
  47. }
  48. if($allFiles = $this->filterTestFiles($files, $arguments, $options))
  49. {
  50. foreach ($allFiles as $file)
  51. {
  52. include($file);
  53. }
  54. }
  55. else
  56. {
  57. $this->logSection('test', 'no tests found', null, 'ERROR');
  58. }
  59. }
  60. else
  61. {
  62. require_once dirname(__FILE__).'/sfLimeHarness.class.php';
  63. $h = new sfLimeHarness(array(
  64. 'force_colors' => isset($options['color']) && $options['color'],
  65. 'verbose' => isset($options['trace']) && $options['trace'],
  66. ));
  67. $h->addPlugins(array_map(array($this->configuration, 'getPluginConfiguration'), $this->configuration->getPlugins()));
  68. $h->base_dir = sfConfig::get('sf_test_dir').'/unit';
  69. // filter and register unit tests
  70. $finder = sfFinder::type('file')->follow_link()->name('*Test.php');
  71. $h->register($this->filterTestFiles($finder->in($h->base_dir), $arguments, $options));
  72. $ret = $h->run() ? 0 : 1;
  73. if ($options['xml'])
  74. {
  75. file_put_contents($options['xml'], $h->to_xml());
  76. }
  77. return $ret;
  78. }
  79. }
  80. }

Debug toolbar