From 78ca18d022f627a26f43bdab6f6cd7b15466c858 Mon Sep 17 00:00:00 2001 From: liujinxia Date: Thu, 23 Sep 2021 16:44:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A2=9E=E9=87=8F=E6=8A=A5=E5=91=8A?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- prepend.php | 19 +++ src/Diff.php | 82 ++++++++++++ src/Report/Html/Facade.php | 254 +++++++++++++++++++++++++++++-------- 3 files changed, 299 insertions(+), 56 deletions(-) create mode 100644 prepend.php create mode 100644 src/Diff.php diff --git a/prepend.php b/prepend.php new file mode 100644 index 000000000..c1fb8bd2f --- /dev/null +++ b/prepend.php @@ -0,0 +1,19 @@ +filter()->addDirectoryToWhitelist('/opt/web/phpc/application/hbg_php_houseapp/phpapps/*'); +# 白名单
$coverage->filter()->removeDirectoryFromWhitelist('/var/www/html/userinfo.php'); +# 从白名单中移除文件夹
$coverage->filter()->removeFileFromWhitelist('/var/www/html/userinfo.php'); # 从白名单中移除文件 + + +$coverage->start('');#开始统计 +register_shutdown_function('__coverage_stop',$coverage);#注册关闭方法 + +function __coverage_stop(CodeCoverage $coverage){ + $coverage->stop();#停止统计 + $cov = ' result.txt 处理之后的文件,将结果封装成数组 key=》文件名 value=》新增的行 + * + */ +namespace SebastianBergmann\CodeCoverage; +class Diff +{ + private $result = []; + + + function main() + { + $fileContent = $this->getTxtcontent("RESULT_FILE"); + array_walk($fileContent, function (&$item) { + if (strpos($item, 'diff') !== false) { + $fileName = explode(' ', $item)[3]; + $fileName = str_replace("b/", "", $fileName); + $this->result["PROJECT_ABS_PATH" . "/" . $fileName] = []; + } else { + /** + * 取result最后一个元素 放到value里面去 + */ + //@@ -0,0 +1,19 @@ + $and = explode(' ', $item)[2]; + $beginLine = explode(',', $and)[0]; + $beginLine = str_replace("+", "", $beginLine); + $count = explode(',', $and)[1]; + end($this->result); + $key = key($this->result); + $valueArray = $this->result[$key]; + for ($i = $beginLine; $i <= $beginLine + $count; $i++) { + $valueArray[$i] = 1; + } + /** + * 根据实际情况 是都需要替换成自己的项目目录 + */ + if (strpos($key, '/project') !== false) { + $this->result[$key] = $valueArray; + } else { + $this->result["PROJECT_ABS_PATH" . "/" . $key] = $valueArray; + } + } + }); + + print_r($this->result); + return $this->result; + } + + /* + * 逐行读取TXT文件 + */ + function getTxtcontent($txtfile) + { + $file = @fopen($txtfile, 'r'); + $content = array(); + if (!$file) { + return 'file open fail'; + } else { + $i = 0; + while (!feof($file)) { + $content[$i] = mb_convert_encoding(fgets($file), "UTF-8", "GBK,ASCII,ANSI,UTF-8"); + $i++; + } + fclose($file); + $content = array_filter($content); //数组去空 + } +// print_r($content); + return $content; + } + +} + +$d = new Diff(); +$d->main(); \ No newline at end of file diff --git a/src/Report/Html/Facade.php b/src/Report/Html/Facade.php index db74840d3..64ea6dc5a 100644 --- a/src/Report/Html/Facade.php +++ b/src/Report/Html/Facade.php @@ -1,54 +1,161 @@ - * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ + namespace SebastianBergmann\CodeCoverage\Report\Html; -use const DIRECTORY_SEPARATOR; -use function copy; -use function date; -use function dirname; -use function substr; use SebastianBergmann\CodeCoverage\CodeCoverage; -use SebastianBergmann\CodeCoverage\Directory as DirectoryUtil; use SebastianBergmann\CodeCoverage\Node\Directory as DirectoryNode; +use SebastianBergmann\CodeCoverage\RuntimeException; +use SebastianBergmann\CodeCoverage\Diff; -final class Facade +/** + * Generates an HTML report from a code coverage object. + */ +class Facade { - private string $templatePath; + /** + * @var string + */ + private $templatePath; - private string $generator; + /** + * @var string + */ + private $generator; - private int $lowUpperBound; + /** + * @var int + */ + private $lowUpperBound; - private int $highLowerBound; + /** + * @var int + */ + private $highLowerBound; + private $originData; + private $diffData; - public function __construct(int $lowUpperBound = 50, int $highLowerBound = 90, string $generator = '') + /** + * Constructor. + * + * @param int $lowUpperBound + * @param int $highLowerBound + * @param string $generator + */ + public function __construct($lowUpperBound = 50, $highLowerBound = 90, $generator = '') { - $this->generator = $generator; + $this->generator = $generator; $this->highLowerBound = $highLowerBound; - $this->lowUpperBound = $lowUpperBound; - $this->templatePath = __DIR__ . '/Renderer/Template/'; + $this->lowUpperBound = $lowUpperBound; + $this->templatePath = __DIR__ . '/Renderer/Template/'; } - public function process(CodeCoverage $coverage, string $target): void + + public function zengliangParse() { - $target = $this->directory($target); + $result = []; + /** + * 类在diff中的,都展示到结果中 + */ + print_r("1"); + var_dump($this->diffData); + print_r("======originData======"); + var_dump($this->originData); + print_r("============"); + + foreach ($this->diffData as $diffKey => $diffValue) { + print_r("======diffData======"); + print_r($diffKey); + // foreach($this->originData as $k=>$v){ + // var_dump($k); +// } + print_r(array_key_exists($diffKey, $this->originData)); + print_r(gettype($diffKey) . "++++++++++"); + //$diffKey = $diffKey.replace('\n', '').replace('\r', ''); + $diffKey = str_replace(PHP_EOL, '', $diffKey); + print_r($diffKey . "---new diffKey----"); + print_r("-------test result--"); + if (array_key_exists($diffKey, $this->originData)) { + //有变更且有覆盖信息 覆盖信息不变;有变更 但是没有覆盖信息 红;没有变更 但是有覆盖信息 一律白不加到结果中去; + print_r("------------2-----------"); + $diffValueArray = $diffValue; + $originValueArray = $this->originData[$diffKey]; + + $resultValues = []; + foreach ($diffValueArray as $key => $value) { + $diffLine = $key; + print_r("3"); + if (array_key_exists($diffLine, $originValueArray)) { + $resultValues[$diffKey][$diffLine] = $originValueArray[$diffLine]; + print_r("4"); + unset($originValueArray[$key]); + } else { + print_r("5"); + $resultValues[$diffKey][$diffLine] = []; + } + unset($diffValueArray[$key]); + } + $result[$diffKey] = $resultValues[$diffKey]; + } else { + print_r($diffKey . "not exists in originData"); + } + }; + print_r("-------------result-------------"); + var_dump($result); + return $result; + } + + + /** + * @param CodeCoverage $coverage + * @param string $target + */ + public function process(CodeCoverage $coverage, $target) + { +// file_put_contents(dirname(__FILE__) . '/data.txt', serialize($coverage->getData())); + $d = new Diff(); + $diffData = $d->main(); + $this->originData = $coverage->getData(); + $this->diffData = $diffData; + $zengliangData = $this->zengliangParse(); + $coverage->setData($zengliangData); + + //修改白名单 只展示增量报告: + $whiteFileLists = []; + foreach ($zengliangData as $key => $value) { + $whiteFileLists[$key]=1; + } + $coverage->filter()->setWhitelistedFiles([]); + print_r("-----after init----"); + print_r($coverage->filter()->getWhitelistedFiles()); + + $coverage->filter()->setWhitelistedFiles($whiteFileLists); + print_r("-----after zengliang----"); + print_r($coverage->filter()->getWhitelistedFiles()); + + $target = $this->getDirectory($target); $report = $coverage->getReport(); - $date = date('D M j G:i:s T Y'); + unset($coverage); + + if (!isset($_SERVER['REQUEST_TIME'])) { + $_SERVER['REQUEST_TIME'] = \time(); + } + + $date = \date('D M j G:i:s T Y', $_SERVER['REQUEST_TIME']); $dashboard = new Dashboard( $this->templatePath, $this->generator, $date, $this->lowUpperBound, - $this->highLowerBound, - $coverage->collectsBranchAndPathCoverage() + $this->highLowerBound ); $directory = new Directory( @@ -56,8 +163,7 @@ public function process(CodeCoverage $coverage, string $target): void $this->generator, $date, $this->lowUpperBound, - $this->highLowerBound, - $coverage->collectsBranchAndPathCoverage() + $this->highLowerBound ); $file = new File( @@ -65,64 +171,100 @@ public function process(CodeCoverage $coverage, string $target): void $this->generator, $date, $this->lowUpperBound, - $this->highLowerBound, - $coverage->collectsBranchAndPathCoverage() + $this->highLowerBound ); $directory->render($report, $target . 'index.html'); $dashboard->render($report, $target . 'dashboard.html'); foreach ($report as $node) { - $id = $node->id(); + $id = $node->getId(); if ($node instanceof DirectoryNode) { - DirectoryUtil::create($target . $id); + if (!\file_exists($target . $id)) { + \mkdir($target . $id, 0777, true); + } $directory->render($node, $target . $id . '/index.html'); $dashboard->render($node, $target . $id . '/dashboard.html'); } else { - $dir = dirname($target . $id); + $dir = \dirname($target . $id); - DirectoryUtil::create($dir); + if (!\file_exists($dir)) { + \mkdir($dir, 0777, true); + } - $file->render($node, $target . $id); + $file->render($node, $target . $id . '.html'); } } - $this->copyFiles($target); } - private function copyFiles(string $target): void + /** + * @param string $target + */ + private function copyFiles($target) { - $dir = $this->directory($target . '_css'); - - copy($this->templatePath . 'css/bootstrap.min.css', $dir . 'bootstrap.min.css'); - copy($this->templatePath . 'css/nv.d3.min.css', $dir . 'nv.d3.min.css'); - copy($this->templatePath . 'css/style.css', $dir . 'style.css'); - copy($this->templatePath . 'css/custom.css', $dir . 'custom.css'); - copy($this->templatePath . 'css/octicons.css', $dir . 'octicons.css'); - - $dir = $this->directory($target . '_icons'); - copy($this->templatePath . 'icons/file-code.svg', $dir . 'file-code.svg'); - copy($this->templatePath . 'icons/file-directory.svg', $dir . 'file-directory.svg'); - - $dir = $this->directory($target . '_js'); - copy($this->templatePath . 'js/bootstrap.min.js', $dir . 'bootstrap.min.js'); - copy($this->templatePath . 'js/popper.min.js', $dir . 'popper.min.js'); - copy($this->templatePath . 'js/d3.min.js', $dir . 'd3.min.js'); - copy($this->templatePath . 'js/jquery.min.js', $dir . 'jquery.min.js'); - copy($this->templatePath . 'js/nv.d3.min.js', $dir . 'nv.d3.min.js'); - copy($this->templatePath . 'js/file.js', $dir . 'file.js'); + $dir = $this->getDirectory($target . '.css'); + + \file_put_contents( + $dir . 'bootstrap.min.css', + \str_replace( + 'url(../fonts/', + 'url(../.fonts/', + \file_get_contents($this->templatePath . 'css/bootstrap.min.css') + ) + + ); + + \copy($this->templatePath . 'css/nv.d3.min.css', $dir . 'nv.d3.min.css'); + \copy($this->templatePath . 'css/style.css', $dir . 'style.css'); + + $dir = $this->getDirectory($target . '.fonts'); + \copy($this->templatePath . 'fonts/glyphicons-halflings-regular.eot', $dir . 'glyphicons-halflings-regular.eot'); + \copy($this->templatePath . 'fonts/glyphicons-halflings-regular.svg', $dir . 'glyphicons-halflings-regular.svg'); + \copy($this->templatePath . 'fonts/glyphicons-halflings-regular.ttf', $dir . 'glyphicons-halflings-regular.ttf'); + \copy($this->templatePath . 'fonts/glyphicons-halflings-regular.woff', $dir . 'glyphicons-halflings-regular.woff'); + \copy($this->templatePath . 'fonts/glyphicons-halflings-regular.woff2', $dir . 'glyphicons-halflings-regular.woff2'); + + $dir = $this->getDirectory($target . '.js'); + \copy($this->templatePath . 'js/bootstrap.min.js', $dir . 'bootstrap.min.js'); + \copy($this->templatePath . 'js/d3.min.js', $dir . 'd3.min.js'); + \copy($this->templatePath . 'js/holder.min.js', $dir . 'holder.min.js'); + \copy($this->templatePath . 'js/html5shiv.min.js', $dir . 'html5shiv.min.js'); + \copy($this->templatePath . 'js/jquery.min.js', $dir . 'jquery.min.js'); + \copy($this->templatePath . 'js/nv.d3.min.js', $dir . 'nv.d3.min.js'); + \copy($this->templatePath . 'js/respond.min.js', $dir . 'respond.min.js'); + \copy($this->templatePath . 'js/file.js', $dir . 'file.js'); } - private function directory(string $directory): string + /** + * @param string $directory + * + * @return string + * + * @throws RuntimeException + */ + private function getDirectory($directory) { - if (substr($directory, -1, 1) != DIRECTORY_SEPARATOR) { + if (\substr($directory, -1, 1) != DIRECTORY_SEPARATOR) { $directory .= DIRECTORY_SEPARATOR; } - DirectoryUtil::create($directory); + if (\is_dir($directory)) { + return $directory; + } - return $directory; + if (@\mkdir($directory, 0777, true)) { + return $directory; + } + + throw new RuntimeException( + \sprintf( + 'Directory "%s" does not exist.', + $directory + ) + ); } } + From 8f44d56c4326ac14bddfaa6abf3879b231f07664 Mon Sep 17 00:00:00 2001 From: liujinxia Date: Thu, 23 Sep 2021 17:54:28 +0800 Subject: [PATCH 2/2] Update README.md --- README.md | 54 +++++------------------------------------------------- 1 file changed, 5 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index a0e545cf8..506d354df 100644 --- a/README.md +++ b/README.md @@ -1,49 +1,5 @@ -# phpunit/php-code-coverage - -[![Latest Stable Version](https://poser.pugx.org/phpunit/php-code-coverage/v/stable.png)](https://packagist.org/packages/phpunit/php-code-coverage) -[![CI Status](https://github.com/sebastianbergmann/php-code-coverage/workflows/CI/badge.svg)](https://github.com/sebastianbergmann/php-code-coverage/actions) -[![Type Coverage](https://shepherd.dev/github/sebastianbergmann/php-code-coverage/coverage.svg)](https://shepherd.dev/github/sebastianbergmann/php-code-coverage) -[![codecov](https://codecov.io/gh/sebastianbergmann/php-code-coverage/branch/master/graph/badge.svg)](https://codecov.io/gh/sebastianbergmann/php-code-coverage) - -Provides collection, processing, and rendering functionality for PHP code coverage information. - -## Installation - -You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/): - -``` -composer require phpunit/php-code-coverage -``` - -If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency: - -``` -composer require --dev phpunit/php-code-coverage -``` - -## Usage - -```php -includeDirectory('/path/to/directory'); - -$coverage = new CodeCoverage( - (new Selector)->forLineCoverage($filter), - $filter -); - -$coverage->start(''); - -// ... - -$coverage->stop(); - - -(new HtmlReport)->process($coverage, '/tmp/code-coverage-report'); -``` +# php增量报告展示 +具体 +具体思路 +具体思路参考 +https://www.sohu.com/a/326621612_741445