mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 08:36:39 +00:00
Merge branch 'master' into offset-comment-and-tests
Conflicts: tests/methods/ParseTest.php
This commit is contained in:
76
tests/methods/DataRowCountTest.php
Normal file
76
tests/methods/DataRowCountTest.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace ParseCsv\tests\methods;
|
||||
|
||||
use ParseCsv\Csv;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
|
||||
class DataRowCountTest extends TestCase {
|
||||
|
||||
/**
|
||||
* CSV
|
||||
* The CSV object
|
||||
*
|
||||
* @access protected
|
||||
* @var Csv
|
||||
*/
|
||||
protected $csv;
|
||||
|
||||
/**
|
||||
* Setup
|
||||
* Setup our test environment objects
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function setUp() {
|
||||
$this->csv = new Csv();
|
||||
}
|
||||
|
||||
public function countRowsProvider() {
|
||||
return [
|
||||
'auto-double-enclosure' => [
|
||||
'auto-double-enclosure.csv',
|
||||
2,
|
||||
],
|
||||
'auto-single-enclosure' => [
|
||||
'auto-single-enclosure.csv',
|
||||
2,
|
||||
],
|
||||
'UTF-8_sep_row' => [
|
||||
'datatype.csv',
|
||||
3,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider countRowsProvider
|
||||
*
|
||||
* @param string $file
|
||||
* @param int $expectedRows
|
||||
*/
|
||||
public function testGetTotalRowCountFromFile($file, $expectedRows) {
|
||||
$this->csv->heading = true;
|
||||
$this->csv->load_data(__DIR__ . '/fixtures/' . $file);
|
||||
$this->assertEquals($expectedRows, $this->csv->getTotalDataRowCount());
|
||||
}
|
||||
|
||||
public function testGetTotalRowCountMissingEndingLineBreak() {
|
||||
$this->csv->heading = false;
|
||||
$this->csv->enclosure = '"';
|
||||
$sInput = "86545235689,a\r\n34365587654,b\r\n13469874576,\"c\r\nd\"";
|
||||
$this->csv->load_data($sInput);
|
||||
$this->assertEquals(3, $this->csv->getTotalDataRowCount());
|
||||
}
|
||||
|
||||
|
||||
public function testGetTotalRowCountSingleEnclosure() {
|
||||
$this->csv->heading = false;
|
||||
$this->csv->enclosure = "'";
|
||||
$sInput = "86545235689,a\r\n34365587654,b\r\n13469874576,\'c\r\nd\'";
|
||||
$this->csv->load_data($sInput);
|
||||
$this->assertEquals(3, $this->csv->getTotalDataRowCount());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -85,7 +85,8 @@ class ParseTest extends TestCase {
|
||||
$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_data = $this->invokeMethod($this->csv, 'parse_string', array($sInput));
|
||||
$actual_column = array_map('reset', $actual_data);
|
||||
$this->assertEquals($expected_data, $actual_column);
|
||||
$this->assertEquals([
|
||||
@@ -153,6 +154,34 @@ class ParseTest extends TestCase {
|
||||
$this->assertEquals($expected, $this->csv->data_types);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testSepRowAutoDetection
|
||||
*/
|
||||
public function testAutoDetectFileHasHeading(){
|
||||
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->assertTrue($this->csv->autoDetectFileHasHeading());
|
||||
|
||||
$this->csv->heading = false;
|
||||
$this->csv->auto(__DIR__ . '/fixtures/datatype.csv');
|
||||
$this->assertTrue($this->csv->autoDetectFileHasHeading());
|
||||
|
||||
$this->csv->heading = false;
|
||||
$sInput = "86545235689\r\n34365587654\r\n13469874576";
|
||||
$this->csv->auto($sInput);
|
||||
$this->assertFalse($this->csv->autoDetectFileHasHeading());
|
||||
|
||||
$this->csv->heading = true;
|
||||
$sInput = "86545235689\r\n34365587654\r\n13469874576";
|
||||
$this->csv->auto($sInput);
|
||||
$this->assertFalse($this->csv->autoDetectFileHasHeading());
|
||||
}
|
||||
|
||||
protected function _get_magazines_data() {
|
||||
return [
|
||||
[
|
||||
@@ -194,4 +223,22 @@ class ParseTest extends TestCase {
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call protected/private method of a class.
|
||||
*
|
||||
* @param object &$object Instantiated object that we will run method on.
|
||||
* @param string $methodName Method name to call
|
||||
* @param array $parameters Array of parameters to pass into method.
|
||||
*
|
||||
* @return mixed Method return.
|
||||
*/
|
||||
private function invokeMethod(&$object, $methodName, array $parameters = array())
|
||||
{
|
||||
$reflection = new \ReflectionClass(get_class($object));
|
||||
$method = $reflection->getMethod($methodName);
|
||||
$method->setAccessible(true);
|
||||
|
||||
return $method->invokeArgs($object, $parameters);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,6 +49,13 @@ class SaveTest extends TestCase
|
||||
$this->saveAndCompare($expected);
|
||||
}
|
||||
|
||||
public function testSaveWithNewHeader() {
|
||||
$this->csv->linefeed = "\n";
|
||||
$this->csv->titles = array("NewTitle");
|
||||
$expected = "NewTitle\n0444\n5555\n";
|
||||
$this->saveAndCompare($expected);
|
||||
}
|
||||
|
||||
public function testSaveWithoutHeader() {
|
||||
$this->csv->linefeed = "\n";
|
||||
$this->csv->heading = false;
|
||||
|
||||
62
tests/methods/UnparseTest.php
Normal file
62
tests/methods/UnparseTest.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
namespace ParseCsv\tests\methods;
|
||||
|
||||
use ParseCsv\Csv;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
|
||||
class UnparseTest extends Testcase {
|
||||
/** @var Csv */
|
||||
private $csv;
|
||||
|
||||
/**
|
||||
* Setup our test environment objects; will be called before each test.
|
||||
*/
|
||||
public function setUp() {
|
||||
$this->csv = new Csv();
|
||||
$this->csv->auto(__DIR__ . '/fixtures/auto-double-enclosure.csv');
|
||||
}
|
||||
|
||||
public function testUnparseDefault() {
|
||||
$expected = "column1,column2\rvalue1,value2\rvalue3,value4\r";
|
||||
$this->unparseAndCompare($expected);
|
||||
}
|
||||
|
||||
public function testUnparseDefaultWithoutHeading(){
|
||||
$this->csv->heading = false;
|
||||
$this->csv->auto(__DIR__ . '/fixtures/auto-double-enclosure.csv');
|
||||
$expected = "column1,column2\rvalue1,value2\rvalue3,value4\r";
|
||||
$this->unparseAndCompare($expected);
|
||||
|
||||
}
|
||||
|
||||
public function testUnparseRenameFields() {
|
||||
$expected = "C1,C2\rvalue1,value2\rvalue3,value4\r";
|
||||
$this->unparseAndCompare($expected, array("C1", "C2"));
|
||||
}
|
||||
|
||||
public function testReorderFields() {
|
||||
$expected = "column2,column1\rvalue2,value1\rvalue4,value3\r";
|
||||
$this->unparseAndCompare($expected, array("column2", "column1"));
|
||||
}
|
||||
|
||||
public function testSubsetFields() {
|
||||
$expected = "column1\rvalue1\rvalue3\r";
|
||||
$this->unparseAndCompare($expected, array("column1"));
|
||||
}
|
||||
|
||||
public function testReorderAndRenameFields() {
|
||||
$fields = array(
|
||||
'column2' => 'C2',
|
||||
'column1' => 'C1',
|
||||
);
|
||||
$expected = "C2,C1\rvalue2,value1\rvalue4,value3\r";
|
||||
$this->unparseAndCompare($expected, $fields);
|
||||
}
|
||||
|
||||
private function unparseAndCompare($expected, $fields = array()) {
|
||||
$str = $this->csv->unparse($this->csv->data, $fields);
|
||||
$this->assertEquals($expected, $str);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user