TestCase.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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;
  11. use SebastianBergmann\CodeCoverage\Driver\Driver;
  12. use SebastianBergmann\CodeCoverage\Report\Xml\Coverage;
  13. abstract class TestCase extends \PHPUnit\Framework\TestCase
  14. {
  15. protected static $TEST_TMP_PATH;
  16. public static function setUpBeforeClass(): void
  17. {
  18. self::$TEST_TMP_PATH = TEST_FILES_PATH . 'tmp';
  19. }
  20. protected function getXdebugDataForBankAccount()
  21. {
  22. return [
  23. [
  24. TEST_FILES_PATH . 'BankAccount.php' => [
  25. 8 => 1,
  26. 9 => -2,
  27. 13 => -1,
  28. 14 => -1,
  29. 15 => -1,
  30. 16 => -1,
  31. 18 => -1,
  32. 22 => -1,
  33. 24 => -1,
  34. 25 => -2,
  35. 29 => -1,
  36. 31 => -1,
  37. 32 => -2
  38. ]
  39. ],
  40. [
  41. TEST_FILES_PATH . 'BankAccount.php' => [
  42. 8 => 1,
  43. 13 => 1,
  44. 16 => 1,
  45. 29 => 1,
  46. ]
  47. ],
  48. [
  49. TEST_FILES_PATH . 'BankAccount.php' => [
  50. 8 => 1,
  51. 13 => 1,
  52. 16 => 1,
  53. 22 => 1,
  54. ]
  55. ],
  56. [
  57. TEST_FILES_PATH . 'BankAccount.php' => [
  58. 8 => 1,
  59. 13 => 1,
  60. 14 => 1,
  61. 15 => 1,
  62. 18 => 1,
  63. 22 => 1,
  64. 24 => 1,
  65. 29 => 1,
  66. 31 => 1,
  67. ]
  68. ]
  69. ];
  70. }
  71. protected function getCoverageForBankAccount(): CodeCoverage
  72. {
  73. $data = $this->getXdebugDataForBankAccount();
  74. $stub = $this->createMock(Driver::class);
  75. $stub->expects($this->any())
  76. ->method('stop')
  77. ->will($this->onConsecutiveCalls(
  78. $data[0],
  79. $data[1],
  80. $data[2],
  81. $data[3]
  82. ));
  83. $filter = new Filter;
  84. $filter->addFileToWhitelist(TEST_FILES_PATH . 'BankAccount.php');
  85. $coverage = new CodeCoverage($stub, $filter);
  86. $coverage->start(
  87. new \BankAccountTest('testBalanceIsInitiallyZero'),
  88. true
  89. );
  90. $coverage->stop(
  91. true,
  92. [TEST_FILES_PATH . 'BankAccount.php' => range(6, 9)]
  93. );
  94. $coverage->start(
  95. new \BankAccountTest('testBalanceCannotBecomeNegative')
  96. );
  97. $coverage->stop(
  98. true,
  99. [TEST_FILES_PATH . 'BankAccount.php' => range(27, 32)]
  100. );
  101. $coverage->start(
  102. new \BankAccountTest('testBalanceCannotBecomeNegative2')
  103. );
  104. $coverage->stop(
  105. true,
  106. [TEST_FILES_PATH . 'BankAccount.php' => range(20, 25)]
  107. );
  108. $coverage->start(
  109. new \BankAccountTest('testDepositWithdrawMoney')
  110. );
  111. $coverage->stop(
  112. true,
  113. [
  114. TEST_FILES_PATH . 'BankAccount.php' => array_merge(
  115. range(6, 9),
  116. range(20, 25),
  117. range(27, 32)
  118. )
  119. ]
  120. );
  121. return $coverage;
  122. }
  123. protected function getCoverageForBankAccountForFirstTwoTests(): CodeCoverage
  124. {
  125. $data = $this->getXdebugDataForBankAccount();
  126. $stub = $this->createMock(Driver::class);
  127. $stub->expects($this->any())
  128. ->method('stop')
  129. ->will($this->onConsecutiveCalls(
  130. $data[0],
  131. $data[1]
  132. ));
  133. $filter = new Filter;
  134. $filter->addFileToWhitelist(TEST_FILES_PATH . 'BankAccount.php');
  135. $coverage = new CodeCoverage($stub, $filter);
  136. $coverage->start(
  137. new \BankAccountTest('testBalanceIsInitiallyZero'),
  138. true
  139. );
  140. $coverage->stop(
  141. true,
  142. [TEST_FILES_PATH . 'BankAccount.php' => range(6, 9)]
  143. );
  144. $coverage->start(
  145. new \BankAccountTest('testBalanceCannotBecomeNegative')
  146. );
  147. $coverage->stop(
  148. true,
  149. [TEST_FILES_PATH . 'BankAccount.php' => range(27, 32)]
  150. );
  151. return $coverage;
  152. }
  153. protected function getCoverageForBankAccountForLastTwoTests()
  154. {
  155. $data = $this->getXdebugDataForBankAccount();
  156. $stub = $this->createMock(Driver::class);
  157. $stub->expects($this->any())
  158. ->method('stop')
  159. ->will($this->onConsecutiveCalls(
  160. $data[2],
  161. $data[3]
  162. ));
  163. $filter = new Filter;
  164. $filter->addFileToWhitelist(TEST_FILES_PATH . 'BankAccount.php');
  165. $coverage = new CodeCoverage($stub, $filter);
  166. $coverage->start(
  167. new \BankAccountTest('testBalanceCannotBecomeNegative2')
  168. );
  169. $coverage->stop(
  170. true,
  171. [TEST_FILES_PATH . 'BankAccount.php' => range(20, 25)]
  172. );
  173. $coverage->start(
  174. new \BankAccountTest('testDepositWithdrawMoney')
  175. );
  176. $coverage->stop(
  177. true,
  178. [
  179. TEST_FILES_PATH . 'BankAccount.php' => array_merge(
  180. range(6, 9),
  181. range(20, 25),
  182. range(27, 32)
  183. )
  184. ]
  185. );
  186. return $coverage;
  187. }
  188. protected function getExpectedDataArrayForBankAccount(): array
  189. {
  190. return [
  191. TEST_FILES_PATH . 'BankAccount.php' => [
  192. 8 => [
  193. 0 => 'BankAccountTest::testBalanceIsInitiallyZero',
  194. 1 => 'BankAccountTest::testDepositWithdrawMoney'
  195. ],
  196. 9 => null,
  197. 13 => [],
  198. 14 => [],
  199. 15 => [],
  200. 16 => [],
  201. 18 => [],
  202. 22 => [
  203. 0 => 'BankAccountTest::testBalanceCannotBecomeNegative2',
  204. 1 => 'BankAccountTest::testDepositWithdrawMoney'
  205. ],
  206. 24 => [
  207. 0 => 'BankAccountTest::testDepositWithdrawMoney',
  208. ],
  209. 25 => null,
  210. 29 => [
  211. 0 => 'BankAccountTest::testBalanceCannotBecomeNegative',
  212. 1 => 'BankAccountTest::testDepositWithdrawMoney'
  213. ],
  214. 31 => [
  215. 0 => 'BankAccountTest::testDepositWithdrawMoney'
  216. ],
  217. 32 => null
  218. ]
  219. ];
  220. }
  221. protected function getExpectedDataArrayForBankAccountInReverseOrder(): array
  222. {
  223. return [
  224. TEST_FILES_PATH . 'BankAccount.php' => [
  225. 8 => [
  226. 0 => 'BankAccountTest::testDepositWithdrawMoney',
  227. 1 => 'BankAccountTest::testBalanceIsInitiallyZero'
  228. ],
  229. 9 => null,
  230. 13 => [],
  231. 14 => [],
  232. 15 => [],
  233. 16 => [],
  234. 18 => [],
  235. 22 => [
  236. 0 => 'BankAccountTest::testBalanceCannotBecomeNegative2',
  237. 1 => 'BankAccountTest::testDepositWithdrawMoney'
  238. ],
  239. 24 => [
  240. 0 => 'BankAccountTest::testDepositWithdrawMoney',
  241. ],
  242. 25 => null,
  243. 29 => [
  244. 0 => 'BankAccountTest::testDepositWithdrawMoney',
  245. 1 => 'BankAccountTest::testBalanceCannotBecomeNegative'
  246. ],
  247. 31 => [
  248. 0 => 'BankAccountTest::testDepositWithdrawMoney'
  249. ],
  250. 32 => null
  251. ]
  252. ];
  253. }
  254. protected function getCoverageForFileWithIgnoredLines(): CodeCoverage
  255. {
  256. $filter = new Filter;
  257. $filter->addFileToWhitelist(TEST_FILES_PATH . 'source_with_ignore.php');
  258. $coverage = new CodeCoverage(
  259. $this->setUpXdebugStubForFileWithIgnoredLines(),
  260. $filter
  261. );
  262. $coverage->start('FileWithIgnoredLines', true);
  263. $coverage->stop();
  264. return $coverage;
  265. }
  266. protected function setUpXdebugStubForFileWithIgnoredLines(): Driver
  267. {
  268. $stub = $this->createMock(Driver::class);
  269. $stub->expects($this->any())
  270. ->method('stop')
  271. ->will($this->returnValue(
  272. [
  273. TEST_FILES_PATH . 'source_with_ignore.php' => [
  274. 2 => 1,
  275. 4 => -1,
  276. 6 => -1,
  277. 7 => 1
  278. ]
  279. ]
  280. ));
  281. return $stub;
  282. }
  283. protected function getCoverageForClassWithAnonymousFunction(): CodeCoverage
  284. {
  285. $filter = new Filter;
  286. $filter->addFileToWhitelist(TEST_FILES_PATH . 'source_with_class_and_anonymous_function.php');
  287. $coverage = new CodeCoverage(
  288. $this->setUpXdebugStubForClassWithAnonymousFunction(),
  289. $filter
  290. );
  291. $coverage->start('ClassWithAnonymousFunction', true);
  292. $coverage->stop();
  293. return $coverage;
  294. }
  295. protected function setUpXdebugStubForClassWithAnonymousFunction(): Driver
  296. {
  297. $stub = $this->createMock(Driver::class);
  298. $stub->expects($this->any())
  299. ->method('stop')
  300. ->will($this->returnValue(
  301. [
  302. TEST_FILES_PATH . 'source_with_class_and_anonymous_function.php' => [
  303. 7 => 1,
  304. 9 => 1,
  305. 10 => -1,
  306. 11 => 1,
  307. 12 => 1,
  308. 13 => 1,
  309. 14 => 1,
  310. 17 => 1,
  311. 18 => 1
  312. ]
  313. ]
  314. ));
  315. return $stub;
  316. }
  317. protected function getCoverageForCrashParsing(): CodeCoverage
  318. {
  319. $filter = new Filter;
  320. $filter->addFileToWhitelist(TEST_FILES_PATH . 'Crash.php');
  321. // This is a file with invalid syntax, so it isn't executed.
  322. return new CodeCoverage(
  323. $this->setUpXdebugStubForCrashParsing(),
  324. $filter
  325. );
  326. }
  327. protected function setUpXdebugStubForCrashParsing(): Driver
  328. {
  329. $stub = $this->createMock(Driver::class);
  330. $stub->expects($this->any())
  331. ->method('stop')
  332. ->will($this->returnValue([]));
  333. return $stub;
  334. }
  335. }