From 09d20b7a5af9c7406ddf1969628d249c79ca65c7 Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Thu, 23 Nov 2023 18:18:34 +0100 Subject: [PATCH] Adding tests for TabularData::getObjects --- src/ReaderTest.php | 5 ++- src/TabularDataReaderTestCase.php | 52 +++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/ReaderTest.php b/src/ReaderTest.php index 9c554400..95b12e09 100644 --- a/src/ReaderTest.php +++ b/src/ReaderTest.php @@ -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); } } @@ -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)); } } diff --git a/src/TabularDataReaderTestCase.php b/src/TabularDataReaderTestCase.php index aa07cf52..4d923c99 100644 --- a/src/TabularDataReaderTestCase.php +++ b/src/TabularDataReaderTestCase.php @@ -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; @@ -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'; }