diff --git a/src/Csv.php b/src/Csv.php index f0daee0..fdb34ab 100644 --- a/src/Csv.php +++ b/src/Csv.php @@ -126,7 +126,9 @@ class Csv { /** * Offset - * Number of rows to ignore from beginning of data + * Number of rows to ignore from beginning of data. If present, the heading + * row is also counted (if $this->heading == true). In other words, + * $offset == 1 and $offset == 0 have the same meaning in that situation. * * @var int|null */ diff --git a/tests/methods/ParseTest.php b/tests/methods/ParseTest.php index ee88165..443bced 100644 --- a/tests/methods/ParseTest.php +++ b/tests/methods/ParseTest.php @@ -154,31 +154,6 @@ class ParseTest extends TestCase { $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])); - } - /** * @depends testSepRowAutoDetection */ @@ -205,7 +180,6 @@ class ParseTest extends TestCase { $sInput = "86545235689\r\n34365587654\r\n13469874576"; $this->csv->auto($sInput); $this->assertFalse($this->csv->autoDetectFileHasHeading()); - } protected function _get_magazines_data() { diff --git a/tests/properties/OffsetTest.php b/tests/properties/OffsetTest.php new file mode 100644 index 0000000..728201c --- /dev/null +++ b/tests/properties/OffsetTest.php @@ -0,0 +1,78 @@ +csv->offset = 1; + $this->csv->auto(__DIR__ . '/../methods/fixtures/datatype.csv'); + $this->assertCount(3, $this->csv->data); + + if (!function_exists('array_column')) { + // function only available in PHP >= 5.5 + return; + } + $expected = [ + 'Красивая кулинария', + 'The Wine Connoisseurs', + 'Weißwein', + ]; + $actual = array_column($this->csv->data, 'title'); + $this->assertEquals($expected, $actual); + } + + public function numberRangeZeroToFourProvider() { + return array_map(function ($number) { + return [$number]; + }, range(0, 4)); + } + + /** + * @dataProvider numberRangeZeroToFourProvider + * + * @param integer $offset + */ + public function testOffsetOfOneNoHeader($offset) { + $this->csv->offset = $offset; + $this->csv->heading = false; + $this->csv->auto(__DIR__ . '/../methods/fixtures/datatype.csv'); + $this->assertCount(4 - $offset, $this->csv->data); + } + + public function testDataArrayKeysWhenSettingOffsetWithHeading() { + $this->csv->offset = 2; + $this->csv->auto(__DIR__ . '/../methods/fixtures/datatype.csv'); + $expected = [ + [ + 'title' => 'The Wine Connoisseurs', + 'isbn' => '2547-8548-2541', + 'publishedAt' => '12.12.2011', + 'published' => 'TRUE', + 'count' => '', + 'price' => 20.33, + ], + [ + 'title' => 'Weißwein', + 'isbn' => '1313-4545-8875', + 'publishedAt' => '23.02.2012', + 'published' => 'false', + 'count' => 10, + 'price' => 10, + ], + ]; + + $this->assertEquals($expected, $this->csv->data); + } + + public function testDataArrayKeysWhenSettingOffsetWithoutHeading() { + $this->csv->heading = false; + $this->csv->offset = 2; + $this->csv->auto(__DIR__ . '/../methods/fixtures/datatype.csv'); + $expected = range(0, 5, 1); + + $this->assertEquals($expected, array_keys($this->csv->data[0])); + } +}