/blog/vendor/phpunit/php-code-coverage/tests/PHP/CodeCoverageTest.php

https://gitlab.com/zan_zan/laravel_sample · PHP · 468 lines · 311 code · 43 blank · 114 comment · 3 complexity · baacb0102ec220756a664784248a931a MD5 · raw file

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