暂无描述

TestCase.php 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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\Xdebug;
  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(Xdebug::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(Xdebug::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(Xdebug::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 getCoverageForFileWithIgnoredLines()
  227. {
  228. $filter = new Filter;
  229. $filter->addFileToWhitelist(TEST_FILES_PATH . 'source_with_ignore.php');
  230. $coverage = new CodeCoverage(
  231. $this->setUpXdebugStubForFileWithIgnoredLines(),
  232. $filter
  233. );
  234. $coverage->start('FileWithIgnoredLines', true);
  235. $coverage->stop();
  236. return $coverage;
  237. }
  238. protected function setUpXdebugStubForFileWithIgnoredLines()
  239. {
  240. $stub = $this->createMock(Xdebug::class);
  241. $stub->expects($this->any())
  242. ->method('stop')
  243. ->will($this->returnValue(
  244. [
  245. TEST_FILES_PATH . 'source_with_ignore.php' => [
  246. 2 => 1,
  247. 4 => -1,
  248. 6 => -1,
  249. 7 => 1
  250. ]
  251. ]
  252. ));
  253. return $stub;
  254. }
  255. protected function getCoverageForClassWithAnonymousFunction()
  256. {
  257. $filter = new Filter;
  258. $filter->addFileToWhitelist(TEST_FILES_PATH . 'source_with_class_and_anonymous_function.php');
  259. $coverage = new CodeCoverage(
  260. $this->setUpXdebugStubForClassWithAnonymousFunction(),
  261. $filter
  262. );
  263. $coverage->start('ClassWithAnonymousFunction', true);
  264. $coverage->stop();
  265. return $coverage;
  266. }
  267. protected function setUpXdebugStubForClassWithAnonymousFunction()
  268. {
  269. $stub = $this->createMock(Xdebug::class);
  270. $stub->expects($this->any())
  271. ->method('stop')
  272. ->will($this->returnValue(
  273. [
  274. TEST_FILES_PATH . 'source_with_class_and_anonymous_function.php' => [
  275. 7 => 1,
  276. 9 => 1,
  277. 10 => -1,
  278. 11 => 1,
  279. 12 => 1,
  280. 13 => 1,
  281. 14 => 1,
  282. 17 => 1,
  283. 18 => 1
  284. ]
  285. ]
  286. ));
  287. return $stub;
  288. }
  289. }