Skip to content

Commit

Permalink
Improve codebase and dependencies requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Sep 25, 2023
1 parent 297e839 commit 92aa05b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"ext-simplexml": "*",
"ext-json": "*",
"ext-mbstring": "*",
"league/csv": "^9.11.0"
"league/csv": "^9.6.0"
},
"require-dev": {
"ext-xdebug": "*",
Expand Down
40 changes: 29 additions & 11 deletions src/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use DOMDocument;
use DOMElement;
use League\Csv\TabularDataReader;
use LimitIterator;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use SimpleXMLElement;
Expand Down Expand Up @@ -102,7 +104,7 @@ public function it_can_load_the_first_html_table_found_by_default(): void

self::assertSame(['prenoms', 'nombre', 'sexe', 'annee'], $table->getHeader());
self::assertCount(4, $table);
self::assertSame($table->first(), [
self::assertSame($this->getFirstRecord($table), [
'prenoms' => 'Abdoulaye',
'nombre' => '15',
'sexe' => 'M',
Expand All @@ -117,7 +119,7 @@ public function it_can_load_the_first_html_table_found_by_default_without_the_he

self::assertSame([], $table->getHeader());
self::assertCount(4, $table);
self::assertSame($table->first(), [
self::assertSame($this->getFirstRecord($table), [
'Abdoulaye',
'15',
'M',
Expand Down Expand Up @@ -164,7 +166,7 @@ public function it_uses_the_table_first_tr_to_search_for_the_header(): void
'nombre' => '15',
'sexe' => 'M',
'annee' => '2004',
], $table->first());
], $this->getFirstRecord($table));

fclose($stream);
}
Expand Down Expand Up @@ -204,7 +206,7 @@ public function it_uses_the_table_first_tr_in_the_first_tbody_to_search_for_the_
'nombre' => '15',
'sexe' => 'M',
'annee' => '2004',
], $table->first());
], $this->getFirstRecord($table));
}

#[Test]
Expand Down Expand Up @@ -232,7 +234,7 @@ public function it_will_use_the_submitted_headers(): void
$table = $parser->parseHTML(self::HTML);

self::assertSame(['firstname', 'count', 'gender', 'year'], $table->getHeader());
self::assertSame($table->first(), [
self::assertSame($this->getFirstRecord($table), [
'firstname' => 'Abdoulaye',
'count' => '15',
'gender' => 'M',
Expand Down Expand Up @@ -366,7 +368,7 @@ public function it_will_use_the_table_footer(): void
->parseHTML($html);

self::assertSame([], $table->getHeader());
self::assertSame([], $table->first());
self::assertSame([], $this->getFirstRecord($table));
}

#[Test]
Expand All @@ -393,13 +395,13 @@ public function it_uses_the_parser_formatter(): void
'nombre' => 15,
'sexe' => 'M',
'annee' => 2004,
], $table->first());
], $this->getFirstRecord($table));

fclose($stream);
}

#[Test]
public function it_can_handle_rowspan(): void
public function it_can_handle_rowspan_and_colspan(): void
{
$table = <<<TABLE
<table>
Expand Down Expand Up @@ -445,10 +447,26 @@ public function it_can_handle_rowspan(): void
</table>
TABLE;

$reducer = fn (TabularDataReader $reader, string $value): int => array_reduce([...$reader], fn (int $carry, array $record): int => $carry + (array_count_values($record)[$value] ?? 0), 0);
$table = Parser::new()->parseHTML($table);

self::assertSame(2, $table->reduce(fn (int $carry, array $record): int => $carry + (array_count_values($record)['colspan'] ?? 0), 0));
self::assertSame(2, $table->reduce(fn (int $carry, array $record): int => $carry + (array_count_values($record)['rowspan'] ?? 0), 0));
self::assertSame(6, $table->reduce(fn (int $carry, array $record): int => $carry + (array_count_values($record)['colspan+rowspan'] ?? 0), 0));
self::assertSame(2, $reducer($table, 'colspan'));
self::assertSame(2, $reducer($table, 'rowspan'));
self::assertSame(6, $reducer($table, 'colspan+rowspan'));
}

/**
*
* @return array<string>
*/
private function getFirstRecord(TabularDataReader $reader): array
{
$iterator = new LimitIterator($reader->getRecords(), 0, 1);
$iterator->rewind();

/** @var array<string>|null $result */
$result = $iterator->current();

return $result ?? [];
}
}

0 comments on commit 92aa05b

Please sign in to comment.