sfTestCoverageTask.class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Outputs test code coverage.
  11. *
  12. * @package symfony
  13. * @subpackage task
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfTestCoverageTask.class.php 12069 2008-10-08 12:27:04Z fabien $
  16. */
  17. class sfTestCoverageTask extends sfBaseTask
  18. {
  19. /**
  20. * @see sfTask
  21. */
  22. protected function configure()
  23. {
  24. $this->addArguments(array(
  25. new sfCommandArgument('test_name', sfCommandArgument::REQUIRED, 'A test file name or a test directory'),
  26. new sfCommandArgument('lib_name', sfCommandArgument::REQUIRED, 'A lib file name or a lib directory for wich you want to know the coverage'),
  27. ));
  28. $this->addOptions(array(
  29. new sfCommandOption('detailed', null, sfCommandOption::PARAMETER_NONE, 'Output detailed information'),
  30. ));
  31. $this->namespace = 'test';
  32. $this->name = 'coverage';
  33. $this->briefDescription = 'Outputs test code coverage';
  34. $this->detailedDescription = <<<EOF
  35. The [test:coverage|INFO] task outputs the code coverage
  36. given a test file or test directory
  37. and a lib file or lib directory for which you want code
  38. coverage:
  39. [./symfony test:coverage test/unit/model lib/model|INFO]
  40. To output the lines not covered, pass the [--detailed|INFO] option:
  41. [./symfony test:coverage --detailed test/unit/model lib/model|INFO]
  42. EOF;
  43. }
  44. /**
  45. * @see sfTask
  46. */
  47. protected function execute($arguments = array(), $options = array())
  48. {
  49. require_once(sfConfig::get('sf_symfony_lib_dir').'/vendor/lime/lime.php');
  50. $coverage = $this->getCoverage($this->getTestHarness(), $options['detailed']);
  51. $testFiles = $this->getFiles(sfConfig::get('sf_root_dir').'/'.$arguments['test_name']);
  52. $max = count($testFiles);
  53. foreach ($testFiles as $i => $file)
  54. {
  55. $this->logSection('coverage', sprintf('running %s (%d/%d)', $file, $i + 1, $max));
  56. $coverage->process($file);
  57. }
  58. $coveredFiles = $this->getFiles(sfConfig::get('sf_root_dir').'/'.$arguments['lib_name']);
  59. $coverage->output($coveredFiles);
  60. }
  61. protected function getTestHarness()
  62. {
  63. $harness = new lime_harness(new lime_output_color());
  64. $harness->base_dir = sfConfig::get('sf_root_dir');
  65. return $harness;
  66. }
  67. protected function getCoverage(lime_harness $harness, $detailed = false)
  68. {
  69. $coverage = new lime_coverage($harness);
  70. $coverage->verbose = $detailed;
  71. $coverage->base_dir = sfConfig::get('sf_root_dir');
  72. return $coverage;
  73. }
  74. protected function getFiles($directory)
  75. {
  76. if (is_dir($directory))
  77. {
  78. return sfFinder::type('file')->name('*.php')->in($directory);
  79. }
  80. else if (file_exists($directory))
  81. {
  82. return array($directory);
  83. }
  84. else
  85. {
  86. throw new sfCommandException(sprintf('File or directory "%s" does not exist.', $directory));
  87. }
  88. }
  89. }