Ei kuvausta

TestCase.php 8.2KB

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