-
-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
getHeaders() #52
Merged
Merged
getHeaders() #52
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4eb13ac
Write tests for getHeaders() function
AdamGaskins 85e0acf
Implement getHeaders()
AdamGaskins 9a98a93
Document getHeaders()
AdamGaskins eb52ae3
Simplify implementation
AdamGaskins f588e09
Rearrange getHeaders()
AdamGaskins f61c6d3
Add test for calling getHeaders on empty file
AdamGaskins a5bf146
Fix calling getHeaders() on empty file
AdamGaskins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,19 @@ public function it_can_work_with_a_file_where_the_row_is_too_short() | |
], $rows); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_retrieve_the_headers() | ||
{ | ||
$headers = SimpleExcelReader::create($this->getStubPath('header-and-rows.csv')) | ||
->getHeaders(); | ||
|
||
$this->assertEquals([ | ||
0 => 'email', | ||
1 => 'first_name', | ||
2 => 'last_name' | ||
], $headers); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_ignore_the_headers() | ||
{ | ||
|
@@ -105,6 +118,16 @@ public function it_can_ignore_the_headers() | |
], $rows); | ||
} | ||
|
||
/** @test */ | ||
public function it_doesnt_return_headers_when_headers_are_ignored() | ||
{ | ||
$headers = SimpleExcelReader::create($this->getStubPath('header-and-rows.csv')) | ||
->noHeaderRow() | ||
->getHeaders(); | ||
|
||
$this->assertEquals(null, $headers); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_use_an_alternative_delimiter() | ||
{ | ||
|
@@ -197,6 +220,35 @@ public function it_can_call_getRows_twice() | |
$this->assertNotNull($firstRowAgain); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_call_getRows_after_getHeaders() | ||
{ | ||
$reader = SimpleExcelReader::create($this->getStubPath('header-and-rows.csv')); | ||
|
||
$headers = $reader->getHeaders(); | ||
|
||
$this->assertEquals([ | ||
0 => 'email', | ||
1 => 'first_name', | ||
2 => 'last_name' | ||
], $headers); | ||
|
||
$rows = $reader->getRows()->toArray(); | ||
|
||
$this->assertEquals([ | ||
[ | ||
'email' => '[email protected]', | ||
'first_name' => 'john', | ||
'last_name' => 'doe', | ||
], | ||
[ | ||
'email' => '[email protected]', | ||
'first_name' => 'mary jane', | ||
'last_name' => 'doe', | ||
], | ||
], $rows); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_call_first_on_the_collection_twice() | ||
{ | ||
|
@@ -239,6 +291,20 @@ public function it_can_trim_the_header_row_names() | |
], $rows); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_retrieve_trimmed_header_row_names() | ||
{ | ||
$headers = SimpleExcelReader::create($this->getStubPath('header-with-spaces.csv')) | ||
->trimHeaderRow() | ||
->getHeaders(); | ||
|
||
$this->assertEquals([ | ||
0 => 'email', | ||
1 => 'first_name', | ||
2 => 'last_name', | ||
], $headers); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_trim_the_header_row_names_with_alternate_characters() | ||
{ | ||
|
@@ -285,6 +351,21 @@ public function it_can_convert_headers_to_snake_case() | |
], $rows); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_retrieve_headers_converted_to_snake_case() | ||
{ | ||
$headers = SimpleExcelReader::create($this->getStubPath('headers-not-snake-case.csv')) | ||
->headersToSnakeCase() | ||
->getHeaders(); | ||
|
||
$this->assertEquals([ | ||
0 => 'email', | ||
1 => 'first_name', | ||
2 => 'last_name', | ||
3 => 'job_title', | ||
], $headers); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_use_custom_header_row_formatter() | ||
{ | ||
|
@@ -308,4 +389,20 @@ public function it_can_use_custom_header_row_formatter() | |
], | ||
], $rows); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_retrieve_headers_with_a_custom_formatter() | ||
{ | ||
$headers = SimpleExcelReader::create($this->getStubPath('header-and-rows.csv')) | ||
->formatHeadersUsing(function ($header) { | ||
return $header . '_suffix'; | ||
}) | ||
->getHeaders(); | ||
|
||
$this->assertEquals([ | ||
0 => 'email_suffix', | ||
1 => 'first_name_suffix', | ||
2 => 'last_name_suffix', | ||
], $headers); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a big piece of indented code here. I can be simplified by first checking if headers is set to something. If it is, we can already return the headers.