PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Tests/PHP/CodeCoverage/UtilTest.php

https://github.com/sumitk1/php-code-coverage
PHP | 381 lines | 255 code | 27 blank | 99 comment | 13 complexity | 19e97e856b39b287b123baceaeb3c409 MD5 | raw file
  1. <?php
  2. /**
  3. * PHP_CodeCoverage
  4. *
  5. * Copyright (c) 2009-2011, Sebastian Bergmann <sb@sebastian-bergmann.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @category PHP
  38. * @package CodeCoverage
  39. * @subpackage Tests
  40. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  41. * @copyright 2009-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
  42. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  43. * @link http://github.com/sebastianbergmann/php-code-coverage
  44. * @since File available since Release 1.0.0
  45. */
  46. @include_once 'vfsStream/vfsStream.php';
  47. if (!defined('TEST_FILES_PATH')) {
  48. define(
  49. 'TEST_FILES_PATH',
  50. dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR .
  51. '_files' . DIRECTORY_SEPARATOR
  52. );
  53. }
  54. require_once TEST_FILES_PATH . 'CoverageClassExtendedTest.php';
  55. require_once TEST_FILES_PATH . 'CoverageClassTest.php';
  56. require_once TEST_FILES_PATH . 'CoverageFunctionTest.php';
  57. require_once TEST_FILES_PATH . 'CoverageMethodTest.php';
  58. require_once TEST_FILES_PATH . 'CoverageNoneTest.php';
  59. require_once TEST_FILES_PATH . 'CoverageNotPrivateTest.php';
  60. require_once TEST_FILES_PATH . 'CoverageNotProtectedTest.php';
  61. require_once TEST_FILES_PATH . 'CoverageNotPublicTest.php';
  62. require_once TEST_FILES_PATH . 'CoveragePrivateTest.php';
  63. require_once TEST_FILES_PATH . 'CoverageProtectedTest.php';
  64. require_once TEST_FILES_PATH . 'CoveragePublicTest.php';
  65. require_once TEST_FILES_PATH . 'CoveredClass.php';
  66. require_once TEST_FILES_PATH . 'CoveredFunction.php';
  67. require_once TEST_FILES_PATH . 'NotExistingCoveredElementTest.php';
  68. if (version_compare(PHP_VERSION, '5.3', '>')) {
  69. require_once TEST_FILES_PATH . 'NamespaceCoverageClassExtendedTest.php';
  70. require_once TEST_FILES_PATH . 'NamespaceCoverageClassTest.php';
  71. require_once TEST_FILES_PATH . 'NamespaceCoverageMethodTest.php';
  72. require_once TEST_FILES_PATH . 'NamespaceCoverageNotPrivateTest.php';
  73. require_once TEST_FILES_PATH . 'NamespaceCoverageNotProtectedTest.php';
  74. require_once TEST_FILES_PATH . 'NamespaceCoverageNotPublicTest.php';
  75. require_once TEST_FILES_PATH . 'NamespaceCoveragePrivateTest.php';
  76. require_once TEST_FILES_PATH . 'NamespaceCoverageProtectedTest.php';
  77. require_once TEST_FILES_PATH . 'NamespaceCoveragePublicTest.php';
  78. require_once TEST_FILES_PATH . 'NamespaceCoveredClass.php';
  79. }
  80. /**
  81. * Tests for the PHP_CodeCoverage_Util class.
  82. *
  83. * @category PHP
  84. * @package CodeCoverage
  85. * @subpackage Tests
  86. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  87. * @copyright 2009-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
  88. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  89. * @version Release: @package_version@
  90. * @link http://github.com/sebastianbergmann/php-code-coverage
  91. * @since Class available since Release 1.0.0
  92. */
  93. class PHP_CodeCoverage_UtilTest extends PHPUnit_Framework_TestCase
  94. {
  95. protected function setUp()
  96. {
  97. if (!class_exists('vfsStream')) {
  98. $this->markTestSkipped('vfsStream is not available.');
  99. }
  100. vfsStream::setup('UtilTest');
  101. }
  102. /**
  103. * @covers PHP_CodeCoverage_Util::crap
  104. */
  105. public function testCrap()
  106. {
  107. $this->assertEquals(6, PHP_CodeCoverage_Util::crap(2, 0));
  108. $this->assertEquals(2, PHP_CodeCoverage_Util::crap(2, 95));
  109. $this->assertEquals(2.5, PHP_CodeCoverage_Util::crap(2, 50));
  110. }
  111. /**
  112. * @covers PHP_CodeCoverage_Util::getLinesToBeCovered
  113. * @covers PHP_CodeCoverage_Util::resolveCoversToReflectionObjects
  114. * @dataProvider getLinesToBeCoveredProvider
  115. */
  116. public function testGetLinesToBeCovered($test, $lines)
  117. {
  118. if (strpos($test, 'Namespace') === 0) {
  119. if (!version_compare(PHP_VERSION, '5.3', '>')) {
  120. $this->markTestSkipped('PHP 5.3 (or later) is required.');
  121. }
  122. $expected = array(
  123. TEST_FILES_PATH . 'NamespaceCoveredClass.php' => $lines
  124. );
  125. }
  126. else if ($test === 'CoverageNoneTest') {
  127. $expected = array();
  128. }
  129. else if ($test === 'CoverageFunctionTest') {
  130. $expected = array(
  131. TEST_FILES_PATH . 'CoveredFunction.php' => $lines
  132. );
  133. }
  134. else {
  135. $expected = array(TEST_FILES_PATH . 'CoveredClass.php' => $lines);
  136. }
  137. $this->assertEquals(
  138. $expected,
  139. PHP_CodeCoverage_Util::getLinesToBeCovered(
  140. $test, 'testSomething'
  141. )
  142. );
  143. }
  144. /**
  145. * @covers PHP_CodeCoverage_Util::getLinesToBeCovered
  146. * @covers PHP_CodeCoverage_Util::resolveCoversToReflectionObjects
  147. * @expectedException PHP_CodeCoverage_Exception
  148. */
  149. public function testGetLinesToBeCovered2()
  150. {
  151. PHP_CodeCoverage_Util::getLinesToBeCovered(
  152. 'NotExistingCoveredElementTest', 'testOne'
  153. );
  154. }
  155. /**
  156. * @covers PHP_CodeCoverage_Util::getLinesToBeCovered
  157. * @covers PHP_CodeCoverage_Util::resolveCoversToReflectionObjects
  158. * @expectedException PHP_CodeCoverage_Exception
  159. */
  160. public function testGetLinesToBeCovered3()
  161. {
  162. PHP_CodeCoverage_Util::getLinesToBeCovered(
  163. 'NotExistingCoveredElementTest', 'testTwo'
  164. );
  165. }
  166. /**
  167. * @covers PHP_CodeCoverage_Util::getLinesToBeCovered
  168. * @covers PHP_CodeCoverage_Util::resolveCoversToReflectionObjects
  169. * @expectedException PHP_CodeCoverage_Exception
  170. */
  171. public function testGetLinesToBeCovered4()
  172. {
  173. PHP_CodeCoverage_Util::getLinesToBeCovered(
  174. 'NotExistingCoveredElementTest', 'testThree'
  175. );
  176. }
  177. /**
  178. * @covers PHP_CodeCoverage_Util::getLinesToBeIgnored
  179. */
  180. public function testGetLinesToBeIgnored()
  181. {
  182. $this->assertEquals(
  183. array(
  184. 3 => TRUE,
  185. 4 => TRUE,
  186. 5 => TRUE,
  187. 11 => TRUE,
  188. 12 => TRUE,
  189. 13 => TRUE,
  190. 14 => TRUE,
  191. 15 => TRUE,
  192. 16 => TRUE,
  193. 18 => TRUE,
  194. 19 => TRUE,
  195. 20 => TRUE,
  196. 21 => TRUE,
  197. 22 => TRUE,
  198. 23 => TRUE,
  199. 24 => TRUE,
  200. 25 => TRUE
  201. ),
  202. PHP_CodeCoverage_Util::getLinesToBeIgnored(
  203. TEST_FILES_PATH . 'source_with_ignore.php'
  204. )
  205. );
  206. }
  207. /**
  208. * @covers PHP_CodeCoverage_Util::getLinesToBeIgnored
  209. */
  210. public function testGetLinesToBeIgnored2()
  211. {
  212. $this->assertEquals(
  213. array(),
  214. PHP_CodeCoverage_Util::getLinesToBeIgnored(
  215. TEST_FILES_PATH . 'source_without_ignore.php'
  216. )
  217. );
  218. }
  219. /**
  220. * @covers PHP_CodeCoverage_Util::getDirectory
  221. */
  222. public function testGetDirectory()
  223. {
  224. if (!class_exists('vfsStream')) {
  225. $this->markTestSkipped('vfsStream is not installed');
  226. }
  227. $this->assertEquals(
  228. vfsStream::url('UtilTest') . '/',
  229. PHP_CodeCoverage_Util::getDirectory(vfsStream::url('UtilTest'))
  230. );
  231. }
  232. /**
  233. * @covers PHP_CodeCoverage_Util::getDirectory
  234. */
  235. public function testGetDirectory2()
  236. {
  237. if (!class_exists('vfsStream')) {
  238. $this->markTestSkipped('vfsStream is not installed');
  239. }
  240. PHP_CodeCoverage_Util::getDirectory(
  241. vfsStream::url('UtilTest') . '/report'
  242. );
  243. $this->assertTrue(vfsStreamWrapper::getRoot()->hasChild('report'));
  244. }
  245. /**
  246. * @covers PHP_CodeCoverage_Util::getDirectory
  247. * @expectedException PHP_CodeCoverage_Exception
  248. */
  249. public function testGetDirectory3()
  250. {
  251. if (!class_exists('vfsStream')) {
  252. $this->markTestSkipped('vfsStream is not installed');
  253. }
  254. PHP_CodeCoverage_Util::getDirectory(
  255. vfsStream::url('/not/existing/path')
  256. );
  257. }
  258. /**
  259. * @covers PHP_CodeCoverage_Util::percent
  260. */
  261. public function testPercent()
  262. {
  263. $this->assertEquals(100, PHP_CodeCoverage_Util::percent(100, 0));
  264. $this->assertEquals(100, PHP_CodeCoverage_Util::percent(100, 100));
  265. $this->assertEquals(
  266. '100.00%', PHP_CodeCoverage_Util::percent(100, 100, TRUE)
  267. );
  268. }
  269. public function getLinesToBeCoveredProvider()
  270. {
  271. return array(
  272. array(
  273. 'CoverageNoneTest',
  274. array()
  275. ),
  276. array(
  277. 'CoverageClassExtendedTest',
  278. array_merge(range(19, 36), range(2, 17))
  279. ),
  280. array(
  281. 'CoverageClassTest',
  282. range(19, 36)
  283. ),
  284. array(
  285. 'CoverageMethodTest',
  286. range(31, 35)
  287. ),
  288. array(
  289. 'CoverageNotPrivateTest',
  290. array_merge(range(25, 29), range(31, 35))
  291. ),
  292. array(
  293. 'CoverageNotProtectedTest',
  294. array_merge(range(21, 23), range(31, 35))
  295. ),
  296. array(
  297. 'CoverageNotPublicTest',
  298. array_merge(range(21, 23), range(25, 29))
  299. ),
  300. array(
  301. 'CoveragePrivateTest',
  302. range(21, 23)
  303. ),
  304. array(
  305. 'CoverageProtectedTest',
  306. range(25, 29)
  307. ),
  308. array(
  309. 'CoveragePublicTest',
  310. range(31, 35)
  311. ),
  312. array(
  313. 'CoverageFunctionTest',
  314. range(2, 4)
  315. ),
  316. array(
  317. 'NamespaceCoverageClassExtendedTest',
  318. array_merge(range(21, 38), range(4, 19))
  319. ),
  320. array(
  321. 'NamespaceCoverageClassTest',
  322. range(21, 38)
  323. ),
  324. array(
  325. 'NamespaceCoverageMethodTest',
  326. range(33, 37)
  327. ),
  328. array(
  329. 'NamespaceCoverageNotPrivateTest',
  330. array_merge(range(27, 31), range(33, 37))
  331. ),
  332. array(
  333. 'NamespaceCoverageNotProtectedTest',
  334. array_merge(range(23, 25), range(33, 37))
  335. ),
  336. array(
  337. 'NamespaceCoverageNotPublicTest',
  338. array_merge(range(23, 25), range(27, 31))
  339. ),
  340. array(
  341. 'NamespaceCoveragePrivateTest',
  342. range(23, 25)
  343. ),
  344. array(
  345. 'NamespaceCoverageProtectedTest',
  346. range(27, 31)
  347. ),
  348. array(
  349. 'NamespaceCoveragePublicTest',
  350. range(33, 37)
  351. )
  352. );
  353. }
  354. }