Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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