Skip to content

Commit

Permalink
[#8] Add more tests to increase code coverage (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
tannguyen04 authored Jan 16, 2024
1 parent e5c42ee commit 5303c3d
Show file tree
Hide file tree
Showing 13 changed files with 207 additions and 114 deletions.
19 changes: 19 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## Checklist before requesting a review

- [ ] I have formatted the subject to include ticket number
as `[#123] Verb in past tense with dot at the end.`
- [ ] I have added a link to the issue tracker
- [ ] I have provided information in `Changed` section about WHY something was
done if this was not a normal implementation
- [ ] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have added tests that prove my fix is effective or that my feature works
- [ ] I have run new and existing relevant tests locally with my changes, and
they passed
- [ ] I have provided screenshots, where applicable

## Changed

1.

## Screenshots
8 changes: 5 additions & 3 deletions .github/release-drafter.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
name-template: '$NEXT_MINOR_VERSION'
tag-template: '$NEXT_MINOR_VERSION'
name-template: '$RESOLVED_VERSION'
tag-template: '$RESOLVED_VERSION'
change-template: '- $TITLE @$AUTHOR (#$NUMBER)'
change-title-escapes: '\<*_&' # You can add # and @ to disable mentions, and add ` to disable code blocks.
version-resolver:
default: minor
template: |
## What's new since $PREVIOUS_TAG
$CHANGES
**Full Changelog**: https://github.com/$OWNER/$REPOSITORY/compare/$PREVIOUS_TAG...$NEXT_MINOR_VERSION
**Full Changelog**: https://github.com/$OWNER/$REPOSITORY/compare/$PREVIOUS_TAG...$RESOLVED_VERSION
$CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ permissions:
jobs:
assign-author:
runs-on: ubuntu-latest

steps:
- uses: toshimaru/auto-author-assign@v2.0.1
- name: Assign author
uses: toshimaru/auto-author-assign@v2.0.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Release
name: Draft release notes

on:
push:
Expand All @@ -15,8 +15,11 @@ jobs:
permissions:
contents: write
pull-requests: write

runs-on: ubuntu-latest

steps:
- uses: release-drafter/release-drafter@v5
- name: Draft release notes
uses: release-drafter/release-drafter@v5
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
56 changes: 56 additions & 0 deletions .github/workflows/test-php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Test PHP

on:
push:
branches:
- main
pull_request:
branches:
- main
- 'feature/**'

jobs:
test-php:
runs-on: ubuntu-latest

strategy:
matrix:
php-versions: ['8.1', '8.2', '8.3']

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Cache Composer dependencies
uses: actions/cache@v3
with:
path: /tmp/composer-cache
key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}

- name: Install dependencies
run: composer install

- name: Check coding standards
run: composer lint

- name: Run tests
run: XDEBUG_MODE=coverage composer test

- name: Upload coverage report as artifact
uses: actions/upload-artifact@v4
with:
name: ${{github.job}}-code-coverage-report-${{ matrix.php-versions }}
path: ./.coverage-html

- name: Upload coverage report to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
with:
files: ./cobertura.xml
fail_ci_if_error: true
29 changes: 0 additions & 29 deletions .github/workflows/test.yml

This file was deleted.

57 changes: 11 additions & 46 deletions CsvTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,6 @@
*/
class CsvTable {

/**
* The CSV data as a string.
*
* @var string
*/
protected string $csvString;

/**
* The character used to separate values in the CSV data.
*
* @var string
*/
protected string $csvSeparator;

/**
* The character used to enclose values in the CSV data.
*
* @var string
*/
protected string $csvEnclosure;

/**
* The character used to escape special characters in the CSV data.
*
* @var string
*/
protected string $csvEscape;

/**
* Array containing the header row from the CSV data.
*
Expand All @@ -55,8 +27,6 @@ class CsvTable {

/**
* Boolean flag indicating whether the header should be parsed.
*
* @var bool
*/
protected bool $useHeader = TRUE;

Expand All @@ -65,23 +35,18 @@ class CsvTable {
*
* @param string|null $csvString
* The CSV data as a string. Defaults to NULL.
* @param string $separator
* @param string $csvSeparator
* The character used to separate values in the CSV data. Defaults to ','.
* @param string $enclosure
* @param string $csvEnclosure
* The character used to enclose values in the CSV data. Defaults to '"'.
* @param string $escape
* @param string $csvEscape
* The character used to escape special characters in the CSV data.
* Defaults to '\\'.
*/
public function __construct(string $csvString = NULL, string $separator = ',', string $enclosure = '"', string $escape = '\\') {
$this->csvSeparator = $separator;
$this->csvEnclosure = $enclosure;
$this->csvEscape = $escape;

if ($csvString) {
$this->csvString = $csvString;
}

public function __construct(protected ?string $csvString = NULL,
protected string $csvSeparator = ',',
protected string $csvEnclosure = '"',
protected string $csvEscape = '\\') {
$this->parse();
}

Expand Down Expand Up @@ -172,7 +137,7 @@ public static function fromFile($filepath, $separator = ',', $enclosure = '"', $
public function render(callable|string $renderer = NULL, array $options = []): string {
$renderer = $renderer
? (is_string($renderer) && class_exists($renderer) ? [$renderer, 'render'] : $renderer)
: [$this, 'renderCsv'];
: $this->renderCsv(...);

if (!is_callable($renderer)) {
throw new \Exception('Renderer must be callable');
Expand Down Expand Up @@ -228,11 +193,11 @@ public static function renderCsv(array $header, array $rows, array $options): st
if ($out) {
if (count($header) > 0) {
/* @phpstan-ignore-next-line */
fputcsv($out, $header, $options['separator'], $options['enclosure'], $options['escape']);
fputcsv($out, $header, (string) $options['separator'], (string) $options['enclosure'], (string) $options['escape']);
}
foreach ($rows as $row) {
/* @phpstan-ignore-next-line */
fputcsv($out, $row, $options['separator'], $options['enclosure'], $options['escape']);
fputcsv($out, $row, (string) $options['separator'], (string) $options['enclosure'], (string) $options['escape']);
}

rewind($out);
Expand Down Expand Up @@ -263,7 +228,7 @@ public static function renderTable(array $header, array $rows): string {
$header = '';
}

return $header . implode("\n", array_map(function ($row) {
return $header . implode("\n", array_map(static function ($row): string {
return implode('|', $row);
}, $rows));
}
Expand Down
9 changes: 3 additions & 6 deletions Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ class Markdown {

/**
* Number of columns.
*
* @var int
*/
protected int $colCount = 0;

Expand All @@ -59,7 +57,7 @@ class Markdown {
public function __construct(array $header, array $rows, array $options = []) {
$this->header = array_map([self::class, 'processValue'], $header);

$this->rows = array_map(function ($row) {
$this->rows = array_map(static function ($row): array {
return array_map([self::class, 'processValue'], $row);
}, $rows);
$this->options = $options + ['column_separator' => '|'];
Expand Down Expand Up @@ -117,9 +115,8 @@ protected function createRow(array $row): string {
}

$output .= str_pad($row[$this->colCount - 1], $this->colWidths[$this->colCount - 1]);
$output .= ' ' . $this->options['column_separator'] . "\n";

return $output;
return $output . (' ' . $this->options['column_separator'] . "\n");
}

/**
Expand Down Expand Up @@ -165,7 +162,7 @@ protected function createRows(array $rows): string {
/**
* Process value.
*/
protected function processValue(string $value): string {
public static function processValue(string $value): string {
return (string) preg_replace('/(\r\n|\n|\r)/', '<br />', $value);
}

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

[![GitHub Issues](https://img.shields.io/github/issues/AlexSkrypnyk/CsvTable.svg)](https://github.com/AlexSkrypnyk/CsvTable/issues)
[![GitHub Pull Requests](https://img.shields.io/github/issues-pr/AlexSkrypnyk/CsvTable.svg)](https://github.com/AlexSkrypnyk/CsvTable/pulls)
[![Test](https://github.com/AlexSkrypnyk/CsvTable/actions/workflows/test.yml/badge.svg)](https://github.com/AlexSkrypnyk/CsvTable/actions/workflows/test.yml)
[![Test](https://github.com/AlexSkrypnyk/CsvTable/actions/workflows/test-php.yml/badge.svg)](https://github.com/AlexSkrypnyk/CsvTable/actions/workflows/test-php.yml)
[![codecov](https://codecov.io/gh/AlexSkrypnyk/CsvTable/graph/badge.svg?token=7WEB1IXBYT)](https://codecov.io/gh/AlexSkrypnyk/CsvTable)
![GitHub release (latest by date)](https://img.shields.io/github/v/release/AlexSkrypnyk/CsvTable)
![LICENSE](https://img.shields.io/github/license/AlexSkrypnyk/CsvTable)
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"drupal/coder": "^8.3",
"phpmd/phpmd": "^2.15",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^10.1"
"phpunit/phpunit": "^10.1",
"rector/rector": "^0.19.0"
},
"autoload": {
"psr-4": {
Expand All @@ -38,10 +39,10 @@
"lint": [
"phpcs",
"phpmd --exclude vendor,vendor-bin,node_modules . text phpmd.xml",
"phpstan"
"phpstan",
"rector --dry-run"
],
"lint:fix": "phpcbf",
"test": "phpunit --no-coverage",
"test:coverage": "XDEBUG_MODE=coverage phpunit"
"test": "if [ \"${XDEBUG_MODE}\" = 'coverage' ]; then phpunit; else phpunit --no-coverage; fi"
}
}
65 changes: 65 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/**
* @file
* Rector configuration.
*
* Usage:
* ./vendor/bin/rector process .
*
* @see https://github.com/palantirnet/drupal-rector/blob/main/rector.php
*/

declare(strict_types=1);

use Rector\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector;
use Rector\CodeQuality\Rector\Empty_\SimplifyEmptyCheckOnEmptyArrayRector;
use Rector\CodingStyle\Rector\ClassMethod\NewlineBeforeNewAssignSetRector;
use Rector\CodingStyle\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector;
use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector;
use Rector\CodingStyle\Rector\PostInc\PostIncDecToPreIncDecRector;
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector;
use Rector\Set\ValueObject\SetList;
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/**',
]);

$rectorConfig->sets([
SetList::PHP_80,
SetList::PHP_81,
SetList::CODE_QUALITY,
SetList::CODING_STYLE,
SetList::DEAD_CODE,
SetList::INSTANCEOF,
SetList::TYPE_DECLARATION,
]);

$rectorConfig->skip([
// Rules added by Rector's rule sets.
ArraySpreadInsteadOfArrayMergeRector::class,
CountArrayToEmptyArrayComparisonRector::class,
DisallowedEmptyRuleFixerRector::class,
InlineArrayReturnAssignRector::class,
NewlineAfterStatementRector::class,
NewlineBeforeNewAssignSetRector::class,
PostIncDecToPreIncDecRector::class,
RemoveAlwaysTrueIfConditionRector::class,
SimplifyEmptyCheckOnEmptyArrayRector::class,
// Dependencies.
'*/vendor/*',
'*/node_modules/*',
]);

$rectorConfig->fileExtensions([
'php',
'inc',
]);

$rectorConfig->importNames(TRUE, FALSE);
$rectorConfig->importShortClasses(FALSE);
};
Loading

0 comments on commit 5303c3d

Please sign in to comment.