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