PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/phpunit-1.0/vendor/PHPUnit/Util/Log/CodeCoverage/XML/Source.php

https://github.com/robertleeplummerjr/bluebox
PHP | 304 lines | 196 code | 44 blank | 64 comment | 16 complexity | 2dd4389609b3fe65fbd5bd4cb44d47a9 MD5 | raw file
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @category Testing
  38. * @package PHPUnit
  39. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  40. * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. * @version SVN: $Id: Source.php 4708 2009-03-08 14:45:40Z sb $
  43. * @link http://www.phpunit.de/
  44. * @since File available since Release 3.3.0
  45. */
  46. require_once 'PHPUnit/Runner/Version.php';
  47. require_once 'PHPUnit/Util/Metrics/File.php';
  48. require_once 'PHPUnit/Util/Class.php';
  49. require_once 'PHPUnit/Util/CodeCoverage.php';
  50. require_once 'PHPUnit/Util/Filter.php';
  51. require_once 'PHPUnit/Util/XML.php';
  52. PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
  53. /**
  54. * Writes one XML file per covered PHP source file to a given directory.
  55. * Each <line> element holds a line of PHP sourcecode that is annotated with
  56. * code coverage information.
  57. *
  58. * @category Testing
  59. * @package PHPUnit
  60. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  61. * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
  62. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  63. * @version Release: @package_version@
  64. * @link http://www.phpunit.de/
  65. * @since Class available since Release 3.3.0
  66. */
  67. class PHPUnit_Util_Log_CodeCoverage_XML_Source
  68. {
  69. protected $directory;
  70. /**
  71. * @param string $directory
  72. */
  73. public function __construct($directory)
  74. {
  75. $this->directory = PHPUnit_Util_Filesystem::getDirectory($directory);
  76. }
  77. /**
  78. * @param PHPUnit_Framework_TestResult $result
  79. */
  80. public function process(PHPUnit_Framework_TestResult $result)
  81. {
  82. $sutData = $result->getCodeCoverageInformation();
  83. $sutFiles = PHPUnit_Util_CodeCoverage::getSummary($sutData, TRUE);
  84. $allData = $result->getCodeCoverageInformation(FALSE);
  85. $allFiles = PHPUnit_Util_CodeCoverage::getSummary($allData, TRUE);
  86. $testFiles = array_diff(array_keys($allFiles), array_keys($sutFiles));
  87. foreach (array_keys($allFiles) as $key) {
  88. if (!in_array($key, $testFiles)) {
  89. unset($allFiles[$key]);
  90. }
  91. }
  92. $allCommonPath = PHPUnit_Util_Filesystem::reducePaths($allFiles);
  93. $sutCommonPath = PHPUnit_Util_Filesystem::reducePaths($sutFiles);
  94. $testFiles = $allFiles;
  95. unset($allData);
  96. unset($allFiles);
  97. unset($sutData);
  98. $testToCoveredLinesMap = array();
  99. $time = time();
  100. foreach ($sutFiles as $filename => $data) {
  101. $fullPath = $sutCommonPath . DIRECTORY_SEPARATOR . $filename;
  102. if (file_exists($fullPath)) {
  103. $fullPath = realpath($fullPath);
  104. $document = new DOMDocument('1.0', 'UTF-8');
  105. $document->formatOutput = TRUE;
  106. $coveredFile = $document->createElement('coveredFile');
  107. $coveredFile->setAttribute('fullPath', $fullPath);
  108. $coveredFile->setAttribute('shortenedPath', $filename);
  109. $coveredFile->setAttribute('generated', $time);
  110. $coveredFile->setAttribute('phpunit', PHPUnit_Runner_Version::id());
  111. $document->appendChild($coveredFile);
  112. $lines = file($fullPath, FILE_IGNORE_NEW_LINES);
  113. $lineNum = 1;
  114. foreach ($lines as $line) {
  115. if (isset($data[$lineNum])) {
  116. if (is_array($data[$lineNum])) {
  117. $count = count($data[$lineNum]);
  118. } else {
  119. $count = $data[$lineNum];
  120. }
  121. } else {
  122. $count = -3;
  123. }
  124. $xmlLine = $coveredFile->appendChild(
  125. $document->createElement('line')
  126. );
  127. $xmlLine->setAttribute('lineNumber', $lineNum);
  128. $xmlLine->setAttribute('executed', $count);
  129. $xmlLine->appendChild(
  130. $document->createElement(
  131. 'body',
  132. htmlspecialchars(
  133. PHPUnit_Util_XML::convertToUtf8($line),
  134. ENT_COMPAT,
  135. 'UTF-8'
  136. )
  137. )
  138. );
  139. if (isset($data[$lineNum]) && is_array($data[$lineNum])) {
  140. $xmlTests = $document->createElement('tests');
  141. $xmlLine->appendChild($xmlTests);
  142. foreach ($data[$lineNum] as $test) {
  143. $xmlTest = $xmlTests->appendChild(
  144. $document->createElement('test')
  145. );
  146. if ($test instanceof PHPUnit_Framework_TestCase) {
  147. $xmlTest->setAttribute('name', $test->getName());
  148. $xmlTest->setAttribute('status', $test->getStatus());
  149. if ($test->hasFailed()) {
  150. $xmlTest->appendChild(
  151. $document->createElement(
  152. 'message',
  153. htmlspecialchars(
  154. PHPUnit_Util_XML::convertToUtf8($test->getStatusMessage()),
  155. ENT_COMPAT,
  156. 'UTF-8'
  157. )
  158. )
  159. );
  160. }
  161. $class = new ReflectionClass($test);
  162. $testFullPath = $class->getFileName();
  163. $testShortenedPath = str_replace($allCommonPath, '', $testFullPath);
  164. $methodName = $test->getName(FALSE);
  165. if ($class->hasMethod($methodName)) {
  166. $method = $class->getMethod($methodName);
  167. $startLine = $method->getStartLine();
  168. $xmlTest->setAttribute('class', $class->getName());
  169. $xmlTest->setAttribute('fullPath', $testFullPath);
  170. $xmlTest->setAttribute('shortenedPath', $testShortenedPath);
  171. $xmlTest->setAttribute('line', $startLine);
  172. if (!isset($testToCoveredLinesMap[$testFullPath][$startLine])) {
  173. $testToCoveredLinesMap[$testFullPath][$startLine] = array();
  174. }
  175. if (!isset($testToCoveredLinesMap[$testFullPath][$startLine][$fullPath])) {
  176. $testToCoveredLinesMap[$testFullPath][$startLine][$fullPath] = array(
  177. 'coveredLines' => array($lineNum),
  178. 'shortenedPath' => $filename
  179. );
  180. } else {
  181. $testToCoveredLinesMap[$testFullPath][$startLine][$fullPath]['coveredLines'][] = $lineNum;
  182. }
  183. }
  184. }
  185. }
  186. }
  187. $lineNum++;
  188. }
  189. $document->save(
  190. sprintf(
  191. '%s%s.xml',
  192. $this->directory,
  193. PHPUnit_Util_Filesystem::getSafeFilename(
  194. basename($filename)
  195. )
  196. )
  197. );
  198. }
  199. }
  200. foreach ($testFiles as $filename => $data) {
  201. $fullPath = $allCommonPath . DIRECTORY_SEPARATOR . $filename;
  202. if (file_exists($fullPath)) {
  203. $document = new DOMDocument('1.0', 'UTF-8');
  204. $document->formatOutput = TRUE;
  205. $testFile = $document->createElement('testFile');
  206. $testFile->setAttribute('fullPath', $fullPath);
  207. $testFile->setAttribute('shortenedPath', $filename);
  208. $testFile->setAttribute('generated', $time);
  209. $testFile->setAttribute('phpunit', PHPUnit_Runner_Version::id());
  210. $document->appendChild($testFile);
  211. $lines = file($fullPath, FILE_IGNORE_NEW_LINES);
  212. $lineNum = 1;
  213. foreach ($lines as $line) {
  214. $xmlLine = $testFile->appendChild(
  215. $document->createElement('line')
  216. );
  217. $xmlLine->setAttribute('lineNumber', $lineNum);
  218. $xmlLine->appendChild(
  219. $document->createElement(
  220. 'body',
  221. htmlspecialchars(
  222. PHPUnit_Util_XML::convertToUtf8($line),
  223. ENT_COMPAT,
  224. 'UTF-8'
  225. )
  226. )
  227. );
  228. if (isset($testToCoveredLinesMap[$fullPath][$lineNum])) {
  229. $xmlCoveredFiles = $xmlLine->appendChild(
  230. $document->createElement('coveredFiles')
  231. );
  232. foreach ($testToCoveredLinesMap[$fullPath][$lineNum] as $coveredFileFullPath => $coveredFileData) {
  233. $xmlCoveredFile = $xmlCoveredFiles->appendChild(
  234. $document->createElement('coveredFile')
  235. );
  236. $xmlCoveredFile->setAttribute('fullPath', $fullPath);
  237. $xmlCoveredFile->setAttribute('shortenedPath', $coveredFileData['shortenedPath']);
  238. foreach ($coveredFileData['coveredLines'] as $coveredLineNum) {
  239. $xmlCoveredLine = $xmlCoveredFile->appendChild(
  240. $document->createElement('coveredLine', $coveredLineNum)
  241. );
  242. }
  243. }
  244. }
  245. $lineNum++;
  246. }
  247. $document->save(
  248. sprintf(
  249. '%s%s.xml',
  250. $this->directory,
  251. PHPUnit_Util_Filesystem::getSafeFilename(
  252. basename($filename)
  253. )
  254. )
  255. );
  256. }
  257. }
  258. }
  259. }
  260. ?>