BuilderTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /*
  3. * This file is part of the php-code-coverage package.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\CodeCoverage\Report;
  11. use SebastianBergmann\CodeCoverage\Driver\Driver;
  12. use SebastianBergmann\CodeCoverage\CodeCoverage;
  13. use SebastianBergmann\CodeCoverage\Filter;
  14. use SebastianBergmann\CodeCoverage\TestCase;
  15. use SebastianBergmann\CodeCoverage\Node\Builder;
  16. class BuilderTest extends TestCase
  17. {
  18. protected $factory;
  19. protected function setUp()
  20. {
  21. $this->factory = new Builder;
  22. }
  23. public function testSomething()
  24. {
  25. $root = $this->getCoverageForBankAccount()->getReport();
  26. $expectedPath = rtrim(TEST_FILES_PATH, DIRECTORY_SEPARATOR);
  27. $this->assertEquals($expectedPath, $root->getName());
  28. $this->assertEquals($expectedPath, $root->getPath());
  29. $this->assertEquals(10, $root->getNumExecutableLines());
  30. $this->assertEquals(5, $root->getNumExecutedLines());
  31. $this->assertEquals(1, $root->getNumClasses());
  32. $this->assertEquals(0, $root->getNumTestedClasses());
  33. $this->assertEquals(4, $root->getNumMethods());
  34. $this->assertEquals(3, $root->getNumTestedMethods());
  35. $this->assertEquals('0.00%', $root->getTestedClassesPercent());
  36. $this->assertEquals('75.00%', $root->getTestedMethodsPercent());
  37. $this->assertEquals('50.00%', $root->getLineExecutedPercent());
  38. $this->assertEquals(0, $root->getNumFunctions());
  39. $this->assertEquals(0, $root->getNumTestedFunctions());
  40. $this->assertNull($root->getParent());
  41. $this->assertEquals([], $root->getDirectories());
  42. #$this->assertEquals(array(), $root->getFiles());
  43. #$this->assertEquals(array(), $root->getChildNodes());
  44. $this->assertEquals(
  45. [
  46. 'BankAccount' => [
  47. 'methods' => [
  48. 'getBalance' => [
  49. 'signature' => 'getBalance()',
  50. 'startLine' => 6,
  51. 'endLine' => 9,
  52. 'executableLines' => 1,
  53. 'executedLines' => 1,
  54. 'ccn' => 1,
  55. 'coverage' => 100,
  56. 'crap' => '1',
  57. 'link' => 'BankAccount.php.html#6',
  58. 'methodName' => 'getBalance',
  59. 'visibility' => 'public',
  60. ],
  61. 'setBalance' => [
  62. 'signature' => 'setBalance($balance)',
  63. 'startLine' => 11,
  64. 'endLine' => 18,
  65. 'executableLines' => 5,
  66. 'executedLines' => 0,
  67. 'ccn' => 2,
  68. 'coverage' => 0,
  69. 'crap' => 6,
  70. 'link' => 'BankAccount.php.html#11',
  71. 'methodName' => 'setBalance',
  72. 'visibility' => 'protected',
  73. ],
  74. 'depositMoney' => [
  75. 'signature' => 'depositMoney($balance)',
  76. 'startLine' => 20,
  77. 'endLine' => 25,
  78. 'executableLines' => 2,
  79. 'executedLines' => 2,
  80. 'ccn' => 1,
  81. 'coverage' => 100,
  82. 'crap' => '1',
  83. 'link' => 'BankAccount.php.html#20',
  84. 'methodName' => 'depositMoney',
  85. 'visibility' => 'public',
  86. ],
  87. 'withdrawMoney' => [
  88. 'signature' => 'withdrawMoney($balance)',
  89. 'startLine' => 27,
  90. 'endLine' => 32,
  91. 'executableLines' => 2,
  92. 'executedLines' => 2,
  93. 'ccn' => 1,
  94. 'coverage' => 100,
  95. 'crap' => '1',
  96. 'link' => 'BankAccount.php.html#27',
  97. 'methodName' => 'withdrawMoney',
  98. 'visibility' => 'public',
  99. ],
  100. ],
  101. 'startLine' => 2,
  102. 'executableLines' => 10,
  103. 'executedLines' => 5,
  104. 'ccn' => 5,
  105. 'coverage' => 50,
  106. 'crap' => '8.12',
  107. 'package' => [
  108. 'namespace' => '',
  109. 'fullPackage' => '',
  110. 'category' => '',
  111. 'package' => '',
  112. 'subpackage' => ''
  113. ],
  114. 'link' => 'BankAccount.php.html#2',
  115. 'className' => 'BankAccount'
  116. ]
  117. ],
  118. $root->getClasses()
  119. );
  120. $this->assertEquals([], $root->getFunctions());
  121. }
  122. public function testNotCrashParsing()
  123. {
  124. $coverage = $this->getCoverageForCrashParsing();
  125. $root = $coverage->getReport();
  126. $expectedPath = rtrim(TEST_FILES_PATH, DIRECTORY_SEPARATOR);
  127. $this->assertEquals($expectedPath, $root->getName());
  128. $this->assertEquals($expectedPath, $root->getPath());
  129. $this->assertEquals(2, $root->getNumExecutableLines());
  130. $this->assertEquals(0, $root->getNumExecutedLines());
  131. $data = $coverage->getData();
  132. $expectedFile = $expectedPath . DIRECTORY_SEPARATOR . 'Crash.php';
  133. $this->assertSame([$expectedFile => [1 => [], 2 => []]], $data);
  134. }
  135. public function testBuildDirectoryStructure()
  136. {
  137. $s = \DIRECTORY_SEPARATOR;
  138. $method = new \ReflectionMethod(
  139. Builder::class,
  140. 'buildDirectoryStructure'
  141. );
  142. $method->setAccessible(true);
  143. $this->assertEquals(
  144. [
  145. 'src' => [
  146. 'Money.php/f' => [],
  147. 'MoneyBag.php/f' => [],
  148. 'Foo' => [
  149. 'Bar' => [
  150. 'Baz' => [
  151. 'Foo.php/f' => [],
  152. ],
  153. ],
  154. ],
  155. ]
  156. ],
  157. $method->invoke(
  158. $this->factory,
  159. [
  160. "src{$s}Money.php" => [],
  161. "src{$s}MoneyBag.php" => [],
  162. "src{$s}Foo{$s}Bar{$s}Baz{$s}Foo.php" => [],
  163. ]
  164. )
  165. );
  166. }
  167. /**
  168. * @dataProvider reducePathsProvider
  169. */
  170. public function testReducePaths($reducedPaths, $commonPath, $paths)
  171. {
  172. $method = new \ReflectionMethod(
  173. Builder::class,
  174. 'reducePaths'
  175. );
  176. $method->setAccessible(true);
  177. $_commonPath = $method->invokeArgs($this->factory, [&$paths]);
  178. $this->assertEquals($reducedPaths, $paths);
  179. $this->assertEquals($commonPath, $_commonPath);
  180. }
  181. public function reducePathsProvider()
  182. {
  183. $s = \DIRECTORY_SEPARATOR;
  184. yield [
  185. [],
  186. ".",
  187. []
  188. ];
  189. $prefixes = ["C:$s", "$s"];
  190. foreach($prefixes as $p){
  191. yield [
  192. [
  193. "Money.php" => []
  194. ],
  195. "{$p}home{$s}sb{$s}Money{$s}",
  196. [
  197. "{$p}home{$s}sb{$s}Money{$s}Money.php" => []
  198. ]
  199. ];
  200. yield [
  201. [
  202. "Money.php" => [],
  203. "MoneyBag.php" => []
  204. ],
  205. "{$p}home{$s}sb{$s}Money",
  206. [
  207. "{$p}home{$s}sb{$s}Money{$s}Money.php" => [],
  208. "{$p}home{$s}sb{$s}Money{$s}MoneyBag.php" => []
  209. ]
  210. ];
  211. yield [
  212. [
  213. "Money.php" => [],
  214. "MoneyBag.php" => [],
  215. "Cash.phar{$s}Cash.php" => [],
  216. ],
  217. "{$p}home{$s}sb{$s}Money",
  218. [
  219. "{$p}home{$s}sb{$s}Money{$s}Money.php" => [],
  220. "{$p}home{$s}sb{$s}Money{$s}MoneyBag.php" => [],
  221. "phar://{$p}home{$s}sb{$s}Money{$s}Cash.phar{$s}Cash.php" => [],
  222. ],
  223. ];
  224. }
  225. }
  226. }