TestCase.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /*
  3. * This file is part of the PHP_CodeCoverage 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. /**
  11. * Abstract base class for test case classes.
  12. *
  13. * @since Class available since Release 1.0.0
  14. */
  15. abstract class PHP_CodeCoverage_TestCase extends PHPUnit_Framework_TestCase
  16. {
  17. protected function getXdebugDataForBankAccount()
  18. {
  19. return array(
  20. array(
  21. TEST_FILES_PATH . 'BankAccount.php' => array(
  22. 8 => 1,
  23. 9 => -2,
  24. 13 => -1,
  25. 14 => -1,
  26. 15 => -1,
  27. 16 => -1,
  28. 18 => -1,
  29. 22 => -1,
  30. 24 => -1,
  31. 25 => -2,
  32. 29 => -1,
  33. 31 => -1,
  34. 32 => -2
  35. )
  36. ),
  37. array(
  38. TEST_FILES_PATH . 'BankAccount.php' => array(
  39. 8 => 1,
  40. 13 => 1,
  41. 16 => 1,
  42. 29 => 1,
  43. )
  44. ),
  45. array(
  46. TEST_FILES_PATH . 'BankAccount.php' => array(
  47. 8 => 1,
  48. 13 => 1,
  49. 16 => 1,
  50. 22 => 1,
  51. )
  52. ),
  53. array(
  54. TEST_FILES_PATH . 'BankAccount.php' => array(
  55. 8 => 1,
  56. 13 => 1,
  57. 14 => 1,
  58. 15 => 1,
  59. 18 => 1,
  60. 22 => 1,
  61. 24 => 1,
  62. 29 => 1,
  63. 31 => 1,
  64. )
  65. )
  66. );
  67. }
  68. protected function getCoverageForBankAccount()
  69. {
  70. $data = $this->getXdebugDataForBankAccount();
  71. $stub = $this->getMock('PHP_CodeCoverage_Driver_Xdebug');
  72. $stub->expects($this->any())
  73. ->method('stop')
  74. ->will($this->onConsecutiveCalls(
  75. $data[0],
  76. $data[1],
  77. $data[2],
  78. $data[3]
  79. ));
  80. $coverage = new PHP_CodeCoverage($stub, new PHP_CodeCoverage_Filter);
  81. $coverage->start(
  82. new BankAccountTest('testBalanceIsInitiallyZero'),
  83. true
  84. );
  85. $coverage->stop(
  86. true,
  87. array(TEST_FILES_PATH . 'BankAccount.php' => range(6, 9))
  88. );
  89. $coverage->start(
  90. new BankAccountTest('testBalanceCannotBecomeNegative')
  91. );
  92. $coverage->stop(
  93. true,
  94. array(TEST_FILES_PATH . 'BankAccount.php' => range(27, 32))
  95. );
  96. $coverage->start(
  97. new BankAccountTest('testBalanceCannotBecomeNegative2')
  98. );
  99. $coverage->stop(
  100. true,
  101. array(TEST_FILES_PATH . 'BankAccount.php' => range(20, 25))
  102. );
  103. $coverage->start(
  104. new BankAccountTest('testDepositWithdrawMoney')
  105. );
  106. $coverage->stop(
  107. true,
  108. array(
  109. TEST_FILES_PATH . 'BankAccount.php' => array_merge(
  110. range(6, 9),
  111. range(20, 25),
  112. range(27, 32)
  113. )
  114. )
  115. );
  116. return $coverage;
  117. }
  118. protected function getCoverageForBankAccountForFirstTwoTests()
  119. {
  120. $data = $this->getXdebugDataForBankAccount();
  121. $stub = $this->getMock('PHP_CodeCoverage_Driver_Xdebug');
  122. $stub->expects($this->any())
  123. ->method('stop')
  124. ->will($this->onConsecutiveCalls(
  125. $data[0],
  126. $data[1]
  127. ));
  128. $coverage = new PHP_CodeCoverage($stub, new PHP_CodeCoverage_Filter);
  129. $coverage->start(
  130. new BankAccountTest('testBalanceIsInitiallyZero'),
  131. true
  132. );
  133. $coverage->stop(
  134. true,
  135. array(TEST_FILES_PATH . 'BankAccount.php' => range(6, 9))
  136. );
  137. $coverage->start(
  138. new BankAccountTest('testBalanceCannotBecomeNegative')
  139. );
  140. $coverage->stop(
  141. true,
  142. array(TEST_FILES_PATH . 'BankAccount.php' => range(27, 32))
  143. );
  144. return $coverage;
  145. }
  146. protected function getCoverageForBankAccountForLastTwoTests()
  147. {
  148. $data = $this->getXdebugDataForBankAccount();
  149. $stub = $this->getMock('PHP_CodeCoverage_Driver_Xdebug');
  150. $stub->expects($this->any())
  151. ->method('stop')
  152. ->will($this->onConsecutiveCalls(
  153. $data[2],
  154. $data[3]
  155. ));
  156. $coverage = new PHP_CodeCoverage($stub, new PHP_CodeCoverage_Filter);
  157. $coverage->start(
  158. new BankAccountTest('testBalanceCannotBecomeNegative2')
  159. );
  160. $coverage->stop(
  161. true,
  162. array(TEST_FILES_PATH . 'BankAccount.php' => range(20, 25))
  163. );
  164. $coverage->start(
  165. new BankAccountTest('testDepositWithdrawMoney')
  166. );
  167. $coverage->stop(
  168. true,
  169. array(
  170. TEST_FILES_PATH . 'BankAccount.php' => array_merge(
  171. range(6, 9),
  172. range(20, 25),
  173. range(27, 32)
  174. )
  175. )
  176. );
  177. return $coverage;
  178. }
  179. protected function getExpectedDataArrayForBankAccount()
  180. {
  181. return array(
  182. TEST_FILES_PATH . 'BankAccount.php' => array(
  183. 8 => array(
  184. 0 => 'BankAccountTest::testBalanceIsInitiallyZero',
  185. 1 => 'BankAccountTest::testDepositWithdrawMoney'
  186. ),
  187. 9 => null,
  188. 13 => array(),
  189. 14 => array(),
  190. 15 => array(),
  191. 16 => array(),
  192. 18 => array(),
  193. 22 => array(
  194. 0 => 'BankAccountTest::testBalanceCannotBecomeNegative2',
  195. 1 => 'BankAccountTest::testDepositWithdrawMoney'
  196. ),
  197. 24 => array(
  198. 0 => 'BankAccountTest::testDepositWithdrawMoney',
  199. ),
  200. 25 => null,
  201. 29 => array(
  202. 0 => 'BankAccountTest::testBalanceCannotBecomeNegative',
  203. 1 => 'BankAccountTest::testDepositWithdrawMoney'
  204. ),
  205. 31 => array(
  206. 0 => 'BankAccountTest::testDepositWithdrawMoney'
  207. ),
  208. 32 => null
  209. )
  210. );
  211. }
  212. protected function getCoverageForFileWithIgnoredLines()
  213. {
  214. $coverage = new PHP_CodeCoverage(
  215. $this->setUpXdebugStubForFileWithIgnoredLines(),
  216. new PHP_CodeCoverage_Filter
  217. );
  218. $coverage->start('FileWithIgnoredLines', true);
  219. $coverage->stop();
  220. return $coverage;
  221. }
  222. protected function setUpXdebugStubForFileWithIgnoredLines()
  223. {
  224. $stub = $this->getMock('PHP_CodeCoverage_Driver_Xdebug');
  225. $stub->expects($this->any())
  226. ->method('stop')
  227. ->will($this->returnValue(
  228. array(
  229. TEST_FILES_PATH . 'source_with_ignore.php' => array(
  230. 2 => 1,
  231. 4 => -1,
  232. 6 => -1,
  233. 7 => 1
  234. )
  235. )
  236. ));
  237. return $stub;
  238. }
  239. protected function getCoverageForClassWithAnonymousFunction()
  240. {
  241. $coverage = new PHP_CodeCoverage(
  242. $this->setUpXdebugStubForClassWithAnonymousFunction(),
  243. new PHP_CodeCoverage_Filter
  244. );
  245. $coverage->start('ClassWithAnonymousFunction', true);
  246. $coverage->stop();
  247. return $coverage;
  248. }
  249. protected function setUpXdebugStubForClassWithAnonymousFunction()
  250. {
  251. $stub = $this->getMock('PHP_CodeCoverage_Driver_Xdebug');
  252. $stub->expects($this->any())
  253. ->method('stop')
  254. ->will($this->returnValue(
  255. array(
  256. TEST_FILES_PATH . 'source_with_class_and_anonymous_function.php' => array(
  257. 7 => 1,
  258. 9 => 1,
  259. 10 => -1,
  260. 11 => 1,
  261. 12 => 1,
  262. 13 => 1,
  263. 14 => 1,
  264. 17 => 1,
  265. 18 => 1
  266. )
  267. )
  268. ));
  269. return $stub;
  270. }
  271. }