mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 08:36:39 +00:00
Compare commits
212 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9c14bc2f30 | ||
|
|
4b6b7ee0b8 | ||
|
|
a80a6f1862 | ||
|
|
da70838698 | ||
|
|
abecaf1b5d | ||
|
|
3af94399e6 | ||
|
|
499088b3b2 | ||
|
|
fd63ff4a26 | ||
|
|
d250c9034b | ||
|
|
bfc2c0901b | ||
|
|
a0336f02ee | ||
|
|
ea78f38e26 | ||
|
|
c9e7997643 | ||
|
|
68239d6859 | ||
|
|
d05577d303 | ||
|
|
eedbffc375 | ||
|
|
11b20a3144 | ||
|
|
7766bf7c3b | ||
|
|
ef44ea3989 | ||
|
|
2ac9f450f1 | ||
|
|
3968659b9f | ||
|
|
d3a084d973 | ||
|
|
b0355be96a | ||
|
|
b753d9c694 | ||
|
|
2b95ec5aa1 | ||
|
|
3f595dfd3b | ||
|
|
081949339b | ||
|
|
a60aae47a4 | ||
|
|
4cba97ad51 | ||
|
|
2206ec1e7c | ||
|
|
d188921ab6 | ||
|
|
85cc0d9cfc | ||
|
|
a7f2bbb7bc | ||
|
|
d484f3e9e3 | ||
|
|
f8b2fe274a | ||
|
|
17bfd9496a | ||
|
|
ec7b1c1d55 | ||
|
|
ba9eae17d4 | ||
|
|
8f26ce6ea3 | ||
|
|
913dc94e2c | ||
|
|
5ced1f4212 | ||
|
|
ca84469950 | ||
|
|
75902f4a22 | ||
|
|
7470d2b804 | ||
|
|
01f0891cfb | ||
|
|
52a9b61b97 | ||
|
|
5660be3373 | ||
|
|
70b6fe4bd8 | ||
|
|
f1a89127c2 | ||
|
|
31dba0411e | ||
|
|
6da0537197 | ||
|
|
91091ac933 | ||
|
|
2c7c552515 | ||
|
|
52e87953a4 | ||
|
|
2b6e38734c | ||
|
|
af7368378c | ||
|
|
f2fb7c81bd | ||
|
|
92653c99c5 | ||
|
|
d4f9c9f020 | ||
|
|
2b17f01a0a | ||
|
|
9551862a95 | ||
|
|
736bc489e6 | ||
|
|
f702ca93fe | ||
|
|
48cec20f4f | ||
|
|
c266ef9fbc | ||
|
|
76a0406eff | ||
|
|
17ac1dee1a | ||
|
|
f6bd414ae7 | ||
|
|
a7a0bc34a4 | ||
|
|
0e5afb209e | ||
|
|
f8c68a6cd7 | ||
|
|
5babb55879 | ||
|
|
8ec87126dc | ||
|
|
d788135bbe | ||
|
|
301f6da3e6 | ||
|
|
b9721a9aff | ||
|
|
3c31b70724 | ||
|
|
823f51c4f0 | ||
|
|
488b8f9d9b | ||
|
|
b004911218 | ||
|
|
47f691ce33 | ||
|
|
ab6b3d19e0 | ||
|
|
2d7ab4b2f0 | ||
|
|
2bf4a2d0cd | ||
|
|
f41a18b1c0 | ||
|
|
003e98e8ff | ||
|
|
d8aefa792e | ||
|
|
a92506d078 | ||
|
|
54e846ee30 | ||
|
|
63932f67a4 | ||
|
|
44ff705a23 | ||
|
|
6432ba2ced | ||
|
|
8ae09c604b | ||
|
|
5ea1d95f31 | ||
|
|
af549dc47e | ||
|
|
12aec86bf7 | ||
|
|
38944ba221 | ||
|
|
5a9a10aeed | ||
|
|
631fae5191 | ||
|
|
18ddde98c0 | ||
|
|
a07de14bba | ||
|
|
e87a5ae0e6 | ||
|
|
c04fc89c43 | ||
|
|
cc908fd096 | ||
|
|
155c50ad7a | ||
|
|
98edcbfec3 | ||
|
|
378353eb79 | ||
|
|
e4ca0062f6 | ||
|
|
e419663679 | ||
|
|
0ecaa80222 | ||
|
|
0ac5064d58 | ||
|
|
4d011827f5 | ||
|
|
c818fff81a | ||
|
|
ccad5c8360 | ||
|
|
b082849b17 | ||
|
|
7536d55f27 | ||
|
|
db1e242d07 | ||
|
|
fd5fb96bce | ||
|
|
4f08cbb561 | ||
|
|
0d14ca01be | ||
|
|
a4a0dfa2fd | ||
|
|
95ad7c315a | ||
|
|
0e50aec3b9 | ||
|
|
f5851ba9ac | ||
|
|
fad5ebcdc8 | ||
|
|
bd2031582d | ||
|
|
1db4b02e9e | ||
|
|
07f2306a9c | ||
|
|
7eb6daa6fb | ||
|
|
240fa0ce23 | ||
|
|
1321c3b693 | ||
|
|
b9cf7cb0c1 | ||
|
|
4bae0d2bcf | ||
|
|
189746405f | ||
|
|
2ae35c26ff | ||
|
|
2d1b7cebde | ||
|
|
b33cc6c667 | ||
|
|
fa5aea7631 | ||
|
|
4f144f2662 | ||
|
|
ba4c30add1 | ||
| a8b443d48c | |||
|
|
9c6e0d64cf | ||
| 83ec2fbce7 | |||
|
|
e750fcfb07 | ||
|
|
b7d1848363 | ||
|
|
8bb51f8bab | ||
|
|
22fcff2d24 | ||
|
|
5e8a5df988 | ||
| 813ef3849e | |||
| d795ad0e05 | |||
|
|
ac4c4699cb | ||
| e0728a3941 | |||
|
|
2f3732e350 | ||
|
|
01e71adf36 | ||
|
|
0c4a804db1 | ||
|
|
4366201dc0 | ||
|
|
0422d5d9fe | ||
|
|
6f4d150e6f | ||
|
|
494087ab1d | ||
|
|
ce19deec4e | ||
|
|
33825514d9 | ||
|
|
8af4d1e513 | ||
|
|
4d07760933 | ||
|
|
710f22eeb9 | ||
|
|
604872faad | ||
|
|
8e19ecf94f | ||
|
|
31417caafd | ||
|
|
17e060ea33 | ||
|
|
936b6f4a50 | ||
|
|
65061046bd | ||
|
|
4393781abc | ||
|
|
8bbd474e66 | ||
|
|
734b35abc5 | ||
|
|
5cb602be2d | ||
|
|
b8920f99cb | ||
|
|
b8248af92e | ||
|
|
d1f25ee3df | ||
|
|
0d5ae35a34 | ||
|
|
e1c8cae852 | ||
|
|
844a1520f3 | ||
|
|
81746a561f | ||
|
|
1524618293 | ||
|
|
e98cef4846 | ||
|
|
4ed88da7a2 | ||
|
|
18a6479aec | ||
|
|
1c4bcaffa6 | ||
|
|
7527267c68 | ||
|
|
b074f83644 | ||
|
|
7b83653ee2 | ||
|
|
0ed23e7b84 | ||
|
|
4a76ed9ca9 | ||
|
|
68eab2de59 | ||
|
|
32b50efe82 | ||
|
|
372c43fba4 | ||
|
|
7d63a8c894 | ||
|
|
7187bbd68f | ||
|
|
ee784f3180 | ||
|
|
633666060b | ||
|
|
8a5f15bdd8 | ||
|
|
a6f8c708f9 | ||
| cff692d976 | |||
|
|
c3c13258f3 | ||
| 9cf0ba9f97 | |||
|
|
18eb8a8f11 | ||
|
|
44a5507e10 | ||
|
|
3ffcd731ee | ||
|
|
3545dbeb00 | ||
|
|
0235c24b36 | ||
|
|
33b1a0d196 | ||
|
|
f3c70ad17c | ||
|
|
98a146bd38 | ||
|
|
2ed5801b33 |
15
.editorconfig
Normal file
15
.editorconfig
Normal file
@@ -0,0 +1,15 @@
|
||||
# @see http://editorconfig.org/
|
||||
|
||||
# This is the top-most .editorconfig file; do not search in parent directories.
|
||||
root = true
|
||||
|
||||
# All files.
|
||||
[*]
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
|
||||
[composer.json]
|
||||
indent_size = 4
|
||||
6
.gitignore
vendored
6
.gitignore
vendored
@@ -1,2 +1,6 @@
|
||||
*.bak
|
||||
/.idea
|
||||
/phive.xml
|
||||
/tools
|
||||
composer.lock
|
||||
vendor/
|
||||
composer.lock
|
||||
19
.travis.yml
Normal file
19
.travis.yml
Normal file
@@ -0,0 +1,19 @@
|
||||
language: php
|
||||
|
||||
php:
|
||||
- 7.2
|
||||
- 7.1
|
||||
- 7.0
|
||||
- 5.6
|
||||
- 5.5
|
||||
- 5.4
|
||||
|
||||
script:
|
||||
- phpunit --version
|
||||
- phpunit --configuration tests/phpunit.xml
|
||||
|
||||
notifications:
|
||||
email:
|
||||
- will.knauss@gmail.com
|
||||
on_success: never
|
||||
on_failure: always
|
||||
@@ -1,3 +1,29 @@
|
||||
ParseCSV 1.0.0
|
||||
-----------------------------------
|
||||
Date: 3-March-2018
|
||||
|
||||
- Renamed class from parseCSV to Csv and added name-
|
||||
space "ParseCsv" for PSR compliance.
|
||||
|
||||
- Added support for MS Excel's "sep=" to detect the
|
||||
delimiter (Issue #60).
|
||||
|
||||
- Added data type detection - function getDatatypes()
|
||||
guesses the type of each column.
|
||||
|
||||
- MIME: output() sends correct MIME type to browser
|
||||
if the separator is a tab tab (Issue #79).
|
||||
|
||||
- Added support for mb_convert_encoding() instead of
|
||||
iconv() - see issue #109.
|
||||
|
||||
- A number of minor bug fixes - see GitHub issues.
|
||||
|
||||
- Added many more unit tests.
|
||||
|
||||
-----------------------------------
|
||||
|
||||
|
||||
parseCSV 0.4.3 beta
|
||||
-----------------------------------
|
||||
Date: 1-July-2008
|
||||
@@ -10,7 +36,7 @@ Date: 1-July-2008
|
||||
- Issue #6. Raw loaded file data is now cleared from
|
||||
file_data property when it has been successfully
|
||||
parsed to keep parseCSV's memory footprint to a
|
||||
minimum. Specifically handy when using mulitple
|
||||
minimum. Specifically handy when using multiple
|
||||
instances of parseCSV to process large files.
|
||||
|
||||
-----------------------------------
|
||||
@@ -159,7 +185,7 @@ Date: 2-Jan-2007
|
||||
|
||||
- Added auto() function to automatically detect
|
||||
delimiter character.
|
||||
Useful for user upload incase delimiter is
|
||||
Useful for user upload in case delimiter is
|
||||
comma (,), tab, or semi-colon (;). Some
|
||||
versions of MS Excel for Windows use
|
||||
semi-colons instead of commas when saving to
|
||||
@@ -170,7 +196,7 @@ Date: 2-Jan-2007
|
||||
almost no matter what the delimiter is.
|
||||
|
||||
- Generally updated some of the core workings
|
||||
to increase performance, and offer better
|
||||
to increase performance, and offer better
|
||||
support for large (1MB and up) files.
|
||||
|
||||
- Added code examples to header comment.
|
||||
|
||||
15
Makefile
Normal file
15
Makefile
Normal file
@@ -0,0 +1,15 @@
|
||||
COMPOSER_BIN_DIR := vendor/bin
|
||||
PHPUNIT_ARGS = -c tests/phpunit.xml
|
||||
|
||||
test: phpunit-dep
|
||||
${COMPOSER_BIN_DIR}/phpunit ${PHPUNIT_ARGS}
|
||||
|
||||
phpunit-dep:
|
||||
test -f ${COMPOSER_BIN_DIR}/phpunit || ( \
|
||||
echo "phpunit is required to run tests." \
|
||||
"Please run: composer install" >&2 && \
|
||||
exit 1 \
|
||||
)
|
||||
|
||||
.SILENT:
|
||||
.PHONY: test phpunit-dep
|
||||
78
README.md
78
README.md
@@ -1,22 +1,20 @@
|
||||
# parseCSV
|
||||
# ParseCsv
|
||||
|
||||
Note: parseCSV is now being revised to PHP 5 standards. If you have an issue or a feature request we encourage you to open an issue.
|
||||
|
||||
parseCSV is an easy to use PHP class that reads and writes CSV data properly. It
|
||||
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
|
||||
[Wikipedia article][CSV]. It has many advanced features which help make your
|
||||
[Wikipedia article][CSV] (and thus RFC 4180). It has many advanced features which help make your
|
||||
life easier when dealing with CSV data.
|
||||
|
||||
This library was originaly created in early 2007 by [jimeh](https://github.com/jimeh) due to the lack of built-in
|
||||
You may not need a library at all: before using ParseCsv, please make sure if PHP's own `str_getcsv()`, ``fgetcvs()`` or `fputcsv()` meets your needs.
|
||||
|
||||
This library was originally created in early 2007 by [jimeh](https://github.com/jimeh) due to the lack of built-in
|
||||
and third-party support for handling CSV data in PHP.
|
||||
|
||||
[csv]: http://en.wikipedia.org/wiki/Comma-separated_values
|
||||
|
||||
|
||||
## Features
|
||||
|
||||
* parseCSV is the only complete and fully featured CSV solution for PHP (as
|
||||
far as I know).
|
||||
* ParseCsv is a complete and fully featured CSV solution for PHP
|
||||
* Supports enclosed values, enclosed commas, double quotes and new lines.
|
||||
* Automatic delimiter character detection.
|
||||
* Sort data by specific fields/columns.
|
||||
@@ -26,24 +24,45 @@ and third-party support for handling CSV data in PHP.
|
||||
* Error detection for incorrectly formatted input. It attempts to be
|
||||
intelligent, but can not be trusted 100% due to the structure of CSV, and
|
||||
how different programs like Excel for example outputs CSV data.
|
||||
* Support for character encoding conversion using PHP's _iconv_ function
|
||||
(requires PHP 5).
|
||||
* Supports both PHP 4 & 5.
|
||||
* Support for character encoding conversion using PHP's
|
||||
`iconv()` and `mb_convert_encoding()` functions.
|
||||
* Supports PHP 5.4 and higher.
|
||||
It certainly works with PHP 7.2 and all versions in between.
|
||||
|
||||
## Installation
|
||||
|
||||
Installation is easy using Composer. Just run the following on the
|
||||
command line:
|
||||
```
|
||||
composer require parsecsv/php-parsecsv
|
||||
```
|
||||
|
||||
If you don't use a framework such as Drupal, Laravel, Symfony, Yii etc.,
|
||||
you may have to manually include Composer's autoloader file in your PHP
|
||||
script:
|
||||
```php
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
```
|
||||
|
||||
#### Without composer
|
||||
Not recommended, but technically possible: you can also clone the
|
||||
repository or extract the
|
||||
[ZIP](https://github.com/parsecsv/parsecsv-for-php/archive/master.zip).
|
||||
To use ParseCSV, you then have to add a `require 'parsecsv.lib.php';` line.
|
||||
|
||||
## Example Usage
|
||||
|
||||
**General**
|
||||
|
||||
```php
|
||||
$csv = new parseCSV('data.csv');
|
||||
$csv = new ParseCsv\Csv('data.csv');
|
||||
print_r($csv->data);
|
||||
```
|
||||
|
||||
**Tab delimited, and encoding conversion**
|
||||
|
||||
```php
|
||||
$csv = new parseCSV();
|
||||
$csv = new ParseCsv\Csv();
|
||||
$csv->encoding('UTF-16', 'UTF-8');
|
||||
$csv->delimiter = "\t";
|
||||
$csv->parse('data.tsv');
|
||||
@@ -53,7 +72,7 @@ print_r($csv->data);
|
||||
**Auto-detect delimiter character**
|
||||
|
||||
```php
|
||||
$csv = new parseCSV();
|
||||
$csv = new ParseCsv\Csv();
|
||||
$csv->auto('data.csv');
|
||||
print_r($csv->data);
|
||||
```
|
||||
@@ -61,7 +80,7 @@ print_r($csv->data);
|
||||
**Modify data in a CSV file**
|
||||
|
||||
```php
|
||||
$csv = new parseCSV();
|
||||
$csv = new ParseCsv\Csv();
|
||||
$csv->sort_by = 'id';
|
||||
$csv->parse('data.csv');
|
||||
# "4" is the value of the "id" column of the CSV row
|
||||
@@ -69,27 +88,36 @@ $csv->data[4] = array('firstname' => 'John', 'lastname' => 'Doe', 'email' => 'jo
|
||||
$csv->save();
|
||||
```
|
||||
|
||||
**Add row/entry to end of CSV file**
|
||||
|
||||
_Only recommended when you know the extact sctructure of the file._
|
||||
**Replace field names or set ones if missing**
|
||||
|
||||
```php
|
||||
$csv = new parseCSV();
|
||||
$csv->save('data.csv', array('1986', 'Home', 'Nowhere', ''), true);
|
||||
$csv = new ParseCsv\Csv();
|
||||
$csv->fields = ['id', 'name', 'category']
|
||||
$csv->parse('data.csv');
|
||||
```
|
||||
|
||||
**Convert 2D array to csv data and send headers to browser to treat output as
|
||||
**Add row/entry to end of CSV file**
|
||||
|
||||
_Only recommended when you know the exact structure of the file._
|
||||
|
||||
```php
|
||||
$csv = new ParseCsv\Csv();
|
||||
$csv->save('data.csv', array(array('1986', 'Home', 'Nowhere', '')), true);
|
||||
```
|
||||
|
||||
**Convert 2D array to CSV data and send headers to browser to treat output as
|
||||
a file and download it**
|
||||
|
||||
```php
|
||||
$csv = new parseCSV();
|
||||
$csv = new ParseCsv\Csv();
|
||||
$csv->output('movies.csv', $array, array('field 1', 'field 2'), ',');
|
||||
```
|
||||
|
||||
For more complex examples, see the ``tests`` and `examples` directories.
|
||||
|
||||
## Credits
|
||||
|
||||
* parseCSV is based on the concept of [Ming Hong Ng][ming]'s [CsvFileParser][]
|
||||
* ParseCsv is based on the concept of [Ming Hong Ng][ming]'s [CsvFileParser][]
|
||||
class.
|
||||
|
||||
[ming]: http://minghong.blogspot.com/
|
||||
@@ -127,3 +155,5 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
[](https://travis-ci.org/parsecsv/parsecsv-for-php)
|
||||
|
||||
@@ -6,10 +6,23 @@
|
||||
{
|
||||
"name": "Jim Myhrberg",
|
||||
"email": "contact@jimeh.me"
|
||||
},
|
||||
{
|
||||
"name": "William Knauss",
|
||||
"email": "will.knauss@gmail.com"
|
||||
}
|
||||
],
|
||||
"autoload": {
|
||||
"classmap": ["."]
|
||||
"autoload":{
|
||||
"psr-4":{
|
||||
"ParseCsv\\": "src",
|
||||
"ParseCsv\\extensions\\": "src\\extensions",
|
||||
"ParseCsv\\tests\\": "tests"
|
||||
}
|
||||
},
|
||||
"autoload-dev":{
|
||||
"psr-4":{
|
||||
"ParseCsv\\tests\\": "tests"
|
||||
}
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "4.1.*"
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
<pre>
|
||||
<?php
|
||||
|
||||
|
||||
# include parseCSV class.
|
||||
require_once('../parsecsv.lib.php');
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
use ParseCsv\Csv;
|
||||
|
||||
|
||||
# create new parseCSV object.
|
||||
$csv = new parseCSV();
|
||||
$csv = new Csv();
|
||||
|
||||
|
||||
# Parse '_books.csv' using automatic delimiter detection...
|
||||
@@ -28,21 +28,29 @@ $csv->auto('_books.csv');
|
||||
?>
|
||||
</pre>
|
||||
<style type="text/css" media="screen">
|
||||
table { background-color: #BBB; }
|
||||
th { background-color: #EEE; }
|
||||
td { background-color: #FFF; }
|
||||
table {
|
||||
background-color: #BBB;
|
||||
}
|
||||
|
||||
th {
|
||||
background-color: #EEE;
|
||||
}
|
||||
|
||||
td {
|
||||
background-color: #FFF;
|
||||
}
|
||||
</style>
|
||||
<table border="0" cellspacing="1" cellpadding="3">
|
||||
<tr>
|
||||
<?php foreach ($csv->titles as $value): ?>
|
||||
<th><?php echo $value; ?></th>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php foreach ($csv->data as $key => $row): ?>
|
||||
<tr>
|
||||
<?php foreach ($row as $value): ?>
|
||||
<td><?php echo $value; ?></td>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
<tr>
|
||||
<?php foreach ($csv->titles as $value): ?>
|
||||
<th><?php echo $value; ?></th>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php foreach ($csv->data as $key => $row): ?>
|
||||
<tr>
|
||||
<?php foreach ($row as $value): ?>
|
||||
<td><?php echo $value; ?></td>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</table>
|
||||
|
||||
@@ -3,11 +3,12 @@
|
||||
|
||||
|
||||
# include parseCSV class.
|
||||
require_once('../parsecsv.lib.php');
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
use ParseCsv\Csv;
|
||||
|
||||
|
||||
# create new parseCSV object.
|
||||
$csv = new parseCSV();
|
||||
$csv = new Csv();
|
||||
|
||||
|
||||
# Example conditions:
|
||||
@@ -28,21 +29,29 @@ $csv->auto('_books.csv');
|
||||
?>
|
||||
</pre>
|
||||
<style type="text/css" media="screen">
|
||||
table { background-color: #BBB; }
|
||||
th { background-color: #EEE; }
|
||||
td { background-color: #FFF; }
|
||||
table {
|
||||
background-color: #BBB;
|
||||
}
|
||||
|
||||
th {
|
||||
background-color: #EEE;
|
||||
}
|
||||
|
||||
td {
|
||||
background-color: #FFF;
|
||||
}
|
||||
</style>
|
||||
<table border="0" cellspacing="1" cellpadding="3">
|
||||
<tr>
|
||||
<?php foreach ($csv->titles as $value): ?>
|
||||
<th><?php echo $value; ?></th>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php foreach ($csv->data as $key => $row): ?>
|
||||
<tr>
|
||||
<?php foreach ($row as $value): ?>
|
||||
<td><?php echo $value; ?></td>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</table>
|
||||
<tr>
|
||||
<?php foreach ($csv->titles as $value): ?>
|
||||
<th><?php echo $value; ?></th>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php foreach ($csv->data as $key => $row): ?>
|
||||
<tr>
|
||||
<?php foreach ($row as $value): ?>
|
||||
<td><?php echo $value; ?></td>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</table>
|
||||
|
||||
@@ -2,11 +2,12 @@
|
||||
|
||||
|
||||
# include parseCSV class.
|
||||
require_once('../parsecsv.lib.php');
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
use ParseCsv\Csv;
|
||||
|
||||
|
||||
# create new parseCSV object.
|
||||
$csv = new parseCSV();
|
||||
$csv = new Csv();
|
||||
|
||||
|
||||
# Parse '_books.csv' using automatic delimiter detection...
|
||||
@@ -31,4 +32,4 @@ $csv->output('books.csv');
|
||||
# or is set to null, output will only return the generated CSV
|
||||
# output data, and will not output to the browser itself.
|
||||
|
||||
?>
|
||||
?>
|
||||
|
||||
@@ -3,11 +3,12 @@
|
||||
|
||||
|
||||
# include parseCSV class.
|
||||
require_once('../parsecsv.lib.php');
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
use ParseCsv\Csv;
|
||||
|
||||
|
||||
# create new parseCSV object.
|
||||
$csv = new parseCSV();
|
||||
$csv = new Csv();
|
||||
|
||||
|
||||
# if sorting is enabled, the whole CSV file
|
||||
@@ -41,21 +42,29 @@ $csv->auto('_books.csv');
|
||||
?>
|
||||
</pre>
|
||||
<style type="text/css" media="screen">
|
||||
table { background-color: #BBB; }
|
||||
th { background-color: #EEE; }
|
||||
td { background-color: #FFF; }
|
||||
table {
|
||||
background-color: #BBB;
|
||||
}
|
||||
|
||||
th {
|
||||
background-color: #EEE;
|
||||
}
|
||||
|
||||
td {
|
||||
background-color: #FFF;
|
||||
}
|
||||
</style>
|
||||
<table border="0" cellspacing="1" cellpadding="3">
|
||||
<tr>
|
||||
<?php foreach ($csv->titles as $value): ?>
|
||||
<th><?php echo $value; ?></th>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php foreach ($csv->data as $key => $row): ?>
|
||||
<tr>
|
||||
<?php foreach ($row as $value): ?>
|
||||
<td><?php echo $value; ?></td>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</table>
|
||||
<tr>
|
||||
<?php foreach ($csv->titles as $value): ?>
|
||||
<th><?php echo $value; ?></th>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php foreach ($csv->data as $key => $row): ?>
|
||||
<tr>
|
||||
<?php foreach ($row as $value): ?>
|
||||
<td><?php echo $value; ?></td>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</table>
|
||||
|
||||
1184
parsecsv.lib.php
1184
parsecsv.lib.php
File diff suppressed because it is too large
Load Diff
1212
src/Csv.php
Normal file
1212
src/Csv.php
Normal file
File diff suppressed because it is too large
Load Diff
120
src/enums/DatatypeEnum.php
Normal file
120
src/enums/DatatypeEnum.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace ParseCsv\enums;
|
||||
|
||||
/**
|
||||
* Class DatatypeEnum
|
||||
*
|
||||
* @package ParseCsv\enums
|
||||
*
|
||||
* todo: needs a basic parent enum class for error handling.
|
||||
*/
|
||||
class DatatypeEnum {
|
||||
|
||||
const __DEFAULT = self::TYPE_STRING;
|
||||
|
||||
const TYPE_STRING = 'string';
|
||||
|
||||
const TYPE_FLOAT = 'float';
|
||||
|
||||
const TYPE_INT = 'integer';
|
||||
|
||||
const TYPE_BOOL = 'boolean';
|
||||
|
||||
const TYPE_DATE = 'date';
|
||||
|
||||
const REGEX_FLOAT = '/(^[+-]?$)|(^[+-]?[0-9]+([,.][0-9])?[0-9]*(e[+-]?[0-9]+)?$)/';
|
||||
|
||||
const REGEX_INT = '/^[-+]?[0-9]\d*$/';
|
||||
|
||||
const REGEX_BOOL = '/^(?i:true|false)$/';
|
||||
|
||||
/**
|
||||
* Define validator functions here.
|
||||
*
|
||||
* @var array
|
||||
*
|
||||
* @uses isValidFloat
|
||||
* @uses isValidInteger
|
||||
* @uses isValidBoolean
|
||||
* @uses isValidDate
|
||||
*/
|
||||
private static $validators = array(
|
||||
self::TYPE_STRING => null,
|
||||
self::TYPE_INT => 'isValidInteger',
|
||||
self::TYPE_BOOL => 'isValidBoolean',
|
||||
self::TYPE_FLOAT => 'isValidFloat',
|
||||
self::TYPE_DATE => 'isValidDate',
|
||||
);
|
||||
|
||||
/**
|
||||
* Checks data type for given string.
|
||||
*
|
||||
* @param string $value
|
||||
*
|
||||
* @return bool|string
|
||||
*/
|
||||
public static function getValidTypeFromSample($value) {
|
||||
$value = trim((string) $value);
|
||||
|
||||
if (empty($value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (self::$validators as $type => $validator) {
|
||||
if ($validator === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (method_exists(__CLASS__, $validator) && self::$validator($value)) {
|
||||
return $type;
|
||||
}
|
||||
}
|
||||
|
||||
return self::__DEFAULT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if string is float value.
|
||||
*
|
||||
* @param string $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private static function isValidFloat($value) {
|
||||
return (bool) preg_match(self::REGEX_FLOAT, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if string is integer value.
|
||||
*
|
||||
* @param string $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private static function isValidInteger($value) {
|
||||
return (bool) preg_match(self::REGEX_INT, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if string is boolean.
|
||||
*
|
||||
* @param string $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private static function isValidBoolean($value) {
|
||||
return (bool) preg_match(self::REGEX_BOOL, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if string is date.
|
||||
*
|
||||
* @param string $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private static function isValidDate($value) {
|
||||
return (bool) strtotime($value);
|
||||
}
|
||||
}
|
||||
74
src/extensions/DatatypeTrait.php
Normal file
74
src/extensions/DatatypeTrait.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace ParseCsv\extensions;
|
||||
|
||||
trait DatatypeTrait {
|
||||
|
||||
/**
|
||||
* Datatypes
|
||||
* Datatypes of CSV data-columns
|
||||
*
|
||||
* @access public
|
||||
* @var array
|
||||
*/
|
||||
public $data_types = [];
|
||||
|
||||
/**
|
||||
* Check data type for one column.
|
||||
* Check for most commonly data type for one column.
|
||||
*
|
||||
* @access private
|
||||
*
|
||||
* @param array $datatypes
|
||||
*
|
||||
* @return string|false
|
||||
*/
|
||||
private function getMostFrequentDatatypeForColumn($datatypes) {
|
||||
// remove 'false' from array (can happen if CSV cell is empty)
|
||||
$typesFiltered = array_filter($datatypes);
|
||||
|
||||
if (empty($typesFiltered)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$typesFreq = array_count_values($typesFiltered);
|
||||
arsort($typesFreq);
|
||||
reset($typesFreq);
|
||||
|
||||
return key($typesFreq);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Check data type foreach Column
|
||||
* Check data type for each column and returns the most commonly.
|
||||
*
|
||||
* Requires PHP >= 5.5
|
||||
*
|
||||
* @access public
|
||||
*
|
||||
* @uses getDatatypeFromString
|
||||
*
|
||||
* @return array|bool
|
||||
*/
|
||||
public function getDatatypes() {
|
||||
if (empty($this->data)) {
|
||||
$this->data = $this->parse_string();
|
||||
}
|
||||
if (!is_array($this->data)) {
|
||||
throw new \UnexpectedValueException('No data set yet.');
|
||||
}
|
||||
|
||||
$result = [];
|
||||
foreach ($this->titles as $cName) {
|
||||
$column = array_column($this->data, $cName);
|
||||
$cDatatypes = array_map('ParseCsv\enums\DatatypeEnum::getValidTypeFromSample', $column);
|
||||
|
||||
$result[$cName] = $this->getMostFrequentDatatypeForColumn($cDatatypes);
|
||||
}
|
||||
|
||||
$this->data_types = $result;
|
||||
|
||||
return !empty($this->data_types) ? $this->data_types : [];
|
||||
}
|
||||
}
|
||||
13
tests/Bootstrap.php
Normal file
13
tests/Bootstrap.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
chdir(__DIR__ . '/..');
|
||||
if (!file_exists('vendor/autoload.php')) {
|
||||
`composer dump-autoload`;
|
||||
}
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
if (!class_exists('PHPUnit\Framework\TestCase') && class_exists('PHPUnit_Framework_TestCase')) {
|
||||
// we run on an older PHPUnit version without namespaces.
|
||||
require_once __DIR__ . '/PHPUnit_Framework_TestCase.inc.php';
|
||||
}
|
||||
|
||||
11
tests/PHPUnit_Framework_TestCase.inc.php
Normal file
11
tests/PHPUnit_Framework_TestCase.inc.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace PHPUnit\Framework;
|
||||
|
||||
// Only needed to keep unit test code compatible with older PHPUnit versions
|
||||
|
||||
/** @noinspection PhpUndefinedClassInspection */
|
||||
/** @noinspection AutoloadingIssuesInspection */
|
||||
class TestCase extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
}
|
||||
BIN
tests/example_files/Piwik_API_download.csv
Normal file
BIN
tests/example_files/Piwik_API_download.csv
Normal file
Binary file not shown.
|
BIN
tests/example_files/UTF-16LE_with_BOM_and_sep_row.csv
Normal file
BIN
tests/example_files/UTF-16LE_with_BOM_and_sep_row.csv
Normal file
Binary file not shown.
|
5
tests/example_files/UTF-8_sep_row_but_no_BOM.csv
Normal file
5
tests/example_files/UTF-8_sep_row_but_no_BOM.csv
Normal file
@@ -0,0 +1,5 @@
|
||||
sep=;
|
||||
title;isbn;publishedAt
|
||||
Красивая кулинария;5454-5587-3210;21.05.2011
|
||||
The Wine Connoisseurs;2547-8548-2541;12.12.2011
|
||||
Weißwein;1313-4545-8875;23.02.2012
|
||||
|
5
tests/example_files/UTF-8_with_BOM_and_sep_row.csv
Normal file
5
tests/example_files/UTF-8_with_BOM_and_sep_row.csv
Normal file
@@ -0,0 +1,5 @@
|
||||
sep=;
|
||||
title;isbn;publishedAt
|
||||
Красивая кулинария;5454-5587-3210;21.05.2011
|
||||
The Wine Connoisseurs;2547-8548-2541;12.12.2011
|
||||
Weißwein;1313-4545-8875;23.02.2012
|
||||
|
5
tests/example_files/single_column.csv
Normal file
5
tests/example_files/single_column.csv
Normal file
@@ -0,0 +1,5 @@
|
||||
SMS
|
||||
0444
|
||||
5555
|
||||
6606
|
||||
7777
|
||||
|
67
tests/methods/ConstructTest.php
Normal file
67
tests/methods/ConstructTest.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace ParseCsv\tests\methods;
|
||||
|
||||
use ParseCsv\Csv;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ConstructTest extends TestCase {
|
||||
|
||||
/**
|
||||
* @var Csv object
|
||||
*/
|
||||
protected $csv = null;
|
||||
|
||||
public function test_offset_param() {
|
||||
$offset = 10;
|
||||
$this->csv = new Csv(null, $offset);
|
||||
$this->assertTrue(is_numeric($this->csv->offset));
|
||||
$this->assertEquals($offset, $this->csv->offset);
|
||||
}
|
||||
|
||||
public function test_limit_param() {
|
||||
$limit = 10;
|
||||
$this->csv = new Csv(null, null, $limit);
|
||||
$this->assertTrue(is_numeric($this->csv->limit));
|
||||
$this->assertEquals($limit, $this->csv->limit);
|
||||
}
|
||||
|
||||
public function test_conditions_param() {
|
||||
$conditions = 'some column NOT value';
|
||||
$this->csv = new Csv(null, null, null, $conditions);
|
||||
$this->assertTrue(is_string($this->csv->conditions));
|
||||
$this->assertEquals($conditions, $this->csv->conditions);
|
||||
}
|
||||
|
||||
public function test_keep_file_data_param() {
|
||||
$keep = true;
|
||||
$this->csv = new Csv(null, null, null, null, $keep);
|
||||
$this->assertTrue(is_bool($this->csv->keep_file_data));
|
||||
$this->assertEquals($keep, $this->csv->keep_file_data);
|
||||
}
|
||||
|
||||
public function test_input_param() {
|
||||
$csv = "col1,col2,col3\r\nval1,val2,val3\r\nval1A,val2A,val3A\r\n";
|
||||
$this->csv = new Csv($csv, null, null, null, true);
|
||||
$this->assertTrue(is_string($this->csv->file_data));
|
||||
$this->assertEquals($csv, $this->csv->file_data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess because download.php uses header()
|
||||
*
|
||||
* @see https://github.com/sebastianbergmann/phpunit/issues/720#issuecomment-10421092
|
||||
*/
|
||||
public function testCodeExamples() {
|
||||
chdir('examples');
|
||||
foreach (glob('*.php') as $script_file) {
|
||||
ob_start();
|
||||
/** @noinspection PhpIncludeInspection */
|
||||
require $script_file;
|
||||
if ($script_file != 'download.php') {
|
||||
$this->assertContains('<td>', ob_get_clean());
|
||||
}
|
||||
}
|
||||
chdir('..');
|
||||
}
|
||||
}
|
||||
69
tests/methods/DatatypeTest.php
Normal file
69
tests/methods/DatatypeTest.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: sgorzaly
|
||||
* Date: 19.02.18
|
||||
* Time: 20:52
|
||||
*/
|
||||
|
||||
namespace ParseCsv\tests\methods;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ParseCsv\enums\DatatypeEnum;
|
||||
|
||||
class DatatypeTest extends TestCase
|
||||
{
|
||||
public function testSampleIsValidInteger(){
|
||||
$this->assertEquals(DatatypeEnum::TYPE_INT, DatatypeEnum::getValidTypeFromSample('1'));
|
||||
$this->assertEquals(DatatypeEnum::TYPE_INT, DatatypeEnum::getValidTypeFromSample('+1'));
|
||||
$this->assertEquals(DatatypeEnum::TYPE_INT, DatatypeEnum::getValidTypeFromSample('-1'));
|
||||
$this->assertNotEquals(DatatypeEnum::TYPE_INT, DatatypeEnum::getValidTypeFromSample('--1'));
|
||||
$this->assertNotEquals(DatatypeEnum::TYPE_INT, DatatypeEnum::getValidTypeFromSample('test'));
|
||||
$this->assertNotEquals(DatatypeEnum::TYPE_INT, DatatypeEnum::getValidTypeFromSample('1.0'));
|
||||
$this->assertNotEquals(DatatypeEnum::TYPE_INT, DatatypeEnum::getValidTypeFromSample('1,0'));
|
||||
$this->assertNotEquals(DatatypeEnum::TYPE_INT, DatatypeEnum::getValidTypeFromSample('1,1'));
|
||||
$this->assertNotEquals(DatatypeEnum::TYPE_INT, DatatypeEnum::getValidTypeFromSample('0.1'));
|
||||
$this->assertNotEquals(DatatypeEnum::TYPE_INT, DatatypeEnum::getValidTypeFromSample('true'));
|
||||
$this->assertNotEquals(DatatypeEnum::TYPE_INT, DatatypeEnum::getValidTypeFromSample('2018-02-19'));
|
||||
}
|
||||
|
||||
public function testSampleIsValidBool(){
|
||||
$this->assertEquals(DatatypeEnum::TYPE_BOOL, DatatypeEnum::getValidTypeFromSample('true'));
|
||||
$this->assertEquals(DatatypeEnum::TYPE_BOOL, DatatypeEnum::getValidTypeFromSample('TRUE'));
|
||||
$this->assertEquals(DatatypeEnum::TYPE_BOOL, DatatypeEnum::getValidTypeFromSample('false'));
|
||||
$this->assertEquals(DatatypeEnum::TYPE_BOOL, DatatypeEnum::getValidTypeFromSample('FALSE'));
|
||||
$this->assertNotEquals(DatatypeEnum::TYPE_BOOL, DatatypeEnum::getValidTypeFromSample('FALS'));
|
||||
$this->assertNotEquals(DatatypeEnum::TYPE_BOOL, DatatypeEnum::getValidTypeFromSample('test'));
|
||||
$this->assertNotEquals(DatatypeEnum::TYPE_BOOL, DatatypeEnum::getValidTypeFromSample('0'));
|
||||
$this->assertNotEquals(DatatypeEnum::TYPE_BOOL, DatatypeEnum::getValidTypeFromSample('1'));
|
||||
$this->assertNotEquals(DatatypeEnum::TYPE_BOOL, DatatypeEnum::getValidTypeFromSample('0.1'));
|
||||
}
|
||||
|
||||
public function testSampleIsValidFloat(){
|
||||
$this->assertEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('1.0'));
|
||||
$this->assertEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('-1.1'));
|
||||
$this->assertEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('+1,1'));
|
||||
$this->assertEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('1,1'));
|
||||
$this->assertEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('1,1'));
|
||||
$this->assertEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('1e-03'));
|
||||
$this->assertEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('1e+03'));
|
||||
$this->assertNotEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('1'));
|
||||
$this->assertNotEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('test'));
|
||||
$this->assertNotEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('1,,1'));
|
||||
$this->assertNotEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('1..1'));
|
||||
$this->assertNotEquals(DatatypeEnum::TYPE_FLOAT, DatatypeEnum::getValidTypeFromSample('2018-02-19'));
|
||||
}
|
||||
|
||||
public function testSampleIsValidDate(){
|
||||
$this->assertEquals(DatatypeEnum::TYPE_DATE, DatatypeEnum::getValidTypeFromSample('2018-02-19'));
|
||||
$this->assertEquals(DatatypeEnum::TYPE_DATE, DatatypeEnum::getValidTypeFromSample('18-2-19'));
|
||||
$this->assertEquals(DatatypeEnum::TYPE_DATE, DatatypeEnum::getValidTypeFromSample('01.02.2018'));
|
||||
$this->assertEquals(DatatypeEnum::TYPE_DATE, DatatypeEnum::getValidTypeFromSample('1.2.18'));
|
||||
$this->assertEquals(DatatypeEnum::TYPE_DATE, DatatypeEnum::getValidTypeFromSample('07/31/2018'));
|
||||
$this->assertNotEquals(DatatypeEnum::TYPE_DATE, DatatypeEnum::getValidTypeFromSample('31/07/2018'));
|
||||
$this->assertNotEquals(DatatypeEnum::TYPE_DATE, DatatypeEnum::getValidTypeFromSample('1-2'));
|
||||
$this->assertEquals(DatatypeEnum::TYPE_DATE, DatatypeEnum::getValidTypeFromSample('1.2.'));
|
||||
$this->assertNotEquals(DatatypeEnum::TYPE_DATE, DatatypeEnum::getValidTypeFromSample('test'));
|
||||
}
|
||||
|
||||
}
|
||||
28
tests/methods/OldRequireTest.php
Normal file
28
tests/methods/OldRequireTest.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace ParseCsv\tests\methods;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class OldRequireTest extends TestCase {
|
||||
|
||||
protected function setUp() {
|
||||
rename('vendor/autoload.php', '__autoload');
|
||||
}
|
||||
|
||||
protected function tearDown() {
|
||||
rename('__autoload', 'vendor/autoload.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess because download.php uses header()
|
||||
*/
|
||||
public function testOldLibWithoutComposer() {
|
||||
|
||||
file_put_contents('__eval.php', '<?php require "parsecsv.lib.php"; new \ParseCsv\Csv;');
|
||||
exec("php __eval.php", $output, $return_var);
|
||||
unlink('__eval.php');
|
||||
$this->assertEquals($output, []);
|
||||
$this->assertEquals(0, $return_var);
|
||||
}
|
||||
}
|
||||
222
tests/methods/ParseTest.php
Normal file
222
tests/methods/ParseTest.php
Normal file
@@ -0,0 +1,222 @@
|
||||
<?php
|
||||
|
||||
namespace ParseCsv\tests\methods;
|
||||
|
||||
use ParseCsv\Csv;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ParseTest extends TestCase {
|
||||
|
||||
/**
|
||||
* @var Csv object
|
||||
*/
|
||||
protected $csv;
|
||||
|
||||
/**
|
||||
* Setup
|
||||
* Setup our test environment objects
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function setUp() {
|
||||
$this->csv = new Csv();
|
||||
}
|
||||
|
||||
public function test_parse() {
|
||||
// can we trick 'is_readable' into whining? See #67.
|
||||
$this->parse_repetitive_string('c:/looks/like/a/path');
|
||||
$this->parse_repetitive_string('http://looks/like/an/url');
|
||||
}
|
||||
|
||||
private function parse_repetitive_string($content) {
|
||||
$this->csv->delimiter = ';';
|
||||
$this->csv->heading = false;
|
||||
$success = $this->csv->parse(str_repeat($content . ';', 500));
|
||||
$this->assertEquals(true, $success);
|
||||
|
||||
$row = array_pop($this->csv->data);
|
||||
$expected_data = array_fill(0, 500, $content);
|
||||
$expected_data [] = '';
|
||||
$this->assertEquals($expected_data, $row);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends test_parse
|
||||
*
|
||||
* @dataProvider autoDetectionProvider
|
||||
*/
|
||||
public function testSepRowAutoDetection($file) {
|
||||
// This file (parse_test.php) is encoded in UTF-8, hence comparison will
|
||||
// fail unless we to this:
|
||||
$this->csv->output_encoding = 'UTF-8';
|
||||
|
||||
$this->csv->auto($file);
|
||||
$this->assertEquals($this->_get_magazines_data(), $this->csv->data);
|
||||
}
|
||||
|
||||
public 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'],
|
||||
'UTF16' => [__DIR__ . '/../example_files/UTF-16LE_with_BOM_and_sep_row.csv'],
|
||||
];
|
||||
}
|
||||
|
||||
public function testSingleColumnWithZeros() {
|
||||
$this->csv->delimiter = null;
|
||||
$this->csv->parse("URL\nhttp://www.amazon.com/ROX-Ice-Ball-Maker-Original/dp/B00MX59NMQ/ref=sr_1_1?ie=UTF8&qid=1435604374&sr=8-1&keywords=rox,+ice+molds");
|
||||
$row = array_pop($this->csv->data);
|
||||
$expected_data = ['URL' => 'http://www.amazon.com/ROX-Ice-Ball-Maker-Original/dp/B00MX59NMQ/ref=sr_1_1?ie=UTF8&qid=1435604374&sr=8-1&keywords=rox,+ice+molds'];
|
||||
$this->assertEquals($expected_data, $row);
|
||||
}
|
||||
|
||||
public function testAllNumericalCsv() {
|
||||
$this->csv->heading = false;
|
||||
$sInput = "86545235689\r\n34365587654\r\n13469874576";
|
||||
$this->assertEquals(false, $this->csv->auto($sInput));
|
||||
$this->assertEquals(null, $this->csv->delimiter);
|
||||
$expected_data = explode("\r\n", $sInput);
|
||||
$actual_data = array_map('reset', $this->csv->data);
|
||||
$this->assertEquals($expected_data, $actual_data);
|
||||
}
|
||||
|
||||
public function testMissingEndingLineBreak() {
|
||||
$this->csv->heading = false;
|
||||
$this->csv->enclosure = '"';
|
||||
$sInput = "86545235689,a\r\n34365587654,b\r\n13469874576,\"c\r\nd\"";
|
||||
$expected_data = [86545235689, 34365587654, 13469874576];
|
||||
$actual_data = $this->csv->parse_string($sInput);
|
||||
$actual_column = array_map('reset', $actual_data);
|
||||
$this->assertEquals($expected_data, $actual_column);
|
||||
$this->assertEquals([
|
||||
'a',
|
||||
'b',
|
||||
"c\r\nd",
|
||||
], array_map('next', $actual_data));
|
||||
}
|
||||
|
||||
public function test_single_column() {
|
||||
$this->csv->auto(__DIR__ . '/../example_files/single_column.csv');
|
||||
$expected = [
|
||||
['SMS' => '0444'],
|
||||
['SMS' => '5555'],
|
||||
['SMS' => '6606'],
|
||||
['SMS' => '7777'],
|
||||
];
|
||||
$this->assertEquals($expected, $this->csv->data);
|
||||
}
|
||||
|
||||
public function test_Piwik_data() {
|
||||
if (!function_exists('array_column')) {
|
||||
// function only available in PHP >= 5.5
|
||||
return;
|
||||
}
|
||||
$this->csv->use_mb_convert_encoding = true;
|
||||
$this->csv->output_encoding = 'UTF-8';
|
||||
$this->csv->auto(__DIR__ . '/../example_files/Piwik_API_download.csv');
|
||||
$aAction27 = array_column($this->csv->data, 'url (actionDetails 27)');
|
||||
$this->assertEquals([
|
||||
'http://application/_Main/_GraphicMeanSTD_MDI/btnConfBandOptions',
|
||||
'',
|
||||
'',
|
||||
], $aAction27);
|
||||
|
||||
$aCity = array_column($this->csv->data, 'city');
|
||||
$this->assertEquals([
|
||||
'São Paulo',
|
||||
'Johannesburg',
|
||||
'',
|
||||
], $aCity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testSepRowAutoDetection
|
||||
*/
|
||||
public function testGetColumnDatatypes() {
|
||||
if (!function_exists('array_column')) {
|
||||
// getDatatypes requires array_column, but that
|
||||
// function is only available in PHP >= 5.5
|
||||
return;
|
||||
}
|
||||
|
||||
$this->csv->auto(__DIR__ . '/fixtures/datatype.csv');
|
||||
$this->csv->getDatatypes();
|
||||
$expected = [
|
||||
'title' => 'string',
|
||||
'isbn' => 'string',
|
||||
'publishedAt' => 'date',
|
||||
'published' => 'boolean',
|
||||
'count' => 'integer',
|
||||
'price' => 'float',
|
||||
];
|
||||
|
||||
$this->assertEquals($expected, $this->csv->data_types);
|
||||
}
|
||||
|
||||
|
||||
public function testDataArrayKeysWhenSettingOffsetWithHeading() {
|
||||
$this->csv->offset = 2;
|
||||
$this->csv->auto(__DIR__ . '/fixtures/datatype.csv');
|
||||
$expected = [
|
||||
'title',
|
||||
'isbn',
|
||||
'publishedAt',
|
||||
'published',
|
||||
'count',
|
||||
'price'
|
||||
];
|
||||
|
||||
$this->assertEquals($expected, array_keys($this->csv->data[0]));
|
||||
}
|
||||
|
||||
public function testDataArrayKeysWhenSettingOffsetWithoutHeading() {
|
||||
$this->csv->heading = false;
|
||||
$this->csv->offset = 2;
|
||||
$this->csv->auto(__DIR__ . '/fixtures/datatype.csv');
|
||||
$expected = range(0, 5, 1);
|
||||
|
||||
$this->assertEquals($expected, array_keys($this->csv->data[0]));
|
||||
}
|
||||
|
||||
protected function _get_magazines_data() {
|
||||
return [
|
||||
[
|
||||
'title' => 'Красивая кулинария',
|
||||
'isbn' => '5454-5587-3210',
|
||||
'publishedAt' => '21.05.2011',
|
||||
],
|
||||
[
|
||||
'title' => 'The Wine Connoisseurs',
|
||||
'isbn' => '2547-8548-2541',
|
||||
'publishedAt' => '12.12.2011',
|
||||
],
|
||||
[
|
||||
'title' => 'Weißwein',
|
||||
'isbn' => '1313-4545-8875',
|
||||
'publishedAt' => '23.02.2012',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function autoQuotesDataProvider() {
|
||||
return array(
|
||||
array('auto-double-enclosure.csv', '"'),
|
||||
array('auto-single-enclosure.csv', "'"),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testSepRowAutoDetection
|
||||
*
|
||||
* @dataProvider autoQuotesDataProvider
|
||||
*
|
||||
* @param string $file
|
||||
* @param string $enclosure
|
||||
*/
|
||||
public function testAutoQuotes($file, $enclosure) {
|
||||
$csv = new Csv();
|
||||
$csv->auto(__DIR__ . '/fixtures/' . $file, true, null, null, $enclosure);
|
||||
$this->assertArrayHasKey('column1', $csv->data[0], 'Data parsed incorrectly with enclosure ' . $enclosure);
|
||||
$this->assertEquals('value1', $csv->data[0]['column1'], 'Data parsed incorrectly with enclosure ' . $enclosure);
|
||||
}
|
||||
}
|
||||
70
tests/methods/SaveTest.php
Normal file
70
tests/methods/SaveTest.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
namespace ParseCsv\tests\methods;
|
||||
|
||||
use ParseCsv\Csv;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SaveTest extends TestCase
|
||||
{
|
||||
|
||||
/** @var Csv */
|
||||
private $csv;
|
||||
|
||||
private $temp_filename;
|
||||
|
||||
/**
|
||||
* Setup our test environment objects; will be called before each test.
|
||||
*/
|
||||
public function setUp() {
|
||||
$this->csv = new Csv();
|
||||
$this->csv->auto(__DIR__ . '/../example_files/single_column.csv');
|
||||
|
||||
// Remove last 2 lines to simplify comparison
|
||||
unset($this->csv->data[2], $this->csv->data[3]);
|
||||
|
||||
$temp_dir = str_replace("\\", '/', sys_get_temp_dir());
|
||||
if (substr($temp_dir, -1) != '/') {
|
||||
// From the PHP.net documentation:
|
||||
// This function does not always add trailing slash. This behaviour
|
||||
// is inconsistent across systems.
|
||||
$temp_dir .= '/';
|
||||
}
|
||||
$this->temp_filename = $temp_dir . 'parsecsv_test_file.csv';
|
||||
}
|
||||
|
||||
public function testSaveWithDefaultSettings() {
|
||||
$expected = "SMS\r0444\r5555\r";
|
||||
$this->saveAndCompare($expected);
|
||||
}
|
||||
|
||||
public function testSaveWithDosLineEnding() {
|
||||
$this->csv->linefeed = "\r\n";
|
||||
$expected = "SMS\r\n0444\r\n5555\r\n";
|
||||
$this->saveAndCompare($expected);
|
||||
}
|
||||
|
||||
public function testSaveWithUnixLineEnding() {
|
||||
$this->csv->linefeed = "\n";
|
||||
$expected = "SMS\n0444\n5555\n";
|
||||
$this->saveAndCompare($expected);
|
||||
}
|
||||
|
||||
public function testSaveWithoutHeader() {
|
||||
$this->csv->linefeed = "\n";
|
||||
$this->csv->heading = false;
|
||||
$expected = "0444\n5555\n";
|
||||
$this->saveAndCompare($expected);
|
||||
}
|
||||
|
||||
public function testAllQuotes() {
|
||||
$this->csv->enclose_all = true;
|
||||
$expected = "\"SMS\"\r\"0444\"\r\"5555\"\r";
|
||||
$this->saveAndCompare($expected);
|
||||
}
|
||||
|
||||
private function saveAndCompare($expected) {
|
||||
$this->csv->save($this->temp_filename);
|
||||
$content = file_get_contents($this->temp_filename);
|
||||
$this->assertEquals($expected, $content);
|
||||
}
|
||||
}
|
||||
3
tests/methods/fixtures/auto-double-enclosure.csv
Normal file
3
tests/methods/fixtures/auto-double-enclosure.csv
Normal file
@@ -0,0 +1,3 @@
|
||||
"column1","column2"
|
||||
"value1","value2"
|
||||
"value3","value4"
|
||||
|
3
tests/methods/fixtures/auto-single-enclosure.csv
Normal file
3
tests/methods/fixtures/auto-single-enclosure.csv
Normal file
@@ -0,0 +1,3 @@
|
||||
'column1','column2'
|
||||
'value1','value2'
|
||||
'value3','value4'
|
||||
|
5
tests/methods/fixtures/datatype.csv
Normal file
5
tests/methods/fixtures/datatype.csv
Normal file
@@ -0,0 +1,5 @@
|
||||
sep=;
|
||||
title;isbn;publishedAt;published;count;price
|
||||
Красивая кулинария;5454-5587-3210;21.05.2011;true;1;10.99
|
||||
The Wine Connoisseurs;2547-8548-2541;12.12.2011;TRUE;;20.33
|
||||
Weißwein;1313-4545-8875;23.02.2012;false;10;10
|
||||
|
18
tests/phpunit.xml
Normal file
18
tests/phpunit.xml
Normal file
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<phpunit
|
||||
bootstrap="Bootstrap.php"
|
||||
colors="false"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
stopOnError="true"
|
||||
stopOnFailure="true"
|
||||
stopOnIncomplete="true"
|
||||
stopOnSkipped="false">
|
||||
<testsuites>
|
||||
<testsuite name="parseCSV Test Suite">
|
||||
<directory suffix="est.php">properties/</directory>
|
||||
<directory suffix="est.php">methods/</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
</phpunit>
|
||||
36
tests/properties/BaseClass.php
Normal file
36
tests/properties/BaseClass.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace ParseCsv\tests\properties;
|
||||
|
||||
use ParseCsv\Csv;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class BaseClass extends TestCase {
|
||||
|
||||
/**
|
||||
* CSV
|
||||
* The parseCSV object
|
||||
*
|
||||
* @access protected
|
||||
* @var Csv
|
||||
*/
|
||||
protected $csv;
|
||||
|
||||
/**
|
||||
* Setup
|
||||
* Setup our test environment objects
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function setUp() {
|
||||
$this->csv = new Csv();
|
||||
}
|
||||
|
||||
protected function _compareWithExpected($expected) {
|
||||
$this->csv->auto(__DIR__ . '/../../examples/_books.csv');
|
||||
$actual = array_map(function ($row) {
|
||||
return $row['title'];
|
||||
}, $this->csv->data);
|
||||
$this->assertEquals($expected, array_values($actual));
|
||||
}
|
||||
}
|
||||
49
tests/properties/ConditionsTest.php
Normal file
49
tests/properties/ConditionsTest.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace ParseCsv\tests\properties;
|
||||
|
||||
class ConditionsTest extends BaseClass {
|
||||
|
||||
public function testNotDanBrown() {
|
||||
$this->csv->conditions = 'author does not contain dan brown';
|
||||
|
||||
$this->_compareWithExpected([
|
||||
'The Killing Kind',
|
||||
'The Third Secret',
|
||||
'The Last Templar',
|
||||
'The Traveller',
|
||||
'Crisis Four',
|
||||
'Prey',
|
||||
'The Broker (Paperback)',
|
||||
'Without Blood (Paperback)',
|
||||
'State of Fear (Paperback)',
|
||||
'The Rule of Four (Paperback)',
|
||||
]);
|
||||
}
|
||||
|
||||
public function testRating() {
|
||||
$this->csv->conditions = 'rating < 3';
|
||||
$this->_compareWithExpected([
|
||||
'The Killing Kind',
|
||||
'The Third Secret',
|
||||
]);
|
||||
|
||||
$this->csv->conditions = 'rating >= 5';
|
||||
$this->_compareWithExpected([
|
||||
'The Traveller',
|
||||
'Prey',
|
||||
'State of Fear (Paperback)',
|
||||
'Digital Fortress : A Thriller (Mass Market Paperback)',
|
||||
'Angels & Demons (Mass Market Paperback)',
|
||||
]);
|
||||
}
|
||||
|
||||
public function testTitleContainsSecretOrCode() {
|
||||
$this->csv->conditions = 'title contains code OR title contains SECRET';
|
||||
|
||||
$this->_compareWithExpected([
|
||||
'The Third Secret',
|
||||
'The Da Vinci Code (Hardcover)',
|
||||
]);
|
||||
}
|
||||
}
|
||||
167
tests/properties/DefaultValuesPropertiesTest.php
Normal file
167
tests/properties/DefaultValuesPropertiesTest.php
Normal file
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
|
||||
namespace ParseCsv\tests\properties;
|
||||
|
||||
use ParseCsv\Csv;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class DefaultValuesPropertiesTest extends TestCase {
|
||||
|
||||
/**
|
||||
* CSV
|
||||
* The parseCSV object
|
||||
*
|
||||
* @access protected
|
||||
* @var Csv
|
||||
*/
|
||||
protected $csv = null;
|
||||
|
||||
/**
|
||||
* Setup
|
||||
* Setup our test environment objects
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function setUp() {
|
||||
//setup parse CSV
|
||||
$this->csv = new Csv();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tear down
|
||||
* Tear down our test environment objects
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function tearDown() {
|
||||
$this->csv = null;
|
||||
}
|
||||
|
||||
public function test_heading_default() {
|
||||
$this->assertTrue(is_bool($this->csv->heading));
|
||||
$this->assertTrue($this->csv->heading);
|
||||
}
|
||||
|
||||
public function test_fields_default() {
|
||||
$this->assertTrue(is_array($this->csv->fields));
|
||||
$this->assertCount(0, $this->csv->fields);
|
||||
}
|
||||
|
||||
public function test_sort_by_default() {
|
||||
$this->assertNull($this->csv->sort_by);
|
||||
}
|
||||
|
||||
public function test_sort_reverse_default() {
|
||||
$this->assertTrue(is_bool($this->csv->sort_reverse));
|
||||
$this->assertFalse($this->csv->sort_reverse);
|
||||
}
|
||||
|
||||
public function test_sort_type_default() {
|
||||
$this->assertNull($this->csv->sort_type);
|
||||
}
|
||||
|
||||
public function test_delimiter_default() {
|
||||
$this->assertTrue(is_string($this->csv->delimiter));
|
||||
$this->assertEquals(',', $this->csv->delimiter);
|
||||
}
|
||||
|
||||
public function test_enclosure_default() {
|
||||
$this->assertTrue(is_string($this->csv->enclosure));
|
||||
$this->assertEquals('"', $this->csv->enclosure);
|
||||
}
|
||||
|
||||
public function test_enclose_all_default() {
|
||||
$this->assertTrue(is_bool($this->csv->enclose_all));
|
||||
$this->assertFalse($this->csv->enclose_all);
|
||||
}
|
||||
|
||||
public function test_conditions_default() {
|
||||
$this->assertNull($this->csv->conditions);
|
||||
}
|
||||
|
||||
public function test_offset_default() {
|
||||
$this->assertNull($this->csv->offset);
|
||||
}
|
||||
|
||||
public function test_limit_default() {
|
||||
$this->assertNull($this->csv->limit);
|
||||
}
|
||||
|
||||
public function test_auto_depth_default() {
|
||||
$this->assertTrue(is_numeric($this->csv->auto_depth));
|
||||
$this->assertEquals(15, $this->csv->auto_depth);
|
||||
}
|
||||
|
||||
public function test_auto_non_chars_default() {
|
||||
$this->assertTrue(is_string($this->csv->auto_non_chars));
|
||||
$this->assertEquals("a-zA-Z0-9\n\r", $this->csv->auto_non_chars);
|
||||
}
|
||||
|
||||
public function test_auto_preferred_default() {
|
||||
$this->assertTrue(is_string($this->csv->auto_preferred));
|
||||
$this->assertEquals(",;\t.:|", $this->csv->auto_preferred);
|
||||
}
|
||||
|
||||
public function test_convert_encoding_default() {
|
||||
$this->assertTrue(is_bool($this->csv->convert_encoding));
|
||||
$this->assertFalse($this->csv->convert_encoding);
|
||||
}
|
||||
|
||||
public function test_input_encoding_default() {
|
||||
$this->assertTrue(is_string($this->csv->input_encoding));
|
||||
$this->assertEquals('ISO-8859-1', $this->csv->input_encoding);
|
||||
}
|
||||
|
||||
public function test_output_encoding_default() {
|
||||
$this->assertTrue(is_string($this->csv->output_encoding));
|
||||
$this->assertEquals('ISO-8859-1', $this->csv->output_encoding);
|
||||
}
|
||||
|
||||
public function test_linefeed_default() {
|
||||
$this->assertTrue(is_string($this->csv->linefeed));
|
||||
$this->assertEquals("\r", $this->csv->linefeed);
|
||||
}
|
||||
|
||||
public function test_output_delimiter_default() {
|
||||
$this->assertTrue(is_string($this->csv->output_delimiter));
|
||||
$this->assertEquals(',', $this->csv->output_delimiter);
|
||||
}
|
||||
|
||||
public function test_output_filename_default() {
|
||||
$this->assertTrue(is_string($this->csv->output_filename));
|
||||
$this->assertEquals('data.csv', $this->csv->output_filename);
|
||||
}
|
||||
|
||||
public function test_keep_file_data_default() {
|
||||
$this->assertTrue(is_bool($this->csv->keep_file_data));
|
||||
$this->assertFalse($this->csv->keep_file_data);
|
||||
}
|
||||
|
||||
public function test_file_default() {
|
||||
$this->assertNull($this->csv->file);
|
||||
}
|
||||
|
||||
public function test_file_data_default() {
|
||||
$this->assertNull($this->csv->file_data);
|
||||
}
|
||||
|
||||
public function test_error_default() {
|
||||
$this->assertTrue(is_numeric($this->csv->error));
|
||||
$this->assertEquals(0, $this->csv->error);
|
||||
}
|
||||
|
||||
public function test_error_info_default() {
|
||||
$this->assertTrue(is_array($this->csv->error_info));
|
||||
$this->assertCount(0, $this->csv->error_info);
|
||||
}
|
||||
|
||||
public function test_titles_default() {
|
||||
$this->assertTrue(is_array($this->csv->titles));
|
||||
$this->assertCount(0, $this->csv->titles);
|
||||
}
|
||||
|
||||
public function test_data_default() {
|
||||
$this->assertTrue(is_array($this->csv->data));
|
||||
$this->assertCount(0, $this->csv->data);
|
||||
}
|
||||
}
|
||||
148
tests/properties/PublicPropertiesTest.php
Normal file
148
tests/properties/PublicPropertiesTest.php
Normal file
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
namespace ParseCsv\tests\properties;
|
||||
|
||||
use ParseCsv\Csv;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PublicPropertiesTest extends TestCase {
|
||||
|
||||
/**
|
||||
* CSV
|
||||
* The parseCSV object
|
||||
*
|
||||
* @access protected
|
||||
* @var Csv
|
||||
*/
|
||||
protected $csv = null;
|
||||
|
||||
/**
|
||||
* Reflection Object
|
||||
* The reflection class object
|
||||
*
|
||||
* @access protected
|
||||
* @var \ReflectionClass
|
||||
*/
|
||||
protected $reflection = null;
|
||||
|
||||
/**
|
||||
* Reflection Properties
|
||||
* The reflected class properties
|
||||
*
|
||||
* @var \ReflectionProperty[]
|
||||
* @access protected
|
||||
*/
|
||||
protected $properties = null;
|
||||
|
||||
/**
|
||||
* Setup
|
||||
* Setup our test environment objects
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function setUp() {
|
||||
//setup parse CSV
|
||||
$this->csv = new Csv();
|
||||
|
||||
//setup the reflection class
|
||||
$this->reflection = new \ReflectionClass($this->csv);
|
||||
|
||||
//setup the reflected class properties
|
||||
$this->properties = $this->reflection->getProperties();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tear down
|
||||
* Tear down our test environment objects
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function tearDown() {
|
||||
$this->csv = null;
|
||||
$this->reflection = null;
|
||||
$this->properties = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* test_propertiesCount
|
||||
* Counts the number of properties to make sure we didn't add or
|
||||
* subtract any without thinking
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function test_propertiesCount() {
|
||||
$this->assertCount(29, $this->properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* test_property_names
|
||||
* We have an expected set of properties that should exists
|
||||
* Make sure our expected number of properties matches the real
|
||||
* count of properties and also check to make sure our expected
|
||||
* properties exists within the class
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function test_property_names() {
|
||||
//set our expected properties name(s)
|
||||
$expected_names = array(
|
||||
'heading',
|
||||
'fields',
|
||||
'sort_by',
|
||||
'sort_reverse',
|
||||
'sort_type',
|
||||
'delimiter',
|
||||
'enclosure',
|
||||
'enclose_all',
|
||||
'conditions',
|
||||
'offset',
|
||||
'limit',
|
||||
'auto_depth',
|
||||
'auto_non_chars',
|
||||
'auto_preferred',
|
||||
'convert_encoding',
|
||||
'input_encoding',
|
||||
'output_encoding',
|
||||
'use_mb_convert_encoding',
|
||||
'linefeed',
|
||||
'output_delimiter',
|
||||
'output_filename',
|
||||
'keep_file_data',
|
||||
'file',
|
||||
'file_data',
|
||||
'error',
|
||||
'error_info',
|
||||
'titles',
|
||||
'data',
|
||||
'data_types',
|
||||
);
|
||||
|
||||
// Find our real properties
|
||||
$real_properties = array_map(function (\ReflectionProperty $property) {
|
||||
return $property->getName();
|
||||
}, $this->properties);
|
||||
|
||||
// Lets make sure our expected matches the number of real properties
|
||||
$this->assertEquals($expected_names, $real_properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* test_count_public_properties
|
||||
* We at this point only have public properties so
|
||||
* lets verify all properties are public
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function test_count_public_properties() {
|
||||
$counter = 0;
|
||||
|
||||
$propertiesCount = count($this->properties);
|
||||
for ($a = 0; $a < $propertiesCount; $a++) {
|
||||
if ($this->properties[$a]->isPublic() === true) {
|
||||
$counter++;
|
||||
}
|
||||
}
|
||||
|
||||
$this->assertCount($counter, $this->properties);
|
||||
}
|
||||
}
|
||||
60
tests/properties/SortByTest.php
Normal file
60
tests/properties/SortByTest.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace ParseCsv\tests\properties;
|
||||
|
||||
class SortByTest extends BaseClass {
|
||||
|
||||
public function testSortByRating() {
|
||||
$this->csv->sort_by = 'rating';
|
||||
$this->csv->conditions = 'title does not contain Blood';
|
||||
$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
|
||||
'State of Fear (Paperback)',
|
||||
'Prey',
|
||||
'Digital Fortress : A Thriller (Mass Market Paperback)',
|
||||
'Angels & Demons (Mass Market Paperback)',
|
||||
]);
|
||||
}
|
||||
|
||||
public function testReverseSortByRating() {
|
||||
$this->csv->sort_by = 'rating';
|
||||
$this->csv->conditions =
|
||||
'title does not contain Prey AND ' .
|
||||
'title does not contain Fortress AND ' .
|
||||
'title does not contain Blood AND ' .
|
||||
'title does not contain Fear';
|
||||
$this->csv->sort_reverse = true;
|
||||
$this->_compareWithExpected([
|
||||
|
||||
// Rating 5
|
||||
'Angels & Demons (Mass Market Paperback)',
|
||||
'The Traveller',
|
||||
|
||||
// Rating 4
|
||||
'The Da Vinci Code (Hardcover)',
|
||||
'The Rule of Four (Paperback)',
|
||||
'Deception Point (Paperback)',
|
||||
|
||||
// Rating 3
|
||||
'The Broker (Paperback)',
|
||||
'The Last Templar',
|
||||
|
||||
// Rating 0
|
||||
'The Third Secret',
|
||||
'The Killing Kind',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user