TestCase.php 11 KB

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