Skip to content

Commit

Permalink
A test will now fail in strict mode when it uses the @Covers annotati…
Browse files Browse the repository at this point in the history
…on and code that is not expected to be covered is executed.
  • Loading branch information
sebastianbergmann committed Nov 22, 2012
1 parent bb96133 commit 0809359
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 2 deletions.
5 changes: 3 additions & 2 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This is the list of changes for the PHPUnit 3.8 release series.
PHPUnit 3.8.0
-------------

* PHPUnit 3.8 is only supported on PHP 5.4.7 (or later).
* A test will now fail in strict mode when it uses the `@covers` annotation and code that is not expected to be covered is executed.
* Implemented #711: `coverage-text` now has an XML `showOnlySummary` option.
* Fixed: `phpt` test cases now use the correct php binary when executed through wrapper scripts.
* Implemented #711: `coverage-text` now has a xml `showOnlySummary` option.
* PHPUnit 3.8 is only supported on PHP 5.4.7 (or later).
1 change: 1 addition & 0 deletions PHPUnit/Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ function ($class)
'phpunit_framework_testresult' => '/Framework/TestResult.php',
'phpunit_framework_testsuite' => '/Framework/TestSuite.php',
'phpunit_framework_testsuite_dataprovider' => '/Framework/TestSuite/DataProvider.php',
'phpunit_framework_unintentionallycoveredcodeerror' => '/Framework/UnintentionallyCoveredCodeError.php',
'phpunit_framework_warning' => '/Framework/Warning.php',
'phpunit_runner_basetestrunner' => '/Runner/BaseTestRunner.php',
'phpunit_runner_standardtestsuiteloader' => '/Runner/StandardTestSuiteLoader.php',
Expand Down
10 changes: 10 additions & 0 deletions PHPUnit/Framework/TestResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,16 @@ public function run(PHPUnit_Framework_Test $test)
$this->codeCoverage->stop(!$incomplete && !$skipped);
}

catch (PHP_CodeCoverage_Exception_UnintentionallyCoveredCode $e) {
$this->addFailure(
$test,
new PHPUnit_Framework_UnintentionallyCoveredCodeError(
'This test executed code that is not listed as code to be covered'
),
$time
);
}

catch (PHP_CodeCoverage_Exception $cce) {
$error = TRUE;

Expand Down
60 changes: 60 additions & 0 deletions PHPUnit/Framework/UnintentionallyCoveredCodeError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* PHPUnit
*
* Copyright (c) 2001-2012, Sebastian Bergmann <sebastian@phpunit.de>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Sebastian Bergmann nor the names of his
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package PHPUnit
* @subpackage Framework
* @author Sebastian Bergmann <sebastian@phpunit.de>
* @copyright 2001-2012 Sebastian Bergmann <sebastian@phpunit.de>
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
* @link http://www.phpunit.de/
* @since File available since Release 3.8.0
*/

/**
* Extension to PHPUnit_Framework_AssertionFailedError to mark the special
* case of a test test that unintentionally covers code.
*
* @package PHPUnit
* @subpackage Framework
* @author Sebastian Bergmann <sebastian@phpunit.de>
* @copyright 2001-2012 Sebastian Bergmann <sebastian@phpunit.de>
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
* @link http://www.phpunit.de/
* @since Class available since Release 3.8.0
*/
class PHPUnit_Framework_UnintentionallyCoveredCodeError extends PHPUnit_Framework_AssertionFailedError
{
}
4 changes: 4 additions & 0 deletions PHPUnit/TextUI/TestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ public function doRun(PHPUnit_Framework_Test $suite, array $arguments = array())
$arguments['addUncoveredFilesFromWhitelist']
);

$codeCoverage->setCheckForUnintentionallyCoveredCode(
$arguments['strict']
);

$codeCoverage->setProcessUncoveredFilesFromWhitelist(
$arguments['processUncoveredFilesFromWhitelist']
);
Expand Down
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
<file baseinstalldir="/" name="Test.php" role="php" />
<file baseinstalldir="/" name="TestResult.php" role="php" />
<file baseinstalldir="/" name="TestSuite.php" role="php" />
<file baseinstalldir="/" name="UnintentionallyCoveredCodeError.php" role="php" />
<file baseinstalldir="/" name="Warning.php" role="php" />
</dir>
<dir name="Runner">
Expand Down

0 comments on commit 0809359

Please sign in to comment.