BuilderTest.php 9.0 KB

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