CoverageSetupTask.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * $Id: CoverageSetupTask.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once 'phing/Task.php';
  22. require_once 'phing/system/io/PhingFile.php';
  23. require_once 'phing/system/io/Writer.php';
  24. require_once 'phing/system/util/Properties.php';
  25. require_once 'phing/tasks/ext/coverage/CoverageMerger.php';
  26. /**
  27. * Initializes a code coverage database
  28. *
  29. * @author Michiel Rook <michiel.rook@gmail.com>
  30. * @version $Id: CoverageSetupTask.php 905 2010-10-05 16:28:03Z mrook $
  31. * @package phing.tasks.ext.coverage
  32. * @since 2.1.0
  33. */
  34. class CoverageSetupTask extends Task
  35. {
  36. /** the list of filesets containing the .php filename rules */
  37. private $filesets = array();
  38. /** the filename of the coverage database */
  39. private $database = "coverage.db";
  40. /** the classpath to use (optional) */
  41. private $classpath = NULL;
  42. /**
  43. * Add a new fileset containing the .php files to process
  44. *
  45. * @param FileSet the new fileset containing .php files
  46. */
  47. function addFileSet(FileSet $fileset)
  48. {
  49. $this->filesets[] = $fileset;
  50. }
  51. /**
  52. * Sets the filename of the coverage database to use
  53. *
  54. * @param string the filename of the database
  55. */
  56. function setDatabase($database)
  57. {
  58. $this->database = $database;
  59. }
  60. function setClasspath(Path $classpath)
  61. {
  62. if ($this->classpath === null)
  63. {
  64. $this->classpath = $classpath;
  65. }
  66. else
  67. {
  68. $this->classpath->append($classpath);
  69. }
  70. }
  71. function createClasspath()
  72. {
  73. $this->classpath = new Path();
  74. return $this->classpath;
  75. }
  76. /**
  77. * Iterate over all filesets and return the filename of all files.
  78. *
  79. * @return array an array of (basedir, filenames) pairs
  80. */
  81. private function getFilenames()
  82. {
  83. $files = array();
  84. foreach ($this->filesets as $fileset)
  85. {
  86. $ds = $fileset->getDirectoryScanner($this->project);
  87. $ds->scan();
  88. $includedFiles = $ds->getIncludedFiles();
  89. foreach ($includedFiles as $file)
  90. {
  91. $fs = new PhingFile(realpath($ds->getBaseDir()), $file);
  92. $files[] = array('key' => strtolower($fs->getAbsolutePath()), 'fullname' => $fs->getAbsolutePath());
  93. }
  94. }
  95. return $files;
  96. }
  97. function init()
  98. {
  99. }
  100. function main()
  101. {
  102. $files = $this->getFilenames();
  103. $this->log("Setting up coverage database for " . count($files) . " files");
  104. $props = new Properties();
  105. foreach ($files as $file)
  106. {
  107. $fullname = $file['fullname'];
  108. $filename = $file['key'];
  109. $props->setProperty($filename, serialize(array('fullname' => $fullname, 'coverage' => array())));
  110. }
  111. $dbfile = new PhingFile($this->database);
  112. $props->store($dbfile);
  113. $this->project->setProperty('coverage.database', $dbfile->getAbsolutePath());
  114. }
  115. }