Skip to content

Commit

Permalink
Adding tests for TabularData::getObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Nov 23, 2023
1 parent 0f4d7ea commit 09d20b7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public function testStripBOMWithEnclosure(): void
$csv = Reader::createFromString($source);
$csv->setHeaderOffset(0);
$expected = ['parent name' => 'parentA', 'child name' => 'childA', 'title' => 'titleA'];
foreach ($csv->getRecords() as $offset => $record) {
foreach ($csv->getRecords() as $record) {
self::assertSame($expected, $record);
}
}
Expand Down Expand Up @@ -350,8 +350,7 @@ public function testSetHeaderThrowsExceptionOnWrongInputRange(): void
public function testMapRecordsFields(): void
{
$keys = ['firstname', 'lastname', 'email'];
$res = $this->csv->getRecords($keys);
foreach ($res as $record) {
foreach ($this->csv->getRecords($keys) as $record) {
self::assertSame($keys, array_keys($record));
}
}
Expand Down
52 changes: 52 additions & 0 deletions src/TabularDataReaderTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

namespace League\Csv;

use DateTimeImmutable;
use DateTimeInterface;
use League\Csv\Serializer\Cell;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
Expand Down Expand Up @@ -369,4 +372,53 @@ public function testValue(): void
self::assertSame('Galway', $this->tabularDataWithHeader()->value(2));
self::assertSame('Galway', $this->tabularDataWithHeader()->value('place'));
}

/***************************
* TabularDataReader::getObjects
****************************/

public function testGetObjectWithHeader(): void
{
$class = new class (5, Place::Galway, new DateTimeImmutable()) {
public function __construct(
public readonly float $temperature,
public readonly Place $place,
#[Cell(
column: 'date',
options: ['format' => '!Y-m-d', 'timezone' => 'Africa/Kinshasa'],
)]
public readonly DateTimeInterface $observedOn
) {
}
};

foreach ($this->tabularDataWithHeader()->getObjects($class::class) as $object) {
self::assertInstanceOf($class::class, $object);
}
}

public function testGetObjectWithoutHeader(): void
{
$class = new class (5, Place::Galway, new DateTimeImmutable()) {
public function __construct(
#[Cell(column: 1)]
public readonly float $temperature,
#[Cell(column: 2)]
public readonly Place $place,
#[Cell(column: 0)]
public readonly DateTimeInterface $observedOn
) {
}
};

foreach ($this->tabularDataWithHeader()->getObjects($class::class) as $object) {
self::assertInstanceOf($class::class, $object);
}
}
}

enum Place: string
{
case Galway = 'Galway';
case Berkeley = 'Berkeley';
}

0 comments on commit 09d20b7

Please sign in to comment.