CodeCoverageTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. if (!defined('TEST_FILES_PATH')) {
  11. define(
  12. 'TEST_FILES_PATH',
  13. dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR .
  14. '_files' . DIRECTORY_SEPARATOR
  15. );
  16. }
  17. require_once TEST_FILES_PATH . '../TestCase.php';
  18. require_once TEST_FILES_PATH . 'BankAccount.php';
  19. require_once TEST_FILES_PATH . 'BankAccountTest.php';
  20. /**
  21. * Tests for the PHP_CodeCoverage class.
  22. *
  23. * @since Class available since Release 1.0.0
  24. */
  25. class PHP_CodeCoverageTest extends PHP_CodeCoverage_TestCase
  26. {
  27. /**
  28. * @var PHP_CodeCoverage
  29. */
  30. private $coverage;
  31. protected function setUp()
  32. {
  33. $this->coverage = new PHP_CodeCoverage;
  34. }
  35. /**
  36. * @covers PHP_CodeCoverage::__construct
  37. * @covers PHP_CodeCoverage::filter
  38. */
  39. public function testConstructor()
  40. {
  41. $this->assertAttributeInstanceOf(
  42. 'PHP_CodeCoverage_Driver_Xdebug',
  43. 'driver',
  44. $this->coverage
  45. );
  46. $this->assertAttributeInstanceOf(
  47. 'PHP_CodeCoverage_Filter',
  48. 'filter',
  49. $this->coverage
  50. );
  51. }
  52. /**
  53. * @covers PHP_CodeCoverage::__construct
  54. * @covers PHP_CodeCoverage::filter
  55. */
  56. public function testConstructor2()
  57. {
  58. $filter = new PHP_CodeCoverage_Filter;
  59. $coverage = new PHP_CodeCoverage(null, $filter);
  60. $this->assertAttributeInstanceOf(
  61. 'PHP_CodeCoverage_Driver_Xdebug',
  62. 'driver',
  63. $coverage
  64. );
  65. $this->assertSame($filter, $coverage->filter());
  66. }
  67. /**
  68. * @covers PHP_CodeCoverage::start
  69. * @expectedException PHP_CodeCoverage_Exception
  70. */
  71. public function testStartThrowsExceptionForInvalidArgument()
  72. {
  73. $this->coverage->start(null, array(), null);
  74. }
  75. /**
  76. * @covers PHP_CodeCoverage::stop
  77. * @expectedException PHP_CodeCoverage_Exception
  78. */
  79. public function testStopThrowsExceptionForInvalidArgument()
  80. {
  81. $this->coverage->stop(null);
  82. }
  83. /**
  84. * @covers PHP_CodeCoverage::stop
  85. * @expectedException PHP_CodeCoverage_Exception
  86. */
  87. public function testStopThrowsExceptionForInvalidArgument2()
  88. {
  89. $this->coverage->stop(true, null);
  90. }
  91. /**
  92. * @covers PHP_CodeCoverage::append
  93. * @expectedException PHP_CodeCoverage_Exception
  94. */
  95. public function testAppendThrowsExceptionForInvalidArgument()
  96. {
  97. $this->coverage->append(array(), null);
  98. }
  99. /**
  100. * @covers PHP_CodeCoverage::setCacheTokens
  101. * @expectedException PHP_CodeCoverage_Exception
  102. */
  103. public function testSetCacheTokensThrowsExceptionForInvalidArgument()
  104. {
  105. $this->coverage->setCacheTokens(null);
  106. }
  107. /**
  108. * @covers PHP_CodeCoverage::setCacheTokens
  109. */
  110. public function testSetCacheTokens()
  111. {
  112. $this->coverage->setCacheTokens(true);
  113. $this->assertAttributeEquals(true, 'cacheTokens', $this->coverage);
  114. }
  115. /**
  116. * @covers PHP_CodeCoverage::setCheckForUnintentionallyCoveredCode
  117. * @expectedException PHP_CodeCoverage_Exception
  118. */
  119. public function testSetCheckForUnintentionallyCoveredCodeThrowsExceptionForInvalidArgument()
  120. {
  121. $this->coverage->setCheckForUnintentionallyCoveredCode(null);
  122. }
  123. /**
  124. * @covers PHP_CodeCoverage::setCheckForUnintentionallyCoveredCode
  125. */
  126. public function testSetCheckForUnintentionallyCoveredCode()
  127. {
  128. $this->coverage->setCheckForUnintentionallyCoveredCode(true);
  129. $this->assertAttributeEquals(
  130. true,
  131. 'checkForUnintentionallyCoveredCode',
  132. $this->coverage
  133. );
  134. }
  135. /**
  136. * @covers PHP_CodeCoverage::setForceCoversAnnotation
  137. * @expectedException PHP_CodeCoverage_Exception
  138. */
  139. public function testSetForceCoversAnnotationThrowsExceptionForInvalidArgument()
  140. {
  141. $this->coverage->setForceCoversAnnotation(null);
  142. }
  143. /**
  144. * @covers PHP_CodeCoverage::setForceCoversAnnotation
  145. */
  146. public function testSetForceCoversAnnotation()
  147. {
  148. $this->coverage->setForceCoversAnnotation(true);
  149. $this->assertAttributeEquals(
  150. true,
  151. 'forceCoversAnnotation',
  152. $this->coverage
  153. );
  154. }
  155. /**
  156. * @covers PHP_CodeCoverage::setAddUncoveredFilesFromWhitelist
  157. * @expectedException PHP_CodeCoverage_Exception
  158. */
  159. public function testSetAddUncoveredFilesFromWhitelistThrowsExceptionForInvalidArgument()
  160. {
  161. $this->coverage->setAddUncoveredFilesFromWhitelist(null);
  162. }
  163. /**
  164. * @covers PHP_CodeCoverage::setAddUncoveredFilesFromWhitelist
  165. */
  166. public function testSetAddUncoveredFilesFromWhitelist()
  167. {
  168. $this->coverage->setAddUncoveredFilesFromWhitelist(true);
  169. $this->assertAttributeEquals(
  170. true,
  171. 'addUncoveredFilesFromWhitelist',
  172. $this->coverage
  173. );
  174. }
  175. /**
  176. * @covers PHP_CodeCoverage::setProcessUncoveredFilesFromWhitelist
  177. * @expectedException PHP_CodeCoverage_Exception
  178. */
  179. public function testSetProcessUncoveredFilesFromWhitelistThrowsExceptionForInvalidArgument()
  180. {
  181. $this->coverage->setProcessUncoveredFilesFromWhitelist(null);
  182. }
  183. /**
  184. * @covers PHP_CodeCoverage::setProcessUncoveredFilesFromWhitelist
  185. */
  186. public function testSetProcessUncoveredFilesFromWhitelist()
  187. {
  188. $this->coverage->setProcessUncoveredFilesFromWhitelist(true);
  189. $this->assertAttributeEquals(
  190. true,
  191. 'processUncoveredFilesFromWhitelist',
  192. $this->coverage
  193. );
  194. }
  195. /**
  196. * @covers PHP_CodeCoverage::setMapTestClassNameToCoveredClassName
  197. */
  198. public function testSetMapTestClassNameToCoveredClassName()
  199. {
  200. $this->coverage->setMapTestClassNameToCoveredClassName(true);
  201. $this->assertAttributeEquals(
  202. true,
  203. 'mapTestClassNameToCoveredClassName',
  204. $this->coverage
  205. );
  206. }
  207. /**
  208. * @covers PHP_CodeCoverage::setMapTestClassNameToCoveredClassName
  209. * @expectedException PHP_CodeCoverage_Exception
  210. */
  211. public function testSetMapTestClassNameToCoveredClassNameThrowsExceptionForInvalidArgument()
  212. {
  213. $this->coverage->setMapTestClassNameToCoveredClassName(null);
  214. }
  215. /**
  216. * @covers PHP_CodeCoverage::clear
  217. */
  218. public function testClear()
  219. {
  220. $this->coverage->clear();
  221. $this->assertAttributeEquals(null, 'currentId', $this->coverage);
  222. $this->assertAttributeEquals(array(), 'data', $this->coverage);
  223. $this->assertAttributeEquals(array(), 'tests', $this->coverage);
  224. }
  225. /**
  226. * @covers PHP_CodeCoverage::start
  227. * @covers PHP_CodeCoverage::stop
  228. * @covers PHP_CodeCoverage::append
  229. * @covers PHP_CodeCoverage::applyListsFilter
  230. * @covers PHP_CodeCoverage::initializeFilesThatAreSeenTheFirstTime
  231. * @covers PHP_CodeCoverage::applyCoversAnnotationFilter
  232. * @covers PHP_CodeCoverage::getTests
  233. */
  234. public function testCollect()
  235. {
  236. $coverage = $this->getCoverageForBankAccount();
  237. $this->assertEquals(
  238. $this->getExpectedDataArrayForBankAccount(),
  239. $coverage->getData()
  240. );
  241. if (version_compare(PHPUnit_Runner_Version::id(), '4.7', '>=')) {
  242. $size = 'unknown';
  243. } else {
  244. $size = 'small';
  245. }
  246. $this->assertEquals(
  247. array(
  248. 'BankAccountTest::testBalanceIsInitiallyZero' => array('size' => $size, 'status' => null),
  249. 'BankAccountTest::testBalanceCannotBecomeNegative' => array('size' => $size, 'status' => null),
  250. 'BankAccountTest::testBalanceCannotBecomeNegative2' => array('size' => $size, 'status' => null),
  251. 'BankAccountTest::testDepositWithdrawMoney' => array('size' => $size, 'status' => null)
  252. ),
  253. $coverage->getTests()
  254. );
  255. }
  256. /**
  257. * @covers PHP_CodeCoverage::getData
  258. * @covers PHP_CodeCoverage::merge
  259. */
  260. public function testMerge()
  261. {
  262. $coverage = $this->getCoverageForBankAccountForFirstTwoTests();
  263. $coverage->merge($this->getCoverageForBankAccountForLastTwoTests());
  264. $this->assertEquals(
  265. $this->getExpectedDataArrayForBankAccount(),
  266. $coverage->getData()
  267. );
  268. }
  269. /**
  270. * @covers PHP_CodeCoverage::getData
  271. * @covers PHP_CodeCoverage::merge
  272. */
  273. public function testMerge2()
  274. {
  275. $coverage = new PHP_CodeCoverage(
  276. $this->getMock('PHP_CodeCoverage_Driver_Xdebug'),
  277. new PHP_CodeCoverage_Filter
  278. );
  279. $coverage->merge($this->getCoverageForBankAccount());
  280. $this->assertEquals(
  281. $this->getExpectedDataArrayForBankAccount(),
  282. $coverage->getData()
  283. );
  284. }
  285. /**
  286. * @covers PHP_CodeCoverage::getLinesToBeIgnored
  287. */
  288. public function testGetLinesToBeIgnored()
  289. {
  290. $this->assertEquals(
  291. array(
  292. 1,
  293. 3,
  294. 4,
  295. 5,
  296. 7,
  297. 8,
  298. 9,
  299. 10,
  300. 11,
  301. 12,
  302. 13,
  303. 14,
  304. 15,
  305. 16,
  306. 17,
  307. 18,
  308. 19,
  309. 20,
  310. 21,
  311. 22,
  312. 23,
  313. 24,
  314. 25,
  315. 26,
  316. 27,
  317. 28,
  318. 30,
  319. 32,
  320. 33,
  321. 34,
  322. 35,
  323. 36,
  324. 37,
  325. 38
  326. ),
  327. $this->getLinesToBeIgnored()->invoke(
  328. $this->coverage,
  329. TEST_FILES_PATH . 'source_with_ignore.php'
  330. )
  331. );
  332. }
  333. /**
  334. * @covers PHP_CodeCoverage::getLinesToBeIgnored
  335. */
  336. public function testGetLinesToBeIgnored2()
  337. {
  338. $this->assertEquals(
  339. array(1, 5),
  340. $this->getLinesToBeIgnored()->invoke(
  341. $this->coverage,
  342. TEST_FILES_PATH . 'source_without_ignore.php'
  343. )
  344. );
  345. }
  346. /**
  347. * @covers PHP_CodeCoverage::getLinesToBeIgnored
  348. */
  349. public function testGetLinesToBeIgnored3()
  350. {
  351. $this->assertEquals(
  352. array(
  353. 1,
  354. 2,
  355. 3,
  356. 4,
  357. 5,
  358. 8,
  359. 11,
  360. 15,
  361. 16,
  362. 19,
  363. 20
  364. ),
  365. $this->getLinesToBeIgnored()->invoke(
  366. $this->coverage,
  367. TEST_FILES_PATH . 'source_with_class_and_anonymous_function.php'
  368. )
  369. );
  370. }
  371. /**
  372. * @covers PHP_CodeCoverage::getLinesToBeIgnored
  373. */
  374. public function testGetLinesToBeIgnoredOneLineAnnotations()
  375. {
  376. $this->assertEquals(
  377. array(
  378. 1,
  379. 2,
  380. 3,
  381. 4,
  382. 5,
  383. 6,
  384. 7,
  385. 8,
  386. 9,
  387. 10,
  388. 11,
  389. 12,
  390. 13,
  391. 14,
  392. 15,
  393. 16,
  394. 18,
  395. 20,
  396. 21,
  397. 23,
  398. 24,
  399. 25,
  400. 27,
  401. 28,
  402. 29,
  403. 30,
  404. 31,
  405. 32,
  406. 33,
  407. 34,
  408. 37
  409. ),
  410. $this->getLinesToBeIgnored()->invoke(
  411. $this->coverage,
  412. TEST_FILES_PATH . 'source_with_oneline_annotations.php'
  413. )
  414. );
  415. }
  416. /**
  417. * @return ReflectionMethod
  418. */
  419. private function getLinesToBeIgnored()
  420. {
  421. $getLinesToBeIgnored = new ReflectionMethod(
  422. 'PHP_CodeCoverage',
  423. 'getLinesToBeIgnored'
  424. );
  425. $getLinesToBeIgnored->setAccessible(true);
  426. return $getLinesToBeIgnored;
  427. }
  428. /**
  429. * @covers PHP_CodeCoverage::getLinesToBeIgnored
  430. */
  431. public function testGetLinesToBeIgnoredWhenIgnoreIsDisabled()
  432. {
  433. $this->coverage->setDisableIgnoredLines(true);
  434. $this->assertEquals(
  435. array(),
  436. $this->getLinesToBeIgnored()->invoke(
  437. $this->coverage,
  438. TEST_FILES_PATH . 'source_with_ignore.php'
  439. )
  440. );
  441. }
  442. }