TestCase.php (8746B)
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 /** 12 * Abstract base class for test case classes. 13 * 14 * @since Class available since Release 1.0.0 15 */ 16 abstract class PHP_CodeCoverage_TestCase extends PHPUnit_Framework_TestCase 17 { 18 protected function getXdebugDataForBankAccount() 19 { 20 return array( 21 array( 22 TEST_FILES_PATH . 'BankAccount.php' => array( 23 8 => 1, 24 9 => -2, 25 13 => -1, 26 14 => -1, 27 15 => -1, 28 16 => -1, 29 18 => -1, 30 22 => -1, 31 24 => -1, 32 25 => -2, 33 29 => -1, 34 31 => -1, 35 32 => -2 36 ) 37 ), 38 array( 39 TEST_FILES_PATH . 'BankAccount.php' => array( 40 8 => 1, 41 13 => 1, 42 16 => 1, 43 29 => 1, 44 ) 45 ), 46 array( 47 TEST_FILES_PATH . 'BankAccount.php' => array( 48 8 => 1, 49 13 => 1, 50 16 => 1, 51 22 => 1, 52 ) 53 ), 54 array( 55 TEST_FILES_PATH . 'BankAccount.php' => array( 56 8 => 1, 57 13 => 1, 58 14 => 1, 59 15 => 1, 60 18 => 1, 61 22 => 1, 62 24 => 1, 63 29 => 1, 64 31 => 1, 65 ) 66 ) 67 ); 68 } 69 70 protected function getCoverageForBankAccount() 71 { 72 $data = $this->getXdebugDataForBankAccount(); 73 74 $stub = $this->getMock('PHP_CodeCoverage_Driver_Xdebug'); 75 $stub->expects($this->any()) 76 ->method('stop') 77 ->will($this->onConsecutiveCalls( 78 $data[0], 79 $data[1], 80 $data[2], 81 $data[3] 82 )); 83 84 $coverage = new PHP_CodeCoverage($stub, new PHP_CodeCoverage_Filter); 85 86 $coverage->start( 87 new BankAccountTest('testBalanceIsInitiallyZero'), 88 true 89 ); 90 91 $coverage->stop( 92 true, 93 array(TEST_FILES_PATH . 'BankAccount.php' => range(6, 9)) 94 ); 95 96 $coverage->start( 97 new BankAccountTest('testBalanceCannotBecomeNegative') 98 ); 99 100 $coverage->stop( 101 true, 102 array(TEST_FILES_PATH . 'BankAccount.php' => range(27, 32)) 103 ); 104 105 $coverage->start( 106 new BankAccountTest('testBalanceCannotBecomeNegative2') 107 ); 108 109 $coverage->stop( 110 true, 111 array(TEST_FILES_PATH . 'BankAccount.php' => range(20, 25)) 112 ); 113 114 $coverage->start( 115 new BankAccountTest('testDepositWithdrawMoney') 116 ); 117 118 $coverage->stop( 119 true, 120 array( 121 TEST_FILES_PATH . 'BankAccount.php' => array_merge( 122 range(6, 9), 123 range(20, 25), 124 range(27, 32) 125 ) 126 ) 127 ); 128 129 return $coverage; 130 } 131 132 protected function getCoverageForBankAccountForFirstTwoTests() 133 { 134 $data = $this->getXdebugDataForBankAccount(); 135 136 $stub = $this->getMock('PHP_CodeCoverage_Driver_Xdebug'); 137 $stub->expects($this->any()) 138 ->method('stop') 139 ->will($this->onConsecutiveCalls( 140 $data[0], 141 $data[1] 142 )); 143 144 $coverage = new PHP_CodeCoverage($stub, new PHP_CodeCoverage_Filter); 145 146 $coverage->start( 147 new BankAccountTest('testBalanceIsInitiallyZero'), 148 true 149 ); 150 151 $coverage->stop( 152 true, 153 array(TEST_FILES_PATH . 'BankAccount.php' => range(6, 9)) 154 ); 155 156 $coverage->start( 157 new BankAccountTest('testBalanceCannotBecomeNegative') 158 ); 159 160 $coverage->stop( 161 true, 162 array(TEST_FILES_PATH . 'BankAccount.php' => range(27, 32)) 163 ); 164 165 return $coverage; 166 } 167 168 protected function getCoverageForBankAccountForLastTwoTests() 169 { 170 $data = $this->getXdebugDataForBankAccount(); 171 172 $stub = $this->getMock('PHP_CodeCoverage_Driver_Xdebug'); 173 $stub->expects($this->any()) 174 ->method('stop') 175 ->will($this->onConsecutiveCalls( 176 $data[2], 177 $data[3] 178 )); 179 180 $coverage = new PHP_CodeCoverage($stub, new PHP_CodeCoverage_Filter); 181 182 $coverage->start( 183 new BankAccountTest('testBalanceCannotBecomeNegative2') 184 ); 185 186 $coverage->stop( 187 true, 188 array(TEST_FILES_PATH . 'BankAccount.php' => range(20, 25)) 189 ); 190 191 $coverage->start( 192 new BankAccountTest('testDepositWithdrawMoney') 193 ); 194 195 $coverage->stop( 196 true, 197 array( 198 TEST_FILES_PATH . 'BankAccount.php' => array_merge( 199 range(6, 9), 200 range(20, 25), 201 range(27, 32) 202 ) 203 ) 204 ); 205 206 return $coverage; 207 } 208 209 protected function getExpectedDataArrayForBankAccount() 210 { 211 return array( 212 TEST_FILES_PATH . 'BankAccount.php' => array( 213 8 => array( 214 0 => 'BankAccountTest::testBalanceIsInitiallyZero', 215 1 => 'BankAccountTest::testDepositWithdrawMoney' 216 ), 217 9 => null, 218 13 => array(), 219 14 => array(), 220 15 => array(), 221 16 => array(), 222 18 => array(), 223 22 => array( 224 0 => 'BankAccountTest::testBalanceCannotBecomeNegative2', 225 1 => 'BankAccountTest::testDepositWithdrawMoney' 226 ), 227 24 => array( 228 0 => 'BankAccountTest::testDepositWithdrawMoney', 229 ), 230 25 => null, 231 29 => array( 232 0 => 'BankAccountTest::testBalanceCannotBecomeNegative', 233 1 => 'BankAccountTest::testDepositWithdrawMoney' 234 ), 235 31 => array( 236 0 => 'BankAccountTest::testDepositWithdrawMoney' 237 ), 238 32 => null 239 ) 240 ); 241 } 242 243 protected function getCoverageForFileWithIgnoredLines() 244 { 245 $coverage = new PHP_CodeCoverage( 246 $this->setUpXdebugStubForFileWithIgnoredLines(), 247 new PHP_CodeCoverage_Filter 248 ); 249 250 $coverage->start('FileWithIgnoredLines', true); 251 $coverage->stop(); 252 253 return $coverage; 254 } 255 256 protected function setUpXdebugStubForFileWithIgnoredLines() 257 { 258 $stub = $this->getMock('PHP_CodeCoverage_Driver_Xdebug'); 259 $stub->expects($this->any()) 260 ->method('stop') 261 ->will($this->returnValue( 262 array( 263 TEST_FILES_PATH . 'source_with_ignore.php' => array( 264 2 => 1, 265 4 => -1, 266 6 => -1, 267 7 => 1 268 ) 269 ) 270 )); 271 272 return $stub; 273 } 274 275 protected function getCoverageForClassWithAnonymousFunction() 276 { 277 $coverage = new PHP_CodeCoverage( 278 $this->setUpXdebugStubForClassWithAnonymousFunction(), 279 new PHP_CodeCoverage_Filter 280 ); 281 282 $coverage->start('ClassWithAnonymousFunction', true); 283 $coverage->stop(); 284 285 return $coverage; 286 } 287 288 protected function setUpXdebugStubForClassWithAnonymousFunction() 289 { 290 $stub = $this->getMock('PHP_CodeCoverage_Driver_Xdebug'); 291 $stub->expects($this->any()) 292 ->method('stop') 293 ->will($this->returnValue( 294 array( 295 TEST_FILES_PATH . 'source_with_class_and_anonymous_function.php' => array( 296 7 => 1, 297 9 => 1, 298 10 => -1, 299 11 => 1, 300 12 => 1, 301 13 => 1, 302 14 => 1, 303 17 => 1, 304 18 => 1 305 ) 306 ) 307 )); 308 309 return $stub; 310 } 311 }