1. sfTestCoverageTask.class.php
  2. /** * Outputs test code coverage. * * @package symfony * @subpackage task * @author Fabien Potencier * @version SVN: $Id: sfTestCoverageTask.class.php 25036 2009-12-07 19:41:58Z Kris.Wallsmith $ */
  3. class sfTestCoverageTask extends sfBaseTask
  4. {
  5. /**
  6. * @see sfTask
  7. */
  8. protected function configure()
  9. {
  10. $this->addArguments(array(
  11. new sfCommandArgument('test_name', sfCommandArgument::REQUIRED, 'A test file name or a test directory'),
  12. new sfCommandArgument('lib_name', sfCommandArgument::REQUIRED, 'A lib file name or a lib directory for wich you want to know the coverage'),
  13. ));
  14. $this->addOptions(array(
  15. new sfCommandOption('detailed', null, sfCommandOption::PARAMETER_NONE, 'Output detailed information'),
  16. ));
  17. $this->namespace = 'test';
  18. $this->name = 'coverage';
  19. $this->briefDescription = 'Outputs test code coverage';
  20. $this->detailedDescription = <<<EOF
  21. The [test:coverage|INFO] task outputs the code coverage
  22. given a test file or test directory
  23. and a lib file or lib directory for which you want code
  24. coverage:
  25. [./symfony test:coverage test/unit/model lib/model|INFO]
  26. To output the lines not covered, pass the [--detailed|INFO] option:
  27. [./symfony test:coverage --detailed test/unit/model lib/model|INFO]
  28. EOF;
  29. }
  30. /**
  31. * @see sfTask
  32. */
  33. protected function execute($arguments = array(), $options = array())
  34. {
  35. require_once sfConfig::get('sf_symfony_lib_dir').'/vendor/lime/lime.php';
  36. $coverage = $this->getCoverage($this->getTestHarness(array('force_colors' => isset($options['color']) && $options['color'])), $options['detailed']);
  37. $testFiles = $this->getFiles(sfConfig::get('sf_root_dir').'/'.$arguments['test_name']);
  38. $max = count($testFiles);
  39. foreach ($testFiles as $i => $file)
  40. {
  41. $this->logSection('coverage', sprintf('running %s (%d/%d)', $file, $i + 1, $max));
  42. $coverage->process($file);
  43. }
  44. $coveredFiles = $this->getFiles(sfConfig::get('sf_root_dir').'/'.$arguments['lib_name']);
  45. $coverage->output($coveredFiles);
  46. }
  47. protected function getTestHarness($harnessOptions = array())
  48. {
  49. require_once dirname(__FILE__).'/sfLimeHarness.class.php';
  50. $harness = new sfLimeHarness($harnessOptions);
  51. $harness->addPlugins(array_map(array($this->configuration, 'getPluginConfiguration'), $this->configuration->getPlugins()));
  52. $harness->base_dir = sfConfig::get('sf_root_dir');
  53. return $harness;
  54. }
  55. protected function getCoverage(lime_harness $harness, $detailed = false)
  56. {
  57. $coverage = new lime_coverage($harness);
  58. $coverage->verbose = $detailed;
  59. $coverage->base_dir = sfConfig::get('sf_root_dir');
  60. return $coverage;
  61. }
  62. protected function getFiles($directory)
  63. {
  64. if (is_dir($directory))
  65. {
  66. return sfFinder::type('file')->name('*.php')->in($directory);
  67. }
  68. else if (file_exists($directory))
  69. {
  70. return array($directory);
  71. }
  72. else
  73. {
  74. throw new sfCommandException(sprintf('File or directory "%s" does not exist.', $directory));
  75. }
  76. }
  77. }

Debug toolbar