23 Commits
master ... main

Author SHA1 Message Date
Christian Bläul
6c0e74c89e Newer Rector versions don’t have AddCoversClassAttributeRector anymore
https://github.com/rectorphp/rector-phpunit/pull/561/
2026-02-02 16:02:11 +01:00
Christian Bläul
c47ba7934c Let’s see if we can update the GH Action to cache@v4 2026-02-02 16:02:11 +01:00
Christian Bläul
653d5c0307 Upgrade to PHPUnit 8.5.52 2026-02-02 16:02:11 +01:00
Christian Bläul
ff0e0c77fe PHPUnit test was not compatible with PHP ^7.0 2024-08-29 17:33:01 +02:00
Christian Bläul
f9b08cb0ac Re-add support for PHPUnit 6.5.14 and PHP ^7.0 2024-08-29 17:30:33 +02:00
Christian Bläul
71e5f223a4 Make PHP 8.3 support visible in README 2024-08-29 17:20:31 +02:00
Christian Bläul
24e229ddfa Convert some old array() calls to new [] syntax 2024-08-29 17:20:31 +02:00
Christian Bläul
259e9a6c31 Extend test coverage to PHP 8.3 2024-08-29 17:20:31 +02:00
Christian Bläul
27ab8a3e05 README.md: Remove OpenCollective as no funds are needed nor given 2024-07-29 21:53:36 +02:00
Fonata
fe27ca6a7d docs: add badge for PHPUnit 2022-12-27 16:39:03 +01:00
Daniele Scasciafratte
33f0b6aa7c Support to search for numbers 2022-12-27 16:31:34 +01:00
Fonata
260de6126c test: make code compatible with PHP 8 2022-12-26 23:45:55 +01:00
Fonata
1465973860 ci: make actions use node 16 instead of node 12 2022-12-26 23:45:55 +01:00
Fonata
2c46b3fd14 test: make code compatible with more recent PHPUnit versions 2022-12-26 23:45:55 +01:00
Fonata
bb3abe97de ci: update phpunit if allowed by the PHP version 2022-12-26 23:45:55 +01:00
Fonata
ac6b2665cd ci: tell GitHub to also run tests for PHP 8.* 2022-12-26 23:45:55 +01:00
Fonata
919528bb2f test: fix expected string: '\r' were 2 characters, not 0x0d
Solved with var_dump(bin2hex($expected));
2022-12-26 20:29:04 +01:00
Fonata
c4a90941e2 docs: fix grammar mistake 2022-12-26 20:29:04 +01:00
Fonata
e967067a46 refactor: simplify code 2022-12-26 20:29:04 +01:00
Fonata
11dbbcb40e Simplified code - the string content is exactly the same 2022-12-26 20:29:04 +01:00
Fonata
67163568ee Improved test name to make it easier to find 2022-12-26 20:29:04 +01:00
Fonata
6648dc99d5 Added a test for issue #177: Saving data with commas 2022-12-26 20:29:04 +01:00
Fonata
2d6236cae0 docs(changelog): add version 1.3.2 2021-11-07 15:15:46 +01:00
15 changed files with 234 additions and 108 deletions

View File

@@ -1,38 +0,0 @@
---
name: CI
on:
push:
jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php_version:
- "7.4"
- "7.3"
- "7.2"
- "7.1"
steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php_version }}
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache composer dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install dependencies
run: composer update
- name: Validate dependencies
run: composer validate
- name: Run tests
run: vendor/bin/phpunit --configuration tests/phpunit.xml

82
.github/workflows/phpunit.yml vendored Normal file
View File

@@ -0,0 +1,82 @@
---
name: PHPUnit
on:
push:
jobs:
test_php_82_and_newer:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php_version:
- "8.3"
- "8.2"
steps:
- uses: actions/checkout@v3
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php_version }}
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache composer dependencies
uses: actions/cache@v4
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-
- name: Update PHPUnit
run: composer require phpunit/phpunit --dev -W
- name: Install dependencies
run: composer update
- name: Validate dependencies
run: composer validate
- name: install Rector
run: composer require rector/rector --dev -W
- name: run Rector
run: cd tests && ../vendor/bin/phpunit --migrate-configuration
shell: bash
- name: run Rector
run: cd tests && ../vendor/bin/rector process .
shell: bash
- name: Run tests
run: vendor/bin/phpunit --configuration tests/phpunit.xml
test_php_81_and_lower:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php_version:
- "8.1"
- "8.0"
- "7.4"
- "7.3"
- "7.2"
- "7.1"
steps:
- uses: actions/checkout@v3
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php_version }}
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache composer dependencies
uses: actions/cache@v4
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-
- name: Update PHPUnit
run: composer require phpunit/phpunit --dev -W
- name: Install dependencies
run: composer update
- name: Validate dependencies
run: composer validate
- name: Run tests
run: vendor/bin/phpunit --configuration tests/phpunit.xml

7
.gitignore vendored
View File

@@ -1,7 +1,14 @@
*.bak
.env
.envrc
/.idea
/.vscode
/composer-setup.php
/composer.lock
/coverage_clover.xml
/docker-compose.yml
/examples/people.csv
/phive.xml
/tests/.phpunit.result.cache
/tools
/vendor/

View File

@@ -1,3 +1,16 @@
ParseCSV 1.3.2
-----------------------------------
Date: 07-Nov-2021
Bugfix:
- Allow _guess_delimiter to work with a single
row of data. As a consequence, `$csv->auto()`
now works for files with just one row of data.
See issue #206.
-----------------------------------
ParseCSV 1.3.1
-----------------------------------
Date: 20-Jun-2021

View File

@@ -1,8 +1,8 @@
# ParseCsv
[![Financial Contributors on Open Collective](https://opencollective.com/parsecsv/all/badge.svg?label=financial+contributors)](https://opencollective.com/parsecsv)
![PHPUnit](https://github.com/parsecsv/parsecsv-for-php/actions/workflows/phpunit.yml/badge.svg)
ParseCsv is an easy-to-use PHP class that reads and writes CSV data properly. It
fully conforms to the specifications outlined on the on the
fully conforms to the specifications outlined on the
[Wikipedia article][CSV] (and thus RFC 4180). It has many advanced features which help make your
life easier when dealing with CSV data.
@@ -28,7 +28,7 @@ and third-party support for handling CSV data in PHP.
* Support for character encoding conversion using PHP's
`iconv()` and `mb_convert_encoding()` functions.
* Supports PHP 5.5 and higher.
It certainly works with PHP 7.2 and all versions in between.
It certainly works with PHP 8.3 and all versions in between.
## Installation
@@ -165,6 +165,8 @@ composer install
composer run test
````
Note that PHP 8.2 and newer allow PHPUnit versions that deprecate `@annotations`. The GitHub actions use Rector to
convert them to `#[attributes]`.
When pushing code to GitHub, tests will be executed using GitHub Actions. The relevant configuration is in the
file `.github/workflows/ci.yml`. To run the `test` action locally, you can execute the following command:
@@ -194,30 +196,6 @@ This project exists thanks to all the people who contribute.
Please find a complete list on the project's [contributors][] page.
[contributors]: https://github.com/parsecsv/parsecsv-for-php/graphs/contributors
<a href="https://github.com/parsecsv/parsecsv-for-php/graphs/contributors"><img src="https://opencollective.com/parsecsv/contributors.svg?width=890&button=false" /></a>
### Financial Contributors
Become a financial contributor and help us sustain our community. [[Contribute](https://opencollective.com/parsecsv/contribute)]
#### Individuals
<a href="https://opencollective.com/parsecsv"><img src="https://opencollective.com/parsecsv/individuals.svg?width=890"></a>
#### Organizations
Support this project with your organization. Your logo will show up here with a link to your website. [[Contribute](https://opencollective.com/parsecsv/contribute)]
<a href="https://opencollective.com/parsecsv/organization/0/website"><img src="https://opencollective.com/parsecsv/organization/0/avatar.svg"></a>
<a href="https://opencollective.com/parsecsv/organization/1/website"><img src="https://opencollective.com/parsecsv/organization/1/avatar.svg"></a>
<a href="https://opencollective.com/parsecsv/organization/2/website"><img src="https://opencollective.com/parsecsv/organization/2/avatar.svg"></a>
<a href="https://opencollective.com/parsecsv/organization/3/website"><img src="https://opencollective.com/parsecsv/organization/3/avatar.svg"></a>
<a href="https://opencollective.com/parsecsv/organization/4/website"><img src="https://opencollective.com/parsecsv/organization/4/avatar.svg"></a>
<a href="https://opencollective.com/parsecsv/organization/5/website"><img src="https://opencollective.com/parsecsv/organization/5/avatar.svg"></a>
<a href="https://opencollective.com/parsecsv/organization/6/website"><img src="https://opencollective.com/parsecsv/organization/6/avatar.svg"></a>
<a href="https://opencollective.com/parsecsv/organization/7/website"><img src="https://opencollective.com/parsecsv/organization/7/avatar.svg"></a>
<a href="https://opencollective.com/parsecsv/organization/8/website"><img src="https://opencollective.com/parsecsv/organization/8/avatar.svg"></a>
<a href="https://opencollective.com/parsecsv/organization/9/website"><img src="https://opencollective.com/parsecsv/organization/9/avatar.svg"></a>
## License

View File

@@ -34,7 +34,7 @@
"php": ">=5.5"
},
"require-dev": {
"phpunit/phpunit": "^6",
"phpunit/phpunit": "8.5.52",
"squizlabs/php_codesniffer": "^3.5"
},
"suggest": {

View File

@@ -419,7 +419,7 @@ class Csv {
}
$mode = FileProcessingModeEnum::getAppendMode($append);
$is_php = preg_match('/\.php$/i', $file) ? true : false;
$is_php = (bool) preg_match('/\.php$/i', $file);
return $this->_wfile($file, $this->unparse($data, $fields, $append, $is_php), $mode);
}
@@ -855,9 +855,8 @@ class Csv {
$string .= implode($delimiter, $entry) . $this->linefeed;
$entry = array();
}
// create data
foreach ($data as $key => $row) {
foreach ($data as $row) {
foreach (array_keys($fieldOrder) as $index) {
$cell_value = $row[$index];
$entry[] = $this->_enclose_value($cell_value, $delimiter);
@@ -1109,6 +1108,8 @@ class Csv {
'is greater than or equals',
'contains',
'does not contain',
'is number',
'is not number',
);
$operators_regex = array();
@@ -1150,6 +1151,10 @@ class Csv {
$op_equals = in_array($op, ['=', 'equals', 'is'], true);
if ($op_equals && $row[$field] == $value) {
return '1';
} elseif ($op_equals && $value == 'number' && is_numeric($row[$field])) {
return '1';
} elseif (($op == '!=' || $op == 'is not') && $value == 'number' && !is_numeric($row[$field])) {
return '1';
} elseif (($op == '!=' || $op == 'is not') && $row[$field] != $value) {
return '1';
} elseif (($op == '<' || $op == 'is less than') && $row[$field] < $value) {

View File

@@ -61,10 +61,15 @@ class ConstructTest extends TestCase {
$ob_get_clean = ob_get_clean();
$verb = strtok($script_file, '_.');
if (!in_array($verb, ['download', 'save'], true)) {
if (!in_array($verb, ['download', 'save'], TRUE)) {
if (method_exists($this, 'assertStringContainsString')) {
$this->assertStringContainsString('<td>', $ob_get_clean);
}
else {
$this->assertContains('<td>', $ob_get_clean);
}
}
}
chdir('..');
}
}

View File

@@ -21,7 +21,7 @@ class DataRowCountTest extends TestCase {
$this->csv = new Csv();
}
public function countRowsProvider() {
public static function countRowsProvider() {
return [
'auto-double-enclosure' => [
'auto-double-enclosure.csv',

View File

@@ -63,7 +63,7 @@ class ParseTest extends TestCase {
/**
* @return array
*/
public function autoDetectionProvider() {
public static function autoDetectionProvider() {
return [
'UTF8_no_BOM' => [__DIR__ . '/../example_files/UTF-8_sep_row_but_no_BOM.csv'],
'UTF8' => [__DIR__ . '/../example_files/UTF-8_with_BOM_and_sep_row.csv'],
@@ -87,7 +87,9 @@ class ParseTest extends TestCase {
self::assertEquals(false, $this->csv->autoDetectionForDataString($sInput));
self::assertEquals(null, $this->csv->delimiter);
$expected_data = explode("\r\n", $sInput);
$actual_data = array_map('reset', $this->csv->data);
$actual_data = array_map(function ($k) {
return reset($k);
}, $this->csv->data);
self::assertEquals($expected_data, $actual_data);
}
@@ -98,7 +100,9 @@ class ParseTest extends TestCase {
$expected_data = [86545235689, 34365587654, 13469874576];
$actual_data = $this->invokeMethod($this->csv, '_parse_string', [$sInput]);
$actual_column = array_map('reset', $actual_data);
$actual_column = array_map(function ($k) {
return reset($k);
}, $actual_data);
self::assertEquals($expected_data, $actual_column);
self::assertEquals(
[
@@ -106,7 +110,9 @@ class ParseTest extends TestCase {
'b',
"c\r\nd",
],
array_map('next', $actual_data)
array_map(function ($el) {
return next($el);
}, $actual_data)
);
}
@@ -288,7 +294,7 @@ class ParseTest extends TestCase {
/**
* @return array
*/
public function autoQuotesDataProvider(): array {
public static function autoQuotesDataProvider(): array {
return array(
array('auto-double-enclosure.csv', '"'),
array('auto-single-enclosure.csv', "'"),

View File

@@ -51,11 +51,39 @@ class SaveTest extends TestCase {
public function testSaveWithNewHeader() {
$this->csv->linefeed = "\n";
$this->csv->titles = array("NewTitle");
$this->csv->titles = ["NewTitle"];
$expected = "NewTitle\n0444\n5555\n";
$this->saveAndCompare($expected);
}
public function testSaveWithDelimiterOfComma() {
$this->csv = new Csv();
$this->csv->heading = false;
$this->csv->delimiter = ",";
$this->csv->linefeed = "\n";
$this->csv->data = [
[
'3,21',
'Twitter',
'Monsieur',
'eat more vegan food',
],
[
'"9,72"',
'newsletter',
'Madame',
'"free travel"',
],
];
// Yep, these double quotes are what Excel and Open Office understand.
$expected =
'"3,21",Twitter,Monsieur,eat more vegan food' . "\n" .
'"""9,72""",newsletter,Madame,"""free travel"""' . "\n";
$actual = $this->csv->unparse();
self::assertSame($expected, $actual);
}
public function testSaveWithoutHeader() {
$this->csv->linefeed = "\n";
$this->csv->heading = false;
@@ -63,9 +91,9 @@ class SaveTest extends TestCase {
$this->saveAndCompare($expected);
}
public function testAllQuotes() {
public function testEncloseAllWithQuotes() {
$this->csv->enclose_all = true;
$expected = "\"SMS\"\r\"0444\"\r\"5555\"\r";
$expected = '"SMS"'."\r".'"0444"'."\r".'"5555"'."\r";
$this->saveAndCompare($expected);
}

View File

@@ -20,7 +20,7 @@ class UnparseTest extends Testcase {
}
public function testUnparseWithParameters() {
$fields = array('a' => 'AA', 'b' => 'BB');
$fields = ['a' => 'AA', 'b' => 'BB'];
$data = [['a' => 'value1', 'b' => 'value2']];
$csv_object = new Csv();
$csv_string = $csv_object->unparse($data, $fields);
@@ -43,6 +43,9 @@ class UnparseTest extends Testcase {
$this->unparseAndCompare($expected);
}
/**
* @doesNotPerformAssertions
*/
public function testUnparseDefaultWithoutHeading() {
$this->csv->heading = false;
$this->csv->auto(__DIR__ . '/fixtures/auto-double-enclosure.csv');
@@ -52,24 +55,21 @@ class UnparseTest extends Testcase {
public function testUnparseRenameFields() {
$expected = "C1,C2\rvalue1,value2\rvalue3,value4\r";
$this->unparseAndCompare($expected, array("C1", "C2"));
$this->unparseAndCompare($expected, ["C1", "C2"]);
}
public function testReorderFields() {
$expected = "column2,column1\rvalue2,value1\rvalue4,value3\r";
$this->unparseAndCompare($expected, array("column2", "column1"));
$this->unparseAndCompare($expected, ["column2", "column1"]);
}
public function testSubsetFields() {
$expected = "column1\rvalue1\rvalue3\r";
$this->unparseAndCompare($expected, array("column1"));
$this->unparseAndCompare($expected, ["column1"]);
}
public function testReorderAndRenameFields() {
$fields = array(
'column2' => 'C2',
'column1' => 'C1',
);
$fields = ['column2' => 'C2', 'column1' => 'C1'];
$expected = "C2,C1\rvalue2,value1\rvalue4,value3\r";
$this->unparseAndCompare($expected, $fields);
}
@@ -99,7 +99,7 @@ class UnparseTest extends Testcase {
$this->unparseAndCompare($expected);
}
private function unparseAndCompare($expected, $fields = array()) {
private function unparseAndCompare($expected, $fields = []) {
$str = $this->csv->unparse($this->csv->data, $fields);
$this->assertEquals($expected, $str);
}

View File

@@ -25,7 +25,7 @@ class OffsetTest extends BaseClass {
$this->assertEquals($expected, $actual);
}
public function numberRangeZeroToFourProvider() {
public static function numberRangeZeroToFourProvider() {
return array_map(function ($number) {
return [$number];
}, range(0, 4));

View File

@@ -7,6 +7,7 @@ class SortByTest extends BaseClass {
public function testSortByRating() {
$this->csv->sort_by = 'rating';
$this->csv->conditions = 'title does not contain Blood';
if (!preg_match('/^8\.2\./', phpversion()) && !preg_match('/^8\.3\./', phpversion()))
$this->_compareWithExpected([
// Rating 0
'The Killing Kind',
@@ -27,6 +28,27 @@ class SortByTest extends BaseClass {
'Digital Fortress : A Thriller (Mass Market Paperback)',
'Angels & Demons (Mass Market Paperback)',
]);
else
$this->_compareWithExpected([
// Rating 0
'The Killing Kind',
'The Third Secret',
// Rating 3
'The Last Templar',
'The Broker (Paperback)',
// Rating 4
'Deception Point (Paperback)',
'The Rule of Four (Paperback)',
'The Da Vinci Code (Hardcover)',
// Rating 5
'Angels & Demons (Mass Market Paperback)',
'State of Fear (Paperback)',
'Prey',
'Digital Fortress : A Thriller (Mass Market Paperback)',
]);
}
public function testReverseSortByRating() {

18
tests/rector.php Normal file
View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
use Rector\Config\RectorConfig;
use Rector\PHPUnit\AnnotationsToAttributes\Rector\ClassMethod\DataProviderAnnotationToAttributeRector;
use Rector\PHPUnit\AnnotationsToAttributes\Rector\ClassMethod\DependsAnnotationWithValueToAttributeRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\AddCoversClassAttributeRector;
use Rector\PHPUnit\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector;
return static function(RectorConfig $rectorConfig): void {
if (class_exists(AddCoversClassAttributeRector::class)) {
$rectorConfig->rule(AddCoversClassAttributeRector::class);
}
$rectorConfig->rule(DataProviderAnnotationToAttributeRector::class);
$rectorConfig->rule(AddDoesNotPerformAssertionToNonAssertingTestRector::class);
$rectorConfig->rule(DependsAnnotationWithValueToAttributeRector::class);
};