/** * @expectedException SebastianBergmann\CodeCoverage\CoveredCodeNotExecutedException */ public function testAppendThrowsExceptionIfUsedCodeWasNotExecuted() { $this->coverage->filter()->addDirectoryToWhitelist(TEST_FILES_PATH); $this->coverage->setCheckForUnexecutedCoveredCode(true); $data = [TEST_FILES_PATH . 'BankAccount.php' => [29 => -1, 31 => -1]]; $linesToBeCovered = [TEST_FILES_PATH . 'BankAccount.php' => [29, 31]]; $linesToBeUsed = [TEST_FILES_PATH . 'BankAccount.php' => [22, 24]]; $this->coverage->append($data, 'File1.php', true, $linesToBeCovered, $linesToBeUsed); }
/** * @param CodeCoverage $coverage * @param string $target * * @return string */ public function process(CodeCoverage $coverage, $target = null) { $filter = $coverage->filter(); $output = sprintf('<?php $coverage = new SebastianBergmann\\CodeCoverage\\CodeCoverage; $coverage->setData(%s); $coverage->setTests(%s); $filter = $coverage->filter(); $filter->setWhitelistedFiles(%s); return $coverage;', var_export($coverage->getData(true), 1), var_export($coverage->getTests(), 1), var_export($filter->getWhitelistedFiles(), 1)); if ($target !== null) { return file_put_contents($target, $output); } else { return $output; } }
/** * @param CodeCoverage $coverage * @param CodeCoverage $that */ private function mergeCoverage(CodeCoverage $coverage, CodeCoverage $that) { $filter = $coverage->filter(); $filter->setWhitelistedFiles(array_merge($filter->getWhitelistedFiles(), $that->filter()->getWhitelistedFiles())); $thisData = $coverage->getData(true); $thatData = $that->getData(true); foreach ($thatData as $file => $lines) { if (!$this->hasCoverage($thatData, $file)) { continue; } if (!$this->hasCoverage($thisData, $file)) { if (!$filter->isFiltered($file)) { $thisData[$file] = $lines; } continue; } foreach ($lines as $line => $data) { if ($data !== null) { if (!isset($thisData[$file][$line])) { $thisData[$file][$line] = $data; } else { $thisData[$file][$line] = array_unique(array_merge($thisData[$file][$line], $data)); } } } } $coverage->setData($thisData); $coverage->setTests(array_merge($coverage->getTests(), $that->getTests())); }
* 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_Selenium * @author Sebastian Bergmann <*****@*****.**> * @copyright 2010-2013 Sebastian Bergmann <*****@*****.**> * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License * @link http://www.phpunit.de/ * @since File available since Release 1.0.0 */ use SebastianBergmann\CodeCoverage\CodeCoverage; use SebastianBergmann\CodeCoverage\Report\PHP as PhpReport; require_once 'libraries/vendor/autoload.php'; $coverage = new CodeCoverage(); $filter = $coverage->filter(); $files = getenv('PHPUNIT_COVERAGE_DATA_DIRECTORY') . '/logs/*.php'; foreach (glob($files) as $file) { /** @var CodeCoverage $cc */ $cc = (function ($filename) { $coverage = null; include $filename; unlink($filename); return $coverage; })($file); $filter->addFilesToWhitelist($cc->filter()->getWhitelistedFiles()); $coverage->merge($cc); } echo "Writing PHP report ...\n"; $writer = new PhpReport(); $writer->process($coverage, getenv('PHPUNIT_COVERAGE_DATA_DIRECTORY') . '/reports/coverage.cli.php');
/** * Merges the data from another instance. * * @param CodeCoverage $that */ public function merge(CodeCoverage $that) { $this->filter->setWhitelistedFiles(array_merge($this->filter->getWhitelistedFiles(), $that->filter()->getWhitelistedFiles())); foreach ($that->data as $file => $lines) { if (!isset($this->data[$file])) { if (!$this->filter->isFiltered($file)) { $this->data[$file] = $lines; } continue; } foreach ($lines as $line => $data) { if ($data !== null) { if (!isset($this->data[$file][$line])) { $this->data[$file][$line] = $data; } else { $this->data[$file][$line] = array_unique(array_merge($this->data[$file][$line], $data)); } } } } $this->tests = array_merge($this->tests, $that->getTests()); }