Skip to content

Instantly share code, notes, and snippets.

@shu-yusa
Last active June 8, 2023 01:32
Show Gist options
  • Save shu-yusa/4b42f944d5dc11b4a2b7f00a2cea6bec to your computer and use it in GitHub Desktop.
Save shu-yusa/4b42f944d5dc11b4a2b7f00a2cea6bec to your computer and use it in GitHub Desktop.
Covert output from `phpunit --coverage-text` into the table form in Markdown
#!/bin/bash
# This script is used to convert the output from "phpunit --coverage-text"
# into a markdown formatted table. It parses the coverage data,
# then generates a table with class names, statement counts, misses, and coverages.
# The output is designed to be easily readable and suitable for inclusion in
# markdown-based documents or reports.
# Ignore output until reaching coverage report
while IFS= read -r line
do
if [[ $line == *"Summary"* ]]; then
read _ # skip overall class coverage
read _ # skip overall method coverage
break
fi
done
echo "| Class | Stmts | Miss | Cover |"
echo "|-------|-------|------|-------|"
# Calculate overall coverage
read overall_coverage
total_stmts=$(echo $overall_coverage | awk -F'[()]' '{print $2}' | awk -F/ '{print $2}')
total_miss=$(echo $overall_coverage | awk -F'[()]' '{print $2}' | awk -F/ '{print $2 - $1}')
total_coverage=$(echo $overall_coverage | awk -F'[:(]' '{print $2}' | tr -d ' ')
echo "| **TOTAL** | **$total_stmts** | **$total_miss** | **$total_coverage** |"
while IFS= read -r line
do
# If line starts with a letter (assumed to be a class name)
if [[ $line =~ ^[A-Za-z] ]]; then
class_name=$(echo $line | cut -d' ' -f1)
read coverage_info
stmts=$(echo $coverage_info | awk -F'[()]' '{print $4}' | awk -F/ '{print $2}')
miss=$(echo $coverage_info | awk -F'[()]' '{print $4}' | awk -F/ '{print $2 - $1}')
coverage=$(echo $coverage_info | awk -F'[:(]' '{print $4}' | tr -d ' ')
# Skip if coverage is empty or 100%
if [ -z "$coverage" ] || [ "$coverage" = "100.00%" ]; then
continue
fi
echo "| $class_name | $stmts | $miss | $coverage |"
fi
done
@shu-yusa
Copy link
Author

shu-yusa commented Jun 8, 2023

Example:

$ phpunit --coverage-text
PHPUnit 9.5.20 #StandWithUkraine

........................                                          24 / 24 (100%)

Time: 00:00.966, Memory: 14.00 MB

OK (24 tests, 43 assertions)

Code Coverage Report:    
  2023-06-08 00:54:50    
                         
 Summary:                
  Classes: 50.00% (2/4)  
  Methods: 78.57% (11/14)
  Lines:   95.16% (59/62)

App\Controller
  Methods:  60.00% ( 3/ 5)   Lines:  90.91% ( 20/ 22)
App\Service
  Methods: 100.00% ( 3/ 3)   Lines: 100.00% ( 14/ 14)
App\Exception
  Methods:  ( 0/ 0)   Lines:  (  0/  0)
App\Entity
  Methods: 100.00% ( 3/ 3)   Lines: 100.00% (  8/  8)
App\Repository
  Methods:  66.67% ( 2/ 3)   Lines:  94.44% ( 17/ 18)

With parsing coverage

$ phpunit --coverage-text | ./parse_coverage.sh
| Class | Stmts | Miss | Cover |
|-------|-------|------|-------|
| **TOTAL** | **62** | **3** | **95.16%** |
| App\Controller |  22 | 2 | 90.91% |
| App\Repository |  18 | 1 | 94.44% |

In Markdown, it looks like

Class Stmts Miss Cover
TOTAL 62 3 95.16%
App\Controller 22 2 90.91%
App\Repository 18 1 94.44%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment