-
-
Notifications
You must be signed in to change notification settings - Fork 336
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
Allow importing the generated XML in a DOMDocument and improving HTML converter output #348
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,59 @@ public function testToHTML() | |
self::assertContains('<table class="table-csv-data" id="test">', $html); | ||
self::assertContains('<tr data-record-offset="', $html); | ||
self::assertContains('<td title="', $html); | ||
self::assertNotContains('<thead>', $html); | ||
self::assertNotContains('<tbody>', $html); | ||
self::assertNotContains('<tfoot>', $html); | ||
} | ||
|
||
/** | ||
* @covers ::__construct | ||
* @covers ::table | ||
* @covers ::tr | ||
* @covers ::td | ||
* @covers ::convert | ||
*/ | ||
public function testToHTMLWithHeaders() | ||
{ | ||
$csv = Reader::createFromPath(__DIR__.'/data/prenoms.csv', 'r') | ||
->setDelimiter(';') | ||
->setHeaderOffset(0) | ||
; | ||
|
||
$stmt = (new Statement()) | ||
->offset(3) | ||
->limit(5) | ||
; | ||
|
||
$records = $stmt->process($csv); | ||
|
||
$converter = (new HTMLConverter()) | ||
->table('table-csv-data', 'test') | ||
->td('title') | ||
->tr('data-record-offset') | ||
; | ||
|
||
$html = $converter->convert($records, $records->getHeader()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you only provide a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Honestly, I assumed that they came as a threesome... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes hides them accordingly ... I prefer this to be explicit. I was unable to find a resource stating for instance that if you use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
self::assertContains('<table class="table-csv-data" id="test">', $html); | ||
self::assertContains('<th scope="col">prenoms</th>', $html); | ||
self::assertContains('<thead>', $html); | ||
self::assertContains('<tbody>', $html); | ||
self::assertNotContains('<tfoot>', $html); | ||
|
||
$html = $converter->convert($records, [], $records->getHeader()); | ||
self::assertContains('<table class="table-csv-data" id="test">', $html); | ||
self::assertContains('<th scope="col">prenoms</th>', $html); | ||
self::assertNotContains('<thead>', $html); | ||
self::assertContains('<tbody>', $html); | ||
self::assertContains('<tfoot>', $html); | ||
|
||
$html = $converter->convert($records, $records->getHeader(), $records->getHeader()); | ||
self::assertContains('<table class="table-csv-data" id="test">', $html); | ||
self::assertContains('<thead>', $html); | ||
self::assertContains('<tbody>', $html); | ||
self::assertContains('<tfoot>', $html); | ||
self::assertNotContains('<thead><tr data-record-offset="0"></tr></thead>', $html); | ||
self::assertNotContains('<tfoot><tr data-record-offset="0"></tr></tfoot>', $html); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
namespace LeagueTest\Csv; | ||
|
||
use DOMDocument; | ||
use DOMElement; | ||
use DOMException; | ||
use League\Csv\Reader; | ||
use League\Csv\Statement; | ||
|
@@ -90,4 +91,45 @@ public function testXmlElementTriggersTypeError() | |
self::expectException(TypeError::class); | ||
(new XMLConverter())->convert('foo'); | ||
} | ||
|
||
/** | ||
* @covers ::rootElement | ||
* @covers ::recordElement | ||
* @covers ::fieldElement | ||
* @covers ::import | ||
* @covers ::recordToElement | ||
* @covers ::recordToElementWithAttribute | ||
* @covers ::fieldToElement | ||
* @covers ::fieldToElementWithAttribute | ||
* @covers ::filterAttributeName | ||
* @covers ::filterElementName | ||
*/ | ||
public function testImport() | ||
{ | ||
$csv = Reader::createFromPath(__DIR__.'/data/prenoms.csv', 'r') | ||
->setDelimiter(';') | ||
->setHeaderOffset(0) | ||
; | ||
|
||
$stmt = (new Statement()) | ||
->offset(3) | ||
->limit(5) | ||
; | ||
|
||
$records = $stmt->process($csv); | ||
|
||
$converter = (new XMLConverter()) | ||
->rootElement('csv') | ||
->recordElement('record', 'offset') | ||
->fieldElement('field', 'name') | ||
; | ||
|
||
$doc = new DOMDocument('1.0'); | ||
$element = $converter->import($records, $doc); | ||
|
||
self::assertInstanceOf(DOMDocument::class, $doc); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
self::assertCount(0, $doc->childNodes); | ||
self::assertInstanceOf(DOMElement::class, $element); | ||
self::assertCount(5, $element->childNodes); | ||
} | ||
} |
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.
return typed whenever you can
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.
Updated