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